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

@@ -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;
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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?

View File

@@ -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;
}