use of org.folio.ActionProfile.FolioRecord.INVOICE in project mod-invoice by folio-org.
the class CreateInvoiceEventHandler method saveInvoiceLines.
private CompletableFuture<List<Pair<InvoiceLine, String>>> saveInvoiceLines(List<InvoiceLine> invoiceLines, Map<String, String> okapiHeaders) {
ArrayList<CompletableFuture<InvoiceLine>> futures = new ArrayList<>();
CompletableFuture<InvoiceLine> future = completedFuture(null);
InvoiceLineHelper helper = new InvoiceLineHelper(okapiHeaders, Vertx.currentContext(), DEFAULT_LANG);
for (InvoiceLine invoiceLine : invoiceLines) {
future = future.thenCompose(v -> helper.createInvoiceLine(invoiceLine));
futures.add(future);
}
List<Pair<InvoiceLine, String>> savingResults = new ArrayList<>();
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).handle((v, throwable) -> {
for (int i = 0; i < futures.size(); i++) {
InvoiceLine mappedInvoiceLine = invoiceLines.get(i);
int invLineNumber = i + 1;
futures.get(i).whenComplete((savedInvLine, e) -> {
if (e != null) {
logger.error("Error to create invoice line with number {}", invLineNumber, e);
}
Pair<InvoiceLine, String> invoiceLineToMsg = e == null ? Pair.of(savedInvLine, null) : Pair.of(mappedInvoiceLine, e.getMessage());
savingResults.add(invoiceLineToMsg);
});
}
return savingResults;
});
}
use of org.folio.ActionProfile.FolioRecord.INVOICE in project mod-invoice by folio-org.
the class CreateInvoiceEventHandler method handle.
@Override
public CompletableFuture<DataImportEventPayload> handle(DataImportEventPayload dataImportEventPayload) {
CompletableFuture<DataImportEventPayload> future = new CompletableFuture<>();
dataImportEventPayload.setEventType(DI_INVOICE_CREATED.value());
try {
HashMap<String, String> payloadContext = dataImportEventPayload.getContext();
if (payloadContext == null || isBlank(payloadContext.get(EDIFACT_INVOICE.value()))) {
logger.error(PAYLOAD_HAS_NO_DATA_MSG);
return CompletableFuture.failedFuture(new EventProcessingException(PAYLOAD_HAS_NO_DATA_MSG));
}
Map<String, String> okapiHeaders = DataImportUtils.getOkapiHeaders(dataImportEventPayload);
CompletableFuture<Map<Integer, PoLine>> poLinesFuture = getAssociatedPoLines(dataImportEventPayload, okapiHeaders);
poLinesFuture.thenAccept(invLineNoToPoLine -> ensureAdditionalData(dataImportEventPayload, invLineNoToPoLine)).thenAccept(v -> prepareEventPayloadForMapping(dataImportEventPayload)).thenAccept(v -> MappingManager.map(dataImportEventPayload, new MappingContext())).thenAccept(v -> prepareMappingResult(dataImportEventPayload)).thenCompose(v -> saveInvoice(dataImportEventPayload, okapiHeaders)).thenApply(savedInvoice -> prepareInvoiceLinesToSave(savedInvoice.getId(), dataImportEventPayload, poLinesFuture.join())).thenCompose(preparedInvoiceLines -> saveInvoiceLines(preparedInvoiceLines, okapiHeaders)).whenComplete((savedInvoiceLines, throwable) -> {
makeLightweightReturnPayload(dataImportEventPayload);
if (throwable == null) {
List<InvoiceLine> invoiceLines = savedInvoiceLines.stream().map(Pair::getLeft).collect(Collectors.toList());
InvoiceLineCollection invoiceLineCollection = new InvoiceLineCollection().withInvoiceLines(invoiceLines).withTotalRecords(invoiceLines.size());
dataImportEventPayload.getContext().put(INVOICE_LINES_KEY, Json.encode(invoiceLineCollection));
Map<Integer, String> invoiceLinesErrors = prepareInvoiceLinesErrors(savedInvoiceLines);
if (!invoiceLinesErrors.isEmpty()) {
dataImportEventPayload.getContext().put(INVOICE_LINES_ERRORS_KEY, Json.encode(invoiceLinesErrors));
future.completeExceptionally(new EventProcessingException("Error during invoice lines creation"));
return;
}
future.complete(dataImportEventPayload);
} else {
preparePayloadWithMappedInvoiceLines(dataImportEventPayload);
logger.error("Error during invoice creation", throwable);
future.completeExceptionally(throwable);
}
});
} catch (Exception e) {
logger.error("Error during creation invoice and invoice lines", e);
future.completeExceptionally(e);
}
return future;
}
Aggregations