feat: enhance stays with api data
This commit is contained in:
@@ -2,15 +2,13 @@ import { z } from "zod"
|
||||
|
||||
export const staysInput = z
|
||||
.object({
|
||||
perPage: z.number().min(0).default(6),
|
||||
page: z.number().min(0).default(0),
|
||||
limit: z.number().min(0).default(6),
|
||||
cursor: z.number().nullish(),
|
||||
})
|
||||
.default({})
|
||||
|
||||
|
||||
export const soonestUpcomingStaysInput = z
|
||||
.object({
|
||||
limit: z.number().int().positive(),
|
||||
})
|
||||
.default({ limit: 3 })
|
||||
export const soonestUpcomingStaysInput = z
|
||||
.object({
|
||||
limit: z.number().int().positive(),
|
||||
})
|
||||
.default({ limit: 3 })
|
||||
|
||||
@@ -23,3 +23,69 @@ export const getUserSchema = z.object({
|
||||
phoneNumber: z.string(),
|
||||
profileId: z.string(),
|
||||
})
|
||||
|
||||
// Schema is the same for upcoming and previous stays endpoints
|
||||
export const getStaysSchema = z.object({
|
||||
data: z.array(
|
||||
z.object({
|
||||
attributes: z.object({
|
||||
hotelOperaId: z.string(),
|
||||
hotelInformation: z.object({
|
||||
hotelContent: z.object({
|
||||
images: z.object({
|
||||
metaData: z.object({
|
||||
title: z.string(),
|
||||
altText: z.string(),
|
||||
altText_En: z.string(),
|
||||
copyRight: z.string(),
|
||||
}),
|
||||
imageSizes: z.object({
|
||||
tiny: z.string(),
|
||||
small: z.string(),
|
||||
medium: z.string(),
|
||||
large: z.string(),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
hotelName: z.string(),
|
||||
cityName: z.string(),
|
||||
}),
|
||||
confirmationNumber: z.string(),
|
||||
checkinDate: z.string(),
|
||||
checkoutDate: z.string(),
|
||||
isWebAppOrigin: z.boolean(),
|
||||
}),
|
||||
relationships: z.object({
|
||||
hotel: z.object({
|
||||
links: z.object({
|
||||
related: z.string(),
|
||||
}),
|
||||
data: z.object({
|
||||
id: z.string(),
|
||||
type: z.string(),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
type: z.string(),
|
||||
id: z.string(),
|
||||
links: z.object({
|
||||
self: z.object({
|
||||
href: z.string(),
|
||||
meta: z.object({
|
||||
method: z.string(),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
),
|
||||
links: z.object({
|
||||
self: z.string(),
|
||||
offset: z.number(),
|
||||
limit: z.number(),
|
||||
totalCount: z.number(),
|
||||
}),
|
||||
})
|
||||
|
||||
type GetStaysData = z.infer<typeof getStaysSchema>
|
||||
|
||||
export type Stay = GetStaysData["data"][number]
|
||||
|
||||
@@ -9,15 +9,9 @@ import {
|
||||
} from "@/server/errors/trpc"
|
||||
import { protectedProcedure, router } from "@/server/trpc"
|
||||
|
||||
import { soonestUpcomingStaysInput, staysInput } from "./input"
|
||||
import { getUserSchema } from "./output"
|
||||
import {
|
||||
benefits,
|
||||
extendedUser,
|
||||
nextLevelPerks,
|
||||
previousStays,
|
||||
upcomingStays,
|
||||
} from "./temp"
|
||||
import { staysInput } from "./input"
|
||||
import { getStaysSchema, getUserSchema } from "./output"
|
||||
import { benefits, extendedUser, nextLevelPerks } from "./temp"
|
||||
|
||||
function fakingRequest<T>(payload: T): Promise<T> {
|
||||
return new Promise((resolve) => {
|
||||
@@ -84,66 +78,130 @@ export const userQueryRouter = router({
|
||||
}),
|
||||
|
||||
stays: router({
|
||||
soonestUpcoming: protectedProcedure
|
||||
.input(soonestUpcomingStaysInput)
|
||||
.query(async ({ input }) => {
|
||||
return upcomingStays.slice(0, input.limit)
|
||||
}),
|
||||
|
||||
previous: protectedProcedure.input(staysInput).query(async (opts) => {
|
||||
const { perPage, page, cursor } = opts.input
|
||||
let nextCursor: typeof cursor | undefined = undefined
|
||||
const nrPages = Math.ceil(previousStays.length / perPage)
|
||||
try {
|
||||
const { limit: perPage, cursor } = opts.input
|
||||
|
||||
let stays, nextPage
|
||||
if (cursor) {
|
||||
stays = previousStays.slice(cursor, perPage + cursor + 1)
|
||||
nextPage = cursor / perPage + 1
|
||||
} else {
|
||||
stays = previousStays.slice(
|
||||
page * perPage,
|
||||
page * perPage + perPage + 1
|
||||
)
|
||||
}
|
||||
const params = new URLSearchParams()
|
||||
params.set("limit", perPage.toString())
|
||||
|
||||
if (
|
||||
(nextPage && nextPage < nrPages && stays.length == perPage + 1) ||
|
||||
(!nextPage && nrPages > 1)
|
||||
) {
|
||||
const nextItem = stays.pop()
|
||||
if (nextItem) {
|
||||
nextCursor = previousStays.indexOf(nextItem)
|
||||
if (cursor) {
|
||||
params.set("offset", cursor.toString())
|
||||
}
|
||||
} // TODO: Make request to get user data from Scandic API
|
||||
return { data: stays, nextCursor }
|
||||
|
||||
const apiResponse = await api.get(
|
||||
api.endpoints.v1.previousStays,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${opts.ctx.session.token.access_token}`,
|
||||
},
|
||||
},
|
||||
params
|
||||
)
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
switch (apiResponse.status) {
|
||||
case 400:
|
||||
throw badRequestError()
|
||||
case 401:
|
||||
throw unauthorizedError()
|
||||
case 403:
|
||||
throw forbiddenError()
|
||||
default:
|
||||
throw internalServerError()
|
||||
}
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
if (!apiJson.data?.length) {
|
||||
throw internalServerError()
|
||||
}
|
||||
|
||||
const verifiedData = getStaysSchema.safeParse(apiJson)
|
||||
|
||||
if (!verifiedData.success) {
|
||||
console.info(`Get Previous Stays - Verified Data Error`)
|
||||
console.error(verifiedData.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
const nextCursor =
|
||||
verifiedData.data.links.offset < verifiedData.data.links.totalCount
|
||||
? verifiedData.data.links.offset
|
||||
: undefined
|
||||
|
||||
return {
|
||||
data: verifiedData.data.data,
|
||||
nextCursor,
|
||||
}
|
||||
} catch (error) {
|
||||
console.info(`Get Previous Stays Error`)
|
||||
console.error(error)
|
||||
throw internalServerError()
|
||||
}
|
||||
}),
|
||||
|
||||
upcoming: protectedProcedure.input(staysInput).query(async (opts) => {
|
||||
const { perPage, page, cursor } = opts.input
|
||||
let nextCursor: typeof cursor | undefined = undefined
|
||||
const nrPages = Math.ceil(upcomingStays.length / perPage)
|
||||
try {
|
||||
const { limit: perPage, cursor } = opts.input
|
||||
|
||||
let stays, nextPage
|
||||
if (cursor) {
|
||||
stays = upcomingStays.slice(cursor, perPage + cursor + 1)
|
||||
nextPage = cursor / perPage + 1
|
||||
} else {
|
||||
stays = upcomingStays.slice(
|
||||
page * perPage,
|
||||
page * perPage + perPage + 1
|
||||
)
|
||||
}
|
||||
const params = new URLSearchParams()
|
||||
params.set("limit", perPage.toString())
|
||||
|
||||
if (
|
||||
(nextPage && nextPage < nrPages && stays.length == perPage + 1) ||
|
||||
(!nextPage && nrPages > 1)
|
||||
) {
|
||||
const nextItem = stays.pop()
|
||||
if (nextItem) {
|
||||
nextCursor = upcomingStays.indexOf(nextItem)
|
||||
if (cursor) {
|
||||
params.set("offset", cursor.toString())
|
||||
}
|
||||
} // TODO: Make request to get user data from Scandic API
|
||||
return { data: stays, nextCursor }
|
||||
|
||||
const apiResponse = await api.get(
|
||||
api.endpoints.v1.upcomingStays,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${opts.ctx.session.token.access_token}`,
|
||||
},
|
||||
},
|
||||
params
|
||||
)
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
switch (apiResponse.status) {
|
||||
case 400:
|
||||
throw badRequestError()
|
||||
case 401:
|
||||
throw unauthorizedError()
|
||||
case 403:
|
||||
throw forbiddenError()
|
||||
default:
|
||||
throw internalServerError()
|
||||
}
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
if (!apiJson.data?.length) {
|
||||
throw internalServerError()
|
||||
}
|
||||
|
||||
const verifiedData = getStaysSchema.safeParse(apiJson)
|
||||
|
||||
if (!verifiedData.success) {
|
||||
console.info(`Get Upcoming Stays - Verified Data Error`)
|
||||
console.error(verifiedData.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
const nextCursor =
|
||||
verifiedData.data.links.offset < verifiedData.data.links.totalCount
|
||||
? verifiedData.data.links.offset
|
||||
: undefined
|
||||
|
||||
return {
|
||||
data: verifiedData.data.data,
|
||||
nextCursor,
|
||||
}
|
||||
} catch (error) {
|
||||
console.info(`Get Upcoming Stays Error`)
|
||||
console.error(error)
|
||||
throw internalServerError()
|
||||
}
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user