Feat/LOY-431: Switch to V2 of Profile endpoint * feat(LOY-431): switch to v2 of profile endpoint * feat(LOY-431): use CreditCard * feat(LOY-431): remove hotelinformation from friendTransaction schema * chore(LOY-431): add hotel data request to transactions * fix(LOY-431): use v1 of friendTransactions Approved-by: Linus Flood Approved-by: Erik Tiekstra Approved-by: Anton Gunnarsson
81 lines
2.5 KiB
TypeScript
81 lines
2.5 KiB
TypeScript
import * as Sentry from "@sentry/nextjs"
|
|
import { z } from "zod"
|
|
|
|
import { createLogger } from "@scandic-hotels/common/logger/createLogger"
|
|
|
|
import * as api from "../../../api"
|
|
import { protectedProcedure } from "../../../procedures"
|
|
import { getOTPState } from "./otp/getOTPState"
|
|
import { getSasToken } from "./getSasToken"
|
|
|
|
const outputSchema = z.object({
|
|
transferState: z.enum(["success", "notLinked", "error"]),
|
|
})
|
|
|
|
const transferPointsInputSchema = z.object({
|
|
points: z.number(),
|
|
})
|
|
|
|
const sasLogger = createLogger("SAS")
|
|
export const transferPoints = protectedProcedure
|
|
.output(outputSchema)
|
|
.input(transferPointsInputSchema)
|
|
.mutation(async function ({ ctx, input }) {
|
|
const sasAuthToken = await getSasToken()
|
|
const { referenceId } = await getOTPState()
|
|
|
|
const apiResponse = await api.post(
|
|
api.endpoints.v2.Profile.pointTransfer,
|
|
{
|
|
headers: {
|
|
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
|
},
|
|
body: {
|
|
partner: "sas_eb",
|
|
partnerPoints: input.points,
|
|
partnerSpecific: {
|
|
eurobonusAccessToken: sasAuthToken,
|
|
eurobonusOtpReferenceId: referenceId,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
language: ctx.lang,
|
|
}
|
|
)
|
|
|
|
if (apiResponse.status === 204) {
|
|
return { transferState: "success" }
|
|
}
|
|
if (apiResponse.status === 400) {
|
|
const result = await apiResponse.json()
|
|
const data = badRequestSchema.safeParse(result)
|
|
if (!data.success) {
|
|
const transferPointsBadRequestSchemaError = `[SAS] failed to parse transfer points bad request schema ${JSON.stringify(data.error)}`
|
|
sasLogger.error(transferPointsBadRequestSchemaError)
|
|
Sentry.captureMessage(transferPointsBadRequestSchemaError)
|
|
return { transferState: "error" }
|
|
}
|
|
}
|
|
if (apiResponse.status === 404) {
|
|
const transferPointsNotFoundError = `[SAS] transfer points failed, no active partner link`
|
|
sasLogger.error(transferPointsNotFoundError)
|
|
Sentry.captureMessage(transferPointsNotFoundError)
|
|
return { transferState: "notLinked" }
|
|
}
|
|
|
|
const errorMessage = `[SAS] transfer points error with status code ${apiResponse.status} and response ${await apiResponse.text()}`
|
|
sasLogger.error(errorMessage)
|
|
Sentry.captureMessage(errorMessage)
|
|
return { transferState: "error" }
|
|
})
|
|
|
|
const badRequestSchema = z.object({
|
|
errors: z.array(
|
|
z.object({
|
|
code: z.string(),
|
|
details: z.string().optional(),
|
|
})
|
|
),
|
|
})
|