public async Task UpdateAsync(ClientProfileSettings model, string username, string correlationId) { var existing = await _regulatorySettingsRepository.GetByIdsAsync(model.ClientProfileId, model.AssetTypeId); if (existing == null) { throw new ClientSettingsDoNotExistException(); } var regulatorySettings = await _regulatorySettingsApi.GetRegulatorySettingsByIdsAsync(existing.RegulatoryProfileId, existing.RegulatoryTypeId); //This should not happen when we handle deleting of RegulatorySettings in MDM if (regulatorySettings.ErrorCode == RegulationsErrorCodesContract.RegulatorySettingsDoNotExist) { throw new RegulatorySettingsDoNotExistException(); } if (model.IsAvailable && !regulatorySettings.RegulatorySettings.IsAvailable) { throw new CannotSetToAvailableException(); } if (model.Margin > 100 || model.Margin < regulatorySettings.RegulatorySettings.MarginMinPercent) { throw new InvalidMarginValueException(); } if (model.OnBehalfFee < 0) { throw new InvalidOnBehalfFeeException(); } if (model.ExecutionFeesRate < 0 || model.ExecutionFeesRate > 100) { throw new InvalidExecutionFeesRateException(); } if (model.ExecutionFeesCap < model.ExecutionFeesFloor) { throw new InvalidExecutionFeesCapException(); } if (model.ExecutionFeesFloor > model.ExecutionFeesCap || model.ExecutionFeesFloor < 0) { throw new InvalidExecutionFeesFloorException(); } await _regulatorySettingsRepository.UpdateAsync(model); var referenceId = $"ClientProfileId:{model.ClientProfileId},AssetTypeId:{model.AssetTypeId}"; await _auditService.TryAudit(correlationId, username, referenceId, AuditDataType.ClientProfileSettings, model.ToJson(), existing.ToJson()); await _entityChangedSender .SendEntityEditedEvent <ClientProfileSettings, ClientProfileSettingsContract, ClientProfileSettingsChangedEvent>(existing, model, username, correlationId); }
public async Task InsertAsync(AssetTypeWithTemplate model, string username, string correlationId) { var brokerSettingsResponse = await _brokerSettingsApi.GetByIdAsync(_brokerId); if (brokerSettingsResponse.ErrorCode == BrokerSettingsErrorCodesContract.BrokerSettingsDoNotExist) { throw new BrokerSettingsDoNotExistException(); } await ValidateUnderlyingCategory(model.UnderlyingCategoryId); var regulationId = brokerSettingsResponse.BrokerSettings.RegulationId; await ValidateRegulatoryType(model.RegulatoryTypeId, regulationId); List <ClientProfileSettings> clientProfileSettings; //duplicate settings if we use template if (!string.IsNullOrEmpty(model.AssetTypeTemplateId)) { var regulatoryProfileTemplateExists = await _assetTypesRepository.ExistsAsync(model.AssetTypeTemplateId); if (!regulatoryProfileTemplateExists) { throw new AssetTypeDoesNotExistException(); } clientProfileSettings = await _clientProfileSettingsRepository.GetAllAsync(null, model.AssetTypeTemplateId); foreach (var clientProfileSetting in clientProfileSettings) { clientProfileSetting.AssetTypeId = model.Id; var regulatorySettings = await _regulatorySettingsApi.GetRegulatorySettingsByIdsAsync( clientProfileSetting.RegulatoryProfileId, model.RegulatoryTypeId); ValidateRegulatoryConstraint(regulatorySettings, clientProfileSetting); } } else { clientProfileSettings = new List <ClientProfileSettings>(); var allRegulatorySettings = await _regulatorySettingsApi.GetRegulatorySettingsByRegulationAsync(regulationId); var clientProfiles = await _clientProfilesRepository.GetAllAsync(); foreach (var clientProfile in clientProfiles) { var regulatorySettings = allRegulatorySettings.RegulatorySettings.Single(x => x.TypeId == model.RegulatoryTypeId && x.ProfileId == clientProfile.RegulatoryProfileId); clientProfileSettings.Add(new ClientProfileSettings { AssetTypeId = model.Id, ClientProfileId = clientProfile.Id, Margin = regulatorySettings.MarginMinPercent, IsAvailable = regulatorySettings.IsAvailable, }); } } await _assetTypesRepository.InsertAsync(model, clientProfileSettings); await _auditService.TryAudit(correlationId, username, model.Id, AuditDataType.AssetType, model.ToJson()); await _entityChangedSender.SendEntityCreatedEvent <AssetType, AssetTypeContract, AssetTypeChangedEvent>(model, username, correlationId); foreach (var profileSettings in clientProfileSettings) { await _entityChangedSender .SendEntityCreatedEvent <ClientProfileSettings, ClientProfileSettingsContract, ClientProfileSettingsChangedEvent>(profileSettings, username, correlationId); } }