Merged in feat/SW-431-payment-flow (pull request #635)
Feat/SW-431 payment flow * feat(SW-431): Update mock hotel data * feat(SW-431): Added route handler and trpc routes * feat(SW-431): List payment methods and handle booking status and redirection * feat(SW-431): Updated booking page to poll for booking status * feat(SW-431): Updated create booking contract * feat(SW-431): small fix * fix(SW-431): Added intl string and sorted dictionaries * fix(SW-431): Changes from PR * fix(SW-431): fixes from PR * fix(SW-431): add todo comments * fix(SW-431): update schema prop Approved-by: Simon.Emanuelsson
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
import { mergeRouters } from "@/server/trpc"
|
||||
|
||||
import { bookingMutationRouter } from "./mutation"
|
||||
import { bookingQueryRouter } from "./query"
|
||||
|
||||
export const bookingRouter = mergeRouters(bookingMutationRouter)
|
||||
export const bookingRouter = mergeRouters(
|
||||
bookingMutationRouter,
|
||||
bookingQueryRouter
|
||||
)
|
||||
|
||||
@@ -1,38 +1,68 @@
|
||||
import { z } from "zod"
|
||||
|
||||
// Query
|
||||
const roomsSchema = z.array(
|
||||
z.object({
|
||||
adults: z.number().int().nonnegative(),
|
||||
childrenAges: z
|
||||
.array(
|
||||
z.object({
|
||||
age: z.number().int().nonnegative(),
|
||||
bedType: z.string(),
|
||||
})
|
||||
)
|
||||
.default([]),
|
||||
rateCode: z.string(),
|
||||
roomTypeCode: z.string(),
|
||||
guest: z.object({
|
||||
title: z.string(),
|
||||
firstName: z.string(),
|
||||
lastName: z.string(),
|
||||
email: z.string().email(),
|
||||
phoneCountryCodePrefix: z.string(),
|
||||
phoneNumber: z.string(),
|
||||
countryCode: z.string(),
|
||||
membershipNumber: z.string().optional(),
|
||||
}),
|
||||
smsConfirmationRequested: z.boolean(),
|
||||
packages: z.object({
|
||||
breakfast: z.boolean(),
|
||||
allergyFriendly: z.boolean(),
|
||||
petFriendly: z.boolean(),
|
||||
accessibility: z.boolean(),
|
||||
}),
|
||||
})
|
||||
)
|
||||
|
||||
const paymentSchema = z.object({
|
||||
paymentMethod: z.string(),
|
||||
card: z
|
||||
.object({
|
||||
alias: z.string(),
|
||||
expiryDate: z.string(),
|
||||
cardType: z.string(),
|
||||
})
|
||||
.optional(),
|
||||
cardHolder: z.object({
|
||||
email: z.string().email(),
|
||||
name: z.string(),
|
||||
phoneCountryCode: z.string(),
|
||||
phoneSubscriber: z.string(),
|
||||
}),
|
||||
success: z.string(),
|
||||
error: z.string(),
|
||||
cancel: z.string(),
|
||||
})
|
||||
|
||||
// Mutation
|
||||
export const createBookingInput = z.object({
|
||||
hotelId: z.string(),
|
||||
checkInDate: z.string(),
|
||||
checkOutDate: z.string(),
|
||||
rooms: z.array(
|
||||
z.object({
|
||||
adults: z.number().int().nonnegative(),
|
||||
children: z.number().int().nonnegative(),
|
||||
rateCode: z.string(),
|
||||
roomTypeCode: z.string(),
|
||||
guest: z.object({
|
||||
title: z.string(),
|
||||
firstName: z.string(),
|
||||
lastName: z.string(),
|
||||
email: z.string().email(),
|
||||
phoneCountryCodePrefix: z.string(),
|
||||
phoneNumber: z.string(),
|
||||
countryCode: z.string(),
|
||||
}),
|
||||
smsConfirmationRequested: z.boolean(),
|
||||
})
|
||||
),
|
||||
payment: z.object({
|
||||
cardHolder: z.object({
|
||||
Email: z.string().email(),
|
||||
Name: z.string(),
|
||||
PhoneCountryCode: z.string(),
|
||||
PhoneSubscriber: z.string(),
|
||||
}),
|
||||
success: z.string(),
|
||||
error: z.string(),
|
||||
cancel: z.string(),
|
||||
}),
|
||||
rooms: roomsSchema,
|
||||
payment: paymentSchema,
|
||||
})
|
||||
|
||||
// Query
|
||||
export const getBookingStatusInput = z.object({
|
||||
confirmationNumber: z.string(),
|
||||
})
|
||||
|
||||
@@ -2,7 +2,7 @@ import { metrics } from "@opentelemetry/api"
|
||||
|
||||
import * as api from "@/lib/api"
|
||||
import { getVerifiedUser } from "@/server/routers/user/query"
|
||||
import { router, safeProtectedProcedure } from "@/server/trpc"
|
||||
import { bookingServiceProcedure, router } from "@/server/trpc"
|
||||
|
||||
import { getMembership } from "@/utils/user"
|
||||
|
||||
@@ -36,13 +36,15 @@ async function getMembershipNumber(
|
||||
|
||||
export const bookingMutationRouter = router({
|
||||
booking: router({
|
||||
create: safeProtectedProcedure
|
||||
create: bookingServiceProcedure
|
||||
.input(createBookingInput)
|
||||
.mutation(async function ({ ctx, input }) {
|
||||
const { checkInDate, checkOutDate, hotelId } = input
|
||||
|
||||
// TODO: add support for user token OR service token in procedure
|
||||
// then we can fetch membership number if user token exists
|
||||
const loggingAttributes = {
|
||||
membershipNumber: await getMembershipNumber(ctx.session),
|
||||
// membershipNumber: await getMembershipNumber(ctx.session),
|
||||
checkInDate,
|
||||
checkOutDate,
|
||||
hotelId,
|
||||
@@ -56,11 +58,10 @@ export const bookingMutationRouter = router({
|
||||
query: loggingAttributes,
|
||||
})
|
||||
)
|
||||
const headers = ctx.session
|
||||
? {
|
||||
Authorization: `Bearer ${ctx.session?.token.access_token}`,
|
||||
}
|
||||
: undefined
|
||||
const headers = {
|
||||
Authorization: `Bearer ${ctx.serviceToken}`,
|
||||
}
|
||||
|
||||
const apiResponse = await api.post(api.endpoints.v1.booking, {
|
||||
headers,
|
||||
body: input,
|
||||
|
||||
@@ -5,9 +5,9 @@ export const createBookingSchema = z
|
||||
data: z.object({
|
||||
attributes: z.object({
|
||||
confirmationNumber: z.string(),
|
||||
cancellationNumber: z.string().nullable(),
|
||||
cancellationNumber: z.string().optional(),
|
||||
reservationStatus: z.string(),
|
||||
paymentUrl: z.string().nullable(),
|
||||
paymentUrl: z.string().optional(),
|
||||
}),
|
||||
type: z.string(),
|
||||
id: z.string(),
|
||||
|
||||
85
server/routers/booking/query.ts
Normal file
85
server/routers/booking/query.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import { metrics } from "@opentelemetry/api"
|
||||
|
||||
import * as api from "@/lib/api"
|
||||
import { badRequestError, serverErrorByStatus } from "@/server/errors/trpc"
|
||||
import { bookingServiceProcedure, router } from "@/server/trpc"
|
||||
|
||||
import { getBookingStatusInput } from "./input"
|
||||
import { createBookingSchema } from "./output"
|
||||
|
||||
const meter = metrics.getMeter("trpc.booking")
|
||||
const getBookingStatusCounter = meter.createCounter("trpc.booking.status")
|
||||
const getBookingStatusSuccessCounter = meter.createCounter(
|
||||
"trpc.booking.status-success"
|
||||
)
|
||||
const getBookingStatusFailCounter = meter.createCounter(
|
||||
"trpc.booking.status-fail"
|
||||
)
|
||||
|
||||
export const bookingQueryRouter = router({
|
||||
status: bookingServiceProcedure
|
||||
.input(getBookingStatusInput)
|
||||
.query(async function ({ ctx, input }) {
|
||||
const { confirmationNumber } = input
|
||||
getBookingStatusCounter.add(1, { confirmationNumber })
|
||||
|
||||
const apiResponse = await api.get(
|
||||
`${api.endpoints.v1.booking}/${confirmationNumber}/status`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${ctx.serviceToken}`,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
const responseMessage = await apiResponse.text()
|
||||
getBookingStatusFailCounter.add(1, {
|
||||
confirmationNumber,
|
||||
error_type: "http_error",
|
||||
error: responseMessage,
|
||||
})
|
||||
console.error(
|
||||
"api.booking.status error",
|
||||
JSON.stringify({
|
||||
query: { confirmationNumber },
|
||||
error: {
|
||||
status: apiResponse.status,
|
||||
statusText: apiResponse.statusText,
|
||||
text: responseMessage,
|
||||
},
|
||||
})
|
||||
)
|
||||
|
||||
throw serverErrorByStatus(apiResponse.status, apiResponse)
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
const verifiedData = createBookingSchema.safeParse(apiJson)
|
||||
if (!verifiedData.success) {
|
||||
getBookingStatusFailCounter.add(1, {
|
||||
confirmationNumber,
|
||||
error_type: "validation_error",
|
||||
error: JSON.stringify(verifiedData.error),
|
||||
})
|
||||
console.error(
|
||||
"api.booking.status validation error",
|
||||
JSON.stringify({
|
||||
query: { confirmationNumber },
|
||||
error: verifiedData.error,
|
||||
})
|
||||
)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
getBookingStatusSuccessCounter.add(1, { confirmationNumber })
|
||||
console.info(
|
||||
"api.booking.status success",
|
||||
JSON.stringify({
|
||||
query: { confirmationNumber },
|
||||
})
|
||||
)
|
||||
|
||||
return verifiedData.data
|
||||
}),
|
||||
})
|
||||
@@ -436,6 +436,22 @@ export const roomSchema = z.object({
|
||||
type: z.enum(["roomcategories"]),
|
||||
})
|
||||
|
||||
const merchantInformationSchema = z.object({
|
||||
webMerchantId: z.string(),
|
||||
cards: z.record(z.string(), z.boolean()).transform((val) => {
|
||||
return Object.entries(val)
|
||||
.filter(([_, enabled]) => enabled)
|
||||
.map(([key]) => key)
|
||||
}),
|
||||
alternatePaymentOptions: z
|
||||
.record(z.string(), z.boolean())
|
||||
.transform((val) => {
|
||||
return Object.entries(val)
|
||||
.filter(([_, enabled]) => enabled)
|
||||
.map(([key]) => key)
|
||||
}),
|
||||
})
|
||||
|
||||
// NOTE: Find schema at: https://aks-test.scandichotels.com/hotel/swagger/v1/index.html
|
||||
export const getHotelDataSchema = z.object({
|
||||
data: z.object({
|
||||
@@ -471,6 +487,7 @@ export const getHotelDataSchema = z.object({
|
||||
hotelContent: hotelContentSchema,
|
||||
detailedFacilities: z.array(detailedFacilitySchema),
|
||||
healthFacilities: z.array(healthFacilitySchema),
|
||||
merchantInformationData: merchantInformationSchema,
|
||||
rewardNight: rewardNightSchema,
|
||||
pointsOfInterest: z
|
||||
.array(pointOfInterestSchema)
|
||||
|
||||
Reference in New Issue
Block a user