public async Task <bool> Handle(UpdateOrganisationCompanyNumberRequest request, CancellationToken cancellationToken) { if (!_validator.IsValidCompanyNumber(request.CompanyNumber)) { var invalidCompanyNumberError = $@"Invalid Organisation Company Number '{request.CompanyNumber}'"; _logger.LogInformation(invalidCompanyNumberError); throw new BadRequestException(invalidCompanyNumberError); } var duplicateCompanyNumberDetails = _validator.DuplicateCompanyNumberInAnotherOrganisation(request.CompanyNumber, request.OrganisationId); if (duplicateCompanyNumberDetails.DuplicateFound) { var duplicateCompanyNumerMessage = $@"Company number '{request.CompanyNumber}' already used against organisation '{duplicateCompanyNumberDetails.DuplicateOrganisationName}'"; _logger.LogInformation(duplicateCompanyNumerMessage); throw new BadRequestException(duplicateCompanyNumerMessage); } _logger.LogInformation($@"Handling Update '{FieldChanged}' for Organisation ID [{request.OrganisationId}]"); var auditRecord = _auditLogService.AuditCompanyNumber(request.OrganisationId, request.UpdatedBy, request.CompanyNumber); if (!auditRecord.ChangesMade) { return(await Task.FromResult(false)); } var success = await _updateOrganisationRepository.UpdateCompanyNumber(request.OrganisationId, request.CompanyNumber?.ToUpper(), request.UpdatedBy); if (!success) { return(await Task.FromResult(false)); } return(await _updateOrganisationRepository.WriteFieldChangesToAuditLog(auditRecord)); }
public Task <Guid?> Handle(CreateOrganisationRequest request, CancellationToken cancellationToken) { request.LegalName = _textSanitiser.SanitiseInputText(request.LegalName); request.TradingName = _textSanitiser.SanitiseInputText(request.TradingName); if (!IsValidCreateOrganisation(request)) { string invalidOrganisationError = $@"Invalid Organisation data"; if (!_organisationValidator.IsValidLegalName(request.LegalName)) { invalidOrganisationError = $"{invalidOrganisationError}: Invalid Legal Name [{request.LegalName}]"; } if (!_organisationValidator.IsValidTradingName(request.TradingName)) { invalidOrganisationError = $"{invalidOrganisationError}: Invalid Trading Name [{request.TradingName}]"; } if (!_providerTypeValidator.IsValidProviderTypeId(request.ProviderTypeId)) { invalidOrganisationError = $"{invalidOrganisationError}: Invalid Provider Type Id [{request.ProviderTypeId}]"; } if (!_organisationValidator.IsValidOrganisationTypeId(request.OrganisationTypeId)) { invalidOrganisationError = $"{invalidOrganisationError}: Invalid Organisation Type Id [{request.OrganisationTypeId}]"; } if (!_organisationValidator.IsValidStatusDate(request.StatusDate)) { invalidOrganisationError = $"{invalidOrganisationError}: Invalid Status Date [{request.StatusDate}]"; } if (!_organisationValidator.IsValidUKPRN(request.Ukprn)) { invalidOrganisationError = $"{invalidOrganisationError}: Invalid ukprn [{request.Ukprn}]"; } var duplicateUkrnDetails = _organisationValidator.DuplicateUkprnInAnotherOrganisation(request.Ukprn, Guid.NewGuid()); if (duplicateUkrnDetails.DuplicateFound) { invalidOrganisationError = $"{invalidOrganisationError}: Duplicate ukprn '{request.Ukprn}' already exists against [{duplicateUkrnDetails.DuplicateOrganisationName}]"; } if (!_organisationValidator.IsValidCompanyNumber(request.CompanyNumber)) { invalidOrganisationError = $"{invalidOrganisationError}: Invalid company number [{request.CompanyNumber}]"; } if (!_organisationValidator.IsValidApplicationDeterminedDate(request.ApplicationDeterminedDate)) { invalidOrganisationError = $"{invalidOrganisationError}: Invalid Application Determined Date [{request.ApplicationDeterminedDate}]"; } if (!string.IsNullOrEmpty(request.CompanyNumber)) { var duplicateCompanyNumber = _organisationValidator.DuplicateCompanyNumberInAnotherOrganisation(request.CompanyNumber, Guid.NewGuid()); if (duplicateCompanyNumber.DuplicateFound) { invalidOrganisationError = $"{invalidOrganisationError}: Duplicate company number '{request.CompanyNumber}' already exists against [{duplicateCompanyNumber.DuplicateOrganisationName}]"; } } if (!_organisationValidator.IsValidCharityNumber(request.CharityNumber)) { invalidOrganisationError = $"{invalidOrganisationError}: Invalid charity registration number [{request.CharityNumber}]"; } if (!string.IsNullOrEmpty(request.CharityNumber)) { var duplicateCharityNumber = _organisationValidator.DuplicateCharityNumberInAnotherOrganisation(request.CharityNumber, Guid.NewGuid()); if (duplicateCharityNumber.DuplicateFound) { invalidOrganisationError = $"{invalidOrganisationError}: Duplicate charity registration number '{request.CharityNumber}' already exists against [{duplicateCharityNumber.DuplicateOrganisationName}]"; } } _logger.LogInformation(invalidOrganisationError); throw new BadRequestException(invalidOrganisationError); } _logger.LogInformation($@"Handling Create Organisation Search for UKPRN [{request.Ukprn}]"); var command = _mapper.Map(request); var organisationId = _organisationRepository.CreateOrganisation(command); if (request.ProviderTypeId != ProviderType.SupportingProvider) { _eventsRepository.AddOrganisationStatusEvents(command.Ukprn, command.OrganisationStatusId, command.StatusDate); } return(organisationId); }