feat: improve structure and error handling
This commit is contained in:
@@ -3,6 +3,7 @@ import {
|
||||
badRequestError,
|
||||
forbiddenError,
|
||||
internalServerError,
|
||||
notFound,
|
||||
unauthorizedError,
|
||||
} from "@/server/errors/trpc"
|
||||
import { protectedProcedure, router } from "@/server/trpc"
|
||||
@@ -21,50 +22,43 @@ function fakingRequest<T>(payload: T): Promise<T> {
|
||||
|
||||
export const userQueryRouter = router({
|
||||
get: protectedProcedure.query(async function ({ ctx }) {
|
||||
try {
|
||||
const apiResponse = await api.get(api.endpoints.v0.profile, {
|
||||
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()
|
||||
}
|
||||
}
|
||||
const apiResponse = await api.get(api.endpoints.v0.profile, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
||||
},
|
||||
})
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
|
||||
if (!apiJson.data?.length) {
|
||||
throw internalServerError()
|
||||
if (!apiResponse.ok) {
|
||||
switch (apiResponse.status) {
|
||||
case 400:
|
||||
throw badRequestError(apiResponse)
|
||||
case 401:
|
||||
throw unauthorizedError(apiResponse)
|
||||
case 403:
|
||||
throw forbiddenError(apiResponse)
|
||||
default:
|
||||
throw internalServerError(apiResponse)
|
||||
}
|
||||
}
|
||||
|
||||
const verifiedData = getUserSchema.safeParse(apiJson.data[0].attributes)
|
||||
if (!verifiedData.success) {
|
||||
console.info(`Get User - Verified Data Error`)
|
||||
console.error(verifiedData.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
const apiJson = await apiResponse.json()
|
||||
if (!apiJson.data?.length) {
|
||||
throw notFound(apiJson)
|
||||
}
|
||||
|
||||
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 verifiedData = getUserSchema.safeParse(apiJson.data[0].attributes)
|
||||
if (!verifiedData.success) {
|
||||
throw internalServerError(verifiedData.error)
|
||||
}
|
||||
|
||||
return {
|
||||
...extendedUser,
|
||||
...verifiedData.data,
|
||||
firstName: verifiedData.data.name,
|
||||
name: `${verifiedData.data.name} ${verifiedData.data.lastName}`,
|
||||
}
|
||||
}),
|
||||
|
||||
benefits: router({
|
||||
current: protectedProcedure.query(async function (opts) {
|
||||
// TODO: Make request to get user data from Scandic API
|
||||
@@ -77,9 +71,10 @@ export const userQueryRouter = router({
|
||||
}),
|
||||
|
||||
stays: router({
|
||||
previous: protectedProcedure.input(staysInput).query(async (opts) => {
|
||||
try {
|
||||
const { limit, cursor } = opts.input
|
||||
previous: protectedProcedure
|
||||
.input(staysInput)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const { limit, cursor } = input
|
||||
|
||||
const params = new URLSearchParams()
|
||||
params.set("limit", limit.toString())
|
||||
@@ -92,7 +87,7 @@ export const userQueryRouter = router({
|
||||
api.endpoints.v1.previousStays,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${opts.ctx.session.token.access_token}`,
|
||||
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
||||
},
|
||||
},
|
||||
params
|
||||
@@ -101,28 +96,24 @@ export const userQueryRouter = router({
|
||||
if (!apiResponse.ok) {
|
||||
switch (apiResponse.status) {
|
||||
case 400:
|
||||
throw badRequestError()
|
||||
throw badRequestError(apiResponse)
|
||||
case 401:
|
||||
throw unauthorizedError()
|
||||
throw unauthorizedError(apiResponse)
|
||||
case 403:
|
||||
throw forbiddenError()
|
||||
throw forbiddenError(apiResponse)
|
||||
default:
|
||||
throw internalServerError()
|
||||
throw internalServerError(apiResponse)
|
||||
}
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
if (!apiJson.data) {
|
||||
console.error(`Get Previous Stays - No data found from api call`)
|
||||
throw internalServerError()
|
||||
if (!apiJson.data?.length) {
|
||||
throw notFound(apiJson)
|
||||
}
|
||||
|
||||
const verifiedData = getStaysSchema.safeParse(apiJson)
|
||||
|
||||
if (!verifiedData.success) {
|
||||
console.info(`Get Previous Stays - Verified Data Error`)
|
||||
console.error(verifiedData.error)
|
||||
throw badRequestError()
|
||||
throw internalServerError(verifiedData.error)
|
||||
}
|
||||
|
||||
const nextCursor =
|
||||
@@ -135,16 +126,12 @@ export const userQueryRouter = router({
|
||||
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) => {
|
||||
try {
|
||||
const { limit, cursor } = opts.input
|
||||
upcoming: protectedProcedure
|
||||
.input(staysInput)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const { limit, cursor } = input
|
||||
|
||||
const params = new URLSearchParams()
|
||||
params.set("limit", limit.toString())
|
||||
@@ -157,7 +144,7 @@ export const userQueryRouter = router({
|
||||
api.endpoints.v1.upcomingStays,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${opts.ctx.session.token.access_token}`,
|
||||
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
||||
},
|
||||
},
|
||||
params
|
||||
@@ -166,28 +153,24 @@ export const userQueryRouter = router({
|
||||
if (!apiResponse.ok) {
|
||||
switch (apiResponse.status) {
|
||||
case 400:
|
||||
throw badRequestError()
|
||||
throw badRequestError(apiResponse)
|
||||
case 401:
|
||||
throw unauthorizedError()
|
||||
throw unauthorizedError(apiResponse)
|
||||
case 403:
|
||||
throw forbiddenError()
|
||||
throw forbiddenError(apiResponse)
|
||||
default:
|
||||
throw internalServerError()
|
||||
throw internalServerError(apiResponse)
|
||||
}
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
if (!apiJson.data) {
|
||||
console.error(`Get Upcoming Stays - No data found from api call`)
|
||||
throw internalServerError()
|
||||
if (!apiJson.data?.length) {
|
||||
throw notFound(apiJson)
|
||||
}
|
||||
|
||||
const verifiedData = getStaysSchema.safeParse(apiJson)
|
||||
|
||||
if (!verifiedData.success) {
|
||||
console.info(`Get Upcoming Stays - Verified Data Error`)
|
||||
console.error(verifiedData.error)
|
||||
throw badRequestError()
|
||||
throw internalServerError(verifiedData.error)
|
||||
}
|
||||
|
||||
const nextCursor =
|
||||
@@ -200,11 +183,6 @@ export const userQueryRouter = router({
|
||||
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