Ejemplo n.º 1
0
            public async Task <Unit> Handle(CreateRecurringPaymentsCommand request, CancellationToken cancellationToken)
            {
                List <RecurringPayment> recurringPayments = await contextAdapter.Context
                                                            .RecurringPayments
                                                            .Include(x => x.ChargedAccount)
                                                            .Include(x => x.TargetAccount)
                                                            .Include(x => x.Category)
                                                            .Include(x => x.RelatedPayments)
                                                            .AsQueryable()
                                                            .IsNotExpired()
                                                            .ToListAsync();

                var recPaymentsToCreate = recurringPayments
                                          .Where(x => x.RelatedPayments.Any())
                                          .Where(x => RecurringPaymentHelper.CheckIfRepeatable(x.RelatedPayments
                                                                                               .OrderByDescending(d => d.Date)
                                                                                               .First()))
                                          .Select(x => new Payment(RecurringPaymentHelper.GetPaymentDateFromRecurring(x),
                                                                   x.Amount,
                                                                   x.Type,
                                                                   x.ChargedAccount,
                                                                   x.TargetAccount,
                                                                   x.Category,
                                                                   x.Note ?? "",
                                                                   x))
                                          .ToList();

                recPaymentsToCreate.ForEach(x => x.RecurringPayment?.SetLastRecurrenceCreatedDate());

                contextAdapter.Context.Payments.AddRange(recPaymentsToCreate);
                await contextAdapter.Context.SaveChangesAsync();

                return(Unit.Value);
            }