Mais alterações em usuários

This commit is contained in:
Matheus Albino Brunhara
2022-06-20 04:06:03 -05:00
parent ac44716428
commit 04de75faf6
5 changed files with 74 additions and 24 deletions

View File

@@ -0,0 +1,24 @@
import { MigrationInterface, QueryRunner, TableColumn } from 'typeorm';
export class RemoveCpfAndCnpjFieldsFromUsersTable1655711281662
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropColumns('users', ['cnpj', 'cpf']);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.addColumns('users', [
new TableColumn({
name: 'cpf',
type: 'varchar',
isNullable: true,
}),
new TableColumn({
name: 'cnpj',
type: 'varchar',
isNullable: true,
}),
]);
}
}

View File

@@ -0,0 +1,24 @@
import { MigrationInterface, QueryRunner, TableColumn } from 'typeorm';
export class AddDocumentAndDocumentTypeFieldsToUsersTable1655711315251
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.addColumns('users', [
new TableColumn({
name: 'document',
type: 'varchar',
isNullable: true,
}),
new TableColumn({
name: 'document_type',
type: 'varchar',
isNullable: true,
}),
]);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropColumns('users', ['document_type', 'document']);
}
}

View File

@@ -39,10 +39,10 @@ class User {
star_rating: number; star_rating: number;
@Column() @Column()
cpf: string; document_type: string;
@Column() @Column()
cnpj: string; document: string;
@CreateDateColumn() @CreateDateColumn()
created_at: Date; created_at: Date;

View File

@@ -29,16 +29,16 @@ usersRouter.get('/list', async (request, response) => {
let usersWithoutSensitiveInfo: userWithoutSensitiveInfo[] = []; let usersWithoutSensitiveInfo: userWithoutSensitiveInfo[] = [];
users.map(user => { // users.map(user => {
usersWithoutSensitiveInfo.push({ // usersWithoutSensitiveInfo.push({
id_user: user.id_user, // id_user: user.id_user,
name: user.name, // name: user.name,
email: user.email, // email: user.email,
avatar_image: user.avatar_image, // avatar_image: user.avatar_image,
}); // });
}); // });
return response.json({ data: usersWithoutSensitiveInfo }); return response.json({ data: users });
}); });
// TODO, criar middleware ensureIsOwnUser é necessário? // TODO, criar middleware ensureIsOwnUser é necessário?
@@ -64,11 +64,12 @@ usersRouter.get('/:id', ensureAuthenticated, async (request, response) => {
name: user.name, name: user.name,
lastname: user.lastname, lastname: user.lastname,
email: user.email, email: user.email,
phone_number: user.phone_number,
birth_date: finalDate, birth_date: finalDate,
avatar_image: user.avatar_image, avatar_image: user.avatar_image,
bio: user.bio, bio: user.bio,
cpf: user.cpf, document_type: user.document_type,
cnpj: user.cnpj, document: user.document
// created_at: user.created_at, // created_at: user.created_at,
// updated_at: user.updated_at, // updated_at: user.updated_at,
}; };
@@ -100,7 +101,7 @@ usersRouter.post('/', async (request, response) => {
}); });
usersRouter.patch('/edit', ensureAuthenticated, async (request, response) => { usersRouter.patch('/edit', ensureAuthenticated, async (request, response) => {
const { name, lastname, username, bio, email, birth_date, cpf, cnpj } = request.body; const { name, lastname, bio, email, phone_number, birth_date, document_type, document } = request.body;
const updateUserService = new UpdateUserService(); const updateUserService = new UpdateUserService();
@@ -108,12 +109,12 @@ usersRouter.patch('/edit', ensureAuthenticated, async (request, response) => {
id_user: request.user.id_user, id_user: request.user.id_user,
name, name,
lastname, lastname,
username,
bio, bio,
email, email,
phone_number,
birth_date, birth_date,
cpf, document_type,
cnpj document
}); });
return response.json({ message: 'User info sucessfully updated.' }); return response.json({ message: 'User info sucessfully updated.' });

View File

@@ -9,16 +9,16 @@ interface Request {
id_user: string; id_user: string;
name: string; name: string;
lastname: string; lastname: string;
username: string;
bio: string; bio: string;
email: string; email: string;
phone_number: string;
birth_date: string; birth_date: string;
cpf: string; document_type: string;
cnpj: string; document: string;
} }
class UpdateUserService { class UpdateUserService {
public async execute({ id_user, name, lastname, username, bio, email, birth_date, cpf, cnpj }: Request): Promise<User> { public async execute({ id_user, name, lastname, bio, email, phone_number, birth_date, document_type, document }: Request): Promise<User> {
const usersRepository = getRepository(User); const usersRepository = getRepository(User);
const socialRepository = getRepository(Social); const socialRepository = getRepository(Social);
@@ -27,16 +27,17 @@ class UpdateUserService {
}); });
if (!user) { if (!user) {
throw new AppError('User does not exist.'); throw new AppError('O usuário informado não existe.');
}; };
if (name) user.name = name if (name) user.name = name
if (lastname) user.lastname = lastname if (lastname) user.lastname = lastname
if (bio) user.bio = bio if (bio) user.bio = bio
if (email) user.email = email if (email) user.email = email
if (phone_number) user.phone_number = phone_number
if (cpf) user.cpf = cpf if (document_type) user.document_type = document_type
if (cnpj) user.cnpj = cnpj if (document) user.document = document
// user.birth_date = new Date(birth_date); // TODO, funciona? // user.birth_date = new Date(birth_date); // TODO, funciona?