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:
@@ -14,32 +14,17 @@ export function SASLevelUpgradeCheck() {
|
||||
|
||||
const { mutate } = trpc.partner.sas.performLevelUpgrade.useMutation({
|
||||
onSuccess(result) {
|
||||
switch (result.tierMatchState) {
|
||||
case "matched":
|
||||
toast.success(
|
||||
intl.formatMessage(
|
||||
{
|
||||
defaultMessage: "Your SAS level has upgraded you to {level}!",
|
||||
},
|
||||
{
|
||||
level: TIER_TO_FRIEND_MAP[result.toLevel],
|
||||
}
|
||||
)
|
||||
if (result.tierMatchState === "matched") {
|
||||
toast.success(
|
||||
intl.formatMessage(
|
||||
{
|
||||
defaultMessage: "Your SAS level has upgraded you to {level}!",
|
||||
},
|
||||
{
|
||||
level: TIER_TO_FRIEND_MAP[result.toLevel],
|
||||
}
|
||||
)
|
||||
break
|
||||
// TODO remove the logs, but keep for now to ease testing
|
||||
case "notLinked":
|
||||
console.log("[sas] not linked - this should never happen")
|
||||
break
|
||||
case "error":
|
||||
console.log("[sas] something went wrong")
|
||||
break
|
||||
case "cached":
|
||||
console.log("[sas] cached")
|
||||
break
|
||||
case "alreadyMatched":
|
||||
console.log("[sas] already matched")
|
||||
break
|
||||
)
|
||||
}
|
||||
},
|
||||
onError() {
|
||||
|
||||
@@ -1311,8 +1311,7 @@
|
||||
"value": "Indtast venligst den kode, der er sendt til "
|
||||
},
|
||||
{
|
||||
"children": [
|
||||
],
|
||||
"children": [],
|
||||
"type": 8,
|
||||
"value": "maskedContactInfo"
|
||||
},
|
||||
@@ -3502,8 +3501,7 @@
|
||||
"value": "Indtast venligst den kode, der er sendt til "
|
||||
},
|
||||
{
|
||||
"children": [
|
||||
],
|
||||
"children": [],
|
||||
"type": 8,
|
||||
"value": "maskedContactInfo"
|
||||
},
|
||||
@@ -5923,4 +5921,4 @@
|
||||
"value": "Kort"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1307,8 +1307,7 @@
|
||||
"value": "Bitte geben Sie den an "
|
||||
},
|
||||
{
|
||||
"children": [
|
||||
],
|
||||
"children": [],
|
||||
"type": 8,
|
||||
"value": "maskedContactInfo"
|
||||
},
|
||||
@@ -1323,8 +1322,7 @@
|
||||
"value": "Mit Ihrer Anmeldung akzeptieren Sie die Allgemeinen Geschäftsbedingungen von Scandic Friends "
|
||||
},
|
||||
{
|
||||
"children": [
|
||||
],
|
||||
"children": [],
|
||||
"type": 8,
|
||||
"value": "termsAndConditionsLink"
|
||||
},
|
||||
@@ -3494,8 +3492,7 @@
|
||||
"value": "Bitte geben Sie den Code ein, der an "
|
||||
},
|
||||
{
|
||||
"children": [
|
||||
],
|
||||
"children": [],
|
||||
"type": 8,
|
||||
"value": "maskedContactInfo"
|
||||
},
|
||||
@@ -5915,4 +5912,4 @@
|
||||
"value": "Karte"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1307,8 +1307,7 @@
|
||||
"value": "Please enter the code sent to "
|
||||
},
|
||||
{
|
||||
"children": [
|
||||
],
|
||||
"children": [],
|
||||
"type": 8,
|
||||
"value": "maskedContactInfo"
|
||||
},
|
||||
@@ -3498,8 +3497,7 @@
|
||||
"value": "Please enter the code sent to "
|
||||
},
|
||||
{
|
||||
"children": [
|
||||
],
|
||||
"children": [],
|
||||
"type": 8,
|
||||
"value": "maskedContactInfo"
|
||||
},
|
||||
@@ -5919,4 +5917,4 @@
|
||||
"value": "Map"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1315,8 +1315,7 @@
|
||||
"value": "Kirjoita osoitteeseen "
|
||||
},
|
||||
{
|
||||
"children": [
|
||||
],
|
||||
"children": [],
|
||||
"type": 8,
|
||||
"value": "maskedContactInfo"
|
||||
},
|
||||
@@ -3514,8 +3513,7 @@
|
||||
"value": "Kirjoita osoitteeseen "
|
||||
},
|
||||
{
|
||||
"children": [
|
||||
],
|
||||
"children": [],
|
||||
"type": 8,
|
||||
"value": "maskedContactInfo"
|
||||
},
|
||||
@@ -5935,4 +5933,4 @@
|
||||
"value": "Kartta"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1307,8 +1307,7 @@
|
||||
"value": "Vennligst skriv inn koden som ble sendt til "
|
||||
},
|
||||
{
|
||||
"children": [
|
||||
],
|
||||
"children": [],
|
||||
"type": 8,
|
||||
"value": "maskedContactInfo"
|
||||
},
|
||||
@@ -5922,4 +5921,4 @@
|
||||
"value": "Kart"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,4 +17,4 @@
|
||||
"value": " m²"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1307,8 +1307,7 @@
|
||||
"value": "Vänligen ange koden som skickats till "
|
||||
},
|
||||
{
|
||||
"children": [
|
||||
],
|
||||
"children": [],
|
||||
"type": 8,
|
||||
"value": "maskedContactInfo"
|
||||
},
|
||||
@@ -3498,8 +3497,7 @@
|
||||
"value": "Vänligen ange den kod som skickats till "
|
||||
},
|
||||
{
|
||||
"children": [
|
||||
],
|
||||
"children": [],
|
||||
"type": 8,
|
||||
"value": "maskedContactInfo"
|
||||
},
|
||||
@@ -5927,4 +5925,4 @@
|
||||
"value": "Karta"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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" }
|
||||
}
|
||||
|
||||
@@ -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" }
|
||||
})
|
||||
|
||||
@@ -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" }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user