Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create user #1

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
}
Loading