Ensured transactions are passed correctly

no-issue
This commit is contained in:
Fabien O'Carroll 2021-01-22 15:15:31 +00:00
parent 0b8fc40cf3
commit 693cc53138

View File

@ -51,15 +51,11 @@ module.exports = class MemberRepository {
}); });
} }
// @NOTE: Use _.pick const memberData = _.pick(data, ['email', 'name', 'note', 'subscribed', 'geolocation', 'created_at']);
return this._Member.add({ return this._Member.add({
labels, ...memberData,
email: data.email, labels
name: data.name,
note: data.note,
subscribed: data.subscribed,
geolocation: data.geolocation,
created_at: data.created_at
}, options); }, options);
} }
@ -128,7 +124,7 @@ module.exports = class MemberRepository {
}); });
} }
async linkStripeCustomer(data) { async linkStripeCustomer(data, options) {
if (!this._stripeAPIService) { if (!this._stripeAPIService) {
return; return;
} }
@ -144,29 +140,29 @@ module.exports = class MemberRepository {
member_id: data.member_id, member_id: data.member_id,
name: customer.name, name: customer.name,
email: customer.email email: customer.email
}); }, options);
for (const subscription of customer.subscriptions.data) { for (const subscription of customer.subscriptions.data) {
await this.linkSubscription({ await this.linkSubscription({
id: data.member_id, id: data.member_id,
subscription subscription
}); }, options);
} }
} }
async linkSubscription(data) { async linkSubscription(data, options) {
if (!this._stripeAPIService) { if (!this._stripeAPIService) {
return; return;
} }
const member = await this._Member.findOne({ const member = await this._Member.findOne({
id: data.id id: data.id
}); }, options);
const customer = await member.related('stripeCustomers').query({ const customer = await member.related('stripeCustomers').query({
where: { where: {
customer_id: data.subscription.customer customer_id: data.subscription.customer
} }
}).fetchOne(); }).fetchOne(options);
if (!customer) { if (!customer) {
// Maybe just link the customer? // Maybe just link the customer?
@ -203,11 +199,12 @@ module.exports = class MemberRepository {
plan_amount: subscription.plan.amount, plan_amount: subscription.plan.amount,
plan_currency: subscription.plan.currency plan_currency: subscription.plan.currency
}, { }, {
...options,
subscription_id: subscription.id subscription_id: subscription.id
}); });
} }
async updateSubscription(data) { async updateSubscription(data, options) {
if (!this._stripeAPIService) { if (!this._stripeAPIService) {
return; return;
} }
@ -219,7 +216,7 @@ module.exports = class MemberRepository {
where: { where: {
subscription_id: data.subscription.subscription_id subscription_id: data.subscription.subscription_id
} }
}).fetchOne(); }).fetchOne(options);
if (!subscription) { if (!subscription) {
throw new Error('Subscription not found'); throw new Error('Subscription not found');
@ -243,15 +240,15 @@ module.exports = class MemberRepository {
}); });
} }
async setComplimentarySubscription(data) { async setComplimentarySubscription(data, options) {
if (!this._stripeAPIService) { if (!this._stripeAPIService) {
return; return;
} }
const member = await this._Member.findOne({ const member = await this._Member.findOne({
id: data.id id: data.id
}); }, options);
const subscriptions = await member.related('stripeSubscriptions').fetch(); const subscriptions = await member.related('stripeSubscriptions').fetch(options);
const activeSubscriptions = subscriptions.models.filter((subscription) => { const activeSubscriptions = subscriptions.models.filter((subscription) => {
return ['active', 'trialing', 'unpaid', 'past_due'].includes(subscription.get('status')); return ['active', 'trialing', 'unpaid', 'past_due'].includes(subscription.get('status'));
@ -275,7 +272,7 @@ module.exports = class MemberRepository {
let stripeCustomer; let stripeCustomer;
await member.related('stripeCustomers').fetch(); await member.related('stripeCustomers').fetch(options);
for (const customer of member.related('stripeCustomers').models) { for (const customer of member.related('stripeCustomers').models) {
try { try {
@ -299,7 +296,7 @@ module.exports = class MemberRepository {
member_id: data.id, member_id: data.id,
email: stripeCustomer.email, email: stripeCustomer.email,
name: stripeCustomer.name name: stripeCustomer.name
}); }, options);
} }
if (!subscriptions.length) { if (!subscriptions.length) {
@ -308,7 +305,7 @@ module.exports = class MemberRepository {
await this.linkSubscription({ await this.linkSubscription({
id: member.id, id: member.id,
subscription subscription
}); }, options);
} else { } else {
// NOTE: we should only ever have 1 active subscription, but just in case there is more update is done on all of them // NOTE: we should only ever have 1 active subscription, but just in case there is more update is done on all of them
for (const subscription of activeSubscriptions) { for (const subscription of activeSubscriptions) {
@ -320,7 +317,7 @@ module.exports = class MemberRepository {
await this.linkSubscription({ await this.linkSubscription({
id: member.id, id: member.id,
subscription: updatedSubscription subscription: updatedSubscription
}); }, options);
} }
} }
} }