feat(SW-3642): Enable SAS EB payments * Wip add SAS eb payment * Add validate payment call * Check booking status payment method to determine validation * Clean up getPaymentData * Fix PartnerPoints casing * Add comment for validatePartnerPayment error handling * Remove comment Approved-by: Joakim Jäderberg
96 lines
2.7 KiB
TypeScript
96 lines
2.7 KiB
TypeScript
import "server-only"
|
|
|
|
import { PaymentMethodEnum } from "@scandic-hotels/common/constants/paymentMethod"
|
|
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import * as api from "../../../../api"
|
|
import { safeProtectedServiceProcedure } from "../../../../procedures"
|
|
import { encrypt } from "../../../../utils/encryption"
|
|
import { createBookingInput, createBookingSchema } from "./schema"
|
|
|
|
export const create = safeProtectedServiceProcedure
|
|
.input(createBookingInput)
|
|
.use(async ({ ctx, next }) => {
|
|
const token = await ctx.getScandicUserToken()
|
|
|
|
return next({
|
|
ctx: {
|
|
token,
|
|
},
|
|
})
|
|
})
|
|
.mutation(async function ({ ctx, input }) {
|
|
const { language, ...inputWithoutLang } = input
|
|
const { rooms, ...loggableInput } = inputWithoutLang
|
|
|
|
const createBookingCounter = createCounter("trpc.booking.create")
|
|
|
|
const metricsCreateBooking = createBookingCounter.init({
|
|
language,
|
|
...loggableInput,
|
|
rooms: inputWithoutLang.rooms.map(({ guest, ...room }) => {
|
|
const { becomeMember, membershipNumber } = guest
|
|
return { ...room, guest: { becomeMember, membershipNumber } }
|
|
}),
|
|
})
|
|
|
|
metricsCreateBooking.start()
|
|
const headers = {
|
|
Authorization: `Bearer ${ctx.token ?? ctx.serviceToken}`,
|
|
}
|
|
|
|
const includePartnerSpecific =
|
|
inputWithoutLang.payment?.paymentMethod ===
|
|
PaymentMethodEnum.PartnerPoints
|
|
if (includePartnerSpecific) {
|
|
const session = await ctx.auth()
|
|
const token = session?.token.access_token
|
|
if (!token) {
|
|
throw new Error(
|
|
"Cannot create booking with partner points without partner token"
|
|
)
|
|
}
|
|
|
|
inputWithoutLang.partnerSpecific = {
|
|
eurobonusAccessToken: session?.token.access_token,
|
|
}
|
|
}
|
|
|
|
const apiResponse = await api.post(
|
|
api.endpoints.v1.Booking.bookings,
|
|
{
|
|
headers,
|
|
body: inputWithoutLang,
|
|
},
|
|
{ language }
|
|
)
|
|
|
|
if (!apiResponse.ok) {
|
|
await metricsCreateBooking.httpError(apiResponse)
|
|
|
|
const apiJson = await apiResponse.json()
|
|
if ("errors" in apiJson && apiJson.errors.length) {
|
|
const error = apiJson.errors[0]
|
|
return { error: true, cause: error.code } as const
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
const apiJson = await apiResponse.json()
|
|
const verifiedData = createBookingSchema.safeParse(apiJson)
|
|
if (!verifiedData.success) {
|
|
metricsCreateBooking.validationError(verifiedData.error)
|
|
return null
|
|
}
|
|
|
|
metricsCreateBooking.success()
|
|
|
|
const expire = Math.floor(Date.now() / 1000) + 60 // 1 minute expiry
|
|
|
|
return {
|
|
booking: verifiedData.data,
|
|
sig: encrypt(expire.toString()),
|
|
}
|
|
})
|