UNPKG

declastruct-stripe-sdk

Version:

declarative control of stripe constructs, via declastruct

36 lines 1.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setInvoiceOpened = void 0; const helpful_errors_1 = require("helpful-errors"); const visualogic_1 = require("visualogic"); const DeclaredStripeInvoice_1 = require("../../domain/objects/DeclaredStripeInvoice"); const castToDeclaredStripeInvoice_1 = require("../cast/castToDeclaredStripeInvoice"); const getInvoice_1 = require("./getInvoice"); /** * .what = set the status of an invoice to "open" * .what.intent * - issues / finalizes the invoice in order to make it payable * - enables autoAdvance to issue automaticPayment once collection config has it enabled */ exports.setInvoiceOpened = (0, visualogic_1.withLogTrail)(async (input, context) => { // find the invoice const invoiceFound = await (0, getInvoice_1.getInvoice)(input, context); if (!invoiceFound) throw new helpful_errors_1.BadRequestError('can not open an invoice that does not exist', { input, invoiceFound, }); // verify it is still in draft mode if (invoiceFound.status === DeclaredStripeInvoice_1.DeclaredStripeInvoiceStatus.open) return invoiceFound; // if its already "open", then this could be an idempotent retry, so just return it if (invoiceFound.status !== DeclaredStripeInvoice_1.DeclaredStripeInvoiceStatus.draft) throw new helpful_errors_1.BadRequestError('can not open an invoice that is not in draft mode', { invoiceFound }); // otherwise, finalize it const stripeInvoiceUpdated = await context.stripe.invoices.finalizeInvoice(invoiceFound.id, { auto_advance: input.with?.autoAdvance, }); return (0, castToDeclaredStripeInvoice_1.castToDeclaredStripeInvoice)(stripeInvoiceUpdated); }, { name: (0, visualogic_1.getResourceNameFromFileName)(__filename), }); //# sourceMappingURL=setInvoiceOpened.js.map