declastruct-stripe-sdk
Version:
declarative control of stripe constructs, via declastruct
38 lines • 1.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.setCoupon = void 0;
const domain_objects_1 = require("domain-objects");
const hash_fns_1 = require("hash-fns");
const helpful_errors_1 = require("helpful-errors");
const visualogic_1 = require("visualogic");
const castToDeclaredStripeDiscountCoupon_1 = require("../../cast/castToDeclaredStripeDiscountCoupon");
/**
* .what = sets a coupon
*
* .note
* - there's no way to find a coupon by unique, so they're always inserted
*/
exports.setCoupon = (0, visualogic_1.withLogTrail)(async (input, context) => {
const couponDesired = input.insert;
const couponCreated = await context.stripe.coupons.create({
name: couponDesired.name ?? undefined,
duration: typeof couponDesired.duration === 'string'
? couponDesired.duration
: couponDesired.duration.choice,
duration_in_months: typeof couponDesired.duration !== 'string'
? couponDesired.duration.months
: undefined,
percent_off: couponDesired.percentOff ?? undefined,
amount_off: couponDesired.amountOff?.amount ?? undefined,
currency: couponDesired.amountOff
? couponDesired.amountOff.currency === 'USD'
? 'usd'
: helpful_errors_1.UnexpectedCodePathError.throw('todo: support other currencies')
: undefined,
metadata: couponDesired.metadata ?? undefined,
}, {
idempotencyKey: (0, hash_fns_1.toHashSha256Sync)(['v1.0.1', (0, domain_objects_1.serialize)({ ...couponDesired })].join(';')),
});
return (0, castToDeclaredStripeDiscountCoupon_1.castToDeclaredStripeDiscountCoupon)(couponCreated);
}, { name: (0, visualogic_1.getResourceNameFromFileName)(__filename) });
//# sourceMappingURL=setCoupon.js.map