Esempio n. 1
0
        public async Task <ClientProfile> InsertAsync(ClientProfileWithTemplate model, IEnumerable <ClientProfileSettings> clientProfileSettingsToAdd)
        {
            var entity = new ClientProfileEntity
            {
                Id = model.Id,
                RegulatoryProfileId = model.RegulatoryProfileId,
                IsDefault           = model.IsDefault,
            };

            var clientProfileSettingsEntities = clientProfileSettingsToAdd.Select(ClientProfileSettingsEntity.Create).ToArray();

            await using var context = _contextFactory.CreateDataContext();
            var currentDefault = await context.ClientProfiles.FirstOrDefaultAsync(x => x.IsDefault);

            var defaultProfileChanged = false;

            if (currentDefault?.Id == model.Id)
            {
                throw new AlreadyExistsException();
            }

            if (currentDefault == null)
            {
                entity.IsDefault = true;
            }
            else if (model.IsDefault)
            {
                currentDefault.IsDefault = false;
                context.Update(currentDefault);
                defaultProfileChanged = true;
            }

            context.ClientProfiles.Add(entity);

            context.ClientProfileSettings.AddRange(clientProfileSettingsEntities);

            try
            {
                await context.SaveChangesAsync();
            }
            catch (DbUpdateException e)
            {
                if (e.InnerException is SqlException sqlException &&
                    sqlException.Number == MsSqlErrorCodes.PrimaryKeyConstraintViolation)
                {
                    throw new AlreadyExistsException();
                }

                throw;
            }

            return(defaultProfileChanged
                ? new ClientProfile(currentDefault.Id, currentDefault.RegulatoryProfileId, currentDefault.IsDefault)
                : null);
        }
        public async Task <ErrorCodeResponse <ClientProfilesErrorCodesContract> > AddClientProfileAsync([FromBody] AddClientProfileRequest request)
        {
            var response = new ErrorCodeResponse <ClientProfilesErrorCodesContract>();

            var correlationId = this.TryGetCorrelationId();

            try
            {
                var clientProfile = new ClientProfileWithTemplate(
                    request.Id,
                    request.RegulatoryProfileId,
                    request.ClientProfileTemplateId,
                    request.IsDefault);
                await _regulatoryProfilesService.InsertAsync(clientProfile, request.Username, correlationId);
            }
            catch (ClientProfileDoesNotExistException)
            {
                response.ErrorCode = ClientProfilesErrorCodesContract.ClientProfileDoesNotExist;
            }
            catch (AlreadyExistsException)
            {
                response.ErrorCode = ClientProfilesErrorCodesContract.AlreadyExist;
            }
            catch (BrokerSettingsDoNotExistException)
            {
                response.ErrorCode = ClientProfilesErrorCodesContract.BrokerSettingsDoNotExist;
            }
            catch (RegulatoryProfileDoesNotExistException)
            {
                response.ErrorCode = ClientProfilesErrorCodesContract.RegulatoryProfileInMdmIsMissing;
            }
            catch (RegulatorySettingsDoNotExistException)
            {
                response.ErrorCode = ClientProfilesErrorCodesContract.RegulatorySettingsAreMissing;
            }
            catch (RegulationConstraintViolationException)
            {
                response.ErrorCode = ClientProfilesErrorCodesContract.RegulationConstraintViolation;
            }
            catch (ClientProfileNonDefaultUpdateForbiddenException)
            {
                response.ErrorCode = ClientProfilesErrorCodesContract.NonDefaultUpdateForbidden;
            }

            return(response);
        }