Example #1
0
        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}.");
        }
Example #2
0
        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);
        }