Updated linkStripeCustomer to error rather than silently fail

no-issue

This allows the importer to surface errors when linking members to customers
This commit is contained in:
Fabien O'Carroll 2020-10-27 11:00:47 +00:00 committed by Fabien 'egg' O'Carroll
parent 0a5f91f3d0
commit d70aab83f6
2 changed files with 22 additions and 3 deletions

View File

@ -119,7 +119,8 @@ module.exports = function MembersApi({
const users = Users({ const users = Users({
stripe, stripe,
Member Member,
StripeCustomer
}); });
async function sendEmailWithMagicLink({email, requestedType, tokenData, options = {forceEmailType: false}, requestSrc = ''}) { async function sendEmailWithMagicLink({email, requestedType, tokenData, options = {forceEmailType: false}, requestSrc = ''}) {

View File

@ -4,7 +4,8 @@ const common = require('../lib/common');
module.exports = function ({ module.exports = function ({
stripe, stripe,
Member Member,
StripeCustomer
}) { }) {
async function get(data, options) { async function get(data, options) {
debug(`get id:${data.id} email:${data.email}`); debug(`get id:${data.id} email:${data.email}`);
@ -127,6 +128,23 @@ module.exports = function ({
await stripe.updateSubscriptionFromClient(subscriptionUpdate); await stripe.updateSubscriptionFromClient(subscriptionUpdate);
} }
async function linkStripeCustomer(id, member, options) {
if (!stripe) {
throw new common.errors.BadRequestError({
message: 'Cannot link Stripe Customer without a Stripe connection'
});
}
const existingCustomer = await StripeCustomer.findOne({customer_id: id}, options);
if (existingCustomer) {
throw new common.errors.BadRequestError({
message: 'Cannot link Stripe Customer already associated with a member'
});
}
return stripe.linkStripeCustomer(id, member, options);
}
return { return {
create, create,
update, update,
@ -141,7 +159,7 @@ module.exports = function ({
getStripeCustomer: safeStripe('getCustomer'), getStripeCustomer: safeStripe('getCustomer'),
createStripeCustomer: safeStripe('createCustomer'), createStripeCustomer: safeStripe('createCustomer'),
createComplimentarySubscription: safeStripe('createComplimentarySubscription'), createComplimentarySubscription: safeStripe('createComplimentarySubscription'),
linkStripeCustomer: safeStripe('linkStripeCustomer'), linkStripeCustomer,
linkStripeCustomerById linkStripeCustomerById
}; };
}; };