diff --git a/apps/scandic-web/components/MyPages/SASLevelUpgradeCheck.tsx b/apps/scandic-web/components/MyPages/SASLevelUpgradeCheck.tsx index 91f162998..b8b24fd3d 100644 --- a/apps/scandic-web/components/MyPages/SASLevelUpgradeCheck.tsx +++ b/apps/scandic-web/components/MyPages/SASLevelUpgradeCheck.tsx @@ -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() { diff --git a/apps/scandic-web/i18n/dictionaries/da.json b/apps/scandic-web/i18n/dictionaries/da.json index fdf761578..991b507d2 100644 --- a/apps/scandic-web/i18n/dictionaries/da.json +++ b/apps/scandic-web/i18n/dictionaries/da.json @@ -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" } ] -} \ No newline at end of file +} diff --git a/apps/scandic-web/i18n/dictionaries/de.json b/apps/scandic-web/i18n/dictionaries/de.json index b2f579e43..448c66dde 100644 --- a/apps/scandic-web/i18n/dictionaries/de.json +++ b/apps/scandic-web/i18n/dictionaries/de.json @@ -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" } ] -} \ No newline at end of file +} diff --git a/apps/scandic-web/i18n/dictionaries/en.json b/apps/scandic-web/i18n/dictionaries/en.json index 1d46fc48d..f3ae91cac 100644 --- a/apps/scandic-web/i18n/dictionaries/en.json +++ b/apps/scandic-web/i18n/dictionaries/en.json @@ -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" } ] -} \ No newline at end of file +} diff --git a/apps/scandic-web/i18n/dictionaries/fi.json b/apps/scandic-web/i18n/dictionaries/fi.json index 55030aa35..5c996c657 100644 --- a/apps/scandic-web/i18n/dictionaries/fi.json +++ b/apps/scandic-web/i18n/dictionaries/fi.json @@ -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" } ] -} \ No newline at end of file +} diff --git a/apps/scandic-web/i18n/dictionaries/no.json b/apps/scandic-web/i18n/dictionaries/no.json index ce0ffb70e..94dc5de03 100644 --- a/apps/scandic-web/i18n/dictionaries/no.json +++ b/apps/scandic-web/i18n/dictionaries/no.json @@ -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" } ] -} \ No newline at end of file +} diff --git a/apps/scandic-web/i18n/dictionaries/pl.json b/apps/scandic-web/i18n/dictionaries/pl.json index 49347814b..3144e5abd 100644 --- a/apps/scandic-web/i18n/dictionaries/pl.json +++ b/apps/scandic-web/i18n/dictionaries/pl.json @@ -17,4 +17,4 @@ "value": " m²" } ] -} \ No newline at end of file +} diff --git a/apps/scandic-web/i18n/dictionaries/sv.json b/apps/scandic-web/i18n/dictionaries/sv.json index 22455a326..5e694948f 100644 --- a/apps/scandic-web/i18n/dictionaries/sv.json +++ b/apps/scandic-web/i18n/dictionaries/sv.json @@ -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" } ] -} \ No newline at end of file +} diff --git a/apps/scandic-web/server/routers/partners/sas/index.ts b/apps/scandic-web/server/routers/partners/sas/index.ts index d0e165a17..579dc87cf 100644 --- a/apps/scandic-web/server/routers/partners/sas/index.ts +++ b/apps/scandic-web/server/routers/partners/sas/index.ts @@ -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, diff --git a/apps/scandic-web/server/routers/partners/sas/linkAccount.ts b/apps/scandic-web/server/routers/partners/sas/linkAccount.ts index 9e75713f4..58eeb873f 100644 --- a/apps/scandic-web/server/routers/partners/sas/linkAccount.ts +++ b/apps/scandic-web/server/routers/partners/sas/linkAccount.ts @@ -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" } } diff --git a/apps/scandic-web/server/routers/partners/sas/performLevelUpgrade.ts b/apps/scandic-web/server/routers/partners/sas/performLevelUpgrade.ts index 0956c0020..92fb79153 100644 --- a/apps/scandic-web/server/routers/partners/sas/performLevelUpgrade.ts +++ b/apps/scandic-web/server/routers/partners/sas/performLevelUpgrade.ts @@ -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" } }) diff --git a/apps/scandic-web/server/routers/partners/sas/unlinkAccount.ts b/apps/scandic-web/server/routers/partners/sas/unlinkAccount.ts index fb0038a8b..cfd8ecbda 100644 --- a/apps/scandic-web/server/routers/partners/sas/unlinkAccount.ts +++ b/apps/scandic-web/server/routers/partners/sas/unlinkAccount.ts @@ -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" } }