aboutsummaryrefslogtreecommitdiff
path: root/api/src/modules/users/users.controller.ts
diff options
context:
space:
mode:
authorzwlucas <lucas.fariamo08@gmail.com>2026-07-25 21:23:29 +0000
committerzwlucas <lucas.fariamo08@gmail.com>2026-07-25 21:23:29 +0000
commitef55d9bb24430610b60c1b77821307cd72c08c55 (patch)
treee6935a810b34370b0a4c821e55f6397aac50c832 /api/src/modules/users/users.controller.ts
downloadhelpdesk-ef55d9bb24430610b60c1b77821307cd72c08c55.tar.gz
helpdesk-ef55d9bb24430610b60c1b77821307cd72c08c55.zip
feat: upload of the projectHEADmaster
Signed-off-by: zwlucas <lucas.fariamo08@gmail.com>
Diffstat (limited to 'api/src/modules/users/users.controller.ts')
-rw-r--r--api/src/modules/users/users.controller.ts129
1 files changed, 129 insertions, 0 deletions
diff --git a/api/src/modules/users/users.controller.ts b/api/src/modules/users/users.controller.ts
new file mode 100644
index 0000000..c3c06fd
--- /dev/null
+++ b/api/src/modules/users/users.controller.ts
@@ -0,0 +1,129 @@
+import type { FastifyRequest, FastifyReply } from 'fastify';
+import bcrypt from 'bcryptjs';
+import { users } from '@/db/schema.js';
+import { eq, and, ne } from 'drizzle-orm';
+import type { CreateUserByAdminBody, UpdateUserBody } from './users.schema.js';
+
+export async function listUsersHandler(request: FastifyRequest, reply: FastifyReply) {
+ const db = request.server.db;
+ const allUsers = await db.query.users.findMany({
+ columns: {
+ password: false, // Don't return passwords
+ },
+ orderBy: (users, { desc }) => [desc(users.createdAt)],
+ });
+
+ return reply.send(allUsers);
+}
+
+export async function createUserHandler(
+ request: FastifyRequest<{ Body: CreateUserByAdminBody }>,
+ reply: FastifyReply,
+) {
+ const db = request.server.db;
+ const { name, email, password, role } = request.body;
+
+ // Verifica se email já existe
+ const existing = await db.query.users.findFirst({
+ where: eq(users.email, email),
+ });
+
+ if (existing) {
+ return reply.conflict('Este e-mail já está cadastrado.');
+ }
+
+ const hashedPassword = await bcrypt.hash(password, 12);
+
+ const [newUser] = await db.insert(users).values({
+ name,
+ email,
+ password: hashedPassword,
+ role,
+ isActive: true,
+ }).returning({
+ id: users.id,
+ name: users.name,
+ email: users.email,
+ role: users.role,
+ isActive: users.isActive,
+ createdAt: users.createdAt,
+ });
+
+ return reply.code(201).send(newUser);
+}
+
+export async function updateUserHandler(
+ request: FastifyRequest<{ Params: { id: number }; Body: UpdateUserBody }>,
+ reply: FastifyReply,
+) {
+ const db = request.server.db;
+ const { id } = request.params;
+ const { name, email, password, role, isActive } = request.body;
+
+ const existingUser = await db.query.users.findFirst({
+ where: eq(users.id, id),
+ });
+
+ if (!existingUser) {
+ return reply.notFound('Usuário não encontrado.');
+ }
+
+ if (email && email !== existingUser.email) {
+ const existingEmail = await db.query.users.findFirst({
+ where: and(eq(users.email, email), ne(users.id, id)),
+ });
+ if (existingEmail) {
+ return reply.conflict('Este e-mail já está em uso por outro usuário.');
+ }
+ }
+
+ const updates: Partial<typeof users.$inferInsert> = {
+ name,
+ email,
+ role,
+ isActive,
+ updatedAt: new Date().toISOString(),
+ };
+
+ if (password && password.trim() !== '') {
+ updates.password = await bcrypt.hash(password, 12);
+ }
+
+ const [updatedUser] = await db.update(users)
+ .set(updates)
+ .where(eq(users.id, id))
+ .returning({
+ id: users.id,
+ name: users.name,
+ email: users.email,
+ role: users.role,
+ isActive: users.isActive,
+ createdAt: users.createdAt,
+ });
+
+ return reply.send(updatedUser);
+}
+
+export async function deleteUserHandler(
+ request: FastifyRequest<{ Params: { id: number } }>,
+ reply: FastifyReply,
+) {
+ const db = request.server.db;
+ const { id } = request.params;
+
+ const existingUser = await db.query.users.findFirst({
+ where: eq(users.id, id),
+ });
+
+ if (!existingUser) {
+ return reply.notFound('Usuário não encontrado.');
+ }
+
+ if (id === request.user?.id) {
+ return reply.badRequest('Você não pode excluir a própria conta.');
+ }
+
+ await db.delete(users).where(eq(users.id, id));
+
+ return reply.code(204).send();
+}