public Task <Result <CounterpartyInfo> > Update(CounterpartyEditRequest changedCounterpartyInfo, int counterpartyId, string languageCode)
        {
            return(GetCounterparty(counterpartyId)
                   .Bind(UpdateCounterparty));


            async Task <Result <CounterpartyInfo> > UpdateCounterparty(Counterparty counterpartyToUpdate)
            {
                var(_, isFailure, error) = CounterpartyValidator.Validate(changedCounterpartyInfo);
                if (isFailure)
                {
                    return(Result.Failure <CounterpartyInfo>(error));
                }

                counterpartyToUpdate.Address                = changedCounterpartyInfo.Address;
                counterpartyToUpdate.City                   = changedCounterpartyInfo.City;
                counterpartyToUpdate.CountryCode            = changedCounterpartyInfo.CountryCode;
                counterpartyToUpdate.Fax                    = changedCounterpartyInfo.Fax;
                counterpartyToUpdate.Name                   = changedCounterpartyInfo.Name;
                counterpartyToUpdate.Phone                  = changedCounterpartyInfo.Phone;
                counterpartyToUpdate.Website                = changedCounterpartyInfo.Website;
                counterpartyToUpdate.PostalCode             = changedCounterpartyInfo.PostalCode;
                counterpartyToUpdate.PreferredCurrency      = changedCounterpartyInfo.PreferredCurrency;
                counterpartyToUpdate.PreferredPaymentMethod = changedCounterpartyInfo.PreferredPaymentMethod;
                counterpartyToUpdate.Updated                = _dateTimeProvider.UtcNow();
                counterpartyToUpdate.VatNumber              = changedCounterpartyInfo.VatNumber;
                counterpartyToUpdate.BillingEmail           = changedCounterpartyInfo.BillingEmail;

                _context.Counterparties.Update(counterpartyToUpdate);
                await _context.SaveChangesAsync();

                return(await Get(counterpartyId, languageCode));
            }
        }
        public async Task Update_not_existing_counterparty_should_fail()
        {
            var counterpartyToUpdate = new CounterpartyEditRequest();

            var(_, isFailure, _, _) = await _counterpartyManagementService.Update(counterpartyToUpdate, 1, "en");

            Assert.True(isFailure);
        }
        public async Task <IActionResult> UpdateCounterparty(int counterpartyId, [FromBody] CounterpartyEditRequest updateCounterpartyRequest)
        {
            var(_, isFailure, savedCounterpartyInfo, error) =
                await _counterpartyManagementService.Update(updateCounterpartyRequest, counterpartyId, LanguageCode);

            if (isFailure)
            {
                return(BadRequest(ProblemDetailsBuilder.Build(error)));
            }

            return(Ok(savedCounterpartyInfo));
        }
Beispiel #4
0
        public async Task <Result <CounterpartyInfo> > Add(CounterpartyEditRequest counterparty)
        {
            var(_, isFailure, error) = CounterpartyValidator.Validate(counterparty);
            if (isFailure)
            {
                return(Result.Failure <CounterpartyInfo>(error));
            }

            var now = _dateTimeProvider.UtcNow();
            var createdCounterparty = new Counterparty
            {
                Address                = counterparty.Address,
                City                   = counterparty.City,
                CountryCode            = counterparty.CountryCode,
                Fax                    = counterparty.Fax,
                Name                   = counterparty.Name,
                Phone                  = counterparty.Phone,
                Website                = counterparty.Website,
                PostalCode             = counterparty.PostalCode,
                PreferredCurrency      = counterparty.PreferredCurrency,
                PreferredPaymentMethod = counterparty.PreferredPaymentMethod,
                State                  = CounterpartyStates.PendingVerification,
                Created                = now,
                Updated                = now
            };

            _context.Counterparties.Add(createdCounterparty);
            await _context.SaveChangesAsync();

            var defaultAgency = new Agency
            {
                Name           = createdCounterparty.Name,
                CounterpartyId = createdCounterparty.Id,
                Created        = now,
                Modified       = now,
                ParentId       = null,
            };

            _context.Agencies.Add(defaultAgency);

            await _context.SaveChangesAsync();

            return(await GetCounterpartyInfo(createdCounterparty.Id));
        }
        public async Task Counterparty_update_should_pass()
        {
            var counterpartyToUpdate = new CounterpartyEditRequest(
                name: "RenamedName",
                address: "changed address",
                countryCode: "AF",
                city: "changed city",
                phone: "79265748556",
                fax: "79265748336",
                postalCode: "changed code",
                preferredCurrency: Currencies.EUR,
                preferredPaymentMethod: PaymentMethods.Offline,
                website: "changed website",
                vatNumber: "changed vatNumber",
                billingEmail: "changed email"
                );

            var(_, isFailure, counterparty, error) = await _counterpartyManagementService.Update(counterpartyToUpdate, 1, "en");

            Assert.False(isFailure);
            Assert.True(counterparty.Name == "RenamedName");
            Assert.True(_context.Counterparties.Single(c => c.Id == 1).Name != "Test");
        }
        public Task <Result> RegisterWithCounterparty(AgentEditableInfo agentData, CounterpartyEditRequest counterpartyData, string externalIdentity,
                                                      string email)
        {
            return(Result.Success()
                   .Ensure(IsIdentityPresent, "User should have identity")
                   .BindWithTransaction(_context, () => Result.Success()
                                        .Bind(CreateCounterparty)
                                        .Bind(CreateAgent)
                                        .Tap(AddMasterCounterpartyRelation))
                   .Bind(LogSuccess)
                   .Bind(SendRegistrationMailToAdmins)
                   .OnFailure(LogFailure));

            bool IsIdentityPresent() => !string.IsNullOrWhiteSpace(externalIdentity);


            Task <Result <CounterpartyInfo> > CreateCounterparty() => _counterpartyService.Add(counterpartyData);


            async Task <Result <(CounterpartyInfo, Agent)> > CreateAgent(CounterpartyInfo counterparty)
            {
                var(_, isFailure, agent, error) = await _agentService.Add(agentData, externalIdentity, email);

                return(isFailure
                    ? Result.Failure <(CounterpartyInfo, Agent)>(error)
                    : Result.Success((counterparty1: counterparty, agent)));
            }

            async Task AddMasterCounterpartyRelation((CounterpartyInfo counterparty, Agent agent) counterpartyUserInfo)
            {
                var(counterparty, agent) = counterpartyUserInfo;
                var defaultAgency = await _counterpartyService.GetDefaultAgency(counterparty.Id);

                await AddCounterpartyRelation(agent,
                                              AgentAgencyRelationTypes.Master,
                                              PermissionSets.Master,
                                              defaultAgency.Id);
            }

            async Task <Result> SendRegistrationMailToAdmins(CounterpartyInfo counterpartyInfo)
            {
                var agent = $"{agentData.Title} {agentData.FirstName} {agentData.LastName}";

                if (!string.IsNullOrWhiteSpace(agentData.Position))
                {
                    agent += $" ({agentData.Position})";
                }

                var messageData = new RegistrationDataForAdmin
                {
                    Counterparty = counterpartyInfo,
                    AgentEmail   = email,
                    AgentName    = agent
                };

                return(await _mailSender.Send(_notificationOptions.MasterAgentMailTemplateId, _notificationOptions.AdministratorsEmails, messageData));
            }

            Result <CounterpartyInfo> LogSuccess((CounterpartyInfo, Agent) registrationData)
            {
                var(counterparty, agent) = registrationData;
                _logger.LogAgentRegistrationSuccess($"Agent {agent.Email} with counterparty '{counterparty.Name}' successfully registered");
                return(Result.Success(counterparty));
            }

            void LogFailure(string error)
            {
                _logger.LogAgentRegistrationFailed(error);
            }
        }