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
156 lines
4.3 KiB
TypeScript
156 lines
4.3 KiB
TypeScript
import { dt } from "@scandic-hotels/common/dt"
|
|
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import { router } from "../../.."
|
|
import * as api from "../../../api"
|
|
import { createRefIdPlugin } from "../../../plugins/refIdToConfirmationNumber"
|
|
import { safeProtectedServiceProcedure } from "../../../procedures"
|
|
import { updateBooking } from "../../../services/booking/updateBooking"
|
|
import {
|
|
removePackageInput,
|
|
resendConfirmationInput,
|
|
updateBookingInput,
|
|
} from "../input"
|
|
import { addPackagesRoute } from "./addPackagesRoute"
|
|
import { cancelBookingRoute } from "./cancelBookingRoute"
|
|
import { createBookingRoute } from "./createBookingRoute"
|
|
import { createRefIdRoute } from "./createRefIdRoute"
|
|
import { guaranteeBookingRoute } from "./guaranteeBookingRoute"
|
|
import { priceChangeRoute } from "./priceChangeRoute"
|
|
import { validatePartnerPayment } from "./validatePartnerPayment"
|
|
|
|
const refIdPlugin = createRefIdPlugin()
|
|
|
|
export const bookingMutationRouter = router({
|
|
create: createBookingRoute,
|
|
createRefId: createRefIdRoute,
|
|
validatePartnerPayment,
|
|
priceChange: priceChangeRoute,
|
|
cancel: cancelBookingRoute,
|
|
packages: addPackagesRoute,
|
|
guarantee: guaranteeBookingRoute,
|
|
update: safeProtectedServiceProcedure
|
|
.input(updateBookingInput)
|
|
.concat(refIdPlugin.toConfirmationNumber)
|
|
.use(async ({ ctx, next }) => {
|
|
const token = await ctx.getScandicUserToken()
|
|
|
|
return next({
|
|
ctx: {
|
|
token,
|
|
},
|
|
})
|
|
})
|
|
.mutation(async function ({ ctx, input }) {
|
|
const { confirmationNumber } = ctx
|
|
const { language, refId, ...rest } = input
|
|
const token = ctx.token ?? ctx.serviceToken
|
|
|
|
return updateBooking(
|
|
{
|
|
confirmationNumber,
|
|
lang: language,
|
|
checkInDate: rest.checkInDate ? dt.utc(rest.checkInDate) : undefined,
|
|
checkOutDate: rest.checkOutDate
|
|
? dt.utc(rest.checkOutDate)
|
|
: undefined,
|
|
guest: rest.guest,
|
|
},
|
|
token
|
|
)
|
|
}),
|
|
removePackage: safeProtectedServiceProcedure
|
|
.input(removePackageInput)
|
|
.concat(refIdPlugin.toConfirmationNumber)
|
|
.use(async ({ ctx, next }) => {
|
|
const token = await ctx.getScandicUserToken()
|
|
|
|
return next({
|
|
ctx: {
|
|
token,
|
|
},
|
|
})
|
|
})
|
|
.mutation(async function ({ ctx, input }) {
|
|
const { confirmationNumber } = ctx
|
|
const { codes, language } = input
|
|
|
|
const removePackageCounter = createCounter("trpc.booking.package.remove")
|
|
const metricsRemovePackage = removePackageCounter.init({
|
|
confirmationNumber,
|
|
codes,
|
|
language,
|
|
})
|
|
|
|
metricsRemovePackage.start()
|
|
|
|
const token = ctx.token ?? ctx.serviceToken
|
|
const headers = {
|
|
Authorization: `Bearer ${token}`,
|
|
}
|
|
|
|
const apiResponse = await api.remove(
|
|
api.endpoints.v1.Booking.packages(confirmationNumber),
|
|
{
|
|
headers,
|
|
},
|
|
[["language", language], ...codes.map((code) => ["codes", code])]
|
|
)
|
|
|
|
if (!apiResponse.ok) {
|
|
await metricsRemovePackage.httpError(apiResponse)
|
|
return false
|
|
}
|
|
|
|
metricsRemovePackage.success()
|
|
|
|
return true
|
|
}),
|
|
resendConfirmation: safeProtectedServiceProcedure
|
|
.input(resendConfirmationInput)
|
|
.concat(refIdPlugin.toConfirmationNumber)
|
|
.use(async ({ ctx, next }) => {
|
|
const token = await ctx.getScandicUserToken()
|
|
|
|
return next({
|
|
ctx: {
|
|
token,
|
|
},
|
|
})
|
|
})
|
|
.mutation(async function ({ ctx, input }) {
|
|
const { confirmationNumber } = ctx
|
|
|
|
const resendConfirmationCounter = createCounter(
|
|
"trpc.booking.confirmation.resend"
|
|
)
|
|
const metricsResendConfirmation = resendConfirmationCounter.init({
|
|
confirmationNumber,
|
|
})
|
|
|
|
metricsResendConfirmation.start()
|
|
|
|
const token = ctx.token ?? ctx.serviceToken
|
|
const headers = {
|
|
Authorization: `Bearer ${token}`,
|
|
}
|
|
|
|
const apiResponse = await api.post(
|
|
api.endpoints.v1.Booking.confirmNotification(confirmationNumber),
|
|
{
|
|
headers,
|
|
},
|
|
{ language: input.language }
|
|
)
|
|
|
|
if (!apiResponse.ok) {
|
|
await metricsResendConfirmation.httpError(apiResponse)
|
|
return false
|
|
}
|
|
|
|
metricsResendConfirmation.success()
|
|
|
|
return true
|
|
}),
|
|
})
|