Atualizando backend de usuários para conter informações de contato
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import { MigrationInterface, QueryRunner, Table } from "typeorm";
|
||||
|
||||
export class CreateUsers1617210132141 implements MigrationInterface {
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.createTable(
|
||||
new Table({
|
||||
|
||||
@@ -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']);
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import {
|
||||
|
||||
import User from './User';
|
||||
|
||||
@Entity('socials')
|
||||
@Entity('socialInformation')
|
||||
class Social {
|
||||
@PrimaryGeneratedColumn('increment')
|
||||
id_social: string;
|
||||
@@ -21,16 +21,16 @@ class Social {
|
||||
user: User;
|
||||
|
||||
@Column()
|
||||
telegram: string;
|
||||
phone: string;
|
||||
|
||||
@Column()
|
||||
whatsapp: string;
|
||||
|
||||
@Column()
|
||||
facebook: string;
|
||||
|
||||
@Column()
|
||||
twitter: string;
|
||||
|
||||
@Column()
|
||||
twitch: string;
|
||||
telegram: string;
|
||||
|
||||
@CreateDateColumn()
|
||||
created_at: Date;
|
||||
@@ -38,6 +38,12 @@ class User {
|
||||
@Column()
|
||||
star_rating: number;
|
||||
|
||||
@Column()
|
||||
cpf: string;
|
||||
|
||||
@Column()
|
||||
cnpj: string;
|
||||
|
||||
@CreateDateColumn()
|
||||
created_at: Date;
|
||||
|
||||
|
||||
@@ -67,6 +67,8 @@ usersRouter.get('/:id', ensureAuthenticated, async (request, response) => {
|
||||
birth_date: finalDate,
|
||||
avatar_image: user.avatar_image,
|
||||
bio: user.bio,
|
||||
cpf: user.cpf,
|
||||
cnpj: user.cnpj,
|
||||
// created_at: user.created_at,
|
||||
// updated_at: user.updated_at,
|
||||
};
|
||||
@@ -98,7 +100,7 @@ usersRouter.post('/', 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();
|
||||
|
||||
@@ -110,6 +112,8 @@ usersRouter.patch('/edit', ensureAuthenticated, async (request, response) => {
|
||||
bio,
|
||||
email,
|
||||
birth_date,
|
||||
cpf,
|
||||
cnpj
|
||||
});
|
||||
|
||||
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;
|
||||
|
||||
@@ -5,7 +5,7 @@ import { hash } from 'bcryptjs';
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Social from '../models/Social';
|
||||
import Social from '../models/SocialInformation';
|
||||
|
||||
interface Request {
|
||||
name: string;
|
||||
@@ -18,7 +18,7 @@ interface Request {
|
||||
class CreateUserService {
|
||||
public async execute({ name, lastname, email, birth_date, password }: Request): Promise<User> {
|
||||
const usersRepository = getRepository(User);
|
||||
const socialsRepository = getRepository(Social);
|
||||
const socialInformationRepository = getRepository(Social);
|
||||
|
||||
const checkUserEmailExists = await usersRepository.findOne({
|
||||
where: { email },
|
||||
@@ -40,8 +40,8 @@ class CreateUserService {
|
||||
await usersRepository.save(user);
|
||||
|
||||
// já criar registro na tabela Socials para evitar inconsistências
|
||||
// const social = socialsRepository.create({ user, telegram: "", facebook: "", twitter: "", twitch: "" });
|
||||
// await socialsRepository.save(social);
|
||||
const social = socialInformationRepository.create({ user, phone: "", whatsapp: "", facebook: "", telegram: "" });
|
||||
await socialInformationRepository.save(social);
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
@@ -3,30 +3,35 @@ import { getRepository } from 'typeorm';
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Social from '../models/Social';
|
||||
import Social from '../models/SocialInformation';
|
||||
|
||||
class FindUserSocialService {
|
||||
public async execute(id_user: string): Promise<Social> {
|
||||
const usersRepository = getRepository(User);
|
||||
const socialRepository = getRepository(Social);
|
||||
const socialInformationRepository = getRepository(Social);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user }
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
// TODO, fazer no front um tratamento para isso
|
||||
throw new AppError('User does not exist.');
|
||||
};
|
||||
|
||||
const social = await socialRepository.findOne({
|
||||
let social = await socialInformationRepository.findOne({
|
||||
where: { user },
|
||||
});
|
||||
|
||||
if (!social) {
|
||||
// TODO, lembrar
|
||||
// muito importate colocar o código HTTP de erro
|
||||
throw new AppError('User does not have social information.', 200);
|
||||
social = socialInformationRepository.create({
|
||||
user,
|
||||
phone: '',
|
||||
whatsapp: '',
|
||||
facebook: '',
|
||||
telegram: '',
|
||||
});
|
||||
|
||||
await socialInformationRepository.save(social);
|
||||
};
|
||||
|
||||
return social;
|
||||
|
||||
@@ -3,7 +3,7 @@ import { getRepository } from 'typeorm';
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Social from '../models/Social';
|
||||
import Social from '../models/SocialInformation';
|
||||
|
||||
interface Request {
|
||||
id_user: string;
|
||||
|
||||
@@ -4,7 +4,7 @@ import { hash, compare } from 'bcryptjs';
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Social from '../models/Social';
|
||||
import Social from '../models/SocialInformation';
|
||||
|
||||
interface Request {
|
||||
id_user: string;
|
||||
|
||||
@@ -3,7 +3,7 @@ import { getRepository } from 'typeorm';
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Social from '../models/Social';
|
||||
import Social from '../models/SocialInformation';
|
||||
|
||||
interface Request {
|
||||
id_user: string;
|
||||
@@ -13,10 +13,12 @@ interface Request {
|
||||
bio: string;
|
||||
email: string;
|
||||
birth_date: string;
|
||||
cpf: string;
|
||||
cnpj: string;
|
||||
}
|
||||
|
||||
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 socialRepository = getRepository(Social);
|
||||
|
||||
@@ -28,10 +30,13 @@ class UpdateUserService {
|
||||
throw new AppError('User does not exist.');
|
||||
};
|
||||
|
||||
user.name = name
|
||||
user.lastname = lastname
|
||||
user.bio = bio
|
||||
user.email = email
|
||||
if (name) user.name = name
|
||||
if (lastname) user.lastname = lastname
|
||||
if (bio) user.bio = bio
|
||||
if (email) user.email = email
|
||||
|
||||
if (cpf) user.cpf = cpf
|
||||
if (cnpj) user.cnpj = cnpj
|
||||
|
||||
// user.birth_date = new Date(birth_date); // TODO, funciona?
|
||||
|
||||
|
||||
@@ -3,54 +3,51 @@ import { getRepository } from 'typeorm';
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Social from '../models/Social';
|
||||
import Social from '../models/SocialInformation';
|
||||
|
||||
interface Request {
|
||||
id_user: string;
|
||||
social_network: string;
|
||||
social_network: {
|
||||
phone: string;
|
||||
whatsapp: string;
|
||||
facebook: string;
|
||||
telegram: string;
|
||||
};
|
||||
}
|
||||
|
||||
class UpdateUserSocialService {
|
||||
public async execute({
|
||||
id_user,
|
||||
social_network,
|
||||
}: Request): Promise<Social> {
|
||||
public async execute({ id_user, social_network }: Request): Promise<Social> {
|
||||
const usersRepository = getRepository(User);
|
||||
const socialRepository = getRepository(Social);
|
||||
const socialInformationRepository = getRepository(Social);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user: id_user },
|
||||
where: { id_user },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
}
|
||||
|
||||
const social = await socialRepository.findOne({
|
||||
where: { user: user },
|
||||
let social = await socialInformationRepository.findOne({
|
||||
where: { user },
|
||||
});
|
||||
|
||||
if (!social) throw new AppError('User does not exist.');
|
||||
|
||||
switch (social_network) {
|
||||
case 'telegram':
|
||||
social.telegram = "";
|
||||
break;
|
||||
case 'facebook':
|
||||
social.facebook = "";
|
||||
break;
|
||||
case 'twitter':
|
||||
social.twitter = "";
|
||||
break;
|
||||
case 'twitch':
|
||||
social.twitch = "";
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
if (!social) {
|
||||
social = socialInformationRepository.create({
|
||||
user,
|
||||
phone: '',
|
||||
whatsapp: '',
|
||||
facebook: '',
|
||||
telegram: '',
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user