public async Task <Result> Handle(GenerateDeliveryFormCommand request, CancellationToken token) { var delivery = await _context.Deliveries .SingleAsync(d => d.Id == request.DeliveryId, token); var producer = await _context.Producers.SingleAsync(p => p.Id == delivery.ProducerId, token); var producerSiret = (await _context.Set <BusinessLegal>().FirstOrDefaultAsync(b => b.UserId == producer.Id, token)).Siret; var client = await _context.Users.SingleAsync(s => s.Id == delivery.ClientId, token); var clientSiret = (await _context.Set <BusinessLegal>().FirstOrDefaultAsync(b => b.UserId == client.Id, token)).Siret; var data = DeliveryModelHelpers.GetDeliveryFormModel(producer, client, delivery, producerSiret, clientSiret); var result = await _pdfGenerator.GeneratePdfAsync("Bon de livraison", "DeliveryForm", data, token); if (!result.Succeeded) { return(Failure(result)); } var resultUrl = await _blobService.UploadProducerDeliveryFormAsync(delivery.ProducerId, delivery.Id, $"{delivery.Reference.AsDeliveryIdentifier()}.pdf", result.Data, token); if (!resultUrl.Succeeded) { return(Failure(resultUrl)); } delivery.SetFormUrl(resultUrl.Data); await _context.SaveChangesAsync(token); return(Success()); }
public async Task <Result> Handle(GenerateDeliveryReceiptCommand request, CancellationToken token) { var delivery = await _context.Deliveries .Include(d => d.Products) .Include(d => d.PurchaseOrders) .Include(d => d.ReturnedReturnables) .SingleAsync(d => d.Id == request.DeliveryId, token); var producer = await _context.Producers.SingleAsync(p => p.Id == delivery.ProducerId, token); var client = await _context.Users.SingleAsync(s => s.Id == delivery.ClientId, token); var data = DeliveryModelHelpers.GetDeliveryFormModel(producer, client, delivery); var result = await _pdfGenerator.GeneratePdfAsync("Bon de réception", "DeliveryReceipt", data, token); if (!result.Succeeded) { return(Failure(result)); } var resultUrl = await _blobService.UploadProducerDeliveryReceiptAsync(delivery.ProducerId, delivery.Id, $"{delivery.Reference.AsReceiptIdentifier()}.pdf", result.Data, token); if (!resultUrl.Succeeded) { return(Failure(resultUrl)); } delivery.SetReceiptUrl(resultUrl.Data); await _context.SaveChangesAsync(token); return(Success()); }