fix: referral utils apply to both coupon

This commit is contained in:
John Yeo
2025-09-22 10:06:27 +01:00
parent 8a77c5e050
commit dd0e9f1fd7

View File

@@ -26,6 +26,7 @@ import { StatusCodes } from "http-status-codes";
import { getRewardCat } from "./rewardUtils.js";
import { ExtendedRequest } from "@/utils/models/Request.js";
import { deleteCusCache } from "../customers/cusCache/updateCachedCus.js";
import { RewardProgramService } from "./RewardProgramService.js";
export const generateReferralCode = () => {
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
@@ -62,19 +63,34 @@ export const triggerRedemption = async ({
`Triggering redemption ${redemption.id} for referral code ${referralCode.code}`
);
let applyToCustomer = await CusService.getByInternalId({
const referrer = await CusService.getByInternalId({
db,
internalId: referralCode.internal_customer_id,
});
if (!applyToCustomer) {
const redeemer = await CusService.getByInternalId({
db,
internalId: redemption.internal_customer_id,
});
const rewardProgram = await RewardProgramService.get({
db,
orgId: org.id,
env,
id: referralCode.internal_reward_program_id!,
});
if (!rewardProgram) {
throw new RecaseError({
message: `Customer ${referralCode.internal_customer_id} not found`,
code: ErrCode.CustomerNotFound,
message: `Reward program ${referralCode.internal_reward_program_id} not found`,
code: ErrCode.RewardProgramNotFound,
statusCode: StatusCodes.NOT_FOUND,
});
}
for (let i = 0; i < 2; i++) {
let customer = i === 0 ? referrer : redeemer;
let stripeCli = createStripeCli({
org,
env,
@@ -95,6 +111,7 @@ export const triggerRedemption = async ({
)) as Stripe.Customer;
let applied = false;
if (!stripeCus.discount) {
await stripeCli.customers.update(stripeCusId, {
// @ts-ignore
@@ -117,6 +134,12 @@ export const triggerRedemption = async ({
logger.info(`Successfully triggered redemption, applied: ${applied}`);
return updatedRedemption;
}
// let applyToCustomer = await CusService.getByInternalId({
// db,
// internalId: referralCode.internal_customer_id,
// });
};
export const triggerFreeProduct = async ({