use of org.folio.invoices.utils.ProtectedOperationType.DELETE in project mod-invoice by folio-org.
the class InvoiceLineHelper method deleteInvoiceLine.
/**
* Deletes Invoice Line and update Invoice if deletion is allowed 1. Get invoice via searching for invoices by invoiceLine.id
* field 2. Verify if user has permission to delete invoiceLine based on acquisitions units, if not then return 3. If user has
* permission to delete then delete invoiceLine 4. Update corresponding Invoice
*
* @param lineId invoiceLine id to be deleted
*/
public CompletableFuture<Void> deleteInvoiceLine(String lineId) {
InvoiceHolder invoiceHolder = new InvoiceHolder();
return getInvoicesIfExists(lineId).thenApply(invoiceHolder::setInvoice).thenCompose(invHolder -> protectionHelper.isOperationRestricted(invHolder.getInvoice().getAcqUnitIds(), DELETE).thenApply(vVoid -> invHolder.getInvoice())).thenCompose(InvoiceRestrictionsUtil::checkIfInvoiceDeletionPermitted).thenCompose(v -> invoiceLineService.getInvoiceLine(lineId, buildRequestContext()).thenApply(invoiceHolder::setInvoiceLine)).thenCompose(invoiceHold -> orderService.deleteOrderInvoiceRelationIfLastInvoice(lineId, buildRequestContext()).exceptionally(throwable -> {
logger.error("Can't delete Order Invoice relation for lineId: {}", lineId, throwable);
List<Parameter> parameters = Collections.singletonList(new Parameter().withKey("lineId").withValue(lineId));
Error error = CANNOT_DELETE_INVOICE_LINE.toError().withParameters(parameters);
throw new HttpException(404, error);
}).thenCompose(v -> handleDeleteRequest(resourceByIdPath(INVOICE_LINES, lineId, lang), httpClient, ctx, okapiHeaders, logger)).thenCompose(v -> updateInvoiceAndLines(invoiceHold.getInvoice(), buildRequestContext())).thenCompose(invoiceLine -> deleteInvoicePoNumbers(invoiceHold.getInvoice(), invoiceHolder.getInvoiceLine(), buildRequestContext())));
}
Aggregations