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