From 6cf37e40517b80a08c6b4a034fecedbb5742827c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=A1ssyla=20Lissa=20Lima?= Date: Mon, 8 Jul 2024 18:27:21 -0300 Subject: [PATCH] feat: create user --- src/modules/users/dtos/ICreateUserDTO.ts | 3 +-- .../users/infra/http/controller/UsersController.ts | 6 ++---- src/modules/users/infra/prisma/entities/users.prisma | 3 +-- .../infra/prisma/repositories/UsersRepository.ts | 8 -------- src/modules/users/repositories/IUsersRepository.ts | 1 - src/modules/users/services/CreateUserService.ts | 12 +++++------- src/shared/infra/http/routes/index.ts | 2 +- .../migrations/20240708210626_first/migration.sql | 12 ++++++++++++ src/shared/infra/prisma/schema.prisma | 3 +-- 9 files changed, 23 insertions(+), 27 deletions(-) create mode 100644 src/shared/infra/prisma/migrations/20240708210626_first/migration.sql diff --git a/src/modules/users/dtos/ICreateUserDTO.ts b/src/modules/users/dtos/ICreateUserDTO.ts index 2bc6b42..d79dcd5 100644 --- a/src/modules/users/dtos/ICreateUserDTO.ts +++ b/src/modules/users/dtos/ICreateUserDTO.ts @@ -1,9 +1,8 @@ interface ICreateUserDTO { name: string; email: string; - cpf: string; - phone: string; password: string; + language: string; } export default ICreateUserDTO; diff --git a/src/modules/users/infra/http/controller/UsersController.ts b/src/modules/users/infra/http/controller/UsersController.ts index e8b4278..e9cee83 100644 --- a/src/modules/users/infra/http/controller/UsersController.ts +++ b/src/modules/users/infra/http/controller/UsersController.ts @@ -8,9 +8,8 @@ export default class UserController { const { name, email, - cpf, - phone, password, + language, } = req.body; const createUser = container.resolve(CreateUserService); @@ -18,9 +17,8 @@ export default class UserController { const user = await createUser.execute({ name, email, - cpf, - phone, password, + language, }); user.password = '###'; diff --git a/src/modules/users/infra/prisma/entities/users.prisma b/src/modules/users/infra/prisma/entities/users.prisma index 5c61abf..f4a692e 100644 --- a/src/modules/users/infra/prisma/entities/users.prisma +++ b/src/modules/users/infra/prisma/entities/users.prisma @@ -2,9 +2,8 @@ model Users { id String @id @default(uuid()) name String @unique email String - cpf String - phone String password String + language String created_at DateTime @default(now()) updated_at DateTime @default(now()) } diff --git a/src/modules/users/infra/prisma/repositories/UsersRepository.ts b/src/modules/users/infra/prisma/repositories/UsersRepository.ts index 14d47f1..4f907c4 100644 --- a/src/modules/users/infra/prisma/repositories/UsersRepository.ts +++ b/src/modules/users/infra/prisma/repositories/UsersRepository.ts @@ -19,14 +19,6 @@ export default class UsersRepository implements IUsersRepository { return user; } - public async findByEmailPhoneOrCpf(email: string, phone: string, cpf: string): Promise { - const user = await this.ormRepository.findFirst({ - where: { OR: [{ email }, { phone }, { cpf }] }, - }); - - return user; - } - public async create(data: ICreateUserDTO): Promise { const user = await this.ormRepository.create({ data }); diff --git a/src/modules/users/repositories/IUsersRepository.ts b/src/modules/users/repositories/IUsersRepository.ts index 64db2f4..6be94d3 100644 --- a/src/modules/users/repositories/IUsersRepository.ts +++ b/src/modules/users/repositories/IUsersRepository.ts @@ -4,7 +4,6 @@ import ICreateUserDTO from '../dtos/ICreateUserDTO'; interface IUsersRepository { findByEmailWithRelations(email: string): Promise; - findByEmailPhoneOrCpf(email: string, phone: string, cpf: string): Promise; create(data: ICreateUserDTO): Promise; } diff --git a/src/modules/users/services/CreateUserService.ts b/src/modules/users/services/CreateUserService.ts index c7da09b..162e4d2 100644 --- a/src/modules/users/services/CreateUserService.ts +++ b/src/modules/users/services/CreateUserService.ts @@ -12,9 +12,8 @@ import IUsersRepository from '../repositories/IUsersRepository'; interface IRequest { name: string; email: string; - cpf: string; - phone: string; password: string; + language: string; } @injectable() @@ -31,20 +30,19 @@ export default class CreateUserService { ) { } public async execute({ - cpf, email, name, password, phone, + name, email, password, language, }: IRequest): Promise { - const userAlreadyExists = await this.usersRepository.findByEmailPhoneOrCpf(email, phone, cpf); + const userAlreadyExists = await this.usersRepository.findByEmailWithRelations(email); - if (userAlreadyExists) throw new AppError('User with same name, phone or cpf already exists'); + if (userAlreadyExists) throw new AppError('User with same email already exists'); const hashedPassword = await this.hashProvider.generateHash(password); const user = this.usersRepository.create({ name, email: email.toLowerCase(), - cpf, password: hashedPassword, - phone, + language, }); const templateDataFile = path.resolve(__dirname, '..', 'views', 'create_account.hbs'); diff --git a/src/shared/infra/http/routes/index.ts b/src/shared/infra/http/routes/index.ts index b4fe6a2..98c63aa 100644 --- a/src/shared/infra/http/routes/index.ts +++ b/src/shared/infra/http/routes/index.ts @@ -7,7 +7,7 @@ import sessionsRoutes from '@modules/users/infra/http/routes/sessions.routes'; const routes = Router(); // Users -routes.use('', usersRoutes); +routes.use('/user', usersRoutes); routes.use('/sessions', sessionsRoutes); export default routes; diff --git a/src/shared/infra/prisma/migrations/20240708210626_first/migration.sql b/src/shared/infra/prisma/migrations/20240708210626_first/migration.sql new file mode 100644 index 0000000..0a83ad4 --- /dev/null +++ b/src/shared/infra/prisma/migrations/20240708210626_first/migration.sql @@ -0,0 +1,12 @@ +/* + Warnings: + + - You are about to drop the column `cpf` on the `Users` table. All the data in the column will be lost. + - You are about to drop the column `phone` on the `Users` table. All the data in the column will be lost. + - Added the required column `language` to the `Users` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE "Users" DROP COLUMN "cpf", +DROP COLUMN "phone", +ADD COLUMN "language" TEXT NOT NULL; diff --git a/src/shared/infra/prisma/schema.prisma b/src/shared/infra/prisma/schema.prisma index cfd6bcd..d26fcc5 100644 --- a/src/shared/infra/prisma/schema.prisma +++ b/src/shared/infra/prisma/schema.prisma @@ -12,9 +12,8 @@ model Users { id String @id @default(uuid()) name String @unique email String - cpf String - phone String password String + language String created_at DateTime @default(now()) updated_at DateTime @default(now()) } \ No newline at end of file