feat(BOOK-750): refactor booking endpoints * WIP * wip * wip * parse dates in UTC * wip * no more errors * Merge branch 'master' of bitbucket.org:scandic-swap/web into chore/refactor-trpc-booking-routes * . * cleanup * import named z from zod * fix(BOOK-750): updateBooking api endpoint expects dateOnly, we passed ISO date Approved-by: Anton Gunnarsson
47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
import "server-only"
|
|
|
|
import { PaymentMethodEnum } from "@scandic-hotels/common/constants/paymentMethod"
|
|
|
|
import { safeProtectedServiceProcedure } from "../../../../procedures"
|
|
import { createBooking } from "../../../../services/booking/createBooking"
|
|
import { encrypt } from "../../../../utils/encryption"
|
|
import { createBookingInput } from "./schema"
|
|
export const createBookingRoute = safeProtectedServiceProcedure
|
|
.input(createBookingInput)
|
|
.use(async ({ ctx, next }) => {
|
|
const token = await ctx.getScandicUserToken()
|
|
|
|
return next({
|
|
ctx: {
|
|
token,
|
|
},
|
|
})
|
|
})
|
|
.mutation(async function ({ ctx, input }) {
|
|
if (input.payment?.paymentMethod === PaymentMethodEnum.PartnerPoints) {
|
|
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"
|
|
)
|
|
}
|
|
|
|
input.partnerSpecific = {
|
|
eurobonusAccessToken: session?.token.access_token,
|
|
}
|
|
}
|
|
|
|
const booking = await createBooking(input, ctx.token ?? ctx.serviceToken)
|
|
if ("error" in booking) {
|
|
return { ...booking }
|
|
}
|
|
|
|
const expire = Math.floor(Date.now() / 1000) + 60 // 1 minute expiry
|
|
|
|
return {
|
|
booking,
|
|
sig: encrypt(expire.toString()),
|
|
}
|
|
})
|