Merged in fix/sw-2527-incorrect-toast-when-tier-matching (pull request #1879)
SW-2527 - Fix boosted toast when tier matching * Compare levels to ensure toast is only displayed when actually boosted * Fix 200 name * Update naming Approved-by: Linus Flood
This commit is contained in:
@@ -1,5 +1,15 @@
|
|||||||
import type { EurobonusTier } from "@/types/user"
|
import type { EurobonusTier } from "@/types/user"
|
||||||
|
|
||||||
|
export const FriendsMembershipLevels = [
|
||||||
|
"L1",
|
||||||
|
"L2",
|
||||||
|
"L3",
|
||||||
|
"L4",
|
||||||
|
"L5",
|
||||||
|
"L6",
|
||||||
|
"L7",
|
||||||
|
] as const
|
||||||
|
|
||||||
export enum membershipLevels {
|
export enum membershipLevels {
|
||||||
L1 = 1,
|
L1 = 1,
|
||||||
L2 = 2,
|
L2 = 2,
|
||||||
|
|||||||
@@ -2,10 +2,14 @@ import * as Sentry from "@sentry/nextjs"
|
|||||||
import { cookies } from "next/headers"
|
import { cookies } from "next/headers"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
|
|
||||||
|
import { FriendsMembershipLevels } from "@/constants/membershipLevels"
|
||||||
import * as api from "@/lib/api"
|
import * as api from "@/lib/api"
|
||||||
import { protectedProcedure } from "@/server/trpc"
|
import { protectedProcedure } from "@/server/trpc"
|
||||||
|
|
||||||
import { getUserSchema } from "../../user/output"
|
import { getUserSchema } from "../../user/output"
|
||||||
|
import { getVerifiedUser } from "../../user/utils"
|
||||||
|
|
||||||
|
import type { FriendsTier } from "@/types/user"
|
||||||
|
|
||||||
const matchedSchema = z.object({
|
const matchedSchema = z.object({
|
||||||
tierMatchState: z.enum(["matched"]),
|
tierMatchState: z.enum(["matched"]),
|
||||||
@@ -26,6 +30,12 @@ export const performLevelUpgrade = protectedProcedure
|
|||||||
return { tierMatchState: "cached" }
|
return { tierMatchState: "cached" }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const profile = await getVerifiedUser({ session: ctx.session })
|
||||||
|
if (!profile || "error" in profile || !profile.data.membership) {
|
||||||
|
return { tierMatchState: "error" }
|
||||||
|
}
|
||||||
|
const currentLevel = profile.data.membership.membershipLevel
|
||||||
|
|
||||||
console.log("[SAS] tier match started")
|
console.log("[SAS] tier match started")
|
||||||
|
|
||||||
const apiResponse = await api.post(api.endpoints.v1.Profile.matchTier, {
|
const apiResponse = await api.post(api.endpoints.v1.Profile.matchTier, {
|
||||||
@@ -43,8 +53,8 @@ export const performLevelUpgrade = protectedProcedure
|
|||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
const boosted = apiResponse.status === 200
|
const updated = apiResponse.status === 200
|
||||||
if (boosted) {
|
if (updated) {
|
||||||
console.log("[SAS] tier match complete - boosted")
|
console.log("[SAS] tier match complete - boosted")
|
||||||
const result = await apiResponse.json()
|
const result = await apiResponse.json()
|
||||||
const user = getUserSchema.parse(result)
|
const user = getUserSchema.parse(result)
|
||||||
@@ -57,8 +67,12 @@ export const performLevelUpgrade = protectedProcedure
|
|||||||
return { tierMatchState: "error" }
|
return { tierMatchState: "error" }
|
||||||
}
|
}
|
||||||
|
|
||||||
const afterLevel = user.membership.membershipLevel
|
const newLevel = user.membership.membershipLevel
|
||||||
return { tierMatchState: "matched", toLevel: afterLevel }
|
|
||||||
|
if (isHigherLevel(newLevel, currentLevel)) {
|
||||||
|
return { tierMatchState: "matched", toLevel: newLevel }
|
||||||
|
}
|
||||||
|
return { tierMatchState: "alreadyMatched" }
|
||||||
}
|
}
|
||||||
|
|
||||||
const matchedNoChange = apiResponse.status === 204
|
const matchedNoChange = apiResponse.status === 204
|
||||||
@@ -81,3 +95,12 @@ export const performLevelUpgrade = protectedProcedure
|
|||||||
Sentry.captureException(new Error(tierMatchErrorMessage))
|
Sentry.captureException(new Error(tierMatchErrorMessage))
|
||||||
return { tierMatchState: "error" }
|
return { tierMatchState: "error" }
|
||||||
})
|
})
|
||||||
|
|
||||||
|
function isHigherLevel(
|
||||||
|
newLevel: FriendsTier,
|
||||||
|
currentLevel: FriendsTier
|
||||||
|
): boolean {
|
||||||
|
const currentIndex = FriendsMembershipLevels.indexOf(currentLevel)
|
||||||
|
const newIndex = FriendsMembershipLevels.indexOf(newLevel)
|
||||||
|
return newIndex > currentIndex
|
||||||
|
}
|
||||||
|
|||||||
@@ -29,3 +29,4 @@ export type EurobonusMembership = z.output<typeof sasMembershipSchema>
|
|||||||
export type FriendsMembership = ReturnType<typeof getFriendsMembership>
|
export type FriendsMembership = ReturnType<typeof getFriendsMembership>
|
||||||
|
|
||||||
export type EurobonusTier = EurobonusMembership["tier"]
|
export type EurobonusTier = EurobonusMembership["tier"]
|
||||||
|
export type FriendsTier = NativeFriendsMembership["tier"]
|
||||||
|
|||||||
Reference in New Issue
Block a user