Atualizando backend de usuários para conter informações de contato

This commit is contained in:
Matheus Albino Brunhara
2022-06-19 22:20:30 -05:00
parent 0d2b7a6884
commit 01a30b0f17
11 changed files with 133 additions and 56 deletions

View File

@@ -1,7 +1,6 @@
import { MigrationInterface, QueryRunner, Table } from "typeorm"; import { MigrationInterface, QueryRunner, Table } from "typeorm";
export class CreateUsers1617210132141 implements MigrationInterface { export class CreateUsers1617210132141 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> { public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable( await queryRunner.createTable(
new Table({ new Table({

View File

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

View File

@@ -11,7 +11,7 @@ import {
import User from './User'; import User from './User';
@Entity('socials') @Entity('socialInformation')
class Social { class Social {
@PrimaryGeneratedColumn('increment') @PrimaryGeneratedColumn('increment')
id_social: string; id_social: string;
@@ -21,16 +21,16 @@ class Social {
user: User; user: User;
@Column() @Column()
telegram: string; phone: string;
@Column()
whatsapp: string;
@Column() @Column()
facebook: string; facebook: string;
@Column() @Column()
twitter: string; telegram: string;
@Column()
twitch: string;
@CreateDateColumn() @CreateDateColumn()
created_at: Date; created_at: Date;

View File

@@ -38,6 +38,12 @@ class User {
@Column() @Column()
star_rating: number; star_rating: number;
@Column()
cpf: string;
@Column()
cnpj: string;
@CreateDateColumn() @CreateDateColumn()
created_at: Date; created_at: Date;

View File

@@ -67,6 +67,8 @@ usersRouter.get('/:id', ensureAuthenticated, async (request, response) => {
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,
cnpj: user.cnpj,
// created_at: user.created_at, // created_at: user.created_at,
// updated_at: user.updated_at, // updated_at: user.updated_at,
}; };
@@ -98,7 +100,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 } = request.body; const { name, lastname, username, bio, email, birth_date, cpf, cnpj } = request.body;
const updateUserService = new UpdateUserService(); const updateUserService = new UpdateUserService();
@@ -110,6 +112,8 @@ usersRouter.patch('/edit', ensureAuthenticated, async (request, response) => {
bio, bio,
email, email,
birth_date, birth_date,
cpf,
cnpj
}); });
return response.json({ message: 'User info sucessfully updated.' }); return response.json({ message: 'User info sucessfully updated.' });
@@ -177,4 +181,41 @@ usersRouter.patch(
}, },
); );
usersRouter.get(
'/social/:id_user',
ensureAuthenticated,
async (request, response) => {
const { id_user } = request.params;
const findUserSocial = new FindUserSocialService();
const social = await findUserSocial.execute(id_user);
return response.json({ data: social });
},
);
usersRouter.patch(
'/social',
ensureAuthenticated,
async (request, response) => {
const { id_user, phone, whatsapp, facebook, telegram } = request.body;
const social_network = {
phone,
whatsapp,
facebook,
telegram
}
const updateUserSocialService = new UpdateUserSocialService();
const social = await updateUserSocialService.execute({
id_user,
social_network
});
return response.json({ data: social });
},
);
export default usersRouter; export default usersRouter;

View File

@@ -5,7 +5,7 @@ import { hash } from 'bcryptjs';
import AppError from '../errors/AppError'; import AppError from '../errors/AppError';
import User from '../models/User'; import User from '../models/User';
import Social from '../models/Social'; import Social from '../models/SocialInformation';
interface Request { interface Request {
name: string; name: string;
@@ -18,7 +18,7 @@ interface Request {
class CreateUserService { class CreateUserService {
public async execute({ name, lastname, email, birth_date, password }: Request): Promise<User> { public async execute({ name, lastname, email, birth_date, password }: Request): Promise<User> {
const usersRepository = getRepository(User); const usersRepository = getRepository(User);
const socialsRepository = getRepository(Social); const socialInformationRepository = getRepository(Social);
const checkUserEmailExists = await usersRepository.findOne({ const checkUserEmailExists = await usersRepository.findOne({
where: { email }, where: { email },
@@ -40,8 +40,8 @@ class CreateUserService {
await usersRepository.save(user); await usersRepository.save(user);
// já criar registro na tabela Socials para evitar inconsistências // já criar registro na tabela Socials para evitar inconsistências
// const social = socialsRepository.create({ user, telegram: "", facebook: "", twitter: "", twitch: "" }); const social = socialInformationRepository.create({ user, phone: "", whatsapp: "", facebook: "", telegram: "" });
// await socialsRepository.save(social); await socialInformationRepository.save(social);
return user; return user;
} }

View File

@@ -3,30 +3,35 @@ import { getRepository } from 'typeorm';
import AppError from '../errors/AppError'; import AppError from '../errors/AppError';
import User from '../models/User'; import User from '../models/User';
import Social from '../models/Social'; import Social from '../models/SocialInformation';
class FindUserSocialService { class FindUserSocialService {
public async execute(id_user: string): Promise<Social> { public async execute(id_user: string): Promise<Social> {
const usersRepository = getRepository(User); const usersRepository = getRepository(User);
const socialRepository = getRepository(Social); const socialInformationRepository = getRepository(Social);
const user = await usersRepository.findOne({ const user = await usersRepository.findOne({
where: { id_user } where: { id_user }
}); });
if (!user) { if (!user) {
// TODO, fazer no front um tratamento para isso
throw new AppError('User does not exist.'); throw new AppError('User does not exist.');
}; };
const social = await socialRepository.findOne({ let social = await socialInformationRepository.findOne({
where: { user }, where: { user },
}); });
if (!social) { if (!social) {
// TODO, lembrar social = socialInformationRepository.create({
// muito importate colocar o código HTTP de erro user,
throw new AppError('User does not have social information.', 200); phone: '',
whatsapp: '',
facebook: '',
telegram: '',
});
await socialInformationRepository.save(social);
}; };
return social; return social;

View File

@@ -3,7 +3,7 @@ import { getRepository } from 'typeorm';
import AppError from '../errors/AppError'; import AppError from '../errors/AppError';
import User from '../models/User'; import User from '../models/User';
import Social from '../models/Social'; import Social from '../models/SocialInformation';
interface Request { interface Request {
id_user: string; id_user: string;

View File

@@ -4,7 +4,7 @@ import { hash, compare } from 'bcryptjs';
import AppError from '../errors/AppError'; import AppError from '../errors/AppError';
import User from '../models/User'; import User from '../models/User';
import Social from '../models/Social'; import Social from '../models/SocialInformation';
interface Request { interface Request {
id_user: string; id_user: string;

View File

@@ -3,7 +3,7 @@ import { getRepository } from 'typeorm';
import AppError from '../errors/AppError'; import AppError from '../errors/AppError';
import User from '../models/User'; import User from '../models/User';
import Social from '../models/Social'; import Social from '../models/SocialInformation';
interface Request { interface Request {
id_user: string; id_user: string;
@@ -13,10 +13,12 @@ interface Request {
bio: string; bio: string;
email: string; email: string;
birth_date: string; birth_date: string;
cpf: string;
cnpj: string;
} }
class UpdateUserService { class UpdateUserService {
public async execute({ id_user, name, lastname, username, bio, email, birth_date }: Request): Promise<User> { public async execute({ id_user, name, lastname, username, bio, email, birth_date, cpf, cnpj }: Request): Promise<User> {
const usersRepository = getRepository(User); const usersRepository = getRepository(User);
const socialRepository = getRepository(Social); const socialRepository = getRepository(Social);
@@ -28,10 +30,13 @@ class UpdateUserService {
throw new AppError('User does not exist.'); throw new AppError('User does not exist.');
}; };
user.name = name if (name) user.name = name
user.lastname = lastname if (lastname) user.lastname = lastname
user.bio = bio if (bio) user.bio = bio
user.email = email if (email) user.email = email
if (cpf) user.cpf = cpf
if (cnpj) user.cnpj = cnpj
// user.birth_date = new Date(birth_date); // TODO, funciona? // user.birth_date = new Date(birth_date); // TODO, funciona?

View File

@@ -3,54 +3,51 @@ import { getRepository } from 'typeorm';
import AppError from '../errors/AppError'; import AppError from '../errors/AppError';
import User from '../models/User'; import User from '../models/User';
import Social from '../models/Social'; import Social from '../models/SocialInformation';
interface Request { interface Request {
id_user: string; id_user: string;
social_network: string; social_network: {
phone: string;
whatsapp: string;
facebook: string;
telegram: string;
};
} }
class UpdateUserSocialService { class UpdateUserSocialService {
public async execute({ public async execute({ id_user, social_network }: Request): Promise<Social> {
id_user,
social_network,
}: Request): Promise<Social> {
const usersRepository = getRepository(User); const usersRepository = getRepository(User);
const socialRepository = getRepository(Social); const socialInformationRepository = getRepository(Social);
const user = await usersRepository.findOne({ const user = await usersRepository.findOne({
where: { id_user: id_user }, where: { id_user },
}); });
if (!user) { if (!user) {
throw new AppError('User does not exist.'); throw new AppError('User does not exist.');
} }
const social = await socialRepository.findOne({ let social = await socialInformationRepository.findOne({
where: { user: user }, where: { user },
}); });
if (!social) throw new AppError('User does not exist.'); if (!social) {
social = socialInformationRepository.create({
switch (social_network) { user,
case 'telegram': phone: '',
social.telegram = ""; whatsapp: '',
break; facebook: '',
case 'facebook': telegram: '',
social.facebook = ""; });
break;
case 'twitter':
social.twitter = "";
break;
case 'twitch':
social.twitch = "";
break;
default:
break;
} }
await socialRepository.save(social); if (social_network.phone) social.phone = social_network.phone;
if (social_network.whatsapp) social.whatsapp = social_network.whatsapp;
if (social_network.facebook) social.facebook = social_network.facebook;
if (social_network.telegram) social.telegram = social_network.telegram;
await socialInformationRepository.save(social);
return social; return social;
} }