Files
web/server/routers/booking/output.ts
Tobias Johansson a70f8a3b97 Merged in feat/SW-618-payment-non-happy-path (pull request #874)
Feat/SW-618 payment non happy path

* feat(SW-618): filter out expired saved cards

* feat(SW-618): Added payment error codes and way of showing messages based on code

* feat(SW-618): show error message if max retries has been reached and remove search param after showing toast

* fix(SW-618): move fallback error codes

* fix(SW-618): remove ref from stopping useEffect to run twice

* fix(SW-618): refactored logic for toast message and minor fixes

* fix(SW-618): remove error message enum due to static analysis problems


Approved-by: Christian Andolf
Approved-by: Arvid Norlin
2024-11-18 14:10:11 +00:00

115 lines
3.3 KiB
TypeScript

import { z } from "zod"
import { ChildBedTypeEnum } from "@/constants/booking"
import { phoneValidator } from "@/utils/phoneValidator"
import { CurrencyEnum } from "@/types/enums/currency"
// MUTATION
export const createBookingSchema = z
.object({
data: z.object({
attributes: z.object({
confirmationNumber: z.string(),
cancellationNumber: z.string().nullable(),
reservationStatus: z.string(),
paymentUrl: z.string().nullable(),
metadata: z
.object({
errorCode: z.number().optional(),
errorMessage: z.string().optional(),
priceChangedMetadata: z
.object({
roomPrice: z.number().optional(),
totalPrice: z.number().optional(),
})
.optional(),
})
.nullable(),
}),
type: z.string(),
id: z.string(),
links: z.object({
self: z.object({
href: z.string().url(),
meta: z.object({
method: z.string(),
}),
}),
}),
}),
})
.transform((d) => ({
id: d.data.id,
links: d.data.links,
type: d.data.type,
confirmationNumber: d.data.attributes.confirmationNumber,
cancellationNumber: d.data.attributes.cancellationNumber,
reservationStatus: d.data.attributes.reservationStatus,
paymentUrl: d.data.attributes.paymentUrl,
metadata: d.data.attributes.metadata,
}))
// QUERY
const extraBedTypesSchema = z.object({
quantity: z.number(),
bedType: z.nativeEnum(ChildBedTypeEnum),
})
const guestSchema = z.object({
email: z.string().email().nullable().default(""),
firstName: z.string(),
lastName: z.string(),
phoneNumber: phoneValidator().nullable().default(""),
})
const packageSchema = z.object({
code: z.string().default(""),
currency: z.nativeEnum(CurrencyEnum),
quantity: z.number().int(),
totalPrice: z.number(),
totalQuantity: z.number().int(),
unitPrice: z.number(),
})
export const bookingConfirmationSchema = z
.object({
data: z.object({
attributes: z.object({
adults: z.number(),
checkInDate: z.date({ coerce: true }),
checkOutDate: z.date({ coerce: true }),
createDateTime: z.date({ coerce: true }),
childrenAges: z.array(z.number()),
extraBedTypes: z.array(extraBedTypesSchema).default([]),
computedReservationStatus: z.string(),
confirmationNumber: z.string(),
currencyCode: z.nativeEnum(CurrencyEnum),
guest: guestSchema,
hotelId: z.string(),
packages: z.array(packageSchema),
rateDefinition: z.object({
rateCode: z.string(),
title: z.string().nullable(),
breakfastIncluded: z.boolean(),
isMemberRate: z.boolean(),
generalTerms: z.array(z.string()).optional(),
cancellationRule: z.string().optional(),
cancellationText: z.string().optional(),
mustBeGuaranteed: z.boolean(),
}),
reservationStatus: z.string(),
roomPrice: z.number().int(),
roomTypeCode: z.string(),
totalPrice: z.number(),
totalPriceExVat: z.number(),
vatAmount: z.number(),
vatPercentage: z.number(),
}),
id: z.string(),
type: z.literal("booking"),
}),
})
.transform(({ data }) => data.attributes)