Merged in feat/sw-1948-point-transfer-endpoint (pull request #2353)
feat(SW-1948):Add SAS point transfer endpoint * Add SAS point transfer endpoint Approved-by: Joakim Jäderberg
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
import * as Sentry from "@sentry/nextjs"
|
||||
import { z } from "zod"
|
||||
|
||||
// import * as api from "@/lib/api"
|
||||
import * as api from "@/lib/api"
|
||||
import { protectedProcedure } from "@/server/trpc"
|
||||
|
||||
import { getOTPState } from "./otp/getOTPState"
|
||||
import { getSasToken } from "./getSasToken"
|
||||
|
||||
const outputSchema = z.object({
|
||||
transferState: z.enum(["success"]),
|
||||
transferState: z.enum(["success", "notLinked", "error"]),
|
||||
})
|
||||
|
||||
const transferPointsInputSchema = z.object({
|
||||
@@ -18,24 +20,53 @@ export const transferPoints = protectedProcedure
|
||||
.input(transferPointsInputSchema)
|
||||
.mutation(async function ({ ctx, input }) {
|
||||
const sasAuthToken = await getSasToken()
|
||||
const { referenceId } = await getOTPState()
|
||||
|
||||
console.log("[SAS] transfer points")
|
||||
console.log({ sasAuthToken })
|
||||
console.log({ points: input.points })
|
||||
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,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// 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) {
|
||||
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" }
|
||||
}
|
||||
|
||||
console.log(`[SAS] transfer points success`)
|
||||
return { transferState: "success" }
|
||||
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(),
|
||||
})
|
||||
),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user