feat(SW-160): update profile
This commit is contained in:
committed by
Michael Zetterberg
parent
b6e22d51a5
commit
2337d37f1a
@@ -5,8 +5,6 @@ import { Lang } from "@/constants/languages"
|
||||
|
||||
import { auth } from "@/auth"
|
||||
|
||||
import { unauthorizedError } from "./errors/trpc"
|
||||
|
||||
typeof auth
|
||||
|
||||
type CreateContextOptions = {
|
||||
|
||||
@@ -49,3 +49,13 @@ export function sessionExpiredError() {
|
||||
cause: new SessionExpiredError(SESSION_EXPIRED),
|
||||
})
|
||||
}
|
||||
|
||||
export const PUBLIC_UNAUTHORIZED = "PUBLIC_UNAUTHORIZED"
|
||||
export class PublicUnauthorizedError extends Error {}
|
||||
export function publicUnauthorizedError() {
|
||||
return new TRPCError({
|
||||
code: "UNAUTHORIZED",
|
||||
message: PUBLIC_UNAUTHORIZED,
|
||||
cause: new PublicUnauthorizedError(PUBLIC_UNAUTHORIZED),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ import { router } from "./trpc"
|
||||
|
||||
export const appRouter = router({
|
||||
contentstack: contentstackRouter,
|
||||
user: userRouter,
|
||||
hotel: hotelsRouter,
|
||||
user: userRouter,
|
||||
})
|
||||
|
||||
export type AppRouter = typeof appRouter
|
||||
|
||||
@@ -20,16 +20,16 @@ import tempRatesData from "./tempRatesData.json"
|
||||
export const hotelQueryRouter = router({
|
||||
getHotel: serviceProcedure
|
||||
.input(getHotelInputSchema)
|
||||
.query(async ({ input, ctx }) => {
|
||||
.query(async ({ ctx, input }) => {
|
||||
const { hotelId, language, include } = input
|
||||
|
||||
const params = new URLSearchParams()
|
||||
|
||||
const apiLang = toApiLang(language)
|
||||
params.set("language", apiLang)
|
||||
|
||||
const params: Record<string, string> = {
|
||||
hotelId,
|
||||
language: apiLang,
|
||||
}
|
||||
if (include) {
|
||||
params.set("include", include.join(","))
|
||||
params.include = include.join(",")
|
||||
}
|
||||
|
||||
const apiResponse = await api.get(
|
||||
@@ -61,16 +61,16 @@ export const hotelQueryRouter = router({
|
||||
|
||||
const roomCategories = included
|
||||
? included
|
||||
.filter((item) => item.type === "roomcategories")
|
||||
.map((roomCategory) => {
|
||||
const validatedRoom = roomSchema.safeParse(roomCategory)
|
||||
if (!validatedRoom.success) {
|
||||
console.error(`Get Room Category Data - Verified Data Error`)
|
||||
console.error(validatedRoom.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
return validatedRoom.data
|
||||
})
|
||||
.filter((item) => item.type === "roomcategories")
|
||||
.map((roomCategory) => {
|
||||
const validatedRoom = roomSchema.safeParse(roomCategory)
|
||||
if (!validatedRoom.success) {
|
||||
console.error(`Get Room Category Data - Verified Data Error`)
|
||||
console.error(validatedRoom.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
return validatedRoom.data
|
||||
})
|
||||
: []
|
||||
|
||||
return {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
|
||||
// Query
|
||||
export const getUserInputSchema = z
|
||||
.object({
|
||||
mask: z.boolean().default(true),
|
||||
@@ -10,17 +9,27 @@ export const getUserInputSchema = z
|
||||
|
||||
export const staysInput = z
|
||||
.object({
|
||||
cursor: z.number().optional(),
|
||||
limit: z.number().min(0).default(6),
|
||||
cursor: z
|
||||
.number()
|
||||
.optional()
|
||||
.transform((num) => (num ? String(num) : undefined)),
|
||||
limit: z
|
||||
.number()
|
||||
.min(0)
|
||||
.default(6)
|
||||
.transform((num) => String(num)),
|
||||
})
|
||||
.default({})
|
||||
|
||||
export const soonestUpcomingStaysInput = z
|
||||
export const friendTransactionsInput = z
|
||||
.object({
|
||||
limit: z.number().int().positive(),
|
||||
page: z.number().int().positive(),
|
||||
})
|
||||
.default({ limit: 3 })
|
||||
.default({ limit: 5, page: 1 })
|
||||
|
||||
|
||||
// Mutation
|
||||
export const addCreditCardInput = z.object({
|
||||
language: z.string(),
|
||||
})
|
||||
@@ -33,9 +42,3 @@ export const saveCreditCardInput = z.object({
|
||||
transactionId: z.string(),
|
||||
merchantId: z.string().optional(),
|
||||
})
|
||||
export const friendTransactionsInput = z
|
||||
.object({
|
||||
limit: z.number().int().positive(),
|
||||
page: z.number().int().positive(),
|
||||
})
|
||||
.default({ limit: 5, page: 1 })
|
||||
|
||||
@@ -79,6 +79,7 @@ async function getVerifiedUser({ session }: { session: Session }) {
|
||||
console.error(verifiedData.error)
|
||||
return null
|
||||
}
|
||||
|
||||
return verifiedData
|
||||
}
|
||||
|
||||
@@ -335,12 +336,9 @@ export const userQueryRouter = router({
|
||||
.input(staysInput)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const { limit, cursor } = input
|
||||
|
||||
const params = new URLSearchParams()
|
||||
params.set("limit", limit.toString())
|
||||
|
||||
const params: Record<string, string> = { limit }
|
||||
if (cursor) {
|
||||
params.set("offset", cursor.toString())
|
||||
params.offset = cursor
|
||||
}
|
||||
|
||||
const apiResponse = await api.get(
|
||||
@@ -403,11 +401,9 @@ export const userQueryRouter = router({
|
||||
.query(async ({ ctx, input }) => {
|
||||
const { limit, cursor } = input
|
||||
|
||||
const params = new URLSearchParams()
|
||||
params.set("limit", limit.toString())
|
||||
|
||||
const params: Record<string, string> = { limit }
|
||||
if (cursor) {
|
||||
params.set("offset", cursor.toString())
|
||||
params.offset = cursor
|
||||
}
|
||||
|
||||
const apiResponse = await api.get(
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { initTRPC } from "@trpc/server"
|
||||
import { experimental_nextAppDirCaller } from "@trpc/server/adapters/next-app-dir"
|
||||
import { ZodError } from "zod"
|
||||
|
||||
import { env } from "@/env/server"
|
||||
|
||||
@@ -9,15 +11,30 @@ import {
|
||||
unauthorizedError,
|
||||
} from "./errors/trpc"
|
||||
import { fetchServiceToken } from "./tokenManager"
|
||||
import { type Context, createContext } from "./context"
|
||||
import { transformer } from "./transformer"
|
||||
import { langInput } from "./utils"
|
||||
|
||||
import type { Session } from "next-auth"
|
||||
|
||||
import type { Meta } from "@/types/trpc/meta"
|
||||
import type { Context } from "./context"
|
||||
|
||||
const t = initTRPC.context<Context>().meta<Meta>().create({ transformer })
|
||||
const t = initTRPC
|
||||
.context<Context>()
|
||||
.meta<Meta>()
|
||||
.create({
|
||||
transformer,
|
||||
errorFormatter({ shape, error }) {
|
||||
return {
|
||||
...shape,
|
||||
data: {
|
||||
...shape.data,
|
||||
zodError:
|
||||
error.cause instanceof ZodError ? error.cause.flatten() : null,
|
||||
},
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
export const { createCallerFactory, mergeRouters, router } = t
|
||||
export const publicProcedure = t.procedure
|
||||
@@ -113,3 +130,30 @@ export const serviceProcedure = t.procedure.use(async (opts) => {
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
export const serverActionProcedure = t.procedure.experimental_caller(
|
||||
experimental_nextAppDirCaller({
|
||||
createContext,
|
||||
normalizeFormData: true,
|
||||
})
|
||||
)
|
||||
|
||||
export const protectedServerActionProcedure = serverActionProcedure.use(
|
||||
async (opts) => {
|
||||
const session = await opts.ctx.auth()
|
||||
if (!session) {
|
||||
throw unauthorizedError()
|
||||
}
|
||||
|
||||
if (session && session.error === "RefreshAccessTokenError") {
|
||||
throw sessionExpiredError()
|
||||
}
|
||||
|
||||
return opts.next({
|
||||
ctx: {
|
||||
...opts.ctx,
|
||||
session,
|
||||
},
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user