feat: add data handling to EarnAndBurn

This commit is contained in:
Arvid Norlin
2024-05-14 16:35:44 +02:00
parent 7ad8726633
commit 9ef9de840c
10 changed files with 391 additions and 26 deletions

View File

@@ -12,3 +12,8 @@ export const soonestUpcomingStaysInput = z
limit: z.number().int().positive(),
})
.default({ limit: 3 })
export const friendTransactionsInput = z.object({
cursor: z.number().int().nullish(),
limit: z.number().min(0).default(5),
})

View File

@@ -93,3 +93,72 @@ export const getStaysSchema = z.object({
type GetStaysData = z.infer<typeof getStaysSchema>
export type Stay = GetStaysData["data"][number]
export const getFriendTransactionsSchema = z.object({
data: z.array(
z.object({
attributes: z.object({
hotelOperaId: z.string(),
confirmationNumber: z.string(),
checkInDate: z.string(),
checkOutDate: z.string(),
nights: z.number(),
awardPoints: z.number(),
pointsCalculated: z.boolean(),
hotelInformation: z.object({
hotelName: z.string(),
city: z.string(),
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(),
}),
}),
}),
}),
}),
relationships: z.object({
hotel: z.object({
links: z.object({
related: z.string(),
}),
data: z.object({
id: z.string(),
type: z.string(),
}),
}),
booking: z.object({
links: z.object({
related: z.string(),
}),
data: z.object({
id: z.string(),
type: z.string(),
}),
}),
}),
type: z.string(),
})
),
links: z
.object({
self: z.string(),
offset: z.number(),
limit: z.number(),
totalCount: z.number(),
})
.nullable(),
})
type GetFriendTransactionsData = z.infer<typeof getFriendTransactionsSchema>
export type Transaction = GetFriendTransactionsData["data"][number]

View File

@@ -8,9 +8,14 @@ import {
} from "@/server/errors/trpc"
import { protectedProcedure, router } from "@/server/trpc"
import { staysInput } from "./input"
import { getStaysSchema, getUserSchema } from "./output"
import { friendTransactionsInput, staysInput } from "./input"
import {
getFriendTransactionsSchema,
getStaysSchema,
getUserSchema,
} from "./output"
import { benefits, extendedUser, nextLevelPerks } from "./temp"
import friendTransactionsMockJson from "./tempFriendTransactions.json"
function fakingRequest<T>(payload: T): Promise<T> {
return new Promise((resolve) => {
@@ -177,4 +182,83 @@ export const userQueryRouter = router({
}
}),
}),
transaction: router({
friendTransactions: protectedProcedure
.input(friendTransactionsInput)
.query(async (opts) => {
try {
const { limit, cursor } = opts.input
const params = new URLSearchParams()
params.set("limit", limit.toString())
if (cursor) {
params.set("offset", cursor.toString())
}
// TODO: att these once API data is confirmed to be available
// const apiResponse = await api.get(
// api.endpoints.v1.friendTransactions,
// {
// 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()
const apiJson = friendTransactionsMockJson
debugger
if (!apiJson.data?.length) {
// throw internalServerError()
}
const verifiedData = getFriendTransactionsSchema.safeParse(apiJson)
if (!verifiedData.success) {
console.info(`Get Friend Transactions - Verified Data Error`)
console.error(verifiedData.error)
throw badRequestError()
}
const nextCursor =
verifiedData.data.links &&
verifiedData.data.links.offset < verifiedData.data.links.totalCount
? verifiedData.data.links.offset
: undefined
return {
data: verifiedData.data.data.map(({ attributes }) => ({
checkInDate: attributes.checkInDate,
checkOutDate: attributes.checkOutDate,
awardPoints: attributes.awardPoints,
hotelName: attributes.hotelInformation.hotelName,
city: attributes.hotelInformation.city,
nights: attributes.nights,
confirmationNumber: attributes.confirmationNumber,
})),
nextCursor,
}
} catch (error) {
console.info(`Get Friend Transactions Error`)
console.error(error)
throw internalServerError()
}
}),
}),
})

View File

@@ -0,0 +1,116 @@
{
"data": [
{
"attributes": {
"hotelOperaId": "216",
"confirmationNumber": "991646189",
"checkInDate": "2023-09-16",
"checkOutDate": "2023-09-17",
"transactionDate": "2023-04-18",
"nights": 1,
"awardPoints": 1863,
"pointsCalculated": true,
"hotelInformation": {
"hotelName": "Scandic Landvetter",
"city": "Stockholm",
"hotelContent": {
"images": {
"metaData": {
"title": "Lobby",
"altText": "lobby at scandic landvetter in gothenburg",
"altText_En": "lobby at scandic landvetter in gothenburg",
"copyRight": "Werner Nystrand"
},
"imageSizes": {
"tiny": "https://test3.scandichotels.com/imagevault/publishedmedia/1cz71gn106ej1mz7u4nr/Scandic-Landvetter-lobby-0013-2-vald.jpg",
"small": "https://test3.scandichotels.com/imagevault/publishedmedia/29ejr75mwp7riv63nz0x/Scandic-Landvetter-lobby-0013-2-vald.jpg",
"medium": "https://test3.scandichotels.com/imagevault/publishedmedia/bldh2liyfddkv74szp9v/Scandic-Landvetter-lobby-0013-2-vald.jpg",
"large": "https://test3.scandichotels.com/imagevault/publishedmedia/kbmpmkb714o028ufcgu4/Scandic-Landvetter-lobby-0013-2-vald.jpg"
}
}
}
}
},
"relationships": {
"hotel": {
"links": {
"related": "https://api-test.scandichotels.com/hotels/V0/fdea883a-8092-4604-8afb-032391a59009/hotels"
},
"data": {
"id": "d98c7ab1-ebaa-4102-b351-758daf1ddf55",
"type": "hotels"
}
},
"booking": {
"links": {
"related": "https://api-test.scandichotels.com/booking/v1/bookings/991646189"
},
"data": {
"id": "991646189",
"type": "booking"
}
}
},
"type": "stay"
},
{
"attributes": {
"hotelOperaId": "216",
"confirmationNumber": "991646190",
"checkInDate": "2023-09-16",
"checkOutDate": "2023-09-17",
"transactionDate": "2023-04-18",
"nights": 1,
"awardPoints": 1863,
"pointsCalculated": true,
"hotelInformation": {
"hotelName": "Scandic Landvetter",
"city": "Stockholm",
"hotelContent": {
"images": {
"metaData": {
"title": "Lobby",
"altText": "lobby at scandic landvetter in gothenburg",
"altText_En": "lobby at scandic landvetter in gothenburg",
"copyRight": "Werner Nystrand"
},
"imageSizes": {
"tiny": "https://test3.scandichotels.com/imagevault/publishedmedia/1cz71gn106ej1mz7u4nr/Scandic-Landvetter-lobby-0013-2-vald.jpg",
"small": "https://test3.scandichotels.com/imagevault/publishedmedia/29ejr75mwp7riv63nz0x/Scandic-Landvetter-lobby-0013-2-vald.jpg",
"medium": "https://test3.scandichotels.com/imagevault/publishedmedia/bldh2liyfddkv74szp9v/Scandic-Landvetter-lobby-0013-2-vald.jpg",
"large": "https://test3.scandichotels.com/imagevault/publishedmedia/kbmpmkb714o028ufcgu4/Scandic-Landvetter-lobby-0013-2-vald.jpg"
}
}
}
}
},
"relationships": {
"hotel": {
"links": {
"related": "https://api-test.scandichotels.com/hotels/V0/fdea883a-8092-4604-8afb-032391a59009/hotels"
},
"data": {
"id": "d98c7ab1-ebaa-4102-b351-758daf1ddf55",
"type": "hotels"
}
},
"booking": {
"links": {
"related": "https://api-test.scandichotels.com/booking/v1/bookings/991646189"
},
"data": {
"id": "991646189",
"type": "booking"
}
}
},
"type": "stay"
}
],
"links": {
"self": "https://api-test.scandichotels.com/profile/v1/transaction/friendTransactions?language=en&offset=1&limit=20",
"offset": 2,
"limit": 20,
"totalCount": 40
}
}