UNPKG

declastruct-stripe-sdk

Version:

declarative control of stripe constructs, via declastruct

95 lines 4.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setProduct = void 0; const domain_objects_1 = require("domain-objects"); const hash_fns_1 = require("hash-fns"); const helpful_errors_1 = require("helpful-errors"); const type_fns_1 = require("type-fns"); const visualogic_1 = require("visualogic"); const castToDeclaredStripeProduct_1 = require("../cast/castToDeclaredStripeProduct"); const getProduct_1 = require("./getProduct"); /** * .what = sets a product */ exports.setProduct = (0, visualogic_1.withLogTrail)(async (input, context) => { // define the desired product const productDesired = input.upsert ?? input.finsert; // lookup the product const productFound = await (0, getProduct_1.getProduct)({ by: { unique: { id: input.finsert?.id ?? input.upsert?.id ?? helpful_errors_1.UnexpectedCodePathError.throw('no id in input', { input }), }, }, }, context); // sanity check that if the product exists, their id matches the user's expectations, if any const primaryIdExpected = productDesired.id; if (productFound && primaryIdExpected && primaryIdExpected !== productFound.id) throw new helpful_errors_1.UnexpectedCodePathError('asked to setProduct with a desired.primary=id which does not match the found.primary=id', { primaryIdExpected, primaryIdFound: productFound.id, productFound, }); // if the product was found, then handle that if (productFound) { // if asked to finsert, then we can return it now if (input.finsert) return productFound; // if asked to upsert, then we can update it now if (input.upsert) { // if the price has changed, create the new price explicitly const priceDesired = productFound.price.amount === productDesired.price.amount ? undefined : await context.stripe.prices.create({ currency: productDesired.price.currency === 'USD' ? 'usd' : helpful_errors_1.UnexpectedCodePathError.throw('todo: support non usd currencies', { productDesired, }), unit_amount: productDesired.price.amount, product: productFound.id, }); // cast to the new price return (0, castToDeclaredStripeProduct_1.castToDeclaredStripeProduct)(await context.stripe.products.update(productFound.id, { name: input.upsert.name ?? undefined, description: input.upsert.description ?? undefined, default_price: priceDesired?.id, metadata: input.upsert.metadata ?? undefined, // images: input.upsert.images // todo: support images expand: ['default_price'], })); } } // otherwise, create the product const productCreated = await context.stripe.products.create({ id: productDesired.id, // stripe allows us to choose a custom id... so, this is really an exid... url: productDesired.url ?? undefined, name: productDesired.name, active: productDesired.active, description: productDesired.description ?? undefined, default_price_data: { currency: productDesired.price.currency === 'USD' ? 'usd' : helpful_errors_1.UnexpectedCodePathError.throw('todo: support non usd currencies', { productDesired, }), unit_amount: productDesired.price.amount, }, // images: input.upsert.images // todo: support images metadata: productDesired.metadata ?? undefined, expand: ['default_price'], }, { idempotencyKey: (0, hash_fns_1.toHashSha256Sync)([ 'v1.0.0', (0, domain_objects_1.serialize)((0, type_fns_1.pick)(productDesired, ['id'])), // make it idempotent on the unique keys; // todo: use domain-objects.getUniqueKeys({ of: DeclaredStripeInvoice }) instead of hardcoding the keys ].join(';')), }); return (0, castToDeclaredStripeProduct_1.castToDeclaredStripeProduct)(productCreated); }, { name: (0, visualogic_1.getResourceNameFromFileName)(__filename), }); //# sourceMappingURL=setProduct.js.map