feat(SW-3614): use new loyalty prop in basicProfile * feat(SW-3614): use new loyalty prop in basicProfile * PR fixes Approved-by: Matilda Landström
110 lines
3.6 KiB
TypeScript
110 lines
3.6 KiB
TypeScript
import * as Sentry from "@sentry/nextjs"
|
|
import { cookies } from "next/headers"
|
|
import { z } from "zod"
|
|
|
|
import { FriendsMembershipLevels } from "@scandic-hotels/common/constants/membershipLevels"
|
|
import { createLogger } from "@scandic-hotels/common/logger/createLogger"
|
|
import { safeTry } from "@scandic-hotels/common/utils/safeTry"
|
|
|
|
import * as api from "../../../api"
|
|
import { protectedProcedure } from "../../../procedures"
|
|
import { getUserSchema } from "../../user/output"
|
|
import { getBasicUser } from "../../user/utils/getBasicUser"
|
|
|
|
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])
|
|
const sasLogger = createLogger("SAS")
|
|
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, error] = await safeTry(
|
|
getBasicUser({ token: ctx.session.token })
|
|
)
|
|
if (!profile || error || !profile.loyalty?.tier) {
|
|
return { tierMatchState: "error" }
|
|
}
|
|
const currentLevel = profile.loyalty.tier
|
|
|
|
sasLogger.debug("tier match started")
|
|
|
|
const apiResponse = await api.post(api.endpoints.v2.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) {
|
|
sasLogger.debug("tier match complete - boosted")
|
|
const result = await apiResponse.json()
|
|
const user = getUserSchema.parse(result)
|
|
|
|
if (!user.membership) {
|
|
const tierMatchErrorNoMembershipMessage =
|
|
"tier match error - no membership"
|
|
sasLogger.debug(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) {
|
|
sasLogger.debug("tier match complete - no change")
|
|
return { tierMatchState: "alreadyMatched" }
|
|
}
|
|
|
|
const notLinked = apiResponse.status === 404
|
|
if (notLinked) {
|
|
const tierMatchErrorNotLinkedMessage = "tier match error - not linked"
|
|
sasLogger.error(tierMatchErrorNotLinkedMessage)
|
|
Sentry.captureMessage(tierMatchErrorNotLinkedMessage)
|
|
return { tierMatchState: "notLinked" }
|
|
}
|
|
|
|
const tierMatchErrorMessage = `tier match error with status code ${apiResponse.status} and response ${await apiResponse.text()}`
|
|
sasLogger.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
|
|
}
|