infoalloggi-monorepo/apps/infoalloggi/src/server/api/routers/users.ts

115 lines
2.5 KiB
TypeScript
Raw Normal View History

import { z } from "zod/v4";
import { UsersUpdateSchema, usersId } from "~/schemas/public/Users";
import { UsersAnagraficaUpdateSchema } from "~/schemas/public/UsersAnagrafica";
2025-08-04 17:45:44 +02:00
import {
2025-08-28 18:27:07 +02:00
adminProcedure,
createTRPCRouter,
protectedProcedure,
2025-08-04 17:45:44 +02:00
} from "~/server/api/trpc";
import {
2025-08-28 18:27:07 +02:00
blockUserHandler,
deleteUserHandler,
editAccountHandler,
editAnagraficaHandler,
genUserIntestazione,
2025-08-28 18:27:07 +02:00
getActiveUserWithoutChatHandler,
getUserHandler,
getUsersWithChatInfoHandler,
2025-08-04 17:45:44 +02:00
} from "~/server/controllers/user.controller";
import { db } from "~/server/db";
2025-11-20 16:48:29 +01:00
import { getClientProfilo } from "~/server/services/user.service";
2025-08-04 17:45:44 +02:00
export const usersRouter = createTRPCRouter({
getUser: protectedProcedure
.input(
z.object({
id: usersId,
}),
)
.query(async ({ input }) => {
return await getUserHandler(input.id);
}),
getUsers: adminProcedure.query(async () => {
return await db
.selectFrom("users")
.selectAll()
.orderBy("id", "desc")
.execute();
}),
getAdmins: adminProcedure.query(async () => {
return await db
.selectFrom("users")
.selectAll()
.where("isAdmin", "=", true)
.orderBy("id", "desc")
.execute();
}),
getClientProfilo: protectedProcedure
.input(
z.object({
userId: usersId,
}),
)
.query(async ({ input }) => {
return getClientProfilo({
db,
userId: input.userId,
});
}),
2025-08-29 16:18:32 +02:00
blockUser: adminProcedure
2025-08-28 18:27:07 +02:00
.input(
z.object({
id: usersId,
2025-08-28 18:27:07 +02:00
}),
)
.mutation(async ({ input }) => {
return await blockUserHandler(input.id);
2025-08-28 18:27:07 +02:00
}),
deleteUser: adminProcedure
.input(
z.object({
id: usersId,
2025-08-28 18:27:07 +02:00
}),
)
.mutation(async ({ input }) => {
return await deleteUserHandler(input.id);
2025-08-28 18:27:07 +02:00
}),
editUser: protectedProcedure
.input(
z.object({
id: usersId,
data: UsersUpdateSchema,
2025-08-28 18:27:07 +02:00
}),
)
.mutation(async ({ input }) => {
return await editAccountHandler({ ...input });
2025-08-28 18:27:07 +02:00
}),
editUserAnagrafica: protectedProcedure
.input(
z.object({
userId: usersId,
data: UsersAnagraficaUpdateSchema,
}),
)
2025-08-28 18:27:07 +02:00
.mutation(async ({ input }) => {
return await editAnagraficaHandler({ ...input });
2025-08-28 18:27:07 +02:00
}),
getActiveUsersWithoutChat: adminProcedure.query(async () => {
return await getActiveUserWithoutChatHandler();
2025-08-28 18:27:07 +02:00
}),
2025-08-29 16:18:32 +02:00
getUsersWChatInfo: adminProcedure.query(async () => {
return await getUsersWithChatInfoHandler();
2025-08-28 18:27:07 +02:00
}),
genIntestazione: adminProcedure
.input(
z.object({
userId: usersId,
}),
)
.mutation(async ({ input }) => {
return await genUserIntestazione(input.userId);
}),
2025-08-04 17:45:44 +02:00
});