SW-2232 Adapt SAS router to API changes * Adapt sas procedures to api changes * Remove debug logs * Capture performLevelUpgrade error * More sentry logging * Merge branch 'master' into feat/sw-2232-sas-api-updates Approved-by: Joakim Jäderberg
84 lines
2.7 KiB
TypeScript
84 lines
2.7 KiB
TypeScript
import * as Sentry from "@sentry/nextjs"
|
|
import { cookies } from "next/headers"
|
|
import { z } from "zod"
|
|
|
|
import * as api from "@/lib/api"
|
|
import { protectedProcedure } from "@/server/trpc"
|
|
|
|
import { getUserSchema } from "../../user/output"
|
|
|
|
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 = cookies()
|
|
const sasTierMatch = cookieStore.get("sasTierMatch")
|
|
if (sasTierMatch) {
|
|
return { tierMatchState: "cached" }
|
|
}
|
|
|
|
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 boosted = apiResponse.status === 200
|
|
if (boosted) {
|
|
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 afterLevel = user.membership.membershipLevel
|
|
return { tierMatchState: "matched", toLevel: afterLevel }
|
|
}
|
|
|
|
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.log(tierMatchErrorNotLinkedMessage)
|
|
Sentry.captureMessage(tierMatchErrorNotLinkedMessage)
|
|
return { tierMatchState: "notLinked" }
|
|
}
|
|
|
|
const tierMatchErrorMessage = `[SAS] tier match error with status code ${apiResponse.status} and response ${await apiResponse.text()}`
|
|
console.log(tierMatchErrorMessage)
|
|
Sentry.captureException(new Error(tierMatchErrorMessage))
|
|
return { tierMatchState: "error" }
|
|
})
|