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

110 lines
2.4 KiB
TypeScript
Raw Normal View History

import { z } from "zod/v4";
2025-08-28 18:27:07 +02:00
import type { UsersAnagraficaUpdate } 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,
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-28 18:27:07 +02:00
import { zUserId } from "~/server/utils/zod_types";
2025-08-04 17:45:44 +02:00
export const usersRouter = createTRPCRouter({
2025-08-29 16:18:32 +02:00
blockUser: adminProcedure
2025-08-28 18:27:07 +02:00
.input(
z.object({
id: zUserId,
}),
)
2025-08-29 16:18:32 +02:00
.mutation(async ({ ctx, input }) => {
return await blockUserHandler({ ctx, id: input.id });
2025-08-28 18:27:07 +02:00
}),
deleteUser: adminProcedure
.input(
z.object({
id: zUserId,
}),
)
.mutation(async ({ ctx, input }) => {
return await deleteUserHandler({ ctx, id: input.id });
}),
editUser: protectedProcedure
.input(
z.object({
cognome: z.string(),
email: z.email(),
2025-08-29 16:18:32 +02:00
id: zUserId,
2025-08-28 18:27:07 +02:00
isAdmin: z.boolean().optional(),
2025-08-29 16:18:32 +02:00
nome: z.string(),
telefono: z.string(),
2025-08-28 18:27:07 +02:00
}),
)
.mutation(async ({ ctx, input }) => {
return await editAccountHandler({ ctx, input });
}),
editUserAnagrafica: protectedProcedure
.input(
z.object({
userId: zUserId,
data: z.custom<UsersAnagraficaUpdate>(),
}),
)
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({ db });
}),
2025-08-29 16:18:32 +02:00
getClientProfilo: protectedProcedure
.input(
z.object({
userId: zUserId,
2025-08-29 16:18:32 +02:00
}),
)
.query(async ({ input }) => {
2025-11-20 16:48:29 +01:00
return getClientProfilo({
2025-08-29 16:18:32 +02:00
db,
userId: input.userId,
2025-08-29 16:18:32 +02:00
});
}),
getUser: protectedProcedure
.input(
z.object({
id: zUserId,
}),
)
.query(async ({ input }) => {
return await getUserHandler({ db, id: input.id });
}),
getUsers: adminProcedure.query(async () => {
const query = await db
.selectFrom("users")
.selectAll()
.orderBy("id", "desc")
.execute();
return query;
}),
getUsersWChatInfo: adminProcedure.query(async () => {
2025-08-28 18:27:07 +02:00
return await getUsersWithChatInfoHandler({ db });
}),
getAdmins: adminProcedure.query(async () => {
return await db
.selectFrom("users")
.selectAll()
.where("isAdmin", "=", true)
.orderBy("id", "desc")
.execute();
}),
2025-08-04 17:45:44 +02:00
});