public async Task HandleAsync(CreateAccessRecord message, IRequestInfo requestInfo) { IUserDocument userDocument = await _userRepository.GetByCodeAsync(message.Code); if (userDocument is null) { _publisher.PublishEvent(new AccessRecordRejected(Codes.InvalidId, "The code used is invalid."), requestInfo); _logger.LogError($"User with code: {message.Code} could not be found."); return; } if (!await _servicesRepository.IsSiteIdValid(message.SiteId)) { _publisher.PublishEvent(new AccessRecordRejected(Codes.InvalidId, "The site could not be found."), requestInfo); _logger.LogError($"Site with id: {message.SiteId} could not be found."); return; } await _userStatusService.Update(userDocument.Id, message.Action, message.SiteId); var record = _factory.Create(userDocument.Id, message.SiteId, message.Action, userDocument.BusinessId); await _accessRecordRepository.AddAsync(record); _publisher.PublishEvent(new AccessRecordCreated(), requestInfo); string action = message.Action == AccessAction.In ? "in" : "out"; _logger.LogInformation($"{userDocument.FirstName + " " + userDocument.SecondName} signed {action} on : {record.TimeStamp}.", LoggingCategories.Access); }
public async Task HandleAsync(CreateUser message, IRequestInfo requestInfo) { var account = await _accountRepository.GetAsync(message.AccountId); if (account is null) { _logger.LogWarning($"Account not found when completing user profile account id: {message.AccountId}."); _publisher.PublishEvent(new CreateUserRejected(Codes.InvalidId, $"The account with the id: {message.AccountId} cannot be found"), requestInfo); return; } if (!await _servicesRepository.IsBusinessIdValid(message.BusinessId)) { _logger.LogWarning($"The business id: {message.BusinessId} could not be fetched from the business service."); _publisher.PublishEvent(new CreateUserRejected(Codes.InvalidId, $"The business id: {message.BusinessId} could not be fetched from the business service."), requestInfo); return; } if (!await _servicesRepository.IsSiteIdValid(message.BasedSiteId)) { _logger.LogWarning($"The site id: {message.BasedSiteId} could not be fetched from the site service."); _publisher.PublishEvent(new CreateUserRejected(Codes.InvalidId, $"The site id: {message.BasedSiteId} could not be fetched from the site service."), requestInfo); return; } IUserDocument userDocument = null; try { userDocument = _factory.CreateUser(message.FirstName, message.SecondName, account.Email, message.PhoneNumber, message.BusinessPhoneNumber, message.BasedSiteId, message.BusinessId, message.AccountId, account.Code); } catch (VmsException e) { _publisher.PublishEvent(new CreateUserRejected(e.Code, e.Message), requestInfo); return; } await _userRepository.AddAsync(userDocument); var state = _recordFactory.Create(userDocument.Id, userDocument.BasedSiteId, AccessAction.Out); await _repository.AddAsync(state); _publisher.PublishEvent(new UserCreated(), requestInfo); _logger.LogInformation($"User created with id: {userDocument.Id} and name: {userDocument.FirstName + " " + userDocument.SecondName}."); }
public async Task Update(Guid userId, AccessAction action, Guid siteId) { var status = await repository.GetStatusForUserAsync(userId); if (status is null) { logger.LogInformation("No status found creating one now for user: "******"Updated users status {userId} to: {action}"); status.Update(action, siteId); await repository.UpdateAsync(status); }