From 94a00d40b7802a1721f34f5f165541b337a49867 Mon Sep 17 00:00:00 2001 From: Anton Gunnarsson Date: Mon, 12 May 2025 11:48:31 +0000 Subject: [PATCH] Merged in feat/sw-2526-update-sas-error-handling-after-api-changes (pull request #1886) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SW-2526 - Update error handling for link SAS account * Update error handling for link SAS account * Capture schema validation error and send to Sentry * Add cases for unhandled otp errors Approved-by: Joakim Jäderberg --- .../(protected)/sas-x-scandic/otp/page.tsx | 5 ++ .../MyPages/SASLevelUpgradeCheck.tsx | 3 +- .../routers/partners/sas/linkAccount.ts | 46 ++++++++++++++++++- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/apps/scandic-web/app/[lang]/(partner)/(sas)/(protected)/sas-x-scandic/otp/page.tsx b/apps/scandic-web/app/[lang]/(partner)/(sas)/(protected)/sas-x-scandic/otp/page.tsx index 23dd6c989..968f05899 100644 --- a/apps/scandic-web/app/[lang]/(partner)/(sas)/(protected)/sas-x-scandic/otp/page.tsx +++ b/apps/scandic-web/app/[lang]/(partner)/(sas)/(protected)/sas-x-scandic/otp/page.tsx @@ -190,7 +190,12 @@ async function handleLinkAccount({ url: `/${lang}/sas-x-scandic/error?errorCode=dateOfBirthMismatch`, type: "replace", } + // Currently unhandled errors + case "accountToNew": + case "nameNotMatched": + case "blockedForRelink": case "error": + default: return { url: `/${lang}/sas-x-scandic/error`, type: "replace", diff --git a/apps/scandic-web/components/MyPages/SASLevelUpgradeCheck.tsx b/apps/scandic-web/components/MyPages/SASLevelUpgradeCheck.tsx index d3946e2c3..88d2bd2d0 100644 --- a/apps/scandic-web/components/MyPages/SASLevelUpgradeCheck.tsx +++ b/apps/scandic-web/components/MyPages/SASLevelUpgradeCheck.tsx @@ -24,7 +24,8 @@ export function SASLevelUpgradeCheck() { toast.success( intl.formatMessage( { - defaultMessage: "Your SAS level has upgraded you to {level}!", + defaultMessage: + "Your SAS EuroBonus level has upgraded you to {level}!", }, { level: TIER_TO_FRIEND_MAP[result.toLevel], diff --git a/apps/scandic-web/server/routers/partners/sas/linkAccount.ts b/apps/scandic-web/server/routers/partners/sas/linkAccount.ts index cc760b3d7..de752b024 100644 --- a/apps/scandic-web/server/routers/partners/sas/linkAccount.ts +++ b/apps/scandic-web/server/routers/partners/sas/linkAccount.ts @@ -11,6 +11,9 @@ const outputSchema = z.object({ "linked", "alreadyLinked", "dateOfBirthMismatch", + "nameNotMatched", + "blockedForRelink", + "accountToNew", "error", ]), }) @@ -48,11 +51,34 @@ export const linkAccount = protectedProcedure if (apiResponse.status === 400) { const result = await apiResponse.json() - if (result.errors?.some((x: any) => x.detail.includes("birth date"))) { - return { linkingState: "dateOfBirthMismatch" } + const data = badRequestSchema.safeParse(result) + if (!data.success) { + const linkAccountBadRequestSchemaError = `[SAS] failed to parse link account bad request schema ${JSON.stringify(data.error)}` + console.error(linkAccountBadRequestSchemaError) + Sentry.captureMessage(linkAccountBadRequestSchemaError) + return { linkingState: "error" } } console.log("[SAS] link account error with response", result) + + const { errors } = data.data + + if (errors.some((x) => x.code === "BirthDateNotMatched")) { + return { linkingState: "dateOfBirthMismatch" } + } + if (errors.some((x) => x.code === "BlockedForRelink")) { + return { linkingState: "blockedForRelink" } + } + if (errors.some((x) => x.code === "AccountToNew")) { + return { linkingState: "accountToNew" } + } + if (errors.some((x) => x.code === "NameNotMatched")) { + return { linkingState: "nameNotMatched" } + } + if (errors.some((x) => x.code === "AlreadyLinked")) { + return { linkingState: "alreadyLinked" } + } + return { linkingState: "error" } } @@ -69,3 +95,19 @@ export const linkAccount = protectedProcedure function getCurrentDateWithoutTime() { return new Date().toISOString().slice(0, 10) } + +const badRequestSchema = z.object({ + errors: z.array( + z.object({ + code: z.enum([ + "BirthDateNotMatched", + "NameNotMatched", + "AlreadyLinked", + "BlockedForRelink", + "AccountToNew", + "UnknownReason", + ]), + details: z.string().optional(), + }) + ), +})