public IEnumerable <EndorsementDto> GetEndorsements(int billId)
        {
            var billOfExchange = _billsOfExchangeService.GetById(billId);

            if (billOfExchange.DrawerId == billOfExchange.BeneficiaryId)
            {
                throw new ApplicationException("Bill of exchange has same drawer as beneficiary.");
            }

            var         endorsements       = _repository.GetByBillIds(new[] { billId }).First().ToList();
            var         list               = new LinkedList <Endorsement>();
            Endorsement currentEndorsement = null;

            while (currentEndorsement == null ||
                   endorsements.Any(x => x.PreviousEndorsementId == currentEndorsement.Id))
            {
                currentEndorsement = currentEndorsement == null
                    ? GetFirstEndorsement(endorsements)
                    : endorsements.FirstOrDefault(x => x.PreviousEndorsementId == currentEndorsement.Id);

                if (list.Any(x => x.Id == currentEndorsement.Id))
                {
                    throw new ApplicationException(
                              "Indosament data inconsistency - circle reference between endorsements");
                }

                if (currentEndorsement.NewBeneficiaryId == billOfExchange.BeneficiaryId)
                {
                    throw new ApplicationException(
                              $"Endorsement has the same new beneficiary party as bill of exchange");
                }

                list.AddLast(currentEndorsement);
            }

            if (list.Count != endorsements.Count)
            {
                throw new ApplicationException("List of endorsement is not linked.");
            }

            var newBeneficiaries = list.Select(x => x.NewBeneficiaryId).ToList();

            if (newBeneficiaries.Count() != newBeneficiaries.Distinct().Count())
            {
                throw new ApplicationException("Indosament data inconsistenci - same new beneficiary id");
            }

            return(MapToDto(list));
        }
예제 #2
0
        public IEnumerable <BillOfExchangeDto> GetBillsByOwner(int ownerId)
        {
            var ownedBills = new List <BillOfExchange>();
            List <BillOfExchange> billsByBeneficiaryId =
                billOfExchangeRepository.GetByBeneficiaryIds(new List <int>()
            {
                ownerId
            })
                .SelectMany(x => x ?? new List <BillOfExchange>()).ToList();

            ownedBills.AddRange(GetBillsWithoutEndorsements(billsByBeneficiaryId));
            ownedBills.AddRange(GetOwnedBills(ownerId));

            foreach (BillOfExchange bill in ownedBills)
            {
                ValidateBill(bill);
            }

            return(mapper.Map <IEnumerable <BillOfExchangeDto> >(ownedBills));

            IEnumerable <BillOfExchange> GetBillsWithoutEndorsements(List <BillOfExchange> bills)
            {
                if (!bills.Any() || bills.FirstOrDefault() == null)
                {
                    return(new List <BillOfExchange>());
                }

                IReadOnlyList <IEnumerable <Endorsement> > endorsementsByBillIds =
                    endorsementRepository.GetByBillIds(bills.Select(x => x.Id).ToList());

                bills.RemoveAll(x => endorsementsByBillIds.SelectMany(y => y).Any(y => y.BillId == x.Id));
                return(bills);
            }

            IEnumerable <BillOfExchange> GetOwnedBills(int billOwnerId)
            {
                IReadOnlyList <IEnumerable <Endorsement> > endorsementsByBeneficiaryIds =
                    endorsementRepository.GetByNewBeneficiaryIds(new List <int>()
                {
                    billOwnerId
                });

                if (!endorsementsByBeneficiaryIds.Any() || endorsementsByBeneficiaryIds.FirstOrDefault() == null)
                {
                    return(new List <BillOfExchange>());
                }

                IReadOnlyList <IEnumerable <Endorsement> > endorsementsByBillIds =
                    endorsementRepository.GetByBillIds(endorsementsByBeneficiaryIds.SelectMany(x => x)
                                                       .Select(x => x.BillId)
                                                       .Distinct()
                                                       .ToList());
                List <int> billIds = (from endorsements in endorsementsByBillIds
                                      select endorsements.LastOrDefault()
                                      into endorsement
                                      where endorsement != null && endorsement.NewBeneficiaryId == billOwnerId
                                      select endorsement.BillId).ToList();

                return(billOfExchangeRepository.GetByIds(billIds));
            }
        }