Skip to content

Commit

Permalink
Merge pull request #1 from polijrorg/feat/crud-user
Browse files Browse the repository at this point in the history
feat: create user
  • Loading branch information
tassyla authored Jul 8, 2024
2 parents 901fab5 + 6cf37e4 commit 1b92800
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 27 deletions.
3 changes: 1 addition & 2 deletions src/modules/users/dtos/ICreateUserDTO.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
interface ICreateUserDTO {
name: string;
email: string;
cpf: string;
phone: string;
password: string;
language: string;
}

export default ICreateUserDTO;
6 changes: 2 additions & 4 deletions src/modules/users/infra/http/controller/UsersController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@ export default class UserController {
const {
name,
email,
cpf,
phone,
password,
language,
} = req.body;

const createUser = container.resolve(CreateUserService);

const user = await createUser.execute({
name,
email,
cpf,
phone,
password,
language,
});

user.password = '###';
Expand Down
3 changes: 1 addition & 2 deletions src/modules/users/infra/prisma/entities/users.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ export default class UsersRepository implements IUsersRepository {
return user;
}

public async findByEmailPhoneOrCpf(email: string, phone: string, cpf: string): Promise<Users | null> {
const user = await this.ormRepository.findFirst({
where: { OR: [{ email }, { phone }, { cpf }] },
});

return user;
}

public async create(data: ICreateUserDTO): Promise<Users> {
const user = await this.ormRepository.create({ data });

Expand Down
1 change: 0 additions & 1 deletion src/modules/users/repositories/IUsersRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import ICreateUserDTO from '../dtos/ICreateUserDTO';

interface IUsersRepository {
findByEmailWithRelations(email: string): Promise<Users | null>;
findByEmailPhoneOrCpf(email: string, phone: string, cpf: string): Promise<Users | null>;
create(data: ICreateUserDTO): Promise<Users>;
}

Expand Down
12 changes: 5 additions & 7 deletions src/modules/users/services/CreateUserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import IUsersRepository from '../repositories/IUsersRepository';
interface IRequest {
name: string;
email: string;
cpf: string;
phone: string;
password: string;
language: string;
}

@injectable()
Expand All @@ -31,20 +30,19 @@ export default class CreateUserService {
) { }

public async execute({
cpf, email, name, password, phone,
name, email, password, language,
}: IRequest): Promise<Users> {
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');
Expand Down
2 changes: 1 addition & 1 deletion src/shared/infra/http/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 1 addition & 2 deletions src/shared/infra/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

0 comments on commit 1b92800

Please sign in to comment.