Skip to content

Commit

Permalink
feat: check/apply churn coupon
Browse files Browse the repository at this point in the history
  • Loading branch information
mansaj committed Jan 10, 2025
1 parent 8b97f7c commit d78b744
Showing 1 changed file with 84 additions and 15 deletions.
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

0 comments on commit d78b744

Please sign in to comment.