Merged in feat/sw-2866-move-partners-router-to-trpc-package (pull request #2414)
feat(sw-2866): Move partners router to trpc package * Add eslint to trpc package * Apply lint rules * Use direct imports from trpc package * Add lint-staged config to trpc * Move lang enum to common * Restructure trpc package folder structure * WIP first step * update internal imports in trpc * Fix most errors in scandic-web Just 100 left... * Move Props type out of trpc * Fix CategorizedFilters types * Move more schemas in hotel router * Fix deps * fix getNonContentstackUrls * Fix import error * Fix entry error handling * Fix generateMetadata metrics * Fix alertType enum * Fix duplicated types * lint:fix * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package * Fix broken imports * Move booking router to trpc package * Move partners router to trpc package * Merge branch 'master' into feat/sw-2866-move-partners-router-to-trpc-package Approved-by: Linus Flood
This commit is contained in:
106
packages/trpc/lib/routers/partners/sas/performLevelUpgrade.ts
Normal file
106
packages/trpc/lib/routers/partners/sas/performLevelUpgrade.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
import * as Sentry from "@sentry/nextjs"
|
||||
import { cookies } from "next/headers"
|
||||
import { z } from "zod"
|
||||
|
||||
import { FriendsMembershipLevels } from "@scandic-hotels/common/constants/membershipLevels"
|
||||
|
||||
import * as api from "../../../api"
|
||||
import { protectedProcedure } from "../../../procedures"
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user