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 }); }
public async Task <EmployerAgreementView> CreateLegalEntityWithAgreement(CreateLegalEntityWithAgreementParams createParams) { var parameters = new DynamicParameters(); parameters.Add("@accountId", createParams.AccountId, DbType.Int64); parameters.Add("@companyNumber", createParams.Code, DbType.String); parameters.Add("@companyName", createParams.Name, DbType.String); parameters.Add("@CompanyAddress", createParams.Address, DbType.String); parameters.Add("@CompanyDateOfIncorporation", createParams.DateOfIncorporation, DbType.DateTime); parameters.Add("@legalEntityId", null, DbType.Int64, ParameterDirection.Output); parameters.Add("@employerAgreementId", null, DbType.Int64, ParameterDirection.Output); parameters.Add("@status", createParams.Status, DbType.String); parameters.Add("@source", createParams.Source, DbType.Int16); parameters.Add("@publicSectorDataSource", createParams.PublicSectorDataSource, DbType.Int16); parameters.Add("@sector", createParams.Sector, DbType.String); parameters.Add("@agreementType", createParams.AgreementType, DbType.Int16); parameters.Add("@accountLegalentityId", null, DbType.Int64, ParameterDirection.Output); parameters.Add("@accountLegalEntityCreated", null, DbType.Boolean, ParameterDirection.Output); parameters.Add("@agreementVersion", null, DbType.Int32, ParameterDirection.Output); await _db.Value.Database.Connection.ExecuteAsync( sql : "[employer_account].[CreateLegalEntityWithAgreement]", param : parameters, transaction : _db.Value.Database.CurrentTransaction.UnderlyingTransaction, commandType : CommandType.StoredProcedure); var legalEntityId = parameters.Get <long>("@legalEntityId"); var agreementId = parameters.Get <long>("@employerAgreementId"); var accountLegalEntityId = parameters.Get <long>("@accountLegalentityId"); var accountLegalEntityCreated = parameters.Get <bool>("@accountLegalEntityCreated"); if (accountLegalEntityCreated) { await UpdateAccountLegalEntityPublicHashedIdInternal(_db.Value.Database.Connection, _db.Value.Database.CurrentTransaction.UnderlyingTransaction, accountLegalEntityId); } return(new EmployerAgreementView { Id = agreementId, AccountId = createParams.AccountId, AccountLegalEntityId = accountLegalEntityId, LegalEntityId = legalEntityId, LegalEntityName = createParams.Name, LegalEntityCode = createParams.Code, LegalEntitySource = createParams.Source, LegalEntityAddress = createParams.Address, LegalEntityInceptionDate = createParams.DateOfIncorporation, Sector = createParams.Sector, Status = EmployerAgreementStatus.Pending, VersionNumber = parameters.Get <int>("@agreementVersion") }); }