Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MNTOR-3920: check/apply churn coupon #5481

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 84 additions & 15 deletions src/app/functions/server/applyCoupon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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", {
Expand Down
Loading