public async Task <CreateLegalEntityCommandResponse> Handle(CreateLegalEntityCommand message) { var validationResult = await _validator.ValidateAsync(message); if (!validationResult.IsValid()) { throw new InvalidRequestException(validationResult.ValidationDictionary); } if (validationResult.IsUnauthorized) { throw new UnauthorizedAccessException(); } var owner = await _membershipRepository.GetCaller(message.HashedAccountId, message.ExternalUserId); var ownerExternalUserId = Guid.Parse(owner.UserRef); var createParams = new CreateLegalEntityWithAgreementParams { AccountId = owner.AccountId, Name = message.Name, Status = message.Status, Code = string.IsNullOrEmpty(message.Code) ? Guid.NewGuid().ToString() : message.Code, DateOfIncorporation = message.DateOfIncorporation, PublicSectorDataSource = message.PublicSectorDataSource, Source = message.Source, Address = message.Address, Sector = message.Sector }; var agreementView = await _accountRepository.CreateLegalEntityWithAgreement(createParams); agreementView.HashedAgreementId = _hashingService.HashValue(agreementView.Id); await CreateAuditEntries(owner, agreementView); await NotifyLegalEntityCreated(message.HashedAccountId, agreementView.LegalEntityId); var accountId = _hashingService.DecodeValue(message.HashedAccountId); await EvaluateEmployerLegalEntityAgreementStatus(owner.AccountId, agreementView.LegalEntityId); agreementView.AccountLegalEntityPublicHashedId = _accountLegalEntityPublicHashingService.HashValue(agreementView.AccountLegalEntityId); await PublishLegalEntityAddedMessage(accountId, agreementView.Id, createParams.Name, owner.FullName(), agreementView.LegalEntityId, agreementView.AccountLegalEntityId, agreementView.AccountLegalEntityPublicHashedId, message.Code, message.Address, message.Source, ownerExternalUserId); await PublishAgreementCreatedMessage(accountId, agreementView.Id, createParams.Name, owner.FullName(), agreementView.LegalEntityId, ownerExternalUserId); await _agreementService.RemoveFromCacheAsync(accountId); return(new CreateLegalEntityCommandResponse { AgreementView = agreementView }); }
public async Task <CreateLegalEntityCommandResponse> Handle(CreateLegalEntityCommand message) { var validationResult = await _validator.ValidateAsync(message); if (!validationResult.IsValid()) { throw new InvalidRequestException(validationResult.ValidationDictionary); } if (validationResult.IsUnauthorized) { throw new UnauthorizedAccessException(); } var owner = await _membershipRepository.GetCaller(message.HashedAccountId, message.ExternalUserId); var ownerExternalUserId = owner.UserRef; var createParams = new CreateLegalEntityWithAgreementParams { AccountId = owner.AccountId, Name = message.Name, Status = message.Status, Code = string.IsNullOrEmpty(message.Code) ? Guid.NewGuid().ToString() : message.Code, DateOfIncorporation = message.DateOfIncorporation, PublicSectorDataSource = message.PublicSectorDataSource, Source = message.Source, Address = message.Address, Sector = message.Sector, AgreementType = await UserIsWhitelistedForEOIOrThereIsAlreadyAnEOIAgreementForThisAccount(owner) ? AgreementType.NonLevyExpressionOfInterest : AgreementType.Combined }; var agreementView = await _accountRepository.CreateLegalEntityWithAgreement(createParams); agreementView.HashedAgreementId = _hashingService.HashValue(agreementView.Id); var accountId = _hashingService.DecodeValue(message.HashedAccountId); agreementView.AccountLegalEntityPublicHashedId = _accountLegalEntityPublicHashingService.HashValue(agreementView.AccountLegalEntityId); await Task.WhenAll( CreateAuditEntries(owner, agreementView), NotifyLegalEntityCreated(message.HashedAccountId, agreementView.LegalEntityId), SetEmployerLegalEntityAgreementStatus(agreementView.AccountLegalEntityId, agreementView.Id, agreementView.VersionNumber), PublishLegalEntityAddedMessage(accountId, agreementView.Id, createParams.Name, owner.FullName(), agreementView.LegalEntityId, agreementView.AccountLegalEntityId, agreementView.AccountLegalEntityPublicHashedId, createParams.Code, message.Address, message.Source, ownerExternalUserId), PublishAgreementCreatedMessage(accountId, agreementView.Id, createParams.Name, owner.FullName(), agreementView.LegalEntityId, ownerExternalUserId) ); return(new CreateLegalEntityCommandResponse { AgreementView = agreementView }); }
private Task PublishLegalEntityAddedMessage(long accountId, long legalEntityId, long employerAgreementId, long accountLegalEntityId, string organisationName, string organisationReferenceNumber, string organisationAddress, OrganisationType organisationType, string userName, Guid userRef) { var accountLegalEntityPublicHashedId = _accountLegalEntityPublicHashingService.HashValue(accountLegalEntityId); return(_eventPublisher.Publish(new AddedLegalEntityEvent { AgreementId = employerAgreementId, LegalEntityId = legalEntityId, OrganisationName = organisationName, AccountId = accountId, AccountLegalEntityId = accountLegalEntityId, AccountLegalEntityPublicHashedId = accountLegalEntityPublicHashedId, UserName = userName, UserRef = userRef, Created = DateTime.UtcNow, OrganisationReferenceNumber = organisationReferenceNumber, OrganisationAddress = organisationAddress, OrganisationType = (SFA.DAS.EmployerAccounts.Types.Models.OrganisationType)organisationType })); }