public async Task HandleAsync(CreateBusiness message, IRequestInfo requestInfo) { IBusinessDocument businessDocument; try { var code = await CheckCode(_numberGenerator.GenerateNumber(6)); businessDocument = _businessesFactory.CreateBusiness(message.Name, message.TradingName, message.WebAddress, message.HeadContactFirstName, message.HeadContactSecondName, message.HeadContactContactNumber, message.HeadContactEmail, message.HeadOfficePostCode, message.HeadOfficeAddressLine1, message.HeadOfficeAddressLine2, code); await _repository.Add(businessDocument); } catch (VmsException e) { _logger.LogInformation("Create business rejected: " + e.Code); _publisher.PublishEvent(new CreateBusinessRejected(e.Code, e.Message), requestInfo); return; } _logger.LogInformation("Create business succeeded."); _publisher.PublishEvent(new BusinessCreated(businessDocument.Id, businessDocument.Code), requestInfo); }
public async Task HandleAsync(UpdateHeadContact message, IRequestInfo requestInfo) { var business = await _repository.GetBusinessAsync(message.BusinessId); if (business is null) { _publisher.PublishEvent(new UpdateBusinessRejected(Codes.InvalidId, "A business with the id: " + message.BusinessId + "could not be found"), requestInfo); return; } try { business.GetContact().Update(message.FirstName, message.SecondName, message.ContactNumber, message.Email); } catch (VmsException e) { _publisher.PublishEvent(new UpdateBusinessRejected(e.Code, e.Message), requestInfo); return; } await _repository.UpdateAsync(business); _publisher.PublishEvent(new BusinessContactUpdated(), requestInfo); _logger.LogInformation("The business contact was updated successfully."); }
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(UpdateHeadOffice message, IRequestInfo requestInfo) { var business = await _repository.GetBusinessAsync(message.BusinessId); if (business is null) { _publisher.PublishEvent(new UpdateBusinessRejected(Codes.InvalidId, "A business with the id: " + message.BusinessId + "could not be found"), requestInfo); return; } try { business.GetOffice().Update(message.PostCode, message.AddressLine1, message.AddressLine2); } catch (VmsException e) { _publisher.PublishEvent(new UpdateBusinessRejected(e.Code, e.Message), requestInfo); return; } await _repository.UpdateAsync(business); _publisher.PublishEvent(new BusinessOfficeUpdated(), requestInfo); _logger.LogInformation("The business head office was updated successfully."); }
public async Task HandleAsync(UpdateSiteDetails message, IRequestInfo requestInfo) { var site = await _siteRepository.GetAsync(message.SiteId); if (site is null) { _logger.LogInformation($"Site with id: {message.SiteId} could not be found"); _publisher.PublishEvent( new SiteUpdateRejected(Codes.InvalidId, "The site cannot be retrieved to update."), requestInfo); return; } try { site.Update(message.Name, message.PostCode, message.AddressLine1, message.AddressLine2); } catch (VmsException e) { _logger.LogInformation($"Update failed with code: {e.Code} for reason: {e.Message}"); _publisher.PublishEvent(new SiteUpdateRejected(e.Code, e.Message), requestInfo); return; } await _siteRepository.Update(site); _publisher.PublishEvent(new SiteUpdated(), requestInfo); }
public async Task HandleAsync(UpdateEntryOrder message, IRequestInfo requestInfo) { var entries = await _repository.GetEntriesAsync(message.BusinessId); var specifications = entries.OrderBy(s => s.Order).ToList(); var updating = specifications.FirstOrDefault(d => d.Id == message.EntryId); if (updating is null) { _logger.LogWarning($"No entry found with ID: {message.EntryId}"); _publisher.PublishEvent(new EntryOrderUpdateRejected(Codes.InvalidId, $"Entry with the id {message.EntryId} could not be found."), requestInfo); return; } var replacing = specifications.FirstOrDefault(s => s.Order == message.Order); int oldOrder = updating.Order; _specificationAggregate.UpdateOrder(updating, message.Order); _specificationAggregate.UpdateOrder(replacing, oldOrder); await _repository.UpdateAsync(updating); await _repository.UpdateAsync(replacing); _publisher.PublishEvent(new EntryOrderUpdated(), requestInfo); }
public async Task CompleteUser(Guid code, string email, string password, string passwordConfirm) { var pending = await _pendingIdentityRepository.GetAsync(code, email); if (pending is null) { _logger.LogWarning($"Pending user not found with code: {code} and email: {email}"); throw new VmsException(Codes.InvalidCredentials, "The account registration has not been made."); } //TODO: make sure this check is done on creation of account pending. //var existing = await _identityRepository.GetByEmailAndRole(email, Roles.); //if (existing != null) // throw new VmsException(Codes.EmailInUse, "Their has already been an account created with this email."); if (password != passwordConfirm) { throw new VmsException(Codes.InvalidCredentials, "The credentials are invalid."); } var pword = _passwordManager.EncryptPassword(password); var numberCode = await GetCode(pending.BusinessId); var identity = new Domain.Identity(email, pword.Hash, pword.Salt, pending.Role, pending.BusinessId, numberCode); await _identityRepository.AddAsync(identity); await _pendingIdentityRepository.RemoveAsync(pending); _publisher.PublishEvent(new UserAccountCreated(identity.Id, identity.Email, identity.Code), RequestInfo.Empty); }
public Task HandleAsync(TestCommand message, IRequestInfo requestInfo) { if (message.IsPass) { _serviceBusMessagePublisher.PublishEvent(new TestCommandPassed("This is a happy message"), requestInfo); } else { _serviceBusMessagePublisher.PublishEvent(new TestCommandRejected("failed", "The parameter passed was false."), requestInfo); } return(Task.CompletedTask); }
public async Task CreateAdmin(string email) { if (await _identityRepository.IsEmailInUse(email, Roles.SystemAdmin) || await _pendingIdentityRepository.IsEmailInUse(email, Roles.SystemAdmin)) { throw new VmsException(Codes.EmailInUse, "The email supplied is already in use."); } var pending = new PendingIdentity(Guid.NewGuid(), email, Roles.SystemAdmin); await _pendingIdentityRepository.AddAsync(pending); _logger.LogInformation($"create code issued: {pending.Id}."); _serviceBus.PublishEvent(new PendingAdminCreated(pending.Id, pending.Email), RequestInfo.Empty); }
public async Task HandleAsync(VisitorSignOut message, IRequestInfo requestInfo) { var visitor = await _visitorsRepository.GetAsync(message.VisitorId); if (visitor is null) { _logger.LogWarning($"No visitor found with ID : ${message.VisitorId}"); _publisher.PublishEvent(new VisitorSignOutRejected(Codes.InvalidId, "You could not be signed out."), requestInfo); } _visitorAggregate.SignOut(visitor); await _visitorsRepository.UpdateAsync(visitor); _logger.LogInformation($"Visitor with id: {message.VisitorId} signed out successfully"); _publisher.PublishEvent(new VisitorSignedOut(), requestInfo); }
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 HandleAsync(DeprecateDataEntry message, IRequestInfo requestInfo) { var entries = await _repository.GetEntriesAsync(message.BusinessId); var dataSpecifications = entries.ToList(); if (!dataSpecifications.Any()) { _logger.LogWarning($"No entries found with business ID: {message.BusinessId}"); _publisher.PublishEvent(new DataEntryDeprecationRejected(Codes.InvalidBusinessId, $"Entries with the id {message.BusinessId} could not be found."), requestInfo); return; } var spec = dataSpecifications.FirstOrDefault(d => d.Id == message.Id); if (spec is null) { _logger.LogWarning($"No entry found with ID: {message.Id}"); _publisher.PublishEvent(new DataEntryDeprecationRejected(Codes.InvalidId, $"Entry with the id {message.Id} could not be found."), requestInfo); return; } if (spec.IsMandatory) { _logger.LogInformation($"{spec.Label} is set as mandatory and cannot be deprecated."); _publisher.PublishEvent(new DataEntryDeprecationRejected(Codes.InvalidId, $"{spec.Label} is set as mandatory and cannot be deprecated."), requestInfo); return; } _specificationAggregate.Deprecate(spec); await _repository.UpdateAsync(spec); dataSpecifications.Remove(spec); dataSpecifications = dataSpecifications.Where(s => s.IsMandatory == false).ToList(); var ordered = dataSpecifications.OrderBy(d => d.Order).ToList(); for (int i = 0; i < ordered.Count; i++) { _specificationAggregate.UpdateOrder(ordered[i], i + 1); await _repository.UpdateAsync(ordered[i]); } _publisher.PublishEvent(new DataEntryDeprecated(), requestInfo); _logger.LogInformation($"Entry deprecated with id: {message.Id} and other entries re-ordered."); }
public async Task HandleAsync(object message, IRequestInfo requestInfo) { //var serviceBusMessage = message as IServiceBusMessage; //if (serviceBusMessage.BelongsToSaga()) //{ // var context = SagaContext.Create(new SagaId(), "", new List<ISagaContextMetadata> // { // new SagaData(SagaData.OperationIdKey, requestInfo.OperationId.ToString()), // new SagaData(SagaData.UserIdKey, requestInfo.UserId) // }); // await _sagaCoordinator.ProcessAsync(serviceBusMessage, context); // return; //} switch (message) { case IRejectedEvent rejected: requestInfo.Fail(); _logger.LogInformation($"Operation [{requestInfo.OperationId}]: Rejected Event code: [{rejected.Code}] Reason: {rejected.Reason}"); await _operationsCache.SaveAsync(requestInfo.OperationId, requestInfo.State.ToString().ToLower(), rejected.Code, rejected.Reason); _publisher.PublishEvent(new OperationFailed(rejected.Code, rejected.Reason), requestInfo); break; case ICommand _: _logger.LogInformation($"Operation [{requestInfo.OperationId}]: PENDING"); await _operationsCache.SaveAsync(requestInfo.OperationId, RequestState.Pending.ToString()); break; case IEvent _: requestInfo.Complete(); _logger.LogInformation($"Operation: [{requestInfo.OperationId}] COMPLETE"); await _operationsCache.SaveAsync(requestInfo.OperationId, requestInfo.State.ToString().ToLower()); _publisher.PublishEvent(new OperationComplete(), requestInfo); break; default: break; } }
public async Task HandleAsync(CreateVisitor message, IRequestInfo requestInfo) { if (!await _userServiceClient.ContainsUserAsync(message.VisitingId)) { _logger.LogWarning($"No user with id: {message.VisitingId} could be found"); PublishFailure(requestInfo); return; } var site = await _siteServiceClient.GetSiteAsync(message.SiteId); if (site is null) { _logger.LogInformation($"No site found with id: {message.SiteId}"); PublishFailure(requestInfo); return; } try { await _validatorService.Validate(site.BusinessId, message.Data); } catch (VmsException e) { _messagePublisher.PublishEvent(new CreateVisitorRejected(e.Code, e.Message), requestInfo); return; } //TODO: create service to validate data entrys var visitorData = new List <VisitorData>(); foreach (var visitorDataEntry in message.Data) { visitorData.Add(_visitorAggregate.CreateData(visitorDataEntry.FieldId, visitorDataEntry.Value)); } var visitor = _visitorAggregate.Create(message.VisitingId, site.BusinessId, message.SiteId, visitorData); await _visitorsRepository.AddAsync(visitor); _messagePublisher.PublishEvent(new VisitorCreated(), requestInfo); }
public async Task HandleAsync(CreateDataEntry message, IRequestInfo requestInfo) { SpecificationDocument specificationDocument; var order = await _repository.GetNextOrderNumberAsync(message.BusinessId); try { specificationDocument = _aggregate.Create(message.Label, order, message.ValidationMessage, message.ValidationCode, message.BusinessId); } catch (VmsException e) { _publisher.PublishEvent(new DataSpecificationRejected(e.Code, e.Message), requestInfo); _logger.LogWarning(e.Message, LoggingCategories.DomainValidation); return; } await _repository.AddAsync(specificationDocument); _publisher.PublishEvent(new DataSpecificationCreated(), requestInfo); _logger.LogInformation($"Data specification labeled {message.Label} created with id: {specificationDocument.Id}"); }
public async Task HandleAsync(CreateSiteResource message, IRequestInfo requestInfo) { if (!await _siteRepository.IsSiteIdValid(message.SiteId)) { _publisher.PublishEvent(new SiteResourceRejected(Codes.InvalidSiteId, "The site in which to add the resource could not be found."), requestInfo); return; } ISiteResource resource = null; try { resource = _factory.CreateSiteResource(message.SiteId, message.Name, message.Identifier); } catch (VmsException e) { _publisher.PublishEvent(new SiteResourceRejected(e.Code, e.Message), requestInfo); } await _siteResourceRepository.AddAsync(resource); _publisher.PublishEvent(new SiteResourceCreated(), requestInfo); }
public async Task HandleAsync(UpdateBusinessDetails message, IRequestInfo requestInfo) { var business = await _repository.GetBusinessAsync(message.Id); if (business is null) { _publisher.PublishEvent(new UpdateBusinessRejected(Codes.InvalidId, "A business with the id: " + message.Id + "could not be found"), requestInfo); return; } try { business.Update(message.Name, message.TradingName, message.WebAddress); } catch (VmsException e) { _publisher.PublishEvent(new UpdateBusinessRejected(e.Code, e.Message), requestInfo); return; } await _repository.UpdateAsync(business); _publisher.PublishEvent(new BusinessDetailsUpdated(), requestInfo); _logger.LogInformation("The business was updated successfully."); }
public async Task HandleAsync(CreateSite message, IRequestInfo requestInfo) { if (!await _businessRepository.IsBusinessValidAsync(message.BusinessId)) { _publisher.PublishEvent(new CreateSiteRejected(Codes.InvalidBusinessId, "The business could not be found to create a site for."), requestInfo); return; } try { var contact = _factory.CreateContact(message.FirstName, message.SecondName, message.Email, message.ContactNumber); var site = _factory.CreateSite(message.BusinessId, message.Name, message.PostCode, message.AddressLine1, message.AddressLine2, contact); await _siteRepository.AddAsync(site); _publisher.PublishEvent(new SiteCreated(site.Id, site.Name), requestInfo); } catch (VmsException e) { _publisher.PublishEvent(new CreateSiteRejected(e.Code, e.Message), requestInfo); } }
public async Task HandleAsync(RemoveSiteResource message, IRequestInfo requestInfo) { await _repository.RemoveAsync(message.ResourceId); _publisher.PublishEvent(new SiteResourceRemoved(), requestInfo); }