chore (SW-834): Upgrade to Next 15 * wip: apply codemod and upgrade swc plugin * wip: design-system to react 19, fix issues from async (search)params * wip: fix remaining issues from codemod serverClient is now async because context use headers() getLang is now async because it uses headers() * Minor cleanup * Inline react-material-symbols package Package is seemingly not maintained any more and doesn't support React 19. This copies the package source into `design-system`, makes the necessary changes for 19 and export it for others to use. * Fix missing awaits * Disable modal exit animations Enabling modal exit animations via isExiting prop is causing modals to be rendered in "hidden" state and never unmount. Seems to be an issue with react-aria-components, see https://github.com/adobe/react-spectrum/issues/7563. Can probably be fixed by rewriting to a solution similar to https://react-spectrum.adobe.com/react-aria/examples/framer-modal-sheet.html * Remove unstable cache implementation and use in memory cache locally * Fix ref type in SelectFilter * Use cloneElement to add key prop to element Approved-by: Linus Flood
107 lines
3.4 KiB
TypeScript
107 lines
3.4 KiB
TypeScript
import * as Sentry from "@sentry/nextjs"
|
|
import { cookies } from "next/headers"
|
|
import { z } from "zod"
|
|
|
|
import { FriendsMembershipLevels } from "@/constants/membershipLevels"
|
|
import * as api from "@/lib/api"
|
|
import { protectedProcedure } from "@/server/trpc"
|
|
|
|
import { getUserSchema } from "../../user/output"
|
|
import { getVerifiedUser } from "../../user/utils"
|
|
|
|
import type { FriendsTier } from "@/types/user"
|
|
|
|
const matchedSchema = z.object({
|
|
tierMatchState: z.enum(["matched"]),
|
|
toLevel: z.enum(["L1", "L2", "L3", "L4", "L5", "L6", "L7"]),
|
|
})
|
|
const notMatchedSchema = z.object({
|
|
tierMatchState: z.enum(["alreadyMatched", "notLinked", "error", "cached"]),
|
|
})
|
|
|
|
const outputSchema = z.union([matchedSchema, notMatchedSchema])
|
|
|
|
export const performLevelUpgrade = protectedProcedure
|
|
.output(outputSchema)
|
|
.mutation(async function ({ ctx }) {
|
|
const cookieStore = await cookies()
|
|
const sasTierMatch = cookieStore.get("sasTierMatch")
|
|
if (sasTierMatch) {
|
|
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")
|
|
|
|
const apiResponse = await api.post(api.endpoints.v1.Profile.matchTier, {
|
|
headers: {
|
|
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
|
},
|
|
body: {
|
|
partner: "sas_eb",
|
|
partnerSpecific: {},
|
|
},
|
|
})
|
|
|
|
cookieStore.set("sasTierMatch", "true", {
|
|
maxAge: 60 * 60 * 24,
|
|
httpOnly: true,
|
|
})
|
|
|
|
const updated = apiResponse.status === 200
|
|
if (updated) {
|
|
console.log("[SAS] tier match complete - boosted")
|
|
const result = await apiResponse.json()
|
|
const user = getUserSchema.parse(result)
|
|
|
|
if (!user.membership) {
|
|
const tierMatchErrorNoMembershipMessage =
|
|
"[SAS] tier match error - no membership"
|
|
console.log(tierMatchErrorNoMembershipMessage)
|
|
Sentry.captureException(new Error(tierMatchErrorNoMembershipMessage))
|
|
return { tierMatchState: "error" }
|
|
}
|
|
|
|
const newLevel = user.membership.membershipLevel
|
|
|
|
if (isHigherLevel(newLevel, currentLevel)) {
|
|
return { tierMatchState: "matched", toLevel: newLevel }
|
|
}
|
|
return { tierMatchState: "alreadyMatched" }
|
|
}
|
|
|
|
const matchedNoChange = apiResponse.status === 204
|
|
if (matchedNoChange) {
|
|
console.log("[SAS] tier match complete - no change")
|
|
return { tierMatchState: "alreadyMatched" }
|
|
}
|
|
|
|
const notLinked = apiResponse.status === 404
|
|
if (notLinked) {
|
|
const tierMatchErrorNotLinkedMessage =
|
|
"[SAS] tier match error - not linked"
|
|
console.warn(tierMatchErrorNotLinkedMessage)
|
|
Sentry.captureMessage(tierMatchErrorNotLinkedMessage)
|
|
return { tierMatchState: "notLinked" }
|
|
}
|
|
|
|
const tierMatchErrorMessage = `[SAS] tier match error with status code ${apiResponse.status} and response ${await apiResponse.text()}`
|
|
console.error(tierMatchErrorMessage)
|
|
Sentry.captureException(new Error(tierMatchErrorMessage))
|
|
return { tierMatchState: "error" }
|
|
})
|
|
|
|
function isHigherLevel(
|
|
newLevel: FriendsTier,
|
|
currentLevel: FriendsTier
|
|
): boolean {
|
|
const currentIndex = FriendsMembershipLevels.indexOf(currentLevel)
|
|
const newIndex = FriendsMembershipLevels.indexOf(newLevel)
|
|
return newIndex > currentIndex
|
|
}
|