From ff93dd915909566f84a4843759d95cf648023bd1 Mon Sep 17 00:00:00 2001 From: Sabe Jones Date: Thu, 10 Dec 2020 19:22:54 -0600 Subject: [PATCH] fix(subscriptions): avoid parallelSave from recursive fn call --- website/server/libs/payments/amazon.js | 3 +++ website/server/libs/payments/apple.js | 3 +++ website/server/libs/payments/google.js | 3 +++ website/server/libs/payments/paypal.js | 3 +++ .../server/libs/payments/promoSubscription.js | 22 +++++++++++++++++++ .../server/libs/payments/stripe/checkout.js | 3 +++ website/server/libs/payments/subscriptions.js | 18 --------------- 7 files changed, 37 insertions(+), 18 deletions(-) create mode 100644 website/server/libs/payments/promoSubscription.js diff --git a/website/server/libs/payments/amazon.js b/website/server/libs/payments/amazon.js index 3425fd6127..b190dd5a7f 100644 --- a/website/server/libs/payments/amazon.js +++ b/website/server/libs/payments/amazon.js @@ -18,6 +18,7 @@ import { // eslint-disable-line import/no-cycle } from '../../models/group'; import { model as Coupon } from '../../models/coupon'; import { getGemsBlock } from './gems'; // eslint-disable-line import/no-cycle +import promoSubscription from './promoSubscription'; // eslint-disable-line import/no-cycle // TODO better handling of errors @@ -185,6 +186,8 @@ api.checkout = async function checkout (options = {}) { gift.member = await User.findById(gift.uuid).exec(); data.gift = gift; data.paymentMethod = this.constants.PAYMENT_METHOD_GIFT; + + promoSubscription(data); } await payments[method](data); diff --git a/website/server/libs/payments/apple.js b/website/server/libs/payments/apple.js index c9517784fe..021c590387 100644 --- a/website/server/libs/payments/apple.js +++ b/website/server/libs/payments/apple.js @@ -9,6 +9,7 @@ import { } from '../errors'; import { model as IapPurchaseReceipt } from '../../models/iapPurchaseReceipt'; import { model as User } from '../../models/user'; +import promoSubscription from './promoSubscription'; // eslint-disable-line import/no-cycle const api = {}; @@ -236,6 +237,8 @@ api.noRenewSubscribe = async function noRenewSubscribe (options) { gift.subscription = sub; data.gift = gift; data.paymentMethod = this.constants.PAYMENT_METHOD_GIFT; + + promoSubscription(data); } await payments.createSubscription(data); diff --git a/website/server/libs/payments/google.js b/website/server/libs/payments/google.js index 346dae981e..6cb70b7470 100644 --- a/website/server/libs/payments/google.js +++ b/website/server/libs/payments/google.js @@ -9,6 +9,7 @@ import { import { model as IapPurchaseReceipt } from '../../models/iapPurchaseReceipt'; import { model as User } from '../../models/user'; import { getGemsBlock } from './gems'; +import promoSubscription from './promoSubscription'; // eslint-disable-line import/no-cycle const api = {}; @@ -218,6 +219,8 @@ api.noRenewSubscribe = async function noRenewSubscribe (options) { gift.subscription = sub; data.gift = gift; data.paymentMethod = this.constants.PAYMENT_METHOD_GIFT; + + promoSubscription(data); } await payments.createSubscription(data); diff --git a/website/server/libs/payments/paypal.js b/website/server/libs/payments/paypal.js index e87fa5e99c..8191a3644a 100644 --- a/website/server/libs/payments/paypal.js +++ b/website/server/libs/payments/paypal.js @@ -11,6 +11,7 @@ import payments from './payments'; // eslint-disable-line import/no-cycle import { getGemsBlock } from './gems'; // eslint-disable-line import/no-cycle import { model as Coupon } from '../../models/coupon'; import { model as User } from '../../models/user'; // eslint-disable-line import/no-cycle +import promoSubscription from './promoSubscription'; // eslint-disable-line import/no-cycle import { // eslint-disable-line import/no-cycle model as Group, basicFields as basicGroupFields, @@ -162,6 +163,8 @@ api.checkoutSuccess = async function checkoutSuccess (options = {}) { data.paymentMethod = 'PayPal (Gift)'; data.gift = gift; + + promoSubscription(data); } else { data.gemsBlock = getGemsBlock(gemsBlockKey); } diff --git a/website/server/libs/payments/promoSubscription.js b/website/server/libs/payments/promoSubscription.js new file mode 100644 index 0000000000..c16fedd99d --- /dev/null +++ b/website/server/libs/payments/promoSubscription.js @@ -0,0 +1,22 @@ +import { getCurrentEvent } from '../worldState'; // eslint-disable-line import/no-cycle +import createSubscription from './payments'; // eslint-disable-line import/no-cycle + +const currentEvent = getCurrentEvent(); + +export default async function promoSubscription (data) { + if (currentEvent && currentEvent.promo && currentEvent.promo === 'g1g1') { + const promoData = { + user: data.user, + gift: { + member: data.user, + subscription: { + key: data.gift.subscription.key, + }, + }, + paymentMethod: data.paymentMethod, + promo: 'Winter', + promoUsername: data.gift.member.auth.local.username, + }; + await createSubscription(promoData); + } +} diff --git a/website/server/libs/payments/stripe/checkout.js b/website/server/libs/payments/stripe/checkout.js index fc225493ac..a727571a91 100644 --- a/website/server/libs/payments/stripe/checkout.js +++ b/website/server/libs/payments/stripe/checkout.js @@ -15,6 +15,7 @@ import { import payments from '../payments'; // eslint-disable-line import/no-cycle import { getGemsBlock } from '../gems'; // eslint-disable-line import/no-cycle import stripeConstants from './constants'; +import promoSubscription from '../promoSubscription'; // eslint-disable-line import/no-cycle function getGiftAmount (gift) { if (gift.type === 'subscription') { @@ -98,6 +99,8 @@ async function applyGemPayment (user, response, gemsBlock, gift) { if (gift) { if (gift.type === 'subscription') method = 'createSubscription'; data.paymentMethod = 'Gift'; + + promoSubscription(data); } await payments[method](data); diff --git a/website/server/libs/payments/subscriptions.js b/website/server/libs/payments/subscriptions.js index 72e120e9ff..aef10bb191 100644 --- a/website/server/libs/payments/subscriptions.js +++ b/website/server/libs/payments/subscriptions.js @@ -18,7 +18,6 @@ import { import shared from '../../../common'; import { sendNotification as sendPushNotification } from '../pushNotifications'; // eslint-disable-line import/no-cycle import calculateSubscriptionTerminationDate from './calculateSubscriptionTerminationDate'; -import { getCurrentEvent } from '../worldState'; // eslint-disable-line import/no-cycle // @TODO: Abstract to shared/constant const JOINED_GROUP_PLAN = 'joined group plan'; @@ -244,23 +243,6 @@ async function createSubscription (data) { // Only send push notifications if sending to a user other than yourself if (data.gift.member._id !== data.user._id) { - const currentEvent = getCurrentEvent(); - if (currentEvent && currentEvent.promo && currentEvent.promo === 'g1g1') { - const promoData = { - user: data.user, - gift: { - member: data.user, - subscription: { - key: data.gift.subscription.key, - }, - }, - paymentMethod: data.paymentMethod, - promo: 'Winter', - promoUsername: data.gift.member.auth.local.username, - }; - await this.createSubscription(promoData); - } - if (data.gift.member.preferences.pushNotifications.giftedSubscription !== false) { sendPushNotification(data.gift.member, {