Merged in feat/sw-2232-sas-api-updates (pull request #1786)

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
This commit is contained in:
Anton Gunnarsson
2025-04-14 13:42:58 +00:00
parent f87ea51b11
commit 3449ccec52
12 changed files with 66 additions and 101 deletions

View File

@@ -4,8 +4,8 @@ import { requestOtp } from "./otp/request/requestOtp"
import { verifyOtp } from "./otp/verify/verifyOtp"
import { linkAccount } from "./linkAccount"
import { performLevelUpgrade } from "./performLevelUpgrade"
import { unlinkAccount } from "./unlinkAccount"
import { transferPoints } from "./transferPoints"
import { unlinkAccount } from "./unlinkAccount"
export const sasRouter = router({
verifyOtp,

View File

@@ -34,7 +34,13 @@ export const linkAccount = protectedProcedure
},
})
if (apiResponse.status === 204) {
const linkedAndBoosted = apiResponse.status === 200
const linkedWithoutBoost = apiResponse.status === 204
const linkedWithUnknownBoost = apiResponse.status === 202
const linked =
linkedAndBoosted || linkedWithoutBoost || linkedWithUnknownBoost
if (linked) {
console.log("[SAS] link account done")
return { linkingState: "linked" }
}

View File

@@ -1,11 +1,11 @@
import * as Sentry from "@sentry/nextjs"
import { cookies } from "next/headers"
import { z } from "zod"
import * as api from "@/lib/api"
import { getVerifiedUser } from "@/server/routers/user/query"
import { protectedProcedure } from "@/server/trpc"
import { timeout } from "@/utils/timeout"
import { getUserSchema } from "../../user/output"
const matchedSchema = z.object({
tierMatchState: z.enum(["matched"]),
@@ -20,20 +20,13 @@ const outputSchema = z.union([matchedSchema, notMatchedSchema])
export const performLevelUpgrade = protectedProcedure
.output(outputSchema)
.mutation(async function ({ ctx }) {
console.log("[SAS] tier match")
const cookieStore = cookies()
const sasTierMatch = cookieStore.get("sasTierMatch")
if (sasTierMatch) {
return { tierMatchState: "cached" }
}
const userBeforeTierMatch = await getVerifiedUser({
session: ctx.session,
})
if (!userBeforeTierMatch || userBeforeTierMatch?.error) {
return { tierMatchState: "error" }
}
console.log("[SAS] tier match started")
const apiResponse = await api.post(api.endpoints.v1.Profile.matchTier, {
headers: {
@@ -50,48 +43,41 @@ export const performLevelUpgrade = protectedProcedure
httpOnly: true,
})
if (apiResponse.status === 202) {
console.log("[SAS] tier match started")
const boosted = apiResponse.status === 200
if (boosted) {
console.log("[SAS] tier match complete - boosted")
const result = await apiResponse.json()
const user = getUserSchema.parse(result)
// Since the tier match is async we need to wait for it to complete before checking the result
await timeout(1000)
const userAfterTierMatch = await getVerifiedUser({
session: ctx.session,
})
if (!userAfterTierMatch || userAfterTierMatch?.error) {
if (!user.membership) {
const tierMatchErrorNoMembershipMessage =
"[SAS] tier match error - no membership"
console.log(tierMatchErrorNoMembershipMessage)
Sentry.captureException(new Error(tierMatchErrorNoMembershipMessage))
return { tierMatchState: "error" }
}
const beforeLevel = userBeforeTierMatch.data.membership?.membershipLevel
const afterLevel = userAfterTierMatch.data.membership?.membershipLevel
if (!beforeLevel || !afterLevel) {
console.log("[SAS] tier match error, user tier not found")
return { tierMatchState: "error" }
}
if (beforeLevel !== afterLevel) {
console.log(
`[SAS] tier match success, user tier changed from ${beforeLevel} to ${afterLevel}`
)
return { tierMatchState: "matched", toLevel: afterLevel }
}
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" }
}
if (apiResponse.status === 204) {
console.log("[SAS] tier already matched")
return { tierMatchState: "alreadyMatched" }
}
if (apiResponse.status === 404) {
const notLinked = apiResponse.status === 404
if (notLinked) {
const tierMatchErrorNotLinkedMessage =
"[SAS] tier match error - not linked"
console.log(tierMatchErrorNotLinkedMessage)
Sentry.captureMessage(tierMatchErrorNotLinkedMessage)
return { tierMatchState: "notLinked" }
}
console.log(
`[SAS] tier match error with status code ${apiResponse.status} and response ${await apiResponse.text()}`
)
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" }
})

View File

@@ -26,7 +26,7 @@ export const unlinkAccount = protectedProcedure
},
})
if (apiResponse.status === 204) {
if (apiResponse.status === 204 || apiResponse.status === 202) {
console.log("[SAS] unlink account success")
return { linkingState: "unlinked" }
}