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/update optional #10

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion docs/insomnia.json

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions src/modules/users/dtos/IUpdateUserDTO.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
interface IUpdateUserDTO {
name: string;
email: string;
password: string;
language: string;
phone: string;
name?: string;
email?: string;
password?: string;
language?: string;
phone?: string;
image?: string;
gender?: string;
birthdate?: Date;
}

export default IUpdateUserDTO;
12 changes: 9 additions & 3 deletions src/modules/users/infra/http/controller/UsersController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
});
}

public async readAll(req: Request, res: Response): Promise<Response> {

Check failure on line 41 in src/modules/users/infra/http/controller/UsersController.ts

View workflow job for this annotation

GitHub Actions / Check linting (14.x)

Block must not be padded by blank lines

const readUsers = container.resolve(ReadAllUsersService);

const users = await readUsers.execute();

Check failure on line 46 in src/modules/users/infra/http/controller/UsersController.ts

View workflow job for this annotation

GitHub Actions / Check linting (14.x)

Trailing spaces not allowed
return res.status(201).json(users?.map(user => {

Check failure on line 47 in src/modules/users/infra/http/controller/UsersController.ts

View workflow job for this annotation

GitHub Actions / Check linting (14.x)

Expected parentheses around arrow function argument

Check failure on line 47 in src/modules/users/infra/http/controller/UsersController.ts

View workflow job for this annotation

GitHub Actions / Check linting (14.x)

Unexpected block statement surrounding arrow body; parenthesize the returned value and move it immediately after the `=>`
return {
...user,
password: undefined,
Expand All @@ -56,7 +56,7 @@

public async readById(req: Request, res: Response): Promise<Response> {
const { id } = req.token;

Check failure on line 59 in src/modules/users/infra/http/controller/UsersController.ts

View workflow job for this annotation

GitHub Actions / Check linting (14.x)

Trailing spaces not allowed
const readUser = container.resolve(ReadUserByIdService);

const user = await readUser.execute({
Expand All @@ -80,18 +80,24 @@
password,
language,
phone,
image,
gender,
birthdate,
} = req.body;

const updateUser = container.resolve(UpdateUserService);

const user = await updateUser.execute({
const user = await updateUser.execute(
id,
name,
{ name,

Check failure on line 92 in src/modules/users/infra/http/controller/UsersController.ts

View workflow job for this annotation

GitHub Actions / Check linting (14.x)

Expected a line break after this opening brace
email,

Check failure on line 93 in src/modules/users/infra/http/controller/UsersController.ts

View workflow job for this annotation

GitHub Actions / Check linting (14.x)

Expected indentation of 8 spaces but found 6
password,

Check failure on line 94 in src/modules/users/infra/http/controller/UsersController.ts

View workflow job for this annotation

GitHub Actions / Check linting (14.x)

Expected indentation of 8 spaces but found 6
language,

Check failure on line 95 in src/modules/users/infra/http/controller/UsersController.ts

View workflow job for this annotation

GitHub Actions / Check linting (14.x)

Expected indentation of 8 spaces but found 6
phone,

Check failure on line 96 in src/modules/users/infra/http/controller/UsersController.ts

View workflow job for this annotation

GitHub Actions / Check linting (14.x)

Expected indentation of 8 spaces but found 6
});
image,
gender,
birthdate, }
);

return res.status(201).json({
...user,
Expand Down
3 changes: 3 additions & 0 deletions src/modules/users/infra/prisma/entities/users.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ model Users {
phone String
pin String?
pinExpires DateTime?
image String?
gender String?
birthdate DateTime?
created_at DateTime @default(now())
updated_at DateTime @default(now())
}
54 changes: 40 additions & 14 deletions src/modules/users/services/UpdateUserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import IHashProvider from '@shared/container/providers/HashProvider/models/IHash
import IUsersRepository from '../repositories/IUsersRepository';

interface IRequest {
id: string;
name: string;
email: string;
password: string;
language: string;
phone: string;
name?: string;
email?: string;
password?: string;
language?: string;
phone?: string;
image?: string;
gender?: string;
birthdate?: Date;
}

@injectable()
Expand All @@ -26,23 +28,47 @@ export default class UpdateUserService {
private hashProvider: IHashProvider,
) { }

public async execute({
id, name, email, password, language, phone,
}: IRequest): Promise<Users> {
public async execute(id: string, updateData: IRequest): Promise<Users> {
const userAlreadyExists = await this.usersRepository.findById(id);

if (!userAlreadyExists) throw new AppError('User with this id does not exist');
if (updateData.email) {
const userWithUpdatedEmail = await this.usersRepository.findByEmailWithRelations(updateData.email);
if(userWithUpdatedEmail) {
if (userWithUpdatedEmail.id == id) {
throw new AppError('You cannot update your email to the same email');
}
if (userWithUpdatedEmail.id !== id) {
throw new AppError('User with same email already exists');
}
}
}
if(updateData.birthdate || updateData.birthdate == ''){
const birthdate = new Date(updateData.birthdate);
if (isNaN(birthdate.getTime())) {
throw new AppError('Birthdate is not a valid date');
}

if (birthdate > new Date()) {
throw new AppError('Birthdate cannot be greater than current date');
}
}

const hashedPassword = await this.hashProvider.generateHash(password);
const data = { ...userAlreadyExists, ...updateData };

let hashedPassword;
if(data.password){
hashedPassword = await this.hashProvider.generateHash(data.password.toString());
} else {
hashedPassword = data.password;
}

const updatedUser = this.usersRepository.update(
id,
{
name,
email: email.toLowerCase(),
...data,
email: data.email?.toLowerCase(),
password: hashedPassword,
language,
phone,
});

return updatedUser;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- AlterTable
ALTER TABLE "Users" ADD COLUMN "birthdate" TIMESTAMP(3),
ADD COLUMN "gender" TEXT,
ADD COLUMN "image" TEXT;
3 changes: 3 additions & 0 deletions src/shared/infra/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ model Users {
phone String
pin String?
pinExpires DateTime?
image String?
gender String?
birthdate DateTime?
created_at DateTime @default(now())
updated_at DateTime @default(now())
}
Loading