113 lines
3.2 KiB
TypeScript
113 lines
3.2 KiB
TypeScript
import { badRequestError, internalServerError } from "@/server/errors/trpc"
|
|
import { protectedProcedure, router } from "@/server/trpc"
|
|
import { getUserSchema } from "./output"
|
|
|
|
import { extendedUser } from "./temp"
|
|
|
|
export const userQueryRouter = router({
|
|
get: protectedProcedure.query(async function (opts) {
|
|
// TODO: Make request to get user data from Scandic API
|
|
const response = await fetch(
|
|
"https://jsonplaceholder.typicode.com/users/1",
|
|
{
|
|
cache: "no-store",
|
|
}
|
|
)
|
|
|
|
if (!response.ok) {
|
|
throw internalServerError()
|
|
}
|
|
const json = await response.json()
|
|
const validJson = getUserSchema.safeParse(json)
|
|
if (!validJson.success) {
|
|
throw badRequestError()
|
|
}
|
|
|
|
const [firstname, lastname] = validJson.data.name.split(" ")
|
|
const [phone] = validJson.data.phone.split(" ")
|
|
return {
|
|
...validJson.data,
|
|
firstname,
|
|
lastname,
|
|
phone,
|
|
...extendedUser,
|
|
}
|
|
}),
|
|
benefits: router({
|
|
current: protectedProcedure.query(async function (opts) {
|
|
// TODO: Make request to get user data from Scandic API
|
|
|
|
const currentBenefits = [
|
|
{
|
|
id: 1,
|
|
value: "€5 voucher",
|
|
explanation: "to spend in bar & restaurant for each night",
|
|
subtitle:
|
|
"Lorem ipsum dolor sit amet consectetur. Pharetra lectus sagittis turpis blandit feugiat amet enim massa.",
|
|
href: "#",
|
|
},
|
|
{
|
|
id: 2,
|
|
value: "Breakfast to go",
|
|
explanation: "for early birds, when staying",
|
|
subtitle:
|
|
"Lorem ipsum dolor sit amet consectetur. Pharetra lectus sagittis turpis blandit feugiat amet enim massa.",
|
|
href: "#",
|
|
},
|
|
{
|
|
id: 3,
|
|
value: "15% discount",
|
|
explanation: "in the restaurant & the bar",
|
|
subtitle:
|
|
"Lorem ipsum dolor sit amet consectetur. Pharetra lectus sagittis turpis blandit feugiat amet enim massa.",
|
|
href: "#",
|
|
},
|
|
]
|
|
const response = currentBenefits
|
|
return response
|
|
|
|
// if (!response.ok) {
|
|
// throw internalServerError()
|
|
// }
|
|
// const json = await response.json()
|
|
// const validJson = getUserSchema.parse(json)
|
|
// if (!validJson) {
|
|
// throw badRequestError()
|
|
// }
|
|
// return validJson
|
|
}),
|
|
nextLevel: protectedProcedure.query(async function (opts) {
|
|
// TODO: Make request to get user data from Scandic API
|
|
|
|
const nextLevelPerks = [
|
|
{
|
|
id: 1,
|
|
|
|
explanation: "Free soft drink voucher for the kids when staying",
|
|
},
|
|
{
|
|
id: 2,
|
|
|
|
explanation: "Free early check in",
|
|
},
|
|
{
|
|
id: 3,
|
|
explanation: "25% extra bonus points on each stay",
|
|
},
|
|
]
|
|
const response = { nextLevel: "Close Friend", perks: nextLevelPerks }
|
|
return response
|
|
|
|
// if (!response.ok) {
|
|
// throw internalServerError()
|
|
// }
|
|
// const json = await response.json()
|
|
// const validJson = getUserSchema.parse(json)
|
|
// if (!validJson) {
|
|
// throw badRequestError()
|
|
// }
|
|
// return validJson
|
|
}),
|
|
}),
|
|
})
|