Ghost/ghost/members-api/lib/users.js
Fabien O'Carroll 4ead495b45 Ensured that destroying member removes stripe customer
no-issue

This also mean sthe subscription will be cancelled
2019-09-06 14:30:27 +08:00

52 lines
1.2 KiB
JavaScript

module.exports = function ({
stripe,
createMember,
getMember,
listMembers,
deleteMember
}) {
async function get(data, options) {
const member = await getMember(data, options);
if (!member) {
return member;
}
if (!stripe) {
return Object.assign(member, {
plans: []
});
}
try {
const subscription = await stripe.getSubscription(member);
if (subscription.status !== 'active') {
return Object.assign(member, {
plans: []
});
}
return Object.assign(member, {
plans: [subscription.plan]
});
} catch (err) {
return null;
}
}
async function destroy(data, options) {
const member = await getMember(data, options);
if (!member) {
return;
}
if (stripe) {
await stripe.removeCustomer(member);
}
return deleteMember(data, options);
}
return {
create: createMember,
list: listMembers,
get,
destroy
};
};