use of org.folio.rest.core.models.RequestContext in project mod-invoice by folio-org.
the class InvoiceHelper method handleInvoiceStatusTransition.
private CompletionStage<Void> handleInvoiceStatusTransition(Invoice invoice, Invoice invoiceFromStorage, List<InvoiceLine> invoiceLines) {
if (isTransitionToApproved(invoiceFromStorage, invoice)) {
return approveInvoice(invoice, invoiceLines);
} else if (isAfterApprove(invoice, invoiceFromStorage) && isExchangeRateChanged(invoice, invoiceFromStorage)) {
return handleExchangeRateChange(invoice, invoiceLines);
} else if (isTransitionToPaid(invoiceFromStorage, invoice)) {
RequestContext requestContext = new RequestContext(ctx, okapiHeaders);
if (isExchangeRateChanged(invoice, invoiceFromStorage)) {
return handleExchangeRateChange(invoice, invoiceLines).thenCompose(aVoid1 -> invoicePaymentService.payInvoice(invoice, invoiceLines, requestContext));
}
invoice.setExchangeRate(invoiceFromStorage.getExchangeRate());
return invoicePaymentService.payInvoice(invoice, invoiceLines, requestContext);
} else if (isTransitionToCancelled(invoiceFromStorage, invoice)) {
RequestContext requestContext = new RequestContext(ctx, okapiHeaders);
return invoiceCancelService.cancelInvoice(invoiceFromStorage, invoiceLines, requestContext);
}
return CompletableFuture.completedFuture(null);
}
use of org.folio.rest.core.models.RequestContext in project mod-invoice by folio-org.
the class InvoiceHelper method getInvoices.
/**
* Gets list of invoice
*
* @param limit Limit the number of elements returned in the response
* @param offset Skip over a number of elements by specifying an offset value for the query
* @param query A query expressed as a CQL string using valid searchable fields
* @return completable future with {@link InvoiceCollection} on success or an exception if processing fails
*/
public CompletableFuture<InvoiceCollection> getInvoices(int limit, int offset, String query) {
CompletableFuture<InvoiceCollection> future = new FolioVertxCompletableFuture<>(ctx);
RequestContext requestContext = new RequestContext(ctx, okapiHeaders);
try {
buildGetInvoicesQuery(query).thenCompose(getInvoicesQuery -> invoiceService.getInvoices(getInvoicesQuery, offset, limit, requestContext)).thenCompose(invoiceCollection -> invoiceService.updateInvoicesTotals(invoiceCollection, requestContext).thenAccept(v -> {
logger.info("Successfully retrieved invoices: {}", invoiceCollection);
future.complete(invoiceCollection);
})).exceptionally(t -> {
logger.error("Error getting invoices", t);
future.completeExceptionally(t);
return null;
});
} catch (Exception e) {
future.completeExceptionally(e);
}
return future;
}
use of org.folio.rest.core.models.RequestContext in project mod-invoice by folio-org.
the class VouchersImpl method postVoucherVoucherNumberStartByValue.
@Validate
@Override
public void postVoucherVoucherNumberStartByValue(String value, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
logger.info("== Re(set) the current start value of the voucher number sequence ==");
voucherNumberService.setStartValue(value, new RequestContext(vertxContext, okapiHeaders)).thenAccept(ok -> asyncResultHandler.handle(succeededFuture(buildNoContentResponse()))).exceptionally(fail -> handleErrorResponse(asyncResultHandler, fail));
}
use of org.folio.rest.core.models.RequestContext in project mod-invoice by folio-org.
the class RestClient method post.
public <T> CompletableFuture<T> post(RequestEntry requestEntry, T entity, RequestContext requestContext, Class<T> responseType) {
CompletableFuture<T> future = new CompletableFuture<>();
String endpoint = requestEntry.buildEndpoint();
JsonObject recordData = JsonObject.mapFrom(entity);
if (logger.isDebugEnabled()) {
logger.debug("Sending 'POST {}' with body: {}", endpoint, Optional.ofNullable(recordData).map(JsonObject::encodePrettily).orElse(null));
}
HttpClientInterface client = getHttpClient(requestContext.getHeaders());
try {
client.request(HttpMethod.POST, Optional.ofNullable(recordData).map(JsonObject::toBuffer).orElse(null), endpoint, requestContext.getHeaders()).thenApply(HelperUtils::verifyAndExtractBody).thenAccept(body -> {
client.closeClient();
T responseEntity = Optional.ofNullable(body).map(json -> json.mapTo(responseType)).orElse(null);
if (logger.isDebugEnabled()) {
logger.debug("'POST {}' request successfully processed. Record with '{}' id has been created", endpoint, body);
}
future.complete(responseEntity);
}).exceptionally(t -> {
client.closeClient();
logger.error("'POST {}' request failed. Request body: {}", endpoint, Optional.ofNullable(recordData).map(JsonObject::encodePrettily).orElse(null), t.getCause());
future.completeExceptionally(t.getCause());
return null;
});
} catch (Exception e) {
logger.error("'POST {}' request failed. Request body: {}", endpoint, Optional.ofNullable(recordData).map(JsonObject::encodePrettily).orElse(null), e);
client.closeClient();
future.completeExceptionally(e);
}
return future;
}
use of org.folio.rest.core.models.RequestContext in project mod-invoice by folio-org.
the class RestClient method delete.
public CompletableFuture<Void> delete(RequestEntry requestEntry, RequestContext requestContext) {
CompletableFuture<Void> future = new CompletableFuture<>();
String endpoint = requestEntry.buildEndpoint();
if (logger.isDebugEnabled()) {
logger.debug(CALLING_ENDPOINT_MSG, HttpMethod.DELETE, endpoint);
}
HttpClientInterface client = getHttpClient(requestContext.getHeaders());
setDefaultHeaders(client);
try {
client.request(HttpMethod.DELETE, endpoint, requestContext.getHeaders()).thenAccept(HelperUtils::verifyResponse).thenAccept(aVoid -> {
client.closeClient();
future.complete(null);
}).exceptionally(t -> {
client.closeClient();
logger.error(String.format(EXCEPTION_CALLING_ENDPOINT_MSG, HttpMethod.DELETE, endpoint, requestContext), t);
future.completeExceptionally(t.getCause());
return null;
});
} catch (Exception e) {
client.closeClient();
logger.error(String.format(EXCEPTION_CALLING_ENDPOINT_MSG, HttpMethod.DELETE, endpoint, requestContext), e);
future.completeExceptionally(e);
}
return future;
}
Aggregations