Merged in feat/SW-1266-implement-sas-link-account-endpoint (pull request #1332)
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
This commit is contained in:
@@ -117,7 +117,7 @@ export async function GET(
|
|||||||
/** Record<string, any> is next-auth typings */
|
/** Record<string, any> is next-auth typings */
|
||||||
const params: Record<string, any> = {
|
const params: Record<string, any> = {
|
||||||
ui_locales: context.params.lang,
|
ui_locales: context.params.lang,
|
||||||
scope: ["openid", "profile", "booking"],
|
scope: ["openid", "profile", "booking", "profile_link"],
|
||||||
/**
|
/**
|
||||||
* The `acr_values` param is used to make Curity display the proper login
|
* The `acr_values` param is used to make Curity display the proper login
|
||||||
* page for Scandic. Without the parameter Curity presents some choices
|
* page for Scandic. Without the parameter Curity presents some choices
|
||||||
|
|||||||
@@ -141,16 +141,30 @@ async function handleLinkAccount({
|
|||||||
if (!res || error) {
|
if (!res || error) {
|
||||||
console.error("[SAS] link account error", error)
|
console.error("[SAS] link account error", error)
|
||||||
return {
|
return {
|
||||||
url: `/${lang}/sas-x-scandic/error?errorCode=link_error`,
|
url: `/${lang}/sas-x-scandic/error`,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("[SAS] link account response", res)
|
|
||||||
switch (res.linkingState) {
|
switch (res.linkingState) {
|
||||||
|
case "alreadyLinked":
|
||||||
|
return {
|
||||||
|
url: `/${lang}/sas-x-scandic/error?errorCode=alreadyLinked`,
|
||||||
|
type: "replace",
|
||||||
|
}
|
||||||
case "linked":
|
case "linked":
|
||||||
return {
|
return {
|
||||||
url: `/${lang}/sas-x-scandic/link/success`,
|
url: `/${lang}/sas-x-scandic/link/success`,
|
||||||
type: "replace",
|
type: "replace",
|
||||||
}
|
}
|
||||||
|
case "dateOfBirthMismatch":
|
||||||
|
return {
|
||||||
|
url: `/${lang}/sas-x-scandic/error?errorCode=dateOfBirthMismatch`,
|
||||||
|
type: "replace",
|
||||||
|
}
|
||||||
|
case "error":
|
||||||
|
return {
|
||||||
|
url: `/${lang}/sas-x-scandic/error`,
|
||||||
|
type: "replace",
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -161,6 +161,7 @@ export namespace endpoints {
|
|||||||
export const membership = `${base.path.profile}/${version}/${base.enitity.Profile}/membership`
|
export const membership = `${base.path.profile}/${version}/${base.enitity.Profile}/membership`
|
||||||
export const profile = `${base.path.profile}/${version}/${base.enitity.Profile}`
|
export const profile = `${base.path.profile}/${version}/${base.enitity.Profile}`
|
||||||
export const subscriberId = `${base.path.profile}/${version}/${base.enitity.Profile}/SubscriberId`
|
export const subscriberId = `${base.path.profile}/${version}/${base.enitity.Profile}/SubscriberId`
|
||||||
|
export const link = `${base.path.profile}/${version}/${base.enitity.Profile}/link`
|
||||||
|
|
||||||
// TODO: Remove once new endpoints are out in production.
|
// TODO: Remove once new endpoints are out in production.
|
||||||
export const reward = `${base.path.profile}/${version}/${base.enitity.Profile}/reward`
|
export const reward = `${base.path.profile}/${version}/${base.enitity.Profile}/reward`
|
||||||
|
|||||||
@@ -1,26 +1,64 @@
|
|||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
|
|
||||||
|
import * as api from "@/lib/api"
|
||||||
import { protectedProcedure } from "@/server/trpc"
|
import { protectedProcedure } from "@/server/trpc"
|
||||||
|
|
||||||
import { timeout } from "@/utils/timeout"
|
|
||||||
|
|
||||||
import { getSasToken } from "./getSasToken"
|
import { getSasToken } from "./getSasToken"
|
||||||
|
|
||||||
const outputSchema = z.object({
|
const outputSchema = z.object({
|
||||||
linkingState: z.enum(["linked"]),
|
linkingState: z.enum([
|
||||||
|
"linked",
|
||||||
|
"alreadyLinked",
|
||||||
|
"dateOfBirthMismatch",
|
||||||
|
"error",
|
||||||
|
]),
|
||||||
})
|
})
|
||||||
|
|
||||||
export const linkAccount = protectedProcedure
|
export const linkAccount = protectedProcedure
|
||||||
.output(outputSchema)
|
.output(outputSchema)
|
||||||
.mutation(async function ({ ctx, input }) {
|
.mutation(async function ({ ctx }) {
|
||||||
const sasAuthToken = getSasToken()
|
const sasAuthToken = getSasToken()
|
||||||
|
|
||||||
console.log("[SAS] link account")
|
console.log("[SAS] link account")
|
||||||
await timeout(1000)
|
|
||||||
//TODO: Call actual API here
|
|
||||||
console.log("[SAS] link account done")
|
|
||||||
|
|
||||||
return {
|
const apiResponse = await api.post(api.endpoints.v1.Profile.link, {
|
||||||
linkingState: "linked",
|
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)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user