chore: remove unused filter modal
remove old cms model refactor reward types
This commit is contained in:
@@ -8,7 +8,10 @@ import {
|
||||
} from "@/server/trpc"
|
||||
import { langInput } from "@/server/utils"
|
||||
|
||||
import { getReedemableCoupons } from "@/utils/rewards"
|
||||
import {
|
||||
getRedeemableRewards,
|
||||
getUnwrappedSurpriseRewards,
|
||||
} from "@/utils/rewards"
|
||||
|
||||
import { getAllLoyaltyLevels, getLoyaltyLevel } from "../loyaltyLevel/query"
|
||||
import {
|
||||
@@ -17,7 +20,7 @@ import {
|
||||
rewardsRedeemInput,
|
||||
rewardsUpdateInput,
|
||||
} from "./input"
|
||||
import { type Surprise, validateCategorizedRewardsSchema } from "./output"
|
||||
import { validateCategorizedRewardsSchema } from "./output"
|
||||
import {
|
||||
getAllRewardCounter,
|
||||
getAllRewardFailCounter,
|
||||
@@ -37,15 +40,11 @@ import {
|
||||
getUnwrapSurpriseCounter,
|
||||
getUnwrapSurpriseFailCounter,
|
||||
getUnwrapSurpriseSuccessCounter,
|
||||
isSurpriseReward,
|
||||
} from "./utils"
|
||||
|
||||
import type {
|
||||
Reward,
|
||||
RewardWithRedeem,
|
||||
} from "@/types/components/myPages/rewards"
|
||||
|
||||
const ONE_HOUR = 60 * 60
|
||||
import type { BaseReward, Surprise } from "@/types/components/myPages/rewards"
|
||||
import type { LevelWithRewards } from "@/types/components/overviewTable"
|
||||
import type { CMSReward } from "@/types/trpc/routers/contentstack/reward"
|
||||
|
||||
export const rewardQueryRouter = router({
|
||||
all: contentStackBaseWithServiceProcedure
|
||||
@@ -88,7 +87,7 @@ export const rewardQueryRouter = router({
|
||||
console.error("No contentStackReward found", reward?.rewardId)
|
||||
}
|
||||
})
|
||||
.filter((reward): reward is Reward => Boolean(reward))
|
||||
.filter((reward): reward is CMSReward => Boolean(reward))
|
||||
|
||||
const levelConfig = loyaltyLevelsConfig.find(
|
||||
(l) => l.level_id === level
|
||||
@@ -100,7 +99,11 @@ export const rewardQueryRouter = router({
|
||||
console.error("contentstack.loyaltyLevels level not found")
|
||||
throw notFound()
|
||||
}
|
||||
return { ...levelConfig, rewards: combinedRewards }
|
||||
const result: LevelWithRewards = {
|
||||
...levelConfig,
|
||||
rewards: combinedRewards,
|
||||
}
|
||||
return result
|
||||
}
|
||||
)
|
||||
|
||||
@@ -156,7 +159,7 @@ export const rewardQueryRouter = router({
|
||||
console.info("No contentStackReward found", reward?.rewardId)
|
||||
}
|
||||
})
|
||||
.filter((reward): reward is Reward => Boolean(reward))
|
||||
.filter((reward): reward is CMSReward => Boolean(reward))
|
||||
|
||||
getByLevelRewardSuccessCounter.add(1)
|
||||
return { level: loyaltyLevelsConfig, rewards: levelsWithRewards }
|
||||
@@ -166,13 +169,14 @@ export const rewardQueryRouter = router({
|
||||
.query(async function ({ ctx }) {
|
||||
getCurrentRewardCounter.add(1)
|
||||
|
||||
const endpoint = api.endpoints.v1.Profile.Reward.reward
|
||||
|
||||
const apiResponse = await api.get(endpoint, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
||||
},
|
||||
})
|
||||
const apiResponse = await api.get(
|
||||
api.endpoints.v1.Profile.Reward.reward,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
const text = await apiResponse.text()
|
||||
@@ -219,39 +223,25 @@ export const rewardQueryRouter = router({
|
||||
return null
|
||||
}
|
||||
|
||||
const rewardIds = validatedApiRewards.data
|
||||
.map((reward) => reward.rewardId)
|
||||
.filter((rewardId): rewardId is string => !!rewardId)
|
||||
.sort()
|
||||
|
||||
const { benefits, coupons } = validatedApiRewards.data
|
||||
const redeemableRewards = getRedeemableRewards([...benefits, ...coupons])
|
||||
const rewardIds = redeemableRewards.map(({ rewardId }) => rewardId).sort()
|
||||
const cmsRewards = await getCmsRewards(ctx.lang, rewardIds)
|
||||
if (!cmsRewards) {
|
||||
return null
|
||||
}
|
||||
|
||||
const rewards: Array<Reward | RewardWithRedeem> = cmsRewards
|
||||
.filter(
|
||||
(cmsReward) =>
|
||||
// filters out any rewards tied to wrapped surprises
|
||||
!validatedApiRewards.data
|
||||
.filter(isSurpriseReward)
|
||||
.filter((reward) =>
|
||||
reward.coupon.some(({ unwrapped }) => !unwrapped)
|
||||
)
|
||||
.map(({ rewardId }) => rewardId)
|
||||
.includes(cmsReward.reward_id)
|
||||
)
|
||||
.map((cmsReward) => {
|
||||
// Non-null assertion is used here because we know our reward exist
|
||||
const apiReward = validatedApiRewards.data.find(
|
||||
({ rewardId }) => rewardId === cmsReward.reward_id
|
||||
)!
|
||||
const rewards: BaseReward[] = cmsRewards.map((cmsReward) => {
|
||||
// Non-null assertion is used here because we know our reward exist
|
||||
const apiReward = redeemableRewards.find(
|
||||
({ rewardId }) => rewardId === cmsReward.reward_id
|
||||
)!
|
||||
|
||||
return {
|
||||
...cmsReward,
|
||||
data: apiReward,
|
||||
}
|
||||
})
|
||||
return {
|
||||
...apiReward,
|
||||
...cmsReward,
|
||||
}
|
||||
})
|
||||
|
||||
getCurrentRewardSuccessCounter.add(1)
|
||||
|
||||
@@ -315,10 +305,11 @@ export const rewardQueryRouter = router({
|
||||
return null
|
||||
}
|
||||
|
||||
const rewardIds = validatedApiRewards.data
|
||||
.filter((reward) => getReedemableCoupons(reward).length)
|
||||
.map((reward) => reward.rewardId)
|
||||
.filter((rewardId): rewardId is string => !!rewardId)
|
||||
const unwrappedSurpriseRewards = getUnwrappedSurpriseRewards(
|
||||
validatedApiRewards.data.coupons
|
||||
)
|
||||
const rewardIds = unwrappedSurpriseRewards
|
||||
.map(({ rewardId }) => rewardId)
|
||||
.sort()
|
||||
const cmsRewards = await getCmsRewards(ctx.lang, rewardIds)
|
||||
if (!cmsRewards) {
|
||||
@@ -327,26 +318,20 @@ export const rewardQueryRouter = router({
|
||||
|
||||
getCurrentRewardSuccessCounter.add(1)
|
||||
|
||||
const surprises: Surprise[] = validatedApiRewards.data
|
||||
.filter(isSurpriseReward)
|
||||
.filter((reward) => {
|
||||
const unwrappedCoupons =
|
||||
reward.coupon.filter((coupon) => !coupon.unwrapped) || []
|
||||
|
||||
return unwrappedCoupons.length
|
||||
})
|
||||
.map((surprise) => {
|
||||
const cmsReward = cmsRewards.find(
|
||||
({ reward_id }) => surprise.rewardId === reward_id
|
||||
)
|
||||
const surprises: Surprise[] = cmsRewards
|
||||
.map((cmsReward) => {
|
||||
// Non-null assertion is used here because we know our reward exist
|
||||
const apiReward = unwrappedSurpriseRewards.find(
|
||||
({ rewardId }) => rewardId === cmsReward.reward_id
|
||||
)!
|
||||
|
||||
if (!cmsReward) {
|
||||
return null
|
||||
}
|
||||
|
||||
return {
|
||||
...apiReward,
|
||||
...cmsReward,
|
||||
data: surprise,
|
||||
}
|
||||
})
|
||||
.flatMap((surprises) => (surprises ? [surprises] : []))
|
||||
|
||||
Reference in New Issue
Block a user