2022-09-15 16:40:48 +00:00
|
|
|
import find from 'lodash/find';
|
2020-11-29 22:15:26 +00:00
|
|
|
import { getAnalyticsServiceByEnvironment } from '../analyticsService';
|
2022-09-15 16:40:48 +00:00
|
|
|
import { getCurrentEventList } from '../worldState'; // eslint-disable-line import/no-cycle
|
2019-10-10 18:11:50 +00:00
|
|
|
import { // eslint-disable-line import/no-cycle
|
2018-05-06 20:12:00 +00:00
|
|
|
getUserInfo,
|
|
|
|
|
sendTxn as txnEmail,
|
|
|
|
|
} from '../email';
|
2020-04-15 19:36:53 +00:00
|
|
|
import { sendNotification as sendPushNotification } from '../pushNotifications'; // eslint-disable-line import/no-cycle
|
2018-05-06 20:12:00 +00:00
|
|
|
import shared from '../../../common';
|
Fall Festival Gem Promo (#138)
* content: add gems blocks
* gemsBlocks: include ios and android identifiers
* wip: promo code
* split common constants into multiple files
* add second promo part
* geCurrentEvent, refactor promo
* fix lint
* fix exports, use world state api
* start adding world state tests
* remove console.log
* use gems block for purchases
* remove comments
* fix most unit tests
* restore comment
* fix lint
* prevent apple/google gift tests from breaking other tests when stub is not reset
* fix unit tests, clarify tests names
* iap: use gift object when gifting gems
* allow gift object with less data
* fix iap tests, remove findById stubs
* iap: require less data from the mobile apps
* apply discounts
* add missing worldState file
* fix lint
* add test event
* start removing 20 gems option for web
* start adding support for all gems packages on web
* fix unit tests for apple, stripe and google
* amazon: support all gems blocks
* paypal: support all gems blocks
* fix payments unit tests, add tests for getGemsBlock
* web: add gems plans with discounts, update stripe
* fix amazon and paypal clients, payments success modals
* amazon pay: disabled state
* update icons, start abstracting payments buttons
* begin redesign
* redesign gems modal
* fix buttons
* fix hover color for gems modal close icon
* add key to world state current event
* extend test event length
* implement gems modals designs
* early test fall2020
* fix header banner position
* add missing files
* use iso 8601 for dates, minor ui fixes
* fix time zones
* events: fix ISO8601 format
* fix css indentation
* start abstracting banners
* refactor payments buttons
* test spooky, fix group plans box
* implement gems promo banners, refactor banners, fixes
* fix lint
* fix dates
* remove unused i18n strings
* fix stripe integration test
* fix world state integration tests
* the current active event
* add missing unit tests
* add storybook story for payments buttons component
* fix typo
* fix(stripe): correct label when gifting subscriptions
2020-09-21 14:22:13 +00:00
|
|
|
import {
|
|
|
|
|
BadRequest,
|
|
|
|
|
} from '../errors';
|
2024-03-11 14:59:57 +00:00
|
|
|
import { apiError } from '../apiError';
|
2018-05-06 20:12:00 +00:00
|
|
|
|
2020-11-29 22:15:26 +00:00
|
|
|
const analytics = getAnalyticsServiceByEnvironment();
|
|
|
|
|
|
2018-05-06 20:12:00 +00:00
|
|
|
function getGiftMessage (data, byUsername, gemAmount, language) {
|
|
|
|
|
const senderMsg = shared.i18n.t('giftedGemsFull', {
|
|
|
|
|
username: data.gift.member.profile.name,
|
|
|
|
|
sender: byUsername,
|
|
|
|
|
gemAmount,
|
|
|
|
|
}, language);
|
|
|
|
|
|
|
|
|
|
const quotedMessage = `\`${senderMsg}\``;
|
|
|
|
|
|
|
|
|
|
if (data.gift.message) return `${quotedMessage} ${data.gift.message}`;
|
|
|
|
|
|
|
|
|
|
return quotedMessage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function buyGemGift (data) {
|
|
|
|
|
const byUsername = getUserInfo(data.user, ['name']).name;
|
|
|
|
|
const gemAmount = data.gift.gems.amount || 20;
|
|
|
|
|
|
|
|
|
|
const languages = [data.user.preferences.language, data.gift.member.preferences.language];
|
|
|
|
|
|
|
|
|
|
const senderMsg = getGiftMessage(data, byUsername, gemAmount, languages[0]);
|
2019-10-08 14:57:10 +00:00
|
|
|
const receiverMsg = getGiftMessage(data, byUsername, gemAmount, languages[1]);
|
2018-06-01 13:56:01 +00:00
|
|
|
data.user.sendMessage(data.gift.member, { receiverMsg, senderMsg, save: false });
|
2018-05-06 20:12:00 +00:00
|
|
|
|
|
|
|
|
if (data.gift.member.preferences.emailNotifications.giftedGems !== false) {
|
|
|
|
|
txnEmail(data.gift.member, 'gifted-gems', [
|
2019-10-08 14:57:10 +00:00
|
|
|
{ name: 'GIFTER', content: byUsername },
|
|
|
|
|
{ name: 'X_GEMS_GIFTED', content: gemAmount },
|
2018-05-06 20:12:00 +00:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only send push notifications if sending to a user other than yourself
|
2019-10-10 18:11:50 +00:00
|
|
|
if (
|
|
|
|
|
data.gift.member._id !== data.user._id
|
|
|
|
|
&& data.gift.member.preferences.pushNotifications.giftedGems !== false
|
|
|
|
|
) {
|
2024-06-11 18:19:03 +00:00
|
|
|
await sendPushNotification(
|
2018-05-06 20:12:00 +00:00
|
|
|
data.gift.member,
|
|
|
|
|
{
|
|
|
|
|
title: shared.i18n.t('giftedGems', languages[1]),
|
2019-10-08 14:57:10 +00:00
|
|
|
message: shared.i18n.t('giftedGemsInfo', { amount: gemAmount, name: byUsername }, languages[1]),
|
2018-05-06 20:12:00 +00:00
|
|
|
identifier: 'giftedGems',
|
2019-10-08 14:57:10 +00:00
|
|
|
},
|
2018-05-06 20:12:00 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await data.gift.member.save();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 14:59:17 +00:00
|
|
|
const { MAX_GIFT_MESSAGE_LENGTH } = shared.constants;
|
|
|
|
|
export function validateGiftMessage (gift, user) {
|
|
|
|
|
if (gift.message && gift.message.length > MAX_GIFT_MESSAGE_LENGTH) {
|
|
|
|
|
throw new BadRequest(shared.i18n.t(
|
|
|
|
|
'giftMessageTooLong',
|
|
|
|
|
{ maxGiftMessageLength: MAX_GIFT_MESSAGE_LENGTH },
|
|
|
|
|
user.preferences.language,
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Fall Festival Gem Promo (#138)
* content: add gems blocks
* gemsBlocks: include ios and android identifiers
* wip: promo code
* split common constants into multiple files
* add second promo part
* geCurrentEvent, refactor promo
* fix lint
* fix exports, use world state api
* start adding world state tests
* remove console.log
* use gems block for purchases
* remove comments
* fix most unit tests
* restore comment
* fix lint
* prevent apple/google gift tests from breaking other tests when stub is not reset
* fix unit tests, clarify tests names
* iap: use gift object when gifting gems
* allow gift object with less data
* fix iap tests, remove findById stubs
* iap: require less data from the mobile apps
* apply discounts
* add missing worldState file
* fix lint
* add test event
* start removing 20 gems option for web
* start adding support for all gems packages on web
* fix unit tests for apple, stripe and google
* amazon: support all gems blocks
* paypal: support all gems blocks
* fix payments unit tests, add tests for getGemsBlock
* web: add gems plans with discounts, update stripe
* fix amazon and paypal clients, payments success modals
* amazon pay: disabled state
* update icons, start abstracting payments buttons
* begin redesign
* redesign gems modal
* fix buttons
* fix hover color for gems modal close icon
* add key to world state current event
* extend test event length
* implement gems modals designs
* early test fall2020
* fix header banner position
* add missing files
* use iso 8601 for dates, minor ui fixes
* fix time zones
* events: fix ISO8601 format
* fix css indentation
* start abstracting banners
* refactor payments buttons
* test spooky, fix group plans box
* implement gems promo banners, refactor banners, fixes
* fix lint
* fix dates
* remove unused i18n strings
* fix stripe integration test
* fix world state integration tests
* the current active event
* add missing unit tests
* add storybook story for payments buttons component
* fix typo
* fix(stripe): correct label when gifting subscriptions
2020-09-21 14:22:13 +00:00
|
|
|
export function getGemsBlock (gemsBlock) {
|
|
|
|
|
const block = shared.content.gems[gemsBlock];
|
|
|
|
|
|
|
|
|
|
if (!block) throw new BadRequest(apiError('invalidGemsBlock'));
|
2018-05-06 20:12:00 +00:00
|
|
|
|
Fall Festival Gem Promo (#138)
* content: add gems blocks
* gemsBlocks: include ios and android identifiers
* wip: promo code
* split common constants into multiple files
* add second promo part
* geCurrentEvent, refactor promo
* fix lint
* fix exports, use world state api
* start adding world state tests
* remove console.log
* use gems block for purchases
* remove comments
* fix most unit tests
* restore comment
* fix lint
* prevent apple/google gift tests from breaking other tests when stub is not reset
* fix unit tests, clarify tests names
* iap: use gift object when gifting gems
* allow gift object with less data
* fix iap tests, remove findById stubs
* iap: require less data from the mobile apps
* apply discounts
* add missing worldState file
* fix lint
* add test event
* start removing 20 gems option for web
* start adding support for all gems packages on web
* fix unit tests for apple, stripe and google
* amazon: support all gems blocks
* paypal: support all gems blocks
* fix payments unit tests, add tests for getGemsBlock
* web: add gems plans with discounts, update stripe
* fix amazon and paypal clients, payments success modals
* amazon pay: disabled state
* update icons, start abstracting payments buttons
* begin redesign
* redesign gems modal
* fix buttons
* fix hover color for gems modal close icon
* add key to world state current event
* extend test event length
* implement gems modals designs
* early test fall2020
* fix header banner position
* add missing files
* use iso 8601 for dates, minor ui fixes
* fix time zones
* events: fix ISO8601 format
* fix css indentation
* start abstracting banners
* refactor payments buttons
* test spooky, fix group plans box
* implement gems promo banners, refactor banners, fixes
* fix lint
* fix dates
* remove unused i18n strings
* fix stripe integration test
* fix world state integration tests
* the current active event
* add missing unit tests
* add storybook story for payments buttons component
* fix typo
* fix(stripe): correct label when gifting subscriptions
2020-09-21 14:22:13 +00:00
|
|
|
return block;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getAmountForGems (data) {
|
2018-05-06 20:12:00 +00:00
|
|
|
if (data.gift) return data.gift.gems.amount / 4;
|
|
|
|
|
|
Fall Festival Gem Promo (#138)
* content: add gems blocks
* gemsBlocks: include ios and android identifiers
* wip: promo code
* split common constants into multiple files
* add second promo part
* geCurrentEvent, refactor promo
* fix lint
* fix exports, use world state api
* start adding world state tests
* remove console.log
* use gems block for purchases
* remove comments
* fix most unit tests
* restore comment
* fix lint
* prevent apple/google gift tests from breaking other tests when stub is not reset
* fix unit tests, clarify tests names
* iap: use gift object when gifting gems
* allow gift object with less data
* fix iap tests, remove findById stubs
* iap: require less data from the mobile apps
* apply discounts
* add missing worldState file
* fix lint
* add test event
* start removing 20 gems option for web
* start adding support for all gems packages on web
* fix unit tests for apple, stripe and google
* amazon: support all gems blocks
* paypal: support all gems blocks
* fix payments unit tests, add tests for getGemsBlock
* web: add gems plans with discounts, update stripe
* fix amazon and paypal clients, payments success modals
* amazon pay: disabled state
* update icons, start abstracting payments buttons
* begin redesign
* redesign gems modal
* fix buttons
* fix hover color for gems modal close icon
* add key to world state current event
* extend test event length
* implement gems modals designs
* early test fall2020
* fix header banner position
* add missing files
* use iso 8601 for dates, minor ui fixes
* fix time zones
* events: fix ISO8601 format
* fix css indentation
* start abstracting banners
* refactor payments buttons
* test spooky, fix group plans box
* implement gems promo banners, refactor banners, fixes
* fix lint
* fix dates
* remove unused i18n strings
* fix stripe integration test
* fix world state integration tests
* the current active event
* add missing unit tests
* add storybook story for payments buttons component
* fix typo
* fix(stripe): correct label when gifting subscriptions
2020-09-21 14:22:13 +00:00
|
|
|
const { gemsBlock } = data;
|
|
|
|
|
|
2022-09-15 16:40:48 +00:00
|
|
|
const currentEventsList = getCurrentEventList();
|
|
|
|
|
const promoEvent = find(currentEventsList, event => Boolean(event.gemsPromo));
|
|
|
|
|
if (promoEvent && promoEvent.gemsPromo[gemsBlock.key]) {
|
|
|
|
|
return promoEvent.gemsPromo[gemsBlock.key] / 4;
|
Fall Festival Gem Promo (#138)
* content: add gems blocks
* gemsBlocks: include ios and android identifiers
* wip: promo code
* split common constants into multiple files
* add second promo part
* geCurrentEvent, refactor promo
* fix lint
* fix exports, use world state api
* start adding world state tests
* remove console.log
* use gems block for purchases
* remove comments
* fix most unit tests
* restore comment
* fix lint
* prevent apple/google gift tests from breaking other tests when stub is not reset
* fix unit tests, clarify tests names
* iap: use gift object when gifting gems
* allow gift object with less data
* fix iap tests, remove findById stubs
* iap: require less data from the mobile apps
* apply discounts
* add missing worldState file
* fix lint
* add test event
* start removing 20 gems option for web
* start adding support for all gems packages on web
* fix unit tests for apple, stripe and google
* amazon: support all gems blocks
* paypal: support all gems blocks
* fix payments unit tests, add tests for getGemsBlock
* web: add gems plans with discounts, update stripe
* fix amazon and paypal clients, payments success modals
* amazon pay: disabled state
* update icons, start abstracting payments buttons
* begin redesign
* redesign gems modal
* fix buttons
* fix hover color for gems modal close icon
* add key to world state current event
* extend test event length
* implement gems modals designs
* early test fall2020
* fix header banner position
* add missing files
* use iso 8601 for dates, minor ui fixes
* fix time zones
* events: fix ISO8601 format
* fix css indentation
* start abstracting banners
* refactor payments buttons
* test spooky, fix group plans box
* implement gems promo banners, refactor banners, fixes
* fix lint
* fix dates
* remove unused i18n strings
* fix stripe integration test
* fix world state integration tests
* the current active event
* add missing unit tests
* add storybook story for payments buttons component
* fix typo
* fix(stripe): correct label when gifting subscriptions
2020-09-21 14:22:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return gemsBlock.gems / 4;
|
2018-05-06 20:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-31 21:36:15 +00:00
|
|
|
async function updateUserBalance (data, amount) {
|
2018-05-06 20:12:00 +00:00
|
|
|
if (data.gift) {
|
2022-01-31 21:36:15 +00:00
|
|
|
await data.gift.member.updateBalance(amount, 'gift_receive', data.user._id, data.user.profile.name);
|
2018-05-06 20:12:00 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-31 21:36:15 +00:00
|
|
|
await data.user.updateBalance(amount, 'buy_money');
|
2018-05-06 20:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
Fall Festival Gem Promo (#138)
* content: add gems blocks
* gemsBlocks: include ios and android identifiers
* wip: promo code
* split common constants into multiple files
* add second promo part
* geCurrentEvent, refactor promo
* fix lint
* fix exports, use world state api
* start adding world state tests
* remove console.log
* use gems block for purchases
* remove comments
* fix most unit tests
* restore comment
* fix lint
* prevent apple/google gift tests from breaking other tests when stub is not reset
* fix unit tests, clarify tests names
* iap: use gift object when gifting gems
* allow gift object with less data
* fix iap tests, remove findById stubs
* iap: require less data from the mobile apps
* apply discounts
* add missing worldState file
* fix lint
* add test event
* start removing 20 gems option for web
* start adding support for all gems packages on web
* fix unit tests for apple, stripe and google
* amazon: support all gems blocks
* paypal: support all gems blocks
* fix payments unit tests, add tests for getGemsBlock
* web: add gems plans with discounts, update stripe
* fix amazon and paypal clients, payments success modals
* amazon pay: disabled state
* update icons, start abstracting payments buttons
* begin redesign
* redesign gems modal
* fix buttons
* fix hover color for gems modal close icon
* add key to world state current event
* extend test event length
* implement gems modals designs
* early test fall2020
* fix header banner position
* add missing files
* use iso 8601 for dates, minor ui fixes
* fix time zones
* events: fix ISO8601 format
* fix css indentation
* start abstracting banners
* refactor payments buttons
* test spooky, fix group plans box
* implement gems promo banners, refactor banners, fixes
* fix lint
* fix dates
* remove unused i18n strings
* fix stripe integration test
* fix world state integration tests
* the current active event
* add missing unit tests
* add storybook story for payments buttons component
* fix typo
* fix(stripe): correct label when gifting subscriptions
2020-09-21 14:22:13 +00:00
|
|
|
export async function buyGems (data) {
|
2018-05-06 20:12:00 +00:00
|
|
|
const amt = getAmountForGems(data);
|
|
|
|
|
|
2022-01-31 21:36:15 +00:00
|
|
|
await updateUserBalance(data, amt);
|
2019-10-10 18:11:50 +00:00
|
|
|
data.user.purchased.txnCount += 1;
|
2018-05-06 20:12:00 +00:00
|
|
|
|
|
|
|
|
if (!data.gift) txnEmail(data.user, 'donation');
|
|
|
|
|
|
|
|
|
|
analytics.trackPurchase({
|
|
|
|
|
uuid: data.user._id,
|
|
|
|
|
itemPurchased: 'Gems',
|
|
|
|
|
sku: `${data.paymentMethod.toLowerCase()}-checkout`,
|
|
|
|
|
purchaseType: 'checkout',
|
|
|
|
|
paymentMethod: data.paymentMethod,
|
|
|
|
|
quantity: 1,
|
|
|
|
|
gift: Boolean(data.gift),
|
|
|
|
|
purchaseValue: amt,
|
|
|
|
|
headers: data.headers,
|
2020-10-13 15:35:44 +00:00
|
|
|
firstPurchase: data.user.purchased.txnCount === 1,
|
2018-05-06 20:12:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (data.gift) await buyGemGift(data);
|
|
|
|
|
|
|
|
|
|
await data.user.save();
|
|
|
|
|
}
|