Merged develop into feat/hotel-pages-intro-section
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
import * as api from "@/lib/api"
|
||||
import { protectedProcedure, router } from "@/server/trpc"
|
||||
import {
|
||||
protectedProcedure,
|
||||
router,
|
||||
safeProtectedProcedure,
|
||||
} from "@/server/trpc"
|
||||
|
||||
import { countries } from "@/components/TempDesignSystem/Form/Country/countries"
|
||||
import * as maskValue from "@/utils/maskValue"
|
||||
import { getMembershipCards } from "@/utils/user"
|
||||
import { getMembership, getMembershipCards } from "@/utils/user"
|
||||
|
||||
import {
|
||||
friendTransactionsInput,
|
||||
@@ -19,6 +23,37 @@ import {
|
||||
} from "./output"
|
||||
import { benefits, extendedUser, nextLevelPerks } from "./temp"
|
||||
|
||||
import type { Session } from "next-auth"
|
||||
|
||||
async function getVerifiedUser({ session }: { session: Session }) {
|
||||
const apiResponse = await api.get(api.endpoints.v1.profile, {
|
||||
cache: "no-store",
|
||||
headers: {
|
||||
Authorization: `Bearer ${session.token.access_token}`,
|
||||
},
|
||||
})
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
return null
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
if (!apiJson.data?.attributes) {
|
||||
console.error(`User has no data - (user: ${JSON.stringify(session.user)})`)
|
||||
return null
|
||||
}
|
||||
|
||||
const verifiedData = getUserSchema.safeParse(apiJson.data.attributes)
|
||||
if (!verifiedData.success) {
|
||||
console.info(
|
||||
`Failed to validate User - (User: ${JSON.stringify(session.user)})`
|
||||
)
|
||||
console.error(verifiedData.error)
|
||||
return null
|
||||
}
|
||||
return verifiedData
|
||||
}
|
||||
|
||||
function fakingRequest<T>(payload: T): Promise<T> {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
@@ -31,45 +66,9 @@ export const userQueryRouter = router({
|
||||
get: protectedProcedure
|
||||
.input(getUserInputSchema)
|
||||
.query(async function getUser({ ctx, input }) {
|
||||
const apiResponse = await api.get(api.endpoints.v1.profile, {
|
||||
cache: "no-store",
|
||||
headers: {
|
||||
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
||||
},
|
||||
})
|
||||
const verifiedData = await getVerifiedUser({ session: ctx.session })
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
// switch (apiResponse.status) {
|
||||
// case 400:
|
||||
// throw badRequestError()
|
||||
// case 401:
|
||||
// throw unauthorizedError()
|
||||
// case 403:
|
||||
// throw forbiddenError()
|
||||
// default:
|
||||
// throw internalServerError()
|
||||
// }
|
||||
console.info(`API Response Failed - Getting User`)
|
||||
console.info(`User: (${JSON.stringify(ctx.session.user)})`)
|
||||
console.error(apiResponse)
|
||||
return null
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
if (!apiJson.data?.attributes) {
|
||||
// throw notFound(apiJson)
|
||||
console.error(
|
||||
`User has no data - (user: ${JSON.stringify(ctx.session.user)})`
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
const verifiedData = getUserSchema.safeParse(apiJson.data.attributes)
|
||||
if (!verifiedData.success) {
|
||||
console.info(
|
||||
`Failed to validate User - (User: ${JSON.stringify(ctx.session.user)})`
|
||||
)
|
||||
console.error(verifiedData.error)
|
||||
if (!verifiedData) {
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -119,7 +118,33 @@ export const userQueryRouter = router({
|
||||
|
||||
return user
|
||||
}),
|
||||
name: safeProtectedProcedure.query(async function ({ ctx }) {
|
||||
if (!ctx.session) {
|
||||
return null
|
||||
}
|
||||
const verifiedData = await getVerifiedUser({ session: ctx.session })
|
||||
|
||||
if (!verifiedData) {
|
||||
return null
|
||||
}
|
||||
return {
|
||||
firstName: verifiedData.data.firstName,
|
||||
lastName: verifiedData.data.lastName,
|
||||
}
|
||||
}),
|
||||
membershipLevel: safeProtectedProcedure.query(async function ({ ctx }) {
|
||||
if (!ctx.session) {
|
||||
return null
|
||||
}
|
||||
const verifiedData = await getVerifiedUser({ session: ctx.session })
|
||||
|
||||
if (!verifiedData) {
|
||||
return null
|
||||
}
|
||||
|
||||
const membershipLevel = getMembership(verifiedData.data.memberships)
|
||||
return membershipLevel
|
||||
}),
|
||||
benefits: router({
|
||||
current: protectedProcedure.query(async function (opts) {
|
||||
// TODO: Make request to get user data from Scandic API
|
||||
|
||||
Reference in New Issue
Block a user