From d78b7445f000448488c63a0089d41008e83a25bf Mon Sep 17 00:00:00 2001 From: Joey Zhou Date: Thu, 9 Jan 2025 16:57:58 -0800 Subject: [PATCH] feat: check/apply churn coupon --- src/app/functions/server/applyCoupon.ts | 99 +++++++++++++++++++++---- 1 file changed, 84 insertions(+), 15 deletions(-) diff --git a/src/app/functions/server/applyCoupon.ts b/src/app/functions/server/applyCoupon.ts index 9731976260a..626d411f7c6 100644 --- a/src/app/functions/server/applyCoupon.ts +++ b/src/app/functions/server/applyCoupon.ts @@ -12,16 +12,90 @@ import { import { applyCoupon } from "../../../utils/fxa"; export async function applyCurrentCouponCode(subscriber: SubscriberRow) { - logger.info("fxa_apply_coupon_code", { + logger.info("fxa_apply_current_coupon_code", { subscriber: subscriber.id, }); const currentCouponCode = process.env.CURRENT_COUPON_CODE_ID; if (!currentCouponCode) { logger.error( - "fxa_apply_coupon_code_failed", + "fxa_apply_current_coupon_code_failed", "Coupon code ID is not set. Please set the env var: CURRENT_COUPON_CODE_ID", ); + return { + success: false, + message: "Current coupon code not set", + }; + } + return await applyCouponWithCode(subscriber, currentCouponCode); +} +export async function applyChurnCouponCode(subscriber: SubscriberRow) { + logger.info("fxa_apply_churn_coupon_code", { + subscriber: subscriber.id, + }); + + const churnEmailCouponCode = process.env.CHURN_EMAIL_COUPON_CODE_ID; + if (!churnEmailCouponCode) { + logger.error( + "fxa_apply_churn_coupon_code_failed", + "Coupon code ID is not set. Please set the env var: CHURN_EMAIL_COUPON_CODE_ID", + ); + return { + success: false, + message: "Churn coupon code not set", + }; + } + return await applyCouponWithCode(subscriber, churnEmailCouponCode); +} + +export async function checkCurrentCouponCode( + subscriber: SubscriberRow | SerializedSubscriber, +) { + logger.info("fxa_check_current_coupon", { + subscriber: subscriber.id, + }); + + const currentCouponCode = process.env.CURRENT_COUPON_CODE_ID; + if (!currentCouponCode) { + logger.error("fxa_check_current_coupon_failed", { + exception: + "Coupon code ID is not set. Please set the env var: CURRENT_COUPON_CODE_ID", + }); + return { + success: false, + }; + } + + return await checkCouponWithCode(subscriber, currentCouponCode); +} + +export async function checkChurnCouponCode( + subscriber: SubscriberRow | SerializedSubscriber, +) { + logger.info("fxa_check_churn_coupon", { + subscriber: subscriber.id, + }); + + const currentCouponCode = process.env.CHURN_EMAIL_COUPON_CODE_ID; + if (!currentCouponCode) { + logger.error("fxa_check_churn_coupon_failed", { + exception: + "Coupon code ID is not set. Please set the env var: CHURN_EMAIL_COUPON_CODE_ID", + }); + return { + success: false, + }; + } + + return await checkCouponWithCode(subscriber, currentCouponCode); +} + +export async function applyCouponWithCode( + subscriber: SubscriberRow, + couponCode: string, +) { + if (!couponCode) { + logger.error("fxa_apply_coupon_code_failed", "Coupon code is not passed."); return { success: false, message: "Coupon code not set", @@ -30,11 +104,11 @@ export async function applyCurrentCouponCode(subscriber: SubscriberRow) { try { if ( - !(await checkCouponForSubscriber(subscriber.id, currentCouponCode)) && + !(await checkCouponForSubscriber(subscriber.id, couponCode)) && subscriber.fxa_access_token ) { - await applyCoupon(subscriber.fxa_access_token, currentCouponCode); - await addCouponForSubscriber(subscriber.id, currentCouponCode); + await applyCoupon(subscriber.fxa_access_token, couponCode); + await addCouponForSubscriber(subscriber.id, couponCode); logger.info("fxa_apply_coupon_code_success"); return { success: true, @@ -55,18 +129,13 @@ export async function applyCurrentCouponCode(subscriber: SubscriberRow) { } } -export async function checkCurrentCouponCode( +export async function checkCouponWithCode( subscriber: SubscriberRow | SerializedSubscriber, + couponCode: string, ) { - logger.info("fxa_check_coupon", { - subscriber: subscriber.id, - }); - - const currentCouponCode = process.env.CURRENT_COUPON_CODE_ID; - if (!currentCouponCode) { + if (!couponCode) { logger.error("fxa_check_coupon_failed", { - exception: - "Coupon code ID is not set. Please set the env var: CURRENT_COUPON_CODE_ID", + exception: "Coupon code is not passed in", }); return { success: false, @@ -75,7 +144,7 @@ export async function checkCurrentCouponCode( try { return { - success: await checkCouponForSubscriber(subscriber.id, currentCouponCode), + success: await checkCouponForSubscriber(subscriber.id, couponCode), }; } catch (ex) { logger.error("fxa_check_coupon_failed", {