declastruct-stripe-sdk
Version:
declarative control of stripe constructs, via declastruct
51 lines • 2.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCustomer = void 0;
const domain_objects_1 = require("domain-objects");
const helpful_errors_1 = require("helpful-errors");
const DeclaredStripeCustomer_1 = require("../../domain/objects/DeclaredStripeCustomer");
const castToDeclaredStripeCustomer_1 = require("../cast/castToDeclaredStripeCustomer");
/**
* .what = gets a customer from stripe
*/
const getCustomer = async (input, context) => {
// handle by ref
if (input.by.ref)
return (0, domain_objects_1.isUniqueKeyRef)({ of: DeclaredStripeCustomer_1.DeclaredStripeCustomer })(input.by.ref)
? (0, exports.getCustomer)({ by: { unique: input.by.ref } }, context)
: (0, exports.getCustomer)({ by: { primary: input.by.ref } }, context);
// handle get by id
if (input.by.primary) {
try {
const customer = await context.stripe.customers.retrieve(input.by.primary.id);
if (customer.deleted)
return null;
return (0, castToDeclaredStripeCustomer_1.castToDeclaredStripeCustomer)(customer);
}
catch (error) {
if (!(error instanceof Error))
throw error;
if (error.message.includes('No such customer'))
return null; // handle "null" responses without an error
throw error;
}
}
// handle get by email
if (input.by.unique) {
const { data: [customer, ...otherCustomers], } = await context.stripe.customers.list({
email: input.by.unique.email,
});
if (otherCustomers.length)
throw new helpful_errors_1.BadRequestError('more than one customer for this email', {
input,
customers: [customer, ...otherCustomers],
});
if (!customer)
return null;
return (0, castToDeclaredStripeCustomer_1.castToDeclaredStripeCustomer)(customer);
}
// otherwise, unexpected input
throw new helpful_errors_1.UnexpectedCodePathError('invalid input', { input });
};
exports.getCustomer = getCustomer;
//# sourceMappingURL=getCustomer.js.map