feat: add data handling to EarnAndBurn
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user