Atualizando backend de usuários para conter informações de contato
This commit is contained in:
@@ -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