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({
|
const { mutate } = trpc.partner.sas.performLevelUpgrade.useMutation({
|
||||||
onSuccess(result) {
|
onSuccess(result) {
|
||||||
switch (result.tierMatchState) {
|
if (result.tierMatchState === "matched") {
|
||||||
case "matched":
|
toast.success(
|
||||||
toast.success(
|
intl.formatMessage(
|
||||||
intl.formatMessage(
|
{
|
||||||
{
|
defaultMessage: "Your SAS level has upgraded you to {level}!",
|
||||||
defaultMessage: "Your SAS level has upgraded you to {level}!",
|
},
|
||||||
},
|
{
|
||||||
{
|
level: TIER_TO_FRIEND_MAP[result.toLevel],
|
||||||
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() {
|
onError() {
|
||||||
|
|||||||
@@ -1311,8 +1311,7 @@
|
|||||||
"value": "Indtast venligst den kode, der er sendt til "
|
"value": "Indtast venligst den kode, der er sendt til "
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"children": [
|
"children": [],
|
||||||
],
|
|
||||||
"type": 8,
|
"type": 8,
|
||||||
"value": "maskedContactInfo"
|
"value": "maskedContactInfo"
|
||||||
},
|
},
|
||||||
@@ -3502,8 +3501,7 @@
|
|||||||
"value": "Indtast venligst den kode, der er sendt til "
|
"value": "Indtast venligst den kode, der er sendt til "
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"children": [
|
"children": [],
|
||||||
],
|
|
||||||
"type": 8,
|
"type": 8,
|
||||||
"value": "maskedContactInfo"
|
"value": "maskedContactInfo"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1307,8 +1307,7 @@
|
|||||||
"value": "Bitte geben Sie den an "
|
"value": "Bitte geben Sie den an "
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"children": [
|
"children": [],
|
||||||
],
|
|
||||||
"type": 8,
|
"type": 8,
|
||||||
"value": "maskedContactInfo"
|
"value": "maskedContactInfo"
|
||||||
},
|
},
|
||||||
@@ -1323,8 +1322,7 @@
|
|||||||
"value": "Mit Ihrer Anmeldung akzeptieren Sie die Allgemeinen Geschäftsbedingungen von Scandic Friends "
|
"value": "Mit Ihrer Anmeldung akzeptieren Sie die Allgemeinen Geschäftsbedingungen von Scandic Friends "
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"children": [
|
"children": [],
|
||||||
],
|
|
||||||
"type": 8,
|
"type": 8,
|
||||||
"value": "termsAndConditionsLink"
|
"value": "termsAndConditionsLink"
|
||||||
},
|
},
|
||||||
@@ -3494,8 +3492,7 @@
|
|||||||
"value": "Bitte geben Sie den Code ein, der an "
|
"value": "Bitte geben Sie den Code ein, der an "
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"children": [
|
"children": [],
|
||||||
],
|
|
||||||
"type": 8,
|
"type": 8,
|
||||||
"value": "maskedContactInfo"
|
"value": "maskedContactInfo"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1307,8 +1307,7 @@
|
|||||||
"value": "Please enter the code sent to "
|
"value": "Please enter the code sent to "
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"children": [
|
"children": [],
|
||||||
],
|
|
||||||
"type": 8,
|
"type": 8,
|
||||||
"value": "maskedContactInfo"
|
"value": "maskedContactInfo"
|
||||||
},
|
},
|
||||||
@@ -3498,8 +3497,7 @@
|
|||||||
"value": "Please enter the code sent to "
|
"value": "Please enter the code sent to "
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"children": [
|
"children": [],
|
||||||
],
|
|
||||||
"type": 8,
|
"type": 8,
|
||||||
"value": "maskedContactInfo"
|
"value": "maskedContactInfo"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1315,8 +1315,7 @@
|
|||||||
"value": "Kirjoita osoitteeseen "
|
"value": "Kirjoita osoitteeseen "
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"children": [
|
"children": [],
|
||||||
],
|
|
||||||
"type": 8,
|
"type": 8,
|
||||||
"value": "maskedContactInfo"
|
"value": "maskedContactInfo"
|
||||||
},
|
},
|
||||||
@@ -3514,8 +3513,7 @@
|
|||||||
"value": "Kirjoita osoitteeseen "
|
"value": "Kirjoita osoitteeseen "
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"children": [
|
"children": [],
|
||||||
],
|
|
||||||
"type": 8,
|
"type": 8,
|
||||||
"value": "maskedContactInfo"
|
"value": "maskedContactInfo"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1307,8 +1307,7 @@
|
|||||||
"value": "Vennligst skriv inn koden som ble sendt til "
|
"value": "Vennligst skriv inn koden som ble sendt til "
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"children": [
|
"children": [],
|
||||||
],
|
|
||||||
"type": 8,
|
"type": 8,
|
||||||
"value": "maskedContactInfo"
|
"value": "maskedContactInfo"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1307,8 +1307,7 @@
|
|||||||
"value": "Vänligen ange koden som skickats till "
|
"value": "Vänligen ange koden som skickats till "
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"children": [
|
"children": [],
|
||||||
],
|
|
||||||
"type": 8,
|
"type": 8,
|
||||||
"value": "maskedContactInfo"
|
"value": "maskedContactInfo"
|
||||||
},
|
},
|
||||||
@@ -3498,8 +3497,7 @@
|
|||||||
"value": "Vänligen ange den kod som skickats till "
|
"value": "Vänligen ange den kod som skickats till "
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"children": [
|
"children": [],
|
||||||
],
|
|
||||||
"type": 8,
|
"type": 8,
|
||||||
"value": "maskedContactInfo"
|
"value": "maskedContactInfo"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import { requestOtp } from "./otp/request/requestOtp"
|
|||||||
import { verifyOtp } from "./otp/verify/verifyOtp"
|
import { verifyOtp } from "./otp/verify/verifyOtp"
|
||||||
import { linkAccount } from "./linkAccount"
|
import { linkAccount } from "./linkAccount"
|
||||||
import { performLevelUpgrade } from "./performLevelUpgrade"
|
import { performLevelUpgrade } from "./performLevelUpgrade"
|
||||||
import { unlinkAccount } from "./unlinkAccount"
|
|
||||||
import { transferPoints } from "./transferPoints"
|
import { transferPoints } from "./transferPoints"
|
||||||
|
import { unlinkAccount } from "./unlinkAccount"
|
||||||
|
|
||||||
export const sasRouter = router({
|
export const sasRouter = router({
|
||||||
verifyOtp,
|
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")
|
console.log("[SAS] link account done")
|
||||||
return { linkingState: "linked" }
|
return { linkingState: "linked" }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
|
import * as Sentry from "@sentry/nextjs"
|
||||||
import { cookies } from "next/headers"
|
import { cookies } from "next/headers"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
|
|
||||||
import * as api from "@/lib/api"
|
import * as api from "@/lib/api"
|
||||||
import { getVerifiedUser } from "@/server/routers/user/query"
|
|
||||||
import { protectedProcedure } from "@/server/trpc"
|
import { protectedProcedure } from "@/server/trpc"
|
||||||
|
|
||||||
import { timeout } from "@/utils/timeout"
|
import { getUserSchema } from "../../user/output"
|
||||||
|
|
||||||
const matchedSchema = z.object({
|
const matchedSchema = z.object({
|
||||||
tierMatchState: z.enum(["matched"]),
|
tierMatchState: z.enum(["matched"]),
|
||||||
@@ -20,20 +20,13 @@ const outputSchema = z.union([matchedSchema, notMatchedSchema])
|
|||||||
export const performLevelUpgrade = protectedProcedure
|
export const performLevelUpgrade = protectedProcedure
|
||||||
.output(outputSchema)
|
.output(outputSchema)
|
||||||
.mutation(async function ({ ctx }) {
|
.mutation(async function ({ ctx }) {
|
||||||
console.log("[SAS] tier match")
|
|
||||||
|
|
||||||
const cookieStore = cookies()
|
const cookieStore = cookies()
|
||||||
const sasTierMatch = cookieStore.get("sasTierMatch")
|
const sasTierMatch = cookieStore.get("sasTierMatch")
|
||||||
if (sasTierMatch) {
|
if (sasTierMatch) {
|
||||||
return { tierMatchState: "cached" }
|
return { tierMatchState: "cached" }
|
||||||
}
|
}
|
||||||
|
|
||||||
const userBeforeTierMatch = await getVerifiedUser({
|
console.log("[SAS] tier match started")
|
||||||
session: ctx.session,
|
|
||||||
})
|
|
||||||
if (!userBeforeTierMatch || userBeforeTierMatch?.error) {
|
|
||||||
return { tierMatchState: "error" }
|
|
||||||
}
|
|
||||||
|
|
||||||
const apiResponse = await api.post(api.endpoints.v1.Profile.matchTier, {
|
const apiResponse = await api.post(api.endpoints.v1.Profile.matchTier, {
|
||||||
headers: {
|
headers: {
|
||||||
@@ -50,48 +43,41 @@ export const performLevelUpgrade = protectedProcedure
|
|||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
if (apiResponse.status === 202) {
|
const boosted = apiResponse.status === 200
|
||||||
console.log("[SAS] tier match started")
|
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
|
if (!user.membership) {
|
||||||
await timeout(1000)
|
const tierMatchErrorNoMembershipMessage =
|
||||||
|
"[SAS] tier match error - no membership"
|
||||||
const userAfterTierMatch = await getVerifiedUser({
|
console.log(tierMatchErrorNoMembershipMessage)
|
||||||
session: ctx.session,
|
Sentry.captureException(new Error(tierMatchErrorNoMembershipMessage))
|
||||||
})
|
|
||||||
if (!userAfterTierMatch || userAfterTierMatch?.error) {
|
|
||||||
return { tierMatchState: "error" }
|
return { tierMatchState: "error" }
|
||||||
}
|
}
|
||||||
|
|
||||||
const beforeLevel = userBeforeTierMatch.data.membership?.membershipLevel
|
const afterLevel = user.membership.membershipLevel
|
||||||
const afterLevel = userAfterTierMatch.data.membership?.membershipLevel
|
return { tierMatchState: "matched", toLevel: afterLevel }
|
||||||
|
}
|
||||||
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 matchedNoChange = apiResponse.status === 204
|
||||||
|
if (matchedNoChange) {
|
||||||
|
console.log("[SAS] tier match complete - no change")
|
||||||
return { tierMatchState: "alreadyMatched" }
|
return { tierMatchState: "alreadyMatched" }
|
||||||
}
|
}
|
||||||
|
|
||||||
if (apiResponse.status === 204) {
|
const notLinked = apiResponse.status === 404
|
||||||
console.log("[SAS] tier already matched")
|
if (notLinked) {
|
||||||
return { tierMatchState: "alreadyMatched" }
|
const tierMatchErrorNotLinkedMessage =
|
||||||
}
|
"[SAS] tier match error - not linked"
|
||||||
|
console.log(tierMatchErrorNotLinkedMessage)
|
||||||
if (apiResponse.status === 404) {
|
Sentry.captureMessage(tierMatchErrorNotLinkedMessage)
|
||||||
return { tierMatchState: "notLinked" }
|
return { tierMatchState: "notLinked" }
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(
|
const tierMatchErrorMessage = `[SAS] tier match error with status code ${apiResponse.status} and response ${await apiResponse.text()}`
|
||||||
`[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" }
|
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")
|
console.log("[SAS] unlink account success")
|
||||||
return { linkingState: "unlinked" }
|
return { linkingState: "unlinked" }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user