stripe - invoice - potpone PDF document download
MODIFY
src/integration/billing/stripe/StripeBillingIntegration.ts
src/integration/billing/stripe/StripeBillingIntegration.ts
@@ -334,11 +334,14 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
// Let's persist the up-to-date data
const freshInvoiceId = await BillingStorage.saveInvoice(this.tenantID, invoiceToSave);
const freshBillingInvoice = await BillingStorage.getInvoice(this.tenantID, freshInvoiceId);
- if (!billingInvoice?.downloadable && freshBillingInvoice?.downloadable) {
- // Perf optimization - download the PDF document (only when downloadable flag has changed)
- const invoiceDocument = await this.downloadInvoiceDocument(freshBillingInvoice);
- await BillingStorage.saveInvoiceDocument(this.tenantID, invoiceDocument);
- }
+ // --------------------------------------------------------------------------------------
+ // TODO - downloading the PDF is slow - it makes sense to postpone it!
+ // --------------------------------------------------------------------------------------
+ // if (!billingInvoice?.downloadable && freshBillingInvoice?.downloadable) {
+ // // Perf optimization - download the PDF document (only when downloadable flag has changed)
+ // const invoiceDocument = await this.downloadInvoiceDocument(freshBillingInvoice);
+ // await BillingStorage.saveInvoiceDocument(this.tenantID, invoiceDocument);
+ // }
return freshBillingInvoice;
}
@@ -360,7 +363,7 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
}
public async downloadInvoiceDocument(invoice: BillingInvoice): Promise<BillingInvoiceDocument> {
- if (invoice.downloadUrl && invoice.downloadUrl !== '') {
+ if (invoice.downloadUrl) {
// Get document
const response = await this.axiosInstance.get(invoice.downloadUrl, {
responseType: 'arraybuffer'

MODIFY
src/server/rest/v1/service/BillingService.ts
src/server/rest/v1/service/BillingService.ts
@@ -595,11 +595,11 @@ export default class BillingService {
// Filter
const filteredRequest = BillingSecurity.filterDownloadInvoiceRequest(req.query);
// Get the Invoice
- const invoice = await BillingStorage.getInvoice(req.user.tenantID, filteredRequest.ID);
- UtilsService.assertObjectExists(action, invoice, `Invoice ID '${filteredRequest.ID}' does not exist`,
+ const billingInvoice = await BillingStorage.getInvoice(req.user.tenantID, filteredRequest.ID);
+ UtilsService.assertObjectExists(action, billingInvoice, `Invoice ID '${filteredRequest.ID}' does not exist`,
MODULE_NAME, 'handleDownloadInvoice', req.user);
// Check Auth
- if (!Authorizations.canDownloadInvoiceBilling(req.user, invoice.userID)) {
+ if (!Authorizations.canDownloadInvoiceBilling(req.user, billingInvoice.userID)) {
throw new AppAuthError({
errorCode: HTTPAuthError.FORBIDDEN,
user: req.user,
@@ -619,14 +619,12 @@ export default class BillingService {
user: req.user
});
}
- // Get the Invoice Document
- const invoiceDocument = await BillingStorage.getInvoiceDocument(req.user.tenantID, invoice.id);
- UtilsService.assertObjectExists(action, invoiceDocument, `Invoice Document ID '${filteredRequest.ID}' does not exist`,
- MODULE_NAME, 'handleDownloadInvoice', req.user);
+ // Get the PDF document directly from the Invoice
+ const invoiceDocument = await billingImpl.downloadInvoiceDocument(billingInvoice);
// Send the Document
if (invoiceDocument && invoiceDocument.content) {
const base64RawData = invoiceDocument.content.split(`;${invoiceDocument.encoding},`).pop();
- const filename = 'invoice_' + invoice.id + '.' + invoiceDocument.type;
+ const filename = 'invoice_' + billingInvoice.id + '.' + invoiceDocument.type;
fs.writeFile(filename, base64RawData, { encoding: invoiceDocument.encoding }, (err) => {
if (err) {
console.error(err);
@@ -648,7 +646,7 @@ export default class BillingService {
}
}
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/require-await
public static async handleBillingChargeInvoice(action: ServerAction, req: Request, res: Response): Promise<void> {
// TODO - no use-case for this endpoint so far! - only used for troubleshooting!

stripe - invoice - potpone PDF document download