feat(WEB-169): get profile data from API
This commit is contained in:
@@ -1,112 +1,77 @@
|
||||
import { badRequestError, internalServerError } from "@/server/errors/trpc"
|
||||
import * as api from "@/lib/api"
|
||||
import { benefits, extendedUser, nextLevelPerks } from "./temp"
|
||||
import {
|
||||
badRequestError,
|
||||
forbiddenError,
|
||||
internalServerError,
|
||||
unauthorizedError,
|
||||
} from "@/server/errors/trpc"
|
||||
import { protectedProcedure, router } from "@/server/trpc"
|
||||
import { getUserSchema } from "./output"
|
||||
|
||||
import { extendedUser } from "./temp"
|
||||
function fakingRequest<T>(payload: T): Promise<T> {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve(payload)
|
||||
}, 1500)
|
||||
})
|
||||
}
|
||||
|
||||
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",
|
||||
{
|
||||
get: protectedProcedure.query(async function ({ ctx }) {
|
||||
try {
|
||||
const apiResponse = await api.get(api.endpoints.v0.profile, {
|
||||
cache: "no-store",
|
||||
headers: {
|
||||
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
||||
},
|
||||
})
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
switch (apiResponse.status) {
|
||||
case 400:
|
||||
throw badRequestError()
|
||||
case 401:
|
||||
throw unauthorizedError()
|
||||
case 403:
|
||||
throw forbiddenError()
|
||||
default:
|
||||
throw internalServerError()
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
if (!response.ok) {
|
||||
const apiJson = await apiResponse.json()
|
||||
if (!apiJson.data?.length) {
|
||||
throw internalServerError()
|
||||
}
|
||||
|
||||
const verifiedData = getUserSchema.safeParse(apiJson.data[0].attributes)
|
||||
if (!verifiedData.success) {
|
||||
console.info(`Get User - Verified Data Error`)
|
||||
console.error(verifiedData.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
return {
|
||||
...extendedUser,
|
||||
...verifiedData.data,
|
||||
firstName: verifiedData.data.name,
|
||||
name: `${verifiedData.data.name} ${verifiedData.data.lastName}`,
|
||||
}
|
||||
} catch (error) {
|
||||
console.info(`GEt User Error`)
|
||||
console.error(error)
|
||||
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
|
||||
return await fakingRequest<typeof benefits>(benefits)
|
||||
}),
|
||||
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
|
||||
return await fakingRequest<typeof nextLevelPerks>(nextLevelPerks)
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user