added getting user and self
All checks were successful
backend-action / build-image (push) Successful in 27s

This commit is contained in:
2024-04-21 02:05:41 +07:00
parent f159e3da60
commit 35880519da

View File

@@ -47,6 +47,12 @@ export const userRoute = router({
updateUser: protectedProcedure updateUser: protectedProcedure
.input(userUpdateSchema) .input(userUpdateSchema)
.mutation(async ({ input, ctx }) => await updateUser(ctx.user.id, input)), .mutation(async ({ input, ctx }) => await updateUser(ctx.user.id, input)),
getUser: publicProcedure
.input(z.object({ userId: z.number() }))
.mutation(async ({ input }) => await getUser(input.userId, false)),
getSelf: protectedProcedure.mutation(
async ({ ctx }) => await getUser(ctx.user.id, true)
),
login: publicProcedure login: publicProcedure
.input(z.object({ cid: z.string(), phone: z.string() })) .input(z.object({ cid: z.string(), phone: z.string() }))
.mutation(async ({ input }) => await login(input.cid, input.phone)), .mutation(async ({ input }) => await login(input.cid, input.phone)),
@@ -129,6 +135,24 @@ async function getAllUser(
})); }));
} }
async function getUser(userId: number, showPhone: boolean) {
let user = await db.query.user.findFirst({
where: (user, { eq }) => eq(user.id, userId),
with: {
group: true,
opinions: true,
zone: true,
},
});
if (user === undefined) {
throw new TRPCError({
message: "User not found",
code: "BAD_REQUEST",
});
}
return { ...user, phone: showPhone ? user.phone : hidePhone(user.phone) };
}
async function createUser( async function createUser(
newUser: UserInsertSchema, newUser: UserInsertSchema,
opinions: OpinionInsertSchema opinions: OpinionInsertSchema