Merged in feat/sw-1314-transfer-sas-points (pull request #1508)

SW-1314 Transfer SAS points

Approved-by: Linus Flood
This commit is contained in:
Anton Gunnarsson
2025-03-18 10:07:05 +00:00
parent d4fe8baa49
commit d0b6f3f8b3
32 changed files with 1799 additions and 12 deletions

View File

@@ -8,7 +8,7 @@ import { serverClient } from "@/lib/trpc/server"
import { getIntl } from "@/i18n"
import { safeTry } from "@/utils/safeTry"
import { SAS_TOKEN_STORAGE_KEY } from "../sasUtils"
import { SAS_TOKEN_STORAGE_KEY, SAS_TRANSFER_POINT_KEY } from "../sasUtils"
import OneTimePasswordForm, {
type OnSubmitHandler,
} from "./OneTimePasswordForm"
@@ -19,7 +19,7 @@ import type { LangParams, PageArgs, SearchParams } from "@/types/params"
import type { Lang } from "@/constants/languages"
const otpError = z.enum(["invalidCode", "expiredCode"])
const intent = z.enum(["link", "unlink"])
const intent = z.enum(["link", "unlink", "transfer"])
const searchParamsSchema = z.object({
intent: intent,
to: z.string(),
@@ -95,6 +95,8 @@ export default async function SASxScandicOneTimePasswordPage({
return handleLinkAccount({ lang: params.lang })
case "unlink":
return handleUnlinkAccount({ lang: params.lang })
case "transfer":
return handleTransferPoints({ lang: params.lang })
}
}
@@ -118,6 +120,12 @@ export default async function SASxScandicOneTimePasswordPage({
},
{ maskedContactInfo }
),
transfer: intl.formatMessage(
{
id: "Please enter the code sent to <maskedContactInfo></maskedContactInfo> in order to transfer your points.",
},
{ maskedContactInfo }
),
}
return (
@@ -215,3 +223,44 @@ async function handleUnlinkAccount({
}
}
}
async function handleTransferPoints({
lang,
}: {
lang: Lang
}): ReturnType<OnSubmitHandler> {
const cookieStore = cookies()
const pointsCookie = cookieStore.get(SAS_TRANSFER_POINT_KEY)
const points = Number(pointsCookie?.value)
cookieStore.delete(SAS_TRANSFER_POINT_KEY)
if (!pointsCookie || !points || isNaN(points)) {
return {
url: `/${lang}/sas-x-scandic/error`,
type: "replace",
}
}
const [res, error] = await safeTry(
serverClient().partner.sas.transferPoints({
points,
})
)
if (!res || error) {
console.error("[SAS] transfer points error", error)
return {
// TODO better errors?
url: `/${lang}/sas-x-scandic/error`,
type: "replace",
}
}
console.log("[SAS] transfer points response", res)
return {
url: `/${lang}/sas-x-scandic/transfer/success?p=${points}`,
type: "replace",
}
}