Skip to content

Commit d78b744

Browse files
committed
feat: check/apply churn coupon
1 parent 8b97f7c commit d78b744

File tree

1 file changed

+84
-15
lines changed

1 file changed

+84
-15
lines changed

src/app/functions/server/applyCoupon.ts

Lines changed: 84 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,90 @@ import {
1212
import { applyCoupon } from "../../../utils/fxa";
1313

1414
export async function applyCurrentCouponCode(subscriber: SubscriberRow) {
15-
logger.info("fxa_apply_coupon_code", {
15+
logger.info("fxa_apply_current_coupon_code", {
1616
subscriber: subscriber.id,
1717
});
1818

1919
const currentCouponCode = process.env.CURRENT_COUPON_CODE_ID;
2020
if (!currentCouponCode) {
2121
logger.error(
22-
"fxa_apply_coupon_code_failed",
22+
"fxa_apply_current_coupon_code_failed",
2323
"Coupon code ID is not set. Please set the env var: CURRENT_COUPON_CODE_ID",
2424
);
25+
return {
26+
success: false,
27+
message: "Current coupon code not set",
28+
};
29+
}
30+
return await applyCouponWithCode(subscriber, currentCouponCode);
31+
}
32+
export async function applyChurnCouponCode(subscriber: SubscriberRow) {
33+
logger.info("fxa_apply_churn_coupon_code", {
34+
subscriber: subscriber.id,
35+
});
36+
37+
const churnEmailCouponCode = process.env.CHURN_EMAIL_COUPON_CODE_ID;
38+
if (!churnEmailCouponCode) {
39+
logger.error(
40+
"fxa_apply_churn_coupon_code_failed",
41+
"Coupon code ID is not set. Please set the env var: CHURN_EMAIL_COUPON_CODE_ID",
42+
);
43+
return {
44+
success: false,
45+
message: "Churn coupon code not set",
46+
};
47+
}
48+
return await applyCouponWithCode(subscriber, churnEmailCouponCode);
49+
}
50+
51+
export async function checkCurrentCouponCode(
52+
subscriber: SubscriberRow | SerializedSubscriber,
53+
) {
54+
logger.info("fxa_check_current_coupon", {
55+
subscriber: subscriber.id,
56+
});
57+
58+
const currentCouponCode = process.env.CURRENT_COUPON_CODE_ID;
59+
if (!currentCouponCode) {
60+
logger.error("fxa_check_current_coupon_failed", {
61+
exception:
62+
"Coupon code ID is not set. Please set the env var: CURRENT_COUPON_CODE_ID",
63+
});
64+
return {
65+
success: false,
66+
};
67+
}
68+
69+
return await checkCouponWithCode(subscriber, currentCouponCode);
70+
}
71+
72+
export async function checkChurnCouponCode(
73+
subscriber: SubscriberRow | SerializedSubscriber,
74+
) {
75+
logger.info("fxa_check_churn_coupon", {
76+
subscriber: subscriber.id,
77+
});
78+
79+
const currentCouponCode = process.env.CHURN_EMAIL_COUPON_CODE_ID;
80+
if (!currentCouponCode) {
81+
logger.error("fxa_check_churn_coupon_failed", {
82+
exception:
83+
"Coupon code ID is not set. Please set the env var: CHURN_EMAIL_COUPON_CODE_ID",
84+
});
85+
return {
86+
success: false,
87+
};
88+
}
89+
90+
return await checkCouponWithCode(subscriber, currentCouponCode);
91+
}
92+
93+
export async function applyCouponWithCode(
94+
subscriber: SubscriberRow,
95+
couponCode: string,
96+
) {
97+
if (!couponCode) {
98+
logger.error("fxa_apply_coupon_code_failed", "Coupon code is not passed.");
2599
return {
26100
success: false,
27101
message: "Coupon code not set",
@@ -30,11 +104,11 @@ export async function applyCurrentCouponCode(subscriber: SubscriberRow) {
30104

31105
try {
32106
if (
33-
!(await checkCouponForSubscriber(subscriber.id, currentCouponCode)) &&
107+
!(await checkCouponForSubscriber(subscriber.id, couponCode)) &&
34108
subscriber.fxa_access_token
35109
) {
36-
await applyCoupon(subscriber.fxa_access_token, currentCouponCode);
37-
await addCouponForSubscriber(subscriber.id, currentCouponCode);
110+
await applyCoupon(subscriber.fxa_access_token, couponCode);
111+
await addCouponForSubscriber(subscriber.id, couponCode);
38112
logger.info("fxa_apply_coupon_code_success");
39113
return {
40114
success: true,
@@ -55,18 +129,13 @@ export async function applyCurrentCouponCode(subscriber: SubscriberRow) {
55129
}
56130
}
57131

58-
export async function checkCurrentCouponCode(
132+
export async function checkCouponWithCode(
59133
subscriber: SubscriberRow | SerializedSubscriber,
134+
couponCode: string,
60135
) {
61-
logger.info("fxa_check_coupon", {
62-
subscriber: subscriber.id,
63-
});
64-
65-
const currentCouponCode = process.env.CURRENT_COUPON_CODE_ID;
66-
if (!currentCouponCode) {
136+
if (!couponCode) {
67137
logger.error("fxa_check_coupon_failed", {
68-
exception:
69-
"Coupon code ID is not set. Please set the env var: CURRENT_COUPON_CODE_ID",
138+
exception: "Coupon code is not passed in",
70139
});
71140
return {
72141
success: false,
@@ -75,7 +144,7 @@ export async function checkCurrentCouponCode(
75144

76145
try {
77146
return {
78-
success: await checkCouponForSubscriber(subscriber.id, currentCouponCode),
147+
success: await checkCouponForSubscriber(subscriber.id, couponCode),
79148
};
80149
} catch (ex) {
81150
logger.error("fxa_check_coupon_failed", {

0 commit comments

Comments
 (0)