fix: referral utils apply to both coupon
This commit is contained in:
@@ -26,6 +26,7 @@ import { StatusCodes } from "http-status-codes";
|
|||||||
import { getRewardCat } from "./rewardUtils.js";
|
import { getRewardCat } from "./rewardUtils.js";
|
||||||
import { ExtendedRequest } from "@/utils/models/Request.js";
|
import { ExtendedRequest } from "@/utils/models/Request.js";
|
||||||
import { deleteCusCache } from "../customers/cusCache/updateCachedCus.js";
|
import { deleteCusCache } from "../customers/cusCache/updateCachedCus.js";
|
||||||
|
import { RewardProgramService } from "./RewardProgramService.js";
|
||||||
|
|
||||||
export const generateReferralCode = () => {
|
export const generateReferralCode = () => {
|
||||||
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||||
@@ -62,61 +63,83 @@ export const triggerRedemption = async ({
|
|||||||
`Triggering redemption ${redemption.id} for referral code ${referralCode.code}`
|
`Triggering redemption ${redemption.id} for referral code ${referralCode.code}`
|
||||||
);
|
);
|
||||||
|
|
||||||
let applyToCustomer = await CusService.getByInternalId({
|
const referrer = await CusService.getByInternalId({
|
||||||
db,
|
db,
|
||||||
internalId: referralCode.internal_customer_id,
|
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({
|
throw new RecaseError({
|
||||||
message: `Customer ${referralCode.internal_customer_id} not found`,
|
message: `Reward program ${referralCode.internal_reward_program_id} not found`,
|
||||||
code: ErrCode.CustomerNotFound,
|
code: ErrCode.RewardProgramNotFound,
|
||||||
statusCode: StatusCodes.NOT_FOUND,
|
statusCode: StatusCodes.NOT_FOUND,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let stripeCli = createStripeCli({
|
for (let i = 0; i < 2; i++) {
|
||||||
org,
|
let customer = i === 0 ? referrer : redeemer;
|
||||||
env,
|
|
||||||
legacyVersion: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
await createStripeCusIfNotExists({
|
let stripeCli = createStripeCli({
|
||||||
db,
|
org,
|
||||||
customer: applyToCustomer,
|
env,
|
||||||
org,
|
legacyVersion: true,
|
||||||
env,
|
|
||||||
logger,
|
|
||||||
});
|
|
||||||
|
|
||||||
let stripeCusId = applyToCustomer.processor.id;
|
|
||||||
let stripeCus = (await stripeCli.customers.retrieve(
|
|
||||||
stripeCusId
|
|
||||||
)) as Stripe.Customer;
|
|
||||||
|
|
||||||
let applied = false;
|
|
||||||
if (!stripeCus.discount) {
|
|
||||||
await stripeCli.customers.update(stripeCusId, {
|
|
||||||
// @ts-ignore
|
|
||||||
coupon: reward.id,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
applied = true;
|
await createStripeCusIfNotExists({
|
||||||
logger.info(`Applied coupon to customer in Stripe`);
|
db,
|
||||||
|
customer: applyToCustomer,
|
||||||
|
org,
|
||||||
|
env,
|
||||||
|
logger,
|
||||||
|
});
|
||||||
|
|
||||||
|
let stripeCusId = applyToCustomer.processor.id;
|
||||||
|
let stripeCus = (await stripeCli.customers.retrieve(
|
||||||
|
stripeCusId
|
||||||
|
)) as Stripe.Customer;
|
||||||
|
|
||||||
|
let applied = false;
|
||||||
|
|
||||||
|
if (!stripeCus.discount) {
|
||||||
|
await stripeCli.customers.update(stripeCusId, {
|
||||||
|
// @ts-ignore
|
||||||
|
coupon: reward.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
applied = true;
|
||||||
|
logger.info(`Applied coupon to customer in Stripe`);
|
||||||
|
}
|
||||||
|
|
||||||
|
let updatedRedemption = await RewardRedemptionService.update({
|
||||||
|
db,
|
||||||
|
id: redemption.id,
|
||||||
|
updates: {
|
||||||
|
applied,
|
||||||
|
triggered: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
logger.info(`Successfully triggered redemption, applied: ${applied}`);
|
||||||
|
|
||||||
|
return updatedRedemption;
|
||||||
}
|
}
|
||||||
|
|
||||||
let updatedRedemption = await RewardRedemptionService.update({
|
// let applyToCustomer = await CusService.getByInternalId({
|
||||||
db,
|
// db,
|
||||||
id: redemption.id,
|
// internalId: referralCode.internal_customer_id,
|
||||||
updates: {
|
// });
|
||||||
applied,
|
|
||||||
triggered: true,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
logger.info(`Successfully triggered redemption, applied: ${applied}`);
|
|
||||||
|
|
||||||
return updatedRedemption;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const triggerFreeProduct = async ({
|
export const triggerFreeProduct = async ({
|
||||||
|
|||||||
Reference in New Issue
Block a user