diff --git a/src/database/migrations/1617210132141-CreateUsers.ts b/src/database/migrations/1617210132141-CreateUsers.ts index c810619..64d8b16 100644 --- a/src/database/migrations/1617210132141-CreateUsers.ts +++ b/src/database/migrations/1617210132141-CreateUsers.ts @@ -1,7 +1,6 @@ import { MigrationInterface, QueryRunner, Table } from "typeorm"; export class CreateUsers1617210132141 implements MigrationInterface { - public async up(queryRunner: QueryRunner): Promise { await queryRunner.createTable( new Table({ diff --git a/src/database/migrations/1654814986232-AddCpfAndCnpjFieldToUsers.ts b/src/database/migrations/1654814986232-AddCpfAndCnpjFieldToUsers.ts new file mode 100644 index 0000000..7dbbb73 --- /dev/null +++ b/src/database/migrations/1654814986232-AddCpfAndCnpjFieldToUsers.ts @@ -0,0 +1,24 @@ +import { MigrationInterface, QueryRunner, TableColumn } from 'typeorm'; + +export class AddCpfAndCnpjFieldToUsers1654814986232 + implements MigrationInterface +{ + public async up(queryRunner: QueryRunner): Promise { + 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 { + await queryRunner.dropColumns('users', ['cnpj', 'cpf']); + } +} diff --git a/src/models/Social.ts b/src/models/SocialInformation.ts similarity index 89% rename from src/models/Social.ts rename to src/models/SocialInformation.ts index 068a274..98e4fe2 100644 --- a/src/models/Social.ts +++ b/src/models/SocialInformation.ts @@ -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; diff --git a/src/models/User.ts b/src/models/User.ts index 5c517e3..99ee735 100644 --- a/src/models/User.ts +++ b/src/models/User.ts @@ -38,6 +38,12 @@ class User { @Column() star_rating: number; + @Column() + cpf: string; + + @Column() + cnpj: string; + @CreateDateColumn() created_at: Date; diff --git a/src/routes/users.routes.ts b/src/routes/users.routes.ts index 4d0505e..cb65bec 100644 --- a/src/routes/users.routes.ts +++ b/src/routes/users.routes.ts @@ -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; diff --git a/src/services/CreateUserService.ts b/src/services/CreateUserService.ts index 2df4325..9ba7ad9 100644 --- a/src/services/CreateUserService.ts +++ b/src/services/CreateUserService.ts @@ -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 { 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; } diff --git a/src/services/FindUserSocialService.ts b/src/services/FindUserSocialService.ts index 1f06fc0..9fe7079 100644 --- a/src/services/FindUserSocialService.ts +++ b/src/services/FindUserSocialService.ts @@ -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 { 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; diff --git a/src/services/UpdateUserAvatarService.ts b/src/services/UpdateUserAvatarService.ts index b0fd08c..b41b768 100644 --- a/src/services/UpdateUserAvatarService.ts +++ b/src/services/UpdateUserAvatarService.ts @@ -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; diff --git a/src/services/UpdateUserPasswordService.ts b/src/services/UpdateUserPasswordService.ts index b515a21..72db6ec 100644 --- a/src/services/UpdateUserPasswordService.ts +++ b/src/services/UpdateUserPasswordService.ts @@ -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; diff --git a/src/services/UpdateUserService.ts b/src/services/UpdateUserService.ts index 71968e0..35f8d40 100644 --- a/src/services/UpdateUserService.ts +++ b/src/services/UpdateUserService.ts @@ -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 { + public async execute({ id_user, name, lastname, username, bio, email, birth_date, cpf, cnpj }: Request): Promise { 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? diff --git a/src/services/UpdateUserSocialService.ts b/src/services/UpdateUserSocialService.ts index bf8587d..ee7ec8f 100644 --- a/src/services/UpdateUserSocialService.ts +++ b/src/services/UpdateUserSocialService.ts @@ -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 { + public async execute({ id_user, social_network }: Request): Promise { 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; }