stripe - simplified - - lazy loading of PDF documents
MODIFY
src/integration/billing/stripe/StripeBillingIntegration.ts
src/integration/billing/stripe/StripeBillingIntegration.ts
@@ -333,15 +333,8 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
};
// Let's persist the up-to-date data
const freshInvoiceId = await BillingStorage.saveInvoice(this.tenantID, invoiceToSave);
+ // TODO - perf improvement? - can't we just reuse
const freshBillingInvoice = await BillingStorage.getInvoice(this.tenantID, freshInvoiceId);
- // --------------------------------------------------------------------------------------
- // 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;
}
@@ -381,29 +374,6 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
}
}
- // No use-case so far - exposing it at the Billing Integration level is useless
- // public async finalizeInvoice(invoice: BillingInvoice): Promise<string> {
- // await this.checkConnection();
- // try {
- // const stripeInvoice = await this.stripe.invoices.finalizeInvoice(invoice.invoiceID);
- // invoice.downloadUrl = stripeInvoice.invoice_pdf;
- // invoice.status = BillingInvoiceStatus.OPEN;
- // invoice.downloadable = true;
- // await BillingStorage.saveInvoice(this.tenantID, invoice);
- // const invoiceDocument = await this.downloadInvoiceDocument(invoice);
- // await BillingStorage.saveInvoiceDocument(this.tenantID, invoiceDocument);
- // return stripeInvoice.invoice_pdf;
- // } catch (error) {
- // throw new BackendError({
- // message: 'Failed to finalize invoice',
- // source: Constants.CENTRAL_SERVER,
- // module: MODULE_NAME,
- // method: 'finalizeInvoice',
- // action: ServerAction.BILLING_SEND_INVOICE
- // });
- // }
- // }
-
// eslint-disable-next-line @typescript-eslint/require-await
public async consumeBillingEvent(req: Request): Promise<boolean> {
let event: { data, type: string };

MODIFY
src/storage/mongodb/BillingStorage.ts
src/storage/mongodb/BillingStorage.ts
@@ -208,42 +208,6 @@ export default class BillingStorage {
await Logging.traceEnd(tenantID, MODULE_NAME, 'savePaymentData', uniqueTimerID, updatedInvoiceMDB);
}
- public static async saveInvoiceDocument(tenantID: string, invoiceDocumentToSave: BillingInvoiceDocument): Promise<BillingInvoiceDocument> {
- // Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'saveInvoiceDocument');
- // Build Request
- // Properties to save
- const invoiceDocumentMDB: any = {
- _id: Utils.convertToObjectID(invoiceDocumentToSave.id),
- type: invoiceDocumentToSave.type,
- invoiceID: invoiceDocumentToSave.invoiceID,
- encoding: invoiceDocumentToSave.encoding,
- content: invoiceDocumentToSave.content
- };
- // Modify and return the modified document
- await global.database.getCollection<BillingInvoiceDocument>(tenantID, 'invoicedocuments').findOneAndReplace(
- { _id: invoiceDocumentMDB._id },
- invoiceDocumentMDB,
- { upsert: true }
- );
- // Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'saveInvoiceDocument', uniqueTimerID, invoiceDocumentMDB);
- return invoiceDocumentMDB._id.toHexString();
- }
-
- public static async getInvoiceDocument(tenantID: string, id: string): Promise<BillingInvoiceDocument> {
- // Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getInvoiceDocument');
- // Check Tenant
- await DatabaseUtils.checkTenant(tenantID);
- // Read DB
- const invoiceDocumentMDB = await global.database.getCollection<BillingInvoiceDocument>(tenantID, 'invoicedocuments')
- .findOne({ _id: Utils.convertToObjectID(id) });
- // Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'getInvoiceDocument', uniqueTimerID, invoiceDocumentMDB);
- return invoiceDocumentMDB;
- }
-
public static async deleteInvoice(tenantID: string, id: string): Promise<void> {
// Debug
const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'deleteInvoice');
@@ -252,9 +216,6 @@ export default class BillingStorage {
// Delete the Invoice
await global.database.getCollection<BillingInvoice>(tenantID, 'invoices')
.findOneAndDelete({ '_id': Utils.convertToObjectID(id) });
- // Delete the Invoice Document
- await global.database.getCollection<BillingInvoice>(tenantID, 'invoicedocuments')
- .findOneAndDelete({ '_id': Utils.convertToObjectID(id) });
// Debug
await Logging.traceEnd(tenantID, MODULE_NAME, 'deleteInvoice', uniqueTimerID, { id });
}
@@ -267,9 +228,6 @@ export default class BillingStorage {
// Delete the Invoice
await global.database.getCollection<BillingInvoice>(tenantID, 'invoices')
.findOneAndDelete({ 'invoiceID': id });
- // Delete the Invoice Document
- await global.database.getCollection<BillingInvoice>(tenantID, 'invoicedocuments')
- .findOneAndDelete({ 'invoiceID': id });
// Debug
await Logging.traceEnd(tenantID, MODULE_NAME, 'deleteInvoice', uniqueTimerID, { id });
}

stripe - simplified - - lazy loading of PDF documents