feat(WEB-169): get profile data from API

This commit is contained in:
Simon Emanuelsson
2024-04-16 12:42:44 +02:00
parent d2c1887179
commit 55794034c5
44 changed files with 632 additions and 607 deletions

View File

@@ -11,12 +11,10 @@ export function unauthorizedError() {
})
}
export function internalServerError() {
export function forbiddenError() {
return new TRPCError({
code: TRPC_ERROR_CODES_BY_NUMBER[
TRPC_ERROR_CODES_BY_KEY.INTERNAL_SERVER_ERROR
],
message: `Internal Server Error!`,
code: TRPC_ERROR_CODES_BY_NUMBER[TRPC_ERROR_CODES_BY_KEY.FORBIDDEN],
message: `You do not have permission!`,
})
}
@@ -26,3 +24,12 @@ export function badRequestError(msg = "Bad request!") {
message: msg,
})
}
export function internalServerError() {
return new TRPCError({
code: TRPC_ERROR_CODES_BY_NUMBER[
TRPC_ERROR_CODES_BY_KEY.INTERNAL_SERVER_ERROR
],
message: `Internal Server Error!`,
})
}

View File

@@ -1,27 +1,24 @@
import { countriesMap } from "@/components/TempDesignSystem/Form/Country/countries"
import { z } from "zod"
/**
* Return value from jsonplaceholder.com/users/1
* Add proper user object expectation when fetching
* from Scandic API
*/
export const getUserSchema = z.object({
address: z.object({
city: z.string(),
geo: z.object({}),
street: z.string(),
suite: z.string(),
zipcode: z.string(),
}),
company: z.object({
bs: z.string(),
catchPhrase: z.string(),
name: z.string(),
city: z.string().optional(),
country: z.nativeEnum(countriesMap),
streetAddress: z.string().optional(),
zipCode: z.string(),
}),
email: z.string().email(),
id: z.number(),
gender: z.string(),
name: z.string(),
phone: z.string(),
username: z.string(),
website: z.string(),
language: z.string(),
lastName: z.string(),
membership: z.object({
currentPoints: z.number(),
expirationDate: z.string(),
membershipNumber: z.string(),
memberSince: z.string(),
}),
phoneNumber: z.string(),
profileId: z.string(),
})

View File

@@ -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)
}),
}),
})

View File

@@ -1,5 +1,32 @@
import { dt } from "@/lib/dt"
export const benefits = [
{
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: "#",
},
]
export const challenges = {
journeys: [
{
@@ -27,6 +54,26 @@ export const challenges = {
],
}
export const nextLevelPerks = {
nextLevel: "Close Friend",
perks: [
{
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",
},
],
}
export const shortcuts = [
{
href: "#",
@@ -88,13 +135,9 @@ export const stays = [
]
export const extendedUser = {
country: "United States",
dob: dt("1977-07-05").format("YYYY-MM-DD"),
journeys: challenges.journeys,
membershipId: 30812404844732,
nights: 14,
points: 20720,
qualifyingPoints: 5000,
shortcuts,
stays,
victories: challenges.victories,

View File

@@ -14,17 +14,16 @@ export const publicProcedure = t.procedure
export const protectedProcedure = t.procedure.use(async function (opts) {
const authRequired = opts.meta?.authRequired ?? true
const session = await opts.ctx.auth()
if (authRequired) {
if (!session?.user) {
throw unauthorizedError()
}
} else {
if (env.NODE_ENV === "development") {
console.info(
`❌❌❌❌ You are opting out of authorization, if its done on purpose maybe you should use the publicProcedure instead. ❌❌❌❌`
)
console.info(`path: ${opts.path} | type: ${opts.type}`)
}
if (!authRequired && env.NODE_ENV === "development") {
console.info(
`❌❌❌❌ You are opting out of authorization, if its done on purpose maybe you should use the publicProcedure instead. ❌❌❌❌`
)
console.info(`path: ${opts.path} | type: ${opts.type}`)
}
if (!session?.user) {
throw unauthorizedError()
}
return opts.next({