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

100 lines
2.3 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,
publicProcedure,
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-08-28 18:27:07 +02:00
import { getUserWithAnagraficaHandler } from "~/server/services/user.service";
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.custom<UsersAnagraficaUpdate>())
.mutation(async ({ input }) => {
return await editAnagraficaHandler({ input });
}),
getActiveUsersWithoutChat: adminProcedure.query(async () => {
return await getActiveUserWithoutChatHandler({ db });
}),
2025-08-29 16:18:32 +02:00
getClientProfilo: protectedProcedure
.input(
z.object({
id: zUserId.optional(),
}),
)
.query(async ({ ctx, input }) => {
const userId = input.id || ctx.session.id;
return getUserWithAnagraficaHandler({
db,
userId: userId,
});
}),
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;
}),
2025-08-28 18:27:07 +02:00
getUsersWChatInfo: publicProcedure.query(async () => {
return await getUsersWithChatInfoHandler({ db });
}),
2025-08-04 17:45:44 +02:00
});