Exemplo n.º 1
0
        public async Task <ErrorCodeResponse <ClientProfilesErrorCodesContract> > UpdateClientProfileSettingsAsync(
            [FromBody] UpdateClientProfileSettingsRequest request,
            [FromRoute][Required] string profileId,
            [FromRoute][Required] string typeId)
        {
            var response = new ErrorCodeResponse <ClientProfilesErrorCodesContract>();

            var correlationId = this.TryGetCorrelationId();

            var model = _convertService.Convert <UpdateClientProfileSettingsRequest, ClientProfileSettings>(request);

            model.ClientProfileId = profileId;
            model.AssetTypeId     = typeId;

            try
            {
                await _clientProfileSettingsService.UpdateAsync(model, request.Username, correlationId);
            }
            catch (InvalidMarginValueException)
            {
                response.ErrorCode = ClientProfilesErrorCodesContract.InvalidMarginValue;
            }
            catch (CannotSetToAvailableException)
            {
                response.ErrorCode =
                    ClientProfilesErrorCodesContract.CannotSetToAvailableBecauseOfRegulatoryRestriction;
            }
            catch (ClientSettingsDoNotExistException)
            {
                response.ErrorCode = ClientProfilesErrorCodesContract.ClientProfileSettingsDoNotExist;
            }
            catch (RegulatorySettingsDoNotExistException)
            {
                response.ErrorCode = ClientProfilesErrorCodesContract.RegulatorySettingsAreMissing;
            }
            catch (InvalidOnBehalfFeeException)
            {
                response.ErrorCode = ClientProfilesErrorCodesContract.InvalidOnBehalfFeeValue;
            }
            catch (InvalidExecutionFeesRateException)
            {
                response.ErrorCode = ClientProfilesErrorCodesContract.InvalidExecutionFeesRate;
            }
            catch (InvalidExecutionFeesCapException)
            {
                response.ErrorCode = ClientProfilesErrorCodesContract.InvalidExecutionFeesCap;
            }
            catch (InvalidExecutionFeesFloorException)
            {
                response.ErrorCode = ClientProfilesErrorCodesContract.InvalidExecutionFeesFloor;
            }

            return(response);
        }
        public async Task TestClientProfileSettingsWorkflow()
        {
            _brokerSettingsApiMock.Setup(x => x.GetByIdAsync(It.IsAny <string>()))
            .ReturnsAsync(new GetBrokerSettingsByIdResponse
            {
                ErrorCode      = BrokerSettingsErrorCodesContract.None,
                BrokerSettings = new BrokerSettingsContract {
                    RegulationId = RegulationId
                }
            });

            _regulatoryTypesApiMock.Setup(x => x.GetRegulatoryTypeByIdAsync(RegulatoryTypeId))
            .ReturnsAsync(new GetRegulatoryTypeByIdResponse
            {
                RegulatoryType = new RegulatoryTypeContract
                {
                    RegulationId = RegulationId
                }
            });

            _regulatoryProfilesApiMock.Setup(x => x.GetRegulatoryProfileByIdAsync(RegulatoryProfileId))
            .ReturnsAsync(new GetRegulatoryProfileByIdResponse
            {
                RegulatoryProfile = new RegulatoryProfileContract
                {
                    RegulationId = RegulationId
                }
            });

            _regulatorySettingsApiMock.Setup(x => x.GetRegulatorySettingsByIdsAsync(It.IsAny <string>(), It.IsAny <string>()))
            .ReturnsAsync(new GetRegulatorySettingsByIdsResponse()
            {
                RegulatorySettings = new RegulatorySettingsContract()
                {
                    IsAvailable      = true,
                    MarginMinPercent = MarginMinPercent,
                }
            });

            _regulatorySettingsApiMock.Setup(x => x.GetRegulatorySettingsByRegulationAsync(It.IsAny <string>()))
            .ReturnsAsync(new GetRegulatorySettingsResponse()
            {
                RegulatorySettings = new List <RegulatorySettingsContract>
                {
                    new RegulatorySettingsContract
                    {
                        MarginMinPercent = MarginMinPercent,
                        IsAvailable      = true,
                        TypeId           = RegulatoryTypeId,
                        ProfileId        = RegulatoryProfileId
                    }
                }
            });

            _underlyingCategoriesCacheMock.Setup(x => x.Get()).ReturnsAsync(new List <UnderlyingCategoryCacheModel>
            {
                new UnderlyingCategoryCacheModel
                {
                    Id = UnderlyingCategoryId,
                    FinancingFeesFormula = ""
                }
            });

            _underlyingCategoriesApiMock.Setup(x => x.GetAllAsync()).ReturnsAsync(new GetUnderlyingCategoriesResponse
            {
                UnderlyingCategories = new List <UnderlyingCategoryContract>
                {
                    new UnderlyingCategoryContract
                    {
                        Id = UnderlyingCategoryId,
                        FinancingFeesFormula = ""
                    }
                }
            });

            var client = await TestBootstrapper.CreateTestClientWithInMemoryDb(builder =>
            {
                builder.RegisterInstance(_brokerSettingsApiMock.Object).As <IBrokerSettingsApi>().SingleInstance();
                builder.RegisterInstance(_regulationsApiMock.Object).As <IRegulationsApi>().SingleInstance();
                builder.RegisterInstance(_regulatoryTypesApiMock.Object).As <IRegulatoryTypesApi>().SingleInstance();
                builder.RegisterInstance(_regulatorySettingsApiMock.Object).As <IRegulatorySettingsApi>().SingleInstance();
                builder.RegisterInstance(_regulatoryProfilesApiMock.Object).As <IRegulatoryProfilesApi>().SingleInstance();
                builder.RegisterInstance(_underlyingCategoriesApiMock.Object).As <IUnderlyingCategoriesApi>().SingleInstance();
                builder.RegisterInstance(_underlyingCategoriesCacheMock.Object).As <IUnderlyingCategoriesCache>().SingleInstance();
            });

            await TestRecordsCreator.CreateClientProfileAsync(client, RegulatoryProfileId, ClientProfileId, true);

            await TestRecordsCreator.CreateAssetTypeAsync(client, RegulatoryTypeId, AssetTypeId, UnderlyingCategoryId);

            var updateClientProfileSettingsRequest = new UpdateClientProfileSettingsRequest
            {
                Margin      = MarginRate,
                IsAvailable = true,
                Username    = "******"
            };

            await client.PutAsync($"/api/client-profile-settings/profile/{ClientProfileId}/type/{AssetTypeId}",
                                  updateClientProfileSettingsRequest.ToJsonStringContent());

            await TestRecordsCreator.CreateAssetTypeAsync(client, RegulatoryTypeId, SecondAssetTypeId,
                                                          UnderlyingCategoryId, AssetTypeId);

            await TestRecordsCreator.CreateClientProfileAsync(client, RegulatoryProfileId, SecondClientProfileId, false, ClientProfileId);

            //Get all client profile settings for this regulation
            var getClientProfileSettingsRequest = await client.GetAsync($"/api/client-profile-settings");

            var clientProfileSettings = (await getClientProfileSettingsRequest.Content.ReadAsStringAsync())
                                        .DeserializeJson <GetAllClientProfileSettingsResponse>().ClientProfileSettings;

            //Check if the result contains settings with copied values from the templates
            var containsSettingsCreatedFromProfileTemplate = clientProfileSettings.Any(s =>
                                                                                       s.IsAvailable && s.AssetTypeId == AssetTypeId && s.ClientProfileId == SecondClientProfileId &&
                                                                                       s.Margin == MarginRate);

            var containsSettingsCreatedFromTypeTemplate = clientProfileSettings.Any(s =>
                                                                                    s.IsAvailable && s.AssetTypeId == SecondAssetTypeId && s.ClientProfileId == ClientProfileId &&
                                                                                    s.Margin == MarginRate);

            Assert.True(containsSettingsCreatedFromProfileTemplate);
            Assert.True(containsSettingsCreatedFromTypeTemplate);
            Assert.Equal(4, clientProfileSettings.Count);
        }