private async Task <ApttusQuote> CreateQuote(Nominations request, PartyManagement.Response.IdentifierIdentificationType partyIdentifierIdentification)
        {
            if (request != null && partyIdentifierIdentification != null)
            {
                try
                {
                    var mappedQuoteRequest = MappingHelper.Map(createQuoteMapper, new Tuple <Nominations, PartyManagement.Response.IdentifierIdentificationType>(request, partyIdentifierIdentification), "Mapping to a Create Quote Request");

                    var createQuoteResponse = await quoteManagementClient.CreateQuoteAsync(mappedQuoteRequest).ConfigureAwait(false);

                    if (createQuoteResponse?.Result?.ResultCode != MappingConstants.Success)
                    {
                        var errorData = createQuoteResponse?.Result?.Message?.Errors?.FirstOrDefault();

                        request.DataArea.Nomination.StatusCode        = MappingConstants.Error;
                        request.DataArea.Nomination.StatusDescription = $"Error encountered while creating the quote in Apttus. System: {errorData?.System}, Code:{errorData?.Code}, Description:{errorData?.Description} - {errorData?.ExtraDetail}";

                        return(null);
                    }

                    return(createQuoteResponse);
                }
                catch (Exception ex)
                {
                    request.DataArea.Nomination.StatusCode        = MappingConstants.Error;
                    request.DataArea.Nomination.StatusDescription = ex.Message;
                }
            }
            return(null);
        }
        public async Task <IActionResult> ExecuteAsync(Nominations request, CancellationToken cancellationToken)
        {
            _ = request ?? throw new ArgumentNullException(nameof(request));

            LogRequest(request);

            PartyManagement.Response.IdentifierIdentificationType partyResponse = null;

            var matchPartyResult = await MatchParty(request).ConfigureAwait(false);

            if (matchPartyResult.IsFullMatch())
            {
                if (matchPartyResult.IsPartyDead())
                {
                    await HandleError($"(Party match successful, party is deceased) (D365 Party ID: {matchPartyResult.GetD365Id()})(Apttus ID: {matchPartyResult.GetApttusId()})", request);

                    return(new OkResult());
                }
                else
                {
                    // just mapping?
                    partyResponse = ProcessPartyDataFullMatch(matchPartyResult);
                }
            }
            else if (matchPartyResult.IsPartialMatch())
            {
                await HandleError("Party match failure - Matching returned 'partial'", request);

                return(new OkResult());
            }
            else if (matchPartyResult.IsNoMatch())
            {
                partyResponse = await CreateParty(request).ConfigureAwait(false);
            }

            if (request.DataArea.Nomination.StatusCode == MappingConstants.Error)
            {
                Log.Information($"Validate Nomination to see if required fields exist");
                string validationResult = nominationValidatior.Validate(request.DataArea.Nomination);

                if (validationResult != null)
                {
                    await Reject(validationResult, request);
                }
                else
                {
                    await HandleError(null, request);
                }

                return(new OkResult());
            }

            var newQuote = await CreateQuote(request, partyResponse).ConfigureAwait(false);

            await FinaliseQuote(request, newQuote).ConfigureAwait(false);

            return(new OkResult());
        }
        private PartyManagement.Response.IdentifierIdentificationType ProcessPartyDataFullMatch(MatchPartyResponse.MatchPartyResponse matchPartyResponse)
        {
            var partyResponse        = new PartyManagement.Response.IdentifierIdentificationType();
            var matchedPartyResponse = new MatchPartyResponse.IdentifierIdentificationType();
            var matchedParty         = matchPartyResponse.DataArea.Party;

            if (matchedParty.IdentifierIdentification.Count > 0)
            {
                matchedPartyResponse = matchedParty.IdentifierIdentification.FirstOrDefault(x => string.Equals(x.IdentificationSchemeAgencyIdentifier, MappingConstants.ApttusAgencyIdentifier, StringComparison.OrdinalIgnoreCase));

                if (matchedPartyResponse != null)
                {
                    partyResponse.IdentificationSchemeAgencyIdentifier = matchedPartyResponse.IdentificationSchemeAgencyIdentifier;
                    partyResponse.IdentificationSchemeAgencyName       = matchedPartyResponse.IdentificationSchemeAgencyName;
                    partyResponse.Designation = matchedPartyResponse.Designation;
                }
            }

            return(partyResponse);
        }