Implement API call to link SAS account * Add endpoint to actually link SAS account linking * add logging of error * Refactor tocDate to getCurrentDateWithoutTime Approved-by: Joakim Jäderberg
65 lines
1.6 KiB
TypeScript
65 lines
1.6 KiB
TypeScript
import { z } from "zod"
|
|
|
|
import * as api from "@/lib/api"
|
|
import { protectedProcedure } from "@/server/trpc"
|
|
|
|
import { getSasToken } from "./getSasToken"
|
|
|
|
const outputSchema = z.object({
|
|
linkingState: z.enum([
|
|
"linked",
|
|
"alreadyLinked",
|
|
"dateOfBirthMismatch",
|
|
"error",
|
|
]),
|
|
})
|
|
|
|
export const linkAccount = protectedProcedure
|
|
.output(outputSchema)
|
|
.mutation(async function ({ ctx }) {
|
|
const sasAuthToken = getSasToken()
|
|
|
|
console.log("[SAS] link account")
|
|
|
|
const apiResponse = await api.post(api.endpoints.v1.Profile.link, {
|
|
headers: {
|
|
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
|
},
|
|
body: {
|
|
partner: "sas_eb",
|
|
tocDate: getCurrentDateWithoutTime(),
|
|
partnerSpecific: {
|
|
eurobonusAccessToken: sasAuthToken,
|
|
},
|
|
},
|
|
})
|
|
|
|
if (apiResponse.status === 204) {
|
|
console.log("[SAS] link account done")
|
|
return { linkingState: "linked" }
|
|
}
|
|
|
|
if (apiResponse.status === 400) {
|
|
const result = await apiResponse.json()
|
|
if (result.errors?.some((x: any) => x.detail.includes("birth date"))) {
|
|
return { linkingState: "dateOfBirthMismatch" }
|
|
}
|
|
|
|
console.log("[SAS] link account error with response", result)
|
|
return { linkingState: "error" }
|
|
}
|
|
|
|
if (apiResponse.status === 409) {
|
|
return { linkingState: "alreadyLinked" }
|
|
}
|
|
|
|
console.log(
|
|
`[SAS] link account error with status code ${apiResponse.status} and response ${await apiResponse.text()}`
|
|
)
|
|
return { linkingState: "error" }
|
|
})
|
|
|
|
function getCurrentDateWithoutTime() {
|
|
return new Date().toISOString().slice(0, 10)
|
|
}
|