feat(sw-2866): Move partners router to trpc package * Add eslint to trpc package * Apply lint rules * Use direct imports from trpc package * Add lint-staged config to trpc * Move lang enum to common * Restructure trpc package folder structure * WIP first step * update internal imports in trpc * Fix most errors in scandic-web Just 100 left... * Move Props type out of trpc * Fix CategorizedFilters types * Move more schemas in hotel router * Fix deps * fix getNonContentstackUrls * Fix import error * Fix entry error handling * Fix generateMetadata metrics * Fix alertType enum * Fix duplicated types * lint:fix * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package * Fix broken imports * Move booking router to trpc package * Move partners router to trpc package * Merge branch 'master' into feat/sw-2866-move-partners-router-to-trpc-package Approved-by: Linus Flood
72 lines
2.3 KiB
TypeScript
72 lines
2.3 KiB
TypeScript
import * as Sentry from "@sentry/nextjs"
|
|
import { z } from "zod"
|
|
|
|
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(),
|
|
})
|
|
|
|
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.v1.Profile.pointTransfer, {
|
|
headers: {
|
|
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
|
},
|
|
body: {
|
|
partner: "sas_eb",
|
|
partnerPoints: input.points,
|
|
partnerSpecific: {
|
|
eurobonusAccessToken: sasAuthToken,
|
|
eurobonusOtpReferenceId: referenceId,
|
|
},
|
|
},
|
|
})
|
|
|
|
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)}`
|
|
console.error(transferPointsBadRequestSchemaError)
|
|
Sentry.captureMessage(transferPointsBadRequestSchemaError)
|
|
return { transferState: "error" }
|
|
}
|
|
}
|
|
if (apiResponse.status === 404) {
|
|
const transferPointsNotFoundError = `[SAS] transfer points failed, no active partner link`
|
|
console.error(transferPointsNotFoundError)
|
|
Sentry.captureMessage(transferPointsNotFoundError)
|
|
return { transferState: "notLinked" }
|
|
}
|
|
|
|
const errorMessage = `[SAS] transfer points error with status code ${apiResponse.status} and response ${await apiResponse.text()}`
|
|
console.warn(errorMessage)
|
|
Sentry.captureMessage(errorMessage)
|
|
return { transferState: "error" }
|
|
})
|
|
|
|
const badRequestSchema = z.object({
|
|
errors: z.array(
|
|
z.object({
|
|
code: z.string(),
|
|
details: z.string().optional(),
|
|
})
|
|
),
|
|
})
|