Create user
This commit is contained in:
48
src/services/AddItemService.ts
Normal file
48
src/services/AddItemService.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import Inventario from '../models/Inventario';
|
||||
import User from '../models/User';
|
||||
import Item from '../models/Item';
|
||||
|
||||
interface Request {
|
||||
id_item: string;
|
||||
id_user: string;
|
||||
}
|
||||
|
||||
class AddItemService {
|
||||
public async execute({
|
||||
id_item,
|
||||
id_user
|
||||
}: Request): Promise<Inventario> {
|
||||
const inventarioRepository = getRepository(Inventario);
|
||||
const usersRepository = getRepository(User);
|
||||
const itemsRepository = getRepository(Item);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
}
|
||||
|
||||
const item = await itemsRepository.findOne({
|
||||
where: { id_item },
|
||||
});
|
||||
|
||||
if (!item) {
|
||||
throw new AppError('User does not exist.');
|
||||
}
|
||||
|
||||
const inventario = inventarioRepository.create({
|
||||
item, user
|
||||
});
|
||||
|
||||
await inventarioRepository.save(inventario);
|
||||
|
||||
return inventario;
|
||||
}
|
||||
}
|
||||
|
||||
export default AddItemService;
|
||||
58
src/services/AuthenticateUserService.ts
Normal file
58
src/services/AuthenticateUserService.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
import { compare } from 'bcryptjs';
|
||||
import { sign } from 'jsonwebtoken';
|
||||
import authConfig from '../config/auth';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
|
||||
interface Request {
|
||||
login: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
interface Response {
|
||||
token: string;
|
||||
id_user: string;
|
||||
}
|
||||
|
||||
class AuthenticateUserService {
|
||||
public async execute({ login, password }: Request): Promise<Response> {
|
||||
const usersRepository = getRepository(User);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: [{email: login }],
|
||||
});
|
||||
|
||||
// TODO, ajeitar todos os HTTP status code
|
||||
// Por que tem que deixar 200 para funcionar?
|
||||
if (!user) {
|
||||
throw new AppError('Incorrect login/password combination.', 200);
|
||||
}
|
||||
|
||||
// user.password -> senha criptografada
|
||||
// password -> senha não-criptografada
|
||||
|
||||
const passwordMatched = await compare(password, user.password);
|
||||
|
||||
if (!passwordMatched) {
|
||||
throw new AppError('Incorrect login/password combination.', 200);
|
||||
}
|
||||
|
||||
// usuário autenticado
|
||||
|
||||
const { secret, expiresIn } = authConfig.jwt;
|
||||
|
||||
const token = sign({}, secret, {
|
||||
subject: user.id_user,
|
||||
expiresIn,
|
||||
// pensar na questão "experiência de usuário X segurança"
|
||||
// estratégias de refresh token
|
||||
});
|
||||
|
||||
return { token, id_user: user.id_user };
|
||||
}
|
||||
}
|
||||
|
||||
export default AuthenticateUserService;
|
||||
48
src/services/CheckUserIsFollowingUserService.ts
Normal file
48
src/services/CheckUserIsFollowingUserService.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Follow from '../models/Follow';
|
||||
|
||||
interface Request {
|
||||
id_user_following: string;
|
||||
id_user_followed: string;
|
||||
}
|
||||
|
||||
class CheckUserIsFollowingUserService {
|
||||
public async execute({
|
||||
id_user_following,
|
||||
id_user_followed,
|
||||
}: Request): Promise<boolean> {
|
||||
const usersRepository = getRepository(User);
|
||||
const followsRepository = getRepository(Follow);
|
||||
|
||||
const user_following = await usersRepository.findOne({
|
||||
where: { id_user: id_user_following },
|
||||
});
|
||||
|
||||
if (!user_following) {
|
||||
throw new AppError('User 1 does not exist.');
|
||||
}
|
||||
|
||||
const user_followed = await usersRepository.findOne({
|
||||
where: { id_user: id_user_followed },
|
||||
});
|
||||
|
||||
if (!user_followed) {
|
||||
throw new AppError('User 2 does not exist.');
|
||||
}
|
||||
|
||||
const follow = await followsRepository.findOne({
|
||||
where: { user_following, user_followed },
|
||||
});
|
||||
|
||||
if (!follow) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
export default CheckUserIsFollowingUserService;
|
||||
51
src/services/CheckUserIsKickedFromTournamentService.ts
Normal file
51
src/services/CheckUserIsKickedFromTournamentService.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Tournament from '../models/Tournament';
|
||||
import TournamentParticipants from '../models/TournamentParticipant';
|
||||
|
||||
interface Request {
|
||||
id_user: string;
|
||||
id_tournament: string;
|
||||
}
|
||||
|
||||
class CheckUserIsKickedFromTournamentService {
|
||||
public async execute({ id_user, id_tournament }: Request): Promise<boolean> {
|
||||
const usersRepository = getRepository(User);
|
||||
const tournamentsRepository = getRepository(Tournament);
|
||||
const TournamentsParticipantsRepository = getRepository(
|
||||
TournamentParticipants,
|
||||
);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
}
|
||||
|
||||
const tournament = await tournamentsRepository.findOne({
|
||||
where: { id_tournament },
|
||||
});
|
||||
|
||||
if (!tournament) {
|
||||
throw new AppError('Tournament does not exist.');
|
||||
}
|
||||
|
||||
const userInTournamentParticipants = await TournamentsParticipantsRepository.findOne(
|
||||
{
|
||||
where: { user, tournament },
|
||||
},
|
||||
);
|
||||
|
||||
if (!userInTournamentParticipants) {
|
||||
throw new AppError('User is not in this tournament.');
|
||||
}
|
||||
|
||||
return userInTournamentParticipants.user_kicked;
|
||||
}
|
||||
}
|
||||
|
||||
export default CheckUserIsKickedFromTournamentService;
|
||||
59
src/services/CreateFollowService.ts
Normal file
59
src/services/CreateFollowService.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Follow from '../models/Follow';
|
||||
import CheckUserIsFollowingUserService from './CheckUserIsFollowingUserService';
|
||||
|
||||
interface Request {
|
||||
id_user_following: string;
|
||||
id_user_followed: string;
|
||||
}
|
||||
|
||||
class CreateFollowService {
|
||||
public async execute({
|
||||
id_user_following,
|
||||
id_user_followed,
|
||||
}: Request): Promise<Follow> {
|
||||
const usersRepository = getRepository(User);
|
||||
const followsRepository = getRepository(Follow);
|
||||
|
||||
const user_following = await usersRepository.findOne({
|
||||
where: { id_user: id_user_following },
|
||||
});
|
||||
|
||||
if (!user_following) {
|
||||
throw new AppError('User 1 does not exist.');
|
||||
}
|
||||
|
||||
const user_followed = await usersRepository.findOne({
|
||||
where: { id_user: id_user_followed },
|
||||
});
|
||||
|
||||
if (!user_followed) {
|
||||
throw new AppError('User 2 does not exist.');
|
||||
}
|
||||
|
||||
const checkUserIsFollowingUserService = new CheckUserIsFollowingUserService();
|
||||
|
||||
const userIsFollowing = await checkUserIsFollowingUserService.execute({
|
||||
id_user_following,
|
||||
id_user_followed,
|
||||
});
|
||||
|
||||
if (userIsFollowing) {
|
||||
throw new AppError('You are already following this user.');
|
||||
}
|
||||
|
||||
const follow = followsRepository.create({
|
||||
user_following,
|
||||
user_followed,
|
||||
});
|
||||
|
||||
await followsRepository.save(follow);
|
||||
|
||||
return follow;
|
||||
}
|
||||
}
|
||||
|
||||
export default CreateFollowService;
|
||||
35
src/services/CreateItemService.ts
Normal file
35
src/services/CreateItemService.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import Item from '../models/Item';
|
||||
|
||||
interface Request {
|
||||
nome: string;
|
||||
tipo: string;
|
||||
asset: string;
|
||||
preco: number;
|
||||
}
|
||||
|
||||
class CreateItemService {
|
||||
public async execute({
|
||||
nome,
|
||||
tipo,
|
||||
asset,
|
||||
preco
|
||||
}: Request): Promise<Item> {
|
||||
const itemsRepository = getRepository(Item);
|
||||
|
||||
const item = itemsRepository.create({
|
||||
nome,
|
||||
tipo,
|
||||
asset,
|
||||
preco
|
||||
});
|
||||
|
||||
await itemsRepository.save(item);
|
||||
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
export default CreateItemService;
|
||||
48
src/services/CreatePublicationService.ts
Normal file
48
src/services/CreatePublicationService.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Publication from '../models/Publication';
|
||||
import Tournament from '../models/Tournament';
|
||||
|
||||
interface Request {
|
||||
id_user: string;
|
||||
id_tournament: string;
|
||||
}
|
||||
|
||||
class CreatePublicationService {
|
||||
public async execute({
|
||||
id_user,
|
||||
id_tournament
|
||||
}: Request): Promise<Publication> {
|
||||
const pubsRepository = getRepository(Publication);
|
||||
const usersRepository = getRepository(User);
|
||||
const tournamentsRepository = getRepository(Tournament);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
}
|
||||
|
||||
const tournament = await tournamentsRepository.findOne({
|
||||
where: { id_tournament },
|
||||
});
|
||||
|
||||
if (!tournament) {
|
||||
throw new AppError('Tournament does not exist.');
|
||||
}
|
||||
|
||||
const publication = pubsRepository.create({
|
||||
tournament
|
||||
});
|
||||
|
||||
await pubsRepository.save(publication);
|
||||
|
||||
return publication;
|
||||
}
|
||||
}
|
||||
|
||||
export default CreatePublicationService;
|
||||
70
src/services/CreateTournamentParticipantService.ts
Normal file
70
src/services/CreateTournamentParticipantService.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Tournament from '../models/Tournament';
|
||||
import TournamentParticipant from '../models/TournamentParticipant';
|
||||
import { request } from 'express';
|
||||
|
||||
interface Request {
|
||||
id_tournament: string;
|
||||
id_user: string;
|
||||
creator_id_user: string;
|
||||
}
|
||||
|
||||
class CreateTournamentParticipantService {
|
||||
public async execute({
|
||||
id_tournament,
|
||||
id_user,
|
||||
creator_id_user,
|
||||
}: Request): Promise<TournamentParticipant> {
|
||||
const usersRepository = getRepository(User);
|
||||
const tournamentsRepository = getRepository(Tournament);
|
||||
const tournamentsParticipantRepository = getRepository(
|
||||
TournamentParticipant,
|
||||
);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
}
|
||||
|
||||
if (creator_id_user == user.id_user) {
|
||||
throw new AppError("You can't invite yourself.", 200);
|
||||
}
|
||||
|
||||
const tournament = await tournamentsRepository.findOne({
|
||||
where: { id_tournament },
|
||||
});
|
||||
|
||||
if (!tournament) {
|
||||
throw new AppError('Tournament does not exist.');
|
||||
}
|
||||
|
||||
const userIsAlreadyInvited = await tournamentsParticipantRepository.findOne(
|
||||
{
|
||||
where: { user, tournament },
|
||||
},
|
||||
);
|
||||
|
||||
if (userIsAlreadyInvited) {
|
||||
throw new AppError('User is already invited.', 200);
|
||||
}
|
||||
|
||||
const tournamentParticipant = tournamentsParticipantRepository.create({
|
||||
tournament,
|
||||
user,
|
||||
user_accepted_invite: false,
|
||||
});
|
||||
|
||||
await tournamentsParticipantRepository.save(tournamentParticipant);
|
||||
|
||||
return tournamentParticipant;
|
||||
}
|
||||
}
|
||||
|
||||
export default CreateTournamentParticipantService;
|
||||
60
src/services/CreateTournamentService.ts
Normal file
60
src/services/CreateTournamentService.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import Tournament from '../models/Tournament';
|
||||
import TournamentColumns from '../models/TournamentColumns';
|
||||
import User from '../models/User';
|
||||
|
||||
interface Request {
|
||||
id_user: string;
|
||||
name: string;
|
||||
game: string;
|
||||
description: string;
|
||||
password?: string;
|
||||
number_participants: number;
|
||||
}
|
||||
|
||||
class CreateTournamentService {
|
||||
public async execute({
|
||||
id_user,
|
||||
name,
|
||||
game,
|
||||
description,
|
||||
password,
|
||||
number_participants,
|
||||
}: Request): Promise<Tournament> {
|
||||
const tournamentsRepository = getRepository(Tournament);
|
||||
const usersRepository = getRepository(User);
|
||||
const tournamentColumnsRepository = getRepository(TournamentColumns);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
}
|
||||
|
||||
const tournament = tournamentsRepository.create({
|
||||
user,
|
||||
name,
|
||||
game,
|
||||
description,
|
||||
password,
|
||||
number_participants,
|
||||
});
|
||||
|
||||
await tournamentsRepository.save(tournament);
|
||||
|
||||
// já cria o registro em tournamentColumns para evitar inconsistências
|
||||
// igual foi em Users para Socials
|
||||
const tournamentColumn = tournamentColumnsRepository.create({
|
||||
tournament,
|
||||
});
|
||||
await tournamentColumnsRepository.save(tournamentColumn);
|
||||
|
||||
return tournament;
|
||||
}
|
||||
}
|
||||
|
||||
export default CreateTournamentService;
|
||||
48
src/services/CreateUserService.ts
Normal file
48
src/services/CreateUserService.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
import { v4 } from 'uuid';
|
||||
import { hash } from 'bcryptjs';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Social from '../models/Social';
|
||||
|
||||
interface Request {
|
||||
name: string;
|
||||
email: string;
|
||||
birth_date: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
class CreateUserService {
|
||||
public async execute({ name, email, birth_date, password }: Request): Promise<User> {
|
||||
const usersRepository = getRepository(User);
|
||||
const socialsRepository = getRepository(Social);
|
||||
|
||||
const checkUserEmailExists = await usersRepository.findOne({
|
||||
where: { email },
|
||||
});
|
||||
|
||||
if (checkUserEmailExists) {
|
||||
throw new AppError('Email address is already used.', 200);
|
||||
}
|
||||
|
||||
const hashedPassword = await hash(password, 8);
|
||||
|
||||
// TODO, arrumar regras de negócio para avatar_image e background_image
|
||||
// TODO, arrumar o formato das datas e padronizar com a equipe
|
||||
|
||||
const user = usersRepository.create({
|
||||
id_user: v4(), name, email, birth_date, password: hashedPassword, avatar_image: "", bio: ""
|
||||
});
|
||||
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);
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
export default CreateUserService;
|
||||
62
src/services/DeleteFollowService.ts
Normal file
62
src/services/DeleteFollowService.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Follow from '../models/Follow';
|
||||
import CheckUserIsFollowingUserService from './CheckUserIsFollowingUserService';
|
||||
|
||||
interface Request {
|
||||
id_user_following: string;
|
||||
id_user_followed: string;
|
||||
}
|
||||
|
||||
class DeleteFollowService {
|
||||
public async execute({
|
||||
id_user_following,
|
||||
id_user_followed,
|
||||
}: Request): Promise<boolean> {
|
||||
const usersRepository = getRepository(User);
|
||||
const followsRepository = getRepository(Follow);
|
||||
|
||||
const user_following = await usersRepository.findOne({
|
||||
where: { id_user: id_user_following },
|
||||
});
|
||||
|
||||
if (!user_following) {
|
||||
throw new AppError('User 1 does not exist.');
|
||||
}
|
||||
|
||||
const user_followed = await usersRepository.findOne({
|
||||
where: { id_user: id_user_followed },
|
||||
});
|
||||
|
||||
if (!user_followed) {
|
||||
throw new AppError('User 2 does not exist.');
|
||||
}
|
||||
|
||||
const checkUserIsFollowingUserService = new CheckUserIsFollowingUserService();
|
||||
|
||||
const userIsFollowing = await checkUserIsFollowingUserService.execute({
|
||||
id_user_following,
|
||||
id_user_followed,
|
||||
});
|
||||
|
||||
if (!userIsFollowing) {
|
||||
throw new AppError('You are not following this user.');
|
||||
}
|
||||
|
||||
const follow = await followsRepository.findOne({
|
||||
where: [{ user_following }, { user_followed }],
|
||||
});
|
||||
|
||||
if (!follow) {
|
||||
throw new AppError('User is not following.');
|
||||
}
|
||||
|
||||
await followsRepository.remove(follow);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
export default DeleteFollowService;
|
||||
56
src/services/FindAcceptedTournamentParticipantsService.ts
Normal file
56
src/services/FindAcceptedTournamentParticipantsService.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import Tournament from '../models/Tournament';
|
||||
// TODO, verificar pluralidade (tournamentParticipant (v, certo) != tournamentParticipants (x, errado))
|
||||
import TournamentParticipant from '../models/TournamentParticipant';
|
||||
import User from '../models/User';
|
||||
|
||||
class FindAcceptedTournamentParticipantsService {
|
||||
public async execute(id_tournament: string): Promise<User[]> {
|
||||
const tournamentsRepository = getRepository(Tournament);
|
||||
const tournamentParticipantsRepository = getRepository(
|
||||
TournamentParticipant,
|
||||
);
|
||||
|
||||
const tournament = await tournamentsRepository.findOne({
|
||||
where: { id_tournament },
|
||||
});
|
||||
|
||||
if (!tournament) {
|
||||
throw new AppError('Tournament does not exist.');
|
||||
}
|
||||
|
||||
const tournamentParticipants = await tournamentParticipantsRepository.find({
|
||||
where: { tournament },
|
||||
});
|
||||
|
||||
if (!tournamentParticipants) {
|
||||
throw new AppError('Tournament does not have participants yet.', 200);
|
||||
}
|
||||
|
||||
let tournamentAcceptedParticipantsAsUsers: User[] = [];
|
||||
|
||||
tournamentParticipants.map(tournamentParticipant => {
|
||||
if (tournamentParticipant.user_accepted_invite) {
|
||||
/* TODO, dar um jeito de filtrar as informações desnecessárias
|
||||
const userWithoutSensitiveInfo = {
|
||||
id_user: tournamentParticipant.user.id_user,
|
||||
name: tournamentParticipant.user.name,
|
||||
username: tournamentParticipant.user.username,
|
||||
};
|
||||
*/
|
||||
tournamentAcceptedParticipantsAsUsers.push(tournamentParticipant.user);
|
||||
}
|
||||
});
|
||||
|
||||
const newTournaments = tournamentParticipants.filter((element) => {
|
||||
if (!element.user_accepted_invite) return element;
|
||||
})
|
||||
|
||||
return tournamentAcceptedParticipantsAsUsers;
|
||||
}
|
||||
}
|
||||
|
||||
export default FindAcceptedTournamentParticipantsService;
|
||||
33
src/services/FindInventarioUserService.ts
Normal file
33
src/services/FindInventarioUserService.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { Equal, getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import Inventario from '../models/Inventario';
|
||||
import User from '../models/User';
|
||||
|
||||
class FindInventarioUserService {
|
||||
public async execute(id: string): Promise<Inventario[]> {
|
||||
const inventarioRepository = getRepository(Inventario);
|
||||
const usersRepository = getRepository(User);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user: id },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
}
|
||||
|
||||
const inventario = await inventarioRepository.find({
|
||||
where: {user: Equal (user.id_user) }
|
||||
});
|
||||
|
||||
if (!inventario) {
|
||||
throw new AppError('Tournament does not exist.');
|
||||
}
|
||||
|
||||
return inventario;
|
||||
}
|
||||
}
|
||||
|
||||
export default FindInventarioUserService;
|
||||
33
src/services/FindItensAtivosService.ts
Normal file
33
src/services/FindItensAtivosService.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { Equal, getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import Inventario from '../models/Inventario';
|
||||
import User from '../models/User';
|
||||
|
||||
class FindItensAtivosService {
|
||||
public async execute(id: string): Promise<Inventario[]> {
|
||||
const inventarioRepository = getRepository(Inventario);
|
||||
const usersRepository = getRepository(User);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user: id },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
}
|
||||
|
||||
const inventario = await inventarioRepository.find({
|
||||
where: {user: Equal (user.id_user), ativo: Equal(true) }
|
||||
});
|
||||
|
||||
if (!inventario) {
|
||||
throw new AppError('Tournament does not exist.');
|
||||
}
|
||||
|
||||
return inventario;
|
||||
}
|
||||
}
|
||||
|
||||
export default FindItensAtivosService;
|
||||
44
src/services/FindKickedParticipantsFromTournamentService.ts
Normal file
44
src/services/FindKickedParticipantsFromTournamentService.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import Tournament from '../models/Tournament';
|
||||
import TournamentParticipant from '../models/TournamentParticipant';
|
||||
import User from '../models/User';
|
||||
|
||||
class FindKickedParticipantsFromTournamentService {
|
||||
public async execute(id_tournament: string): Promise<User[]> {
|
||||
const tournamentsRepository = getRepository(Tournament);
|
||||
const TournamentParticipantsRepository = getRepository(
|
||||
TournamentParticipant,
|
||||
);
|
||||
|
||||
const tournament = await tournamentsRepository.findOne({
|
||||
where: { id_tournament },
|
||||
});
|
||||
|
||||
if (!tournament) {
|
||||
throw new AppError('Tournament does not exist.');
|
||||
}
|
||||
|
||||
const tournamentKickedParticipants = await TournamentParticipantsRepository.find(
|
||||
{
|
||||
where: { tournament, user_kicked: true },
|
||||
},
|
||||
);
|
||||
|
||||
if (!tournamentKickedParticipants) {
|
||||
throw new AppError('Tournament does not have participants.');
|
||||
}
|
||||
|
||||
let tournamentKickedParticipantsAsUsers: User[] = [];
|
||||
|
||||
tournamentKickedParticipants.map(element => {
|
||||
tournamentKickedParticipantsAsUsers.push(element.user);
|
||||
});
|
||||
|
||||
return tournamentKickedParticipantsAsUsers;
|
||||
}
|
||||
}
|
||||
|
||||
export default FindKickedParticipantsFromTournamentService;
|
||||
33
src/services/FindPendingTournamentInvitesService.ts
Normal file
33
src/services/FindPendingTournamentInvitesService.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import TournamentParticipants from '../models/TournamentParticipant';
|
||||
import TournamentParticipant from '../models/TournamentParticipant';
|
||||
import User from '../models/User';
|
||||
|
||||
class FindPendingTournamentInvitesService {
|
||||
public async execute(id_user: string): Promise<TournamentParticipants[]> {
|
||||
const usersRepository = getRepository(User);
|
||||
const tournamentParticipantsRepository = getRepository(
|
||||
TournamentParticipant,
|
||||
);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
}
|
||||
|
||||
// TODO, não está filtrando certo
|
||||
const tournamentParticipants = await tournamentParticipantsRepository.find({
|
||||
where: { user, invite_refused: false, user_accepted_invite: false },
|
||||
});
|
||||
|
||||
return tournamentParticipants;
|
||||
}
|
||||
}
|
||||
|
||||
export default FindPendingTournamentInvitesService;
|
||||
33
src/services/FindTournamentColumnsService.ts
Normal file
33
src/services/FindTournamentColumnsService.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import Tournament from '../models/Tournament';
|
||||
import TournamentColumns from '../models/TournamentColumns';
|
||||
|
||||
class FindTournamentColumnsService {
|
||||
public async execute(id_tournament: string): Promise<TournamentColumns> {
|
||||
const tournamentsRepository = getRepository(Tournament);
|
||||
const tournamentColumnsRepository = getRepository(TournamentColumns);
|
||||
|
||||
const tournament = await tournamentsRepository.findOne({
|
||||
where: { id_tournament },
|
||||
});
|
||||
|
||||
if (!tournament) {
|
||||
throw new AppError('Tournament does not exist.');
|
||||
}
|
||||
|
||||
const tournamentColumns = await tournamentColumnsRepository.findOne({
|
||||
where: { tournament },
|
||||
});
|
||||
|
||||
if (!tournamentColumns) {
|
||||
throw new AppError('Tournament does not have columns.');
|
||||
}
|
||||
|
||||
return tournamentColumns;
|
||||
}
|
||||
}
|
||||
|
||||
export default FindTournamentColumnsService;
|
||||
23
src/services/FindTournamentService.ts
Normal file
23
src/services/FindTournamentService.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import Tournament from '../models/Tournament';
|
||||
|
||||
class FindTournamentService {
|
||||
public async execute(id: string): Promise<Tournament> {
|
||||
const tournamentsRepository = getRepository(Tournament);
|
||||
|
||||
const tournament = await tournamentsRepository.findOne({
|
||||
where: { id_tournament: id },
|
||||
});
|
||||
|
||||
if (!tournament) {
|
||||
throw new AppError('Tournament does not exist.');
|
||||
}
|
||||
|
||||
return tournament;
|
||||
}
|
||||
}
|
||||
|
||||
export default FindTournamentService;
|
||||
29
src/services/FindTournamentsByUserService.ts
Normal file
29
src/services/FindTournamentsByUserService.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import Tournament from '../models/Tournament';
|
||||
import User from '../models/User';
|
||||
|
||||
class FindTournamentsByUserService {
|
||||
public async execute(id: string): Promise<Tournament[]> {
|
||||
const tournamentsRepository = getRepository(Tournament);
|
||||
const usersRepository = getRepository(User);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user: id },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
}
|
||||
|
||||
const tournaments = await tournamentsRepository.find({
|
||||
where: { user }
|
||||
})
|
||||
|
||||
return tournaments;
|
||||
}
|
||||
}
|
||||
|
||||
export default FindTournamentsByUserService;
|
||||
33
src/services/FindTournamentsPublicatedService.ts
Normal file
33
src/services/FindTournamentsPublicatedService.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { getRepository, Not } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import Tournament from '../models/Tournament';
|
||||
import User from '../models/User';
|
||||
|
||||
class FindTournamentService {
|
||||
public async execute(id: string): Promise<Tournament[]> {
|
||||
const tournamentsRepository = getRepository(Tournament);
|
||||
const usersRepository = getRepository(User);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user: id },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
}
|
||||
|
||||
const tournaments = await tournamentsRepository.find({
|
||||
where: {user: Not (user.id_user) }
|
||||
});
|
||||
|
||||
if (!tournaments) {
|
||||
throw new AppError('Tournament does not exist.');
|
||||
}
|
||||
|
||||
return tournaments;
|
||||
}
|
||||
}
|
||||
|
||||
export default FindTournamentService;
|
||||
35
src/services/FindTournamentsUserIsParticipatingService.ts
Normal file
35
src/services/FindTournamentsUserIsParticipatingService.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import TournamentParticipant from '../models/TournamentParticipant';
|
||||
import Tournament from '../models/Tournament';
|
||||
|
||||
class FindTournamentsUserIsParticipatingService {
|
||||
public async execute(id_user: string): Promise<TournamentParticipant[]> {
|
||||
const tournamentParticipantRepository = getRepository(
|
||||
TournamentParticipant,
|
||||
);
|
||||
const usersRepository = getRepository(User);
|
||||
const tournamentsRepository = getRepository(Tournament);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
}
|
||||
|
||||
const acceptedTournamentsInvitesOfUser = await tournamentParticipantRepository.find(
|
||||
{
|
||||
where: { user, user_accepted_invite: true },
|
||||
},
|
||||
);
|
||||
|
||||
return acceptedTournamentsInvitesOfUser;
|
||||
}
|
||||
}
|
||||
|
||||
export default FindTournamentsUserIsParticipatingService;
|
||||
113
src/services/FindUserColocationsService.ts
Normal file
113
src/services/FindUserColocationsService.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import TournamentParticipant from '../models/TournamentParticipant';
|
||||
import TournamentColumns from '../models/TournamentColumns';
|
||||
import Tournament from '../models/Tournament';
|
||||
|
||||
interface Colocations {
|
||||
tournament_id: string;
|
||||
tournament_name: string;
|
||||
tournament_game: string;
|
||||
tournament_description: string;
|
||||
colocation: 'Participante' | 'Semifinalista' | 'Vencedor';
|
||||
}
|
||||
|
||||
class FindUserColocationsService {
|
||||
public async execute(id_user: string): Promise<Colocations[]> {
|
||||
const tournamentParticipantRepository = getRepository(
|
||||
TournamentParticipant,
|
||||
);
|
||||
const usersRepository = getRepository(User);
|
||||
const tournamentColumnsRepository = getRepository(TournamentColumns);
|
||||
const tournamentsRepository = getRepository(Tournament);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
}
|
||||
|
||||
const tournamentsUserIsParticipating = await tournamentParticipantRepository.find(
|
||||
{
|
||||
where: { user, user_accepted_invite: true },
|
||||
},
|
||||
);
|
||||
|
||||
if (tournamentsUserIsParticipating.length == 0) {
|
||||
throw new AppError('User is not in any tournament.', 200);
|
||||
}
|
||||
|
||||
const tournamentsUserIsParticipatingFiltered = tournamentsUserIsParticipating.map(
|
||||
element => {
|
||||
return element.tournament;
|
||||
},
|
||||
);
|
||||
|
||||
let endedTournamentsUserIsParticipating: TournamentColumns[][] = [];
|
||||
|
||||
const promises1 = tournamentsUserIsParticipatingFiltered.map(
|
||||
async tournament => {
|
||||
return await tournamentColumnsRepository.find({
|
||||
where: { tournament, tournament_ended: true },
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
endedTournamentsUserIsParticipating = await Promise.all(promises1);
|
||||
|
||||
if (endedTournamentsUserIsParticipating.length == 0) {
|
||||
throw new AppError('User is not in any tournament that has ended.', 200);
|
||||
}
|
||||
|
||||
let userColocations: Colocations[] = [];
|
||||
|
||||
endedTournamentsUserIsParticipating.map(tournamentColumns => {
|
||||
tournamentColumns.map(tournamentColumns2 => {
|
||||
if (tournamentColumns2.column3.includes(user.name)) {
|
||||
userColocations.push({
|
||||
tournament_id: tournamentColumns2.tournament.id_tournament,
|
||||
tournament_name: tournamentColumns2.tournament.name,
|
||||
tournament_game: tournamentColumns2.tournament.game,
|
||||
tournament_description: tournamentColumns2.tournament.description,
|
||||
colocation: 'Vencedor',
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (tournamentColumns2.column2.includes(user.name)) {
|
||||
userColocations.push({
|
||||
tournament_id: tournamentColumns2.tournament.id_tournament,
|
||||
tournament_name: tournamentColumns2.tournament.name,
|
||||
tournament_game: tournamentColumns2.tournament.game,
|
||||
tournament_description: tournamentColumns2.tournament.description,
|
||||
colocation: 'Semifinalista',
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (tournamentColumns2.column1.includes(user.name)) {
|
||||
userColocations.push({
|
||||
tournament_id: tournamentColumns2.tournament.id_tournament,
|
||||
tournament_name: tournamentColumns2.tournament.name,
|
||||
tournament_game: tournamentColumns2.tournament.game,
|
||||
tournament_description: tournamentColumns2.tournament.description,
|
||||
colocation: 'Participante',
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return userColocations;
|
||||
}
|
||||
}
|
||||
|
||||
export default FindUserColocationsService;
|
||||
29
src/services/FindUserFollowersService.ts
Normal file
29
src/services/FindUserFollowersService.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import Follow from '../models/Follow';
|
||||
import User from '../models/User';
|
||||
|
||||
class FindUserFollowersService {
|
||||
public async execute(id_user: string): Promise<Follow[]> {
|
||||
const usersRepository = getRepository(User);
|
||||
const followRepository = getRepository(Follow);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
}
|
||||
|
||||
const followers = await followRepository.find({
|
||||
where: { user_followed: user },
|
||||
});
|
||||
|
||||
return followers;
|
||||
}
|
||||
}
|
||||
|
||||
export default FindUserFollowersService;
|
||||
23
src/services/FindUserService.ts
Normal file
23
src/services/FindUserService.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
|
||||
class CreateUserService {
|
||||
public async execute(id: string): Promise<User> {
|
||||
const usersRepository = getRepository(User);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user: id },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
export default CreateUserService;
|
||||
36
src/services/FindUserSocialService.ts
Normal file
36
src/services/FindUserSocialService.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Social from '../models/Social';
|
||||
|
||||
class FindUserSocialService {
|
||||
public async execute(id_user: string): Promise<Social> {
|
||||
const usersRepository = getRepository(User);
|
||||
const socialRepository = 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({
|
||||
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);
|
||||
};
|
||||
|
||||
return social;
|
||||
}
|
||||
}
|
||||
|
||||
export default FindUserSocialService;
|
||||
56
src/services/KickTournamentParticipantService.ts
Normal file
56
src/services/KickTournamentParticipantService.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Tournament from '../models/Tournament';
|
||||
import TournamentParticipant from '../models/TournamentParticipant';
|
||||
|
||||
interface Request {
|
||||
id_user: string;
|
||||
id_tournament: string;
|
||||
}
|
||||
|
||||
class KickTournamentParticipantService {
|
||||
public async execute({ id_user, id_tournament }: Request): Promise<boolean> {
|
||||
const usersRepository = getRepository(User);
|
||||
const tournamentsRepository = getRepository(Tournament);
|
||||
const tournamentParticipantsRepository = getRepository(
|
||||
TournamentParticipant,
|
||||
);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
}
|
||||
|
||||
const tournament = await tournamentsRepository.findOne({
|
||||
where: { id_tournament },
|
||||
});
|
||||
|
||||
if (!tournament) {
|
||||
throw new AppError('Tournament does not exist.');
|
||||
}
|
||||
|
||||
const tournamentParticipants = await tournamentParticipantsRepository.findOne(
|
||||
{
|
||||
where: { tournament, user },
|
||||
},
|
||||
);
|
||||
|
||||
if (!tournamentParticipants) {
|
||||
throw new AppError('User is not invited to this tournament.');
|
||||
}
|
||||
|
||||
tournamentParticipants.user_kicked = true;
|
||||
|
||||
await tournamentParticipantsRepository.save(tournamentParticipants);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
export default KickTournamentParticipantService;
|
||||
52
src/services/UpdateAtivaItemService.ts
Normal file
52
src/services/UpdateAtivaItemService.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Inventario from '../models/Inventario';
|
||||
import Item from '../models/Item';
|
||||
|
||||
interface Request {
|
||||
id_user: string;
|
||||
id_item: number;
|
||||
}
|
||||
|
||||
class UpdateAtivaItemService {
|
||||
public async execute({ id_user, id_item }: Request): Promise<Inventario> {
|
||||
const usersRepository = getRepository(User);
|
||||
const inventarioRepository = getRepository(Inventario);
|
||||
const ItemRepository = getRepository(Item);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user: id_user }
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
};
|
||||
|
||||
const item = await ItemRepository.findOne({
|
||||
where: { id_item: id_item}
|
||||
});
|
||||
|
||||
if (!item) {
|
||||
throw new AppError('Item does not exist.');
|
||||
};
|
||||
|
||||
const inventario = await inventarioRepository.findOne({
|
||||
where: { item: item, user: user}
|
||||
});
|
||||
|
||||
if (!inventario) {
|
||||
throw new AppError('Inventario não contém esse item.');
|
||||
};
|
||||
|
||||
inventario.ativo = true;
|
||||
|
||||
await inventarioRepository.save(inventario);
|
||||
|
||||
return inventario;
|
||||
}
|
||||
}
|
||||
|
||||
export default UpdateAtivaItemService;
|
||||
52
src/services/UpdateDesativaItemService.ts
Normal file
52
src/services/UpdateDesativaItemService.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Inventario from '../models/Inventario';
|
||||
import Item from '../models/Item';
|
||||
|
||||
interface Request {
|
||||
id_user: string;
|
||||
id_item: number;
|
||||
}
|
||||
|
||||
class UpdateDesativaItemService {
|
||||
public async execute({ id_user, id_item }: Request): Promise<Inventario> {
|
||||
const usersRepository = getRepository(User);
|
||||
const inventarioRepository = getRepository(Inventario);
|
||||
const ItemRepository = getRepository(Item);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user: id_user }
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
};
|
||||
|
||||
const item = await ItemRepository.findOne({
|
||||
where: { id_item: id_item}
|
||||
});
|
||||
|
||||
if (!item) {
|
||||
throw new AppError('Item does not exist.');
|
||||
};
|
||||
|
||||
const inventario = await inventarioRepository.findOne({
|
||||
where: { item: item, user: user}
|
||||
});
|
||||
|
||||
if (!inventario) {
|
||||
throw new AppError('Inventario não contém esse item.');
|
||||
};
|
||||
|
||||
inventario.ativo = false;
|
||||
|
||||
await inventarioRepository.save(inventario);
|
||||
|
||||
return inventario;
|
||||
}
|
||||
}
|
||||
|
||||
export default UpdateDesativaItemService;
|
||||
37
src/services/UpdateFollowCounterService.ts
Normal file
37
src/services/UpdateFollowCounterService.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import Follow from '../models/Follow';
|
||||
import User from '../models/User';
|
||||
|
||||
interface Request {
|
||||
id_user: string;
|
||||
}
|
||||
|
||||
class UpdateFollowCounterService {
|
||||
public async execute({ id_user }: Request): Promise<User> {
|
||||
const followRepository = getRepository(Follow);
|
||||
const usersRepository = getRepository(User);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
}
|
||||
|
||||
const followCount = await followRepository.count({
|
||||
where: { user_followed: user },
|
||||
});
|
||||
|
||||
user.followers = followCount.toString();
|
||||
|
||||
await usersRepository.save(user);
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
export default UpdateFollowCounterService;
|
||||
41
src/services/UpdateTournamentAsEnded.ts
Normal file
41
src/services/UpdateTournamentAsEnded.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import Tournament from '../models/Tournament';
|
||||
import TournamentColumns from '../models/TournamentColumns';
|
||||
|
||||
interface Request {
|
||||
id_tournament: string;
|
||||
}
|
||||
|
||||
class UpdateTournamentAsEndedService {
|
||||
public async execute({ id_tournament }: Request): Promise<Tournament> {
|
||||
const tournamentsRepository = getRepository(Tournament);
|
||||
const tournamentsColumnsRepository = getRepository(TournamentColumns);
|
||||
|
||||
const tournament = await tournamentsRepository.findOne({
|
||||
where: { id_tournament },
|
||||
});
|
||||
|
||||
if (!tournament) {
|
||||
throw new AppError('Tournament does not exist.');
|
||||
}
|
||||
|
||||
const tournamentColumns = await tournamentsColumnsRepository.findOne({
|
||||
where: { tournament },
|
||||
});
|
||||
|
||||
if (!tournamentColumns) {
|
||||
throw new AppError('Tournament does not have columns.');
|
||||
}
|
||||
|
||||
tournamentColumns.tournament_ended = true;
|
||||
|
||||
await tournamentsColumnsRepository.save(tournamentColumns);
|
||||
|
||||
return tournament;
|
||||
}
|
||||
}
|
||||
|
||||
export default UpdateTournamentAsEndedService;
|
||||
@@ -0,0 +1,48 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import Tournament from '../models/Tournament';
|
||||
import TournamentColumns from '../models/TournamentColumns';
|
||||
|
||||
interface Columns {
|
||||
column1: string;
|
||||
column2: string;
|
||||
column3: string;
|
||||
column4: string;
|
||||
}
|
||||
|
||||
class UpdateTournamentColumnsInitializedFlagService {
|
||||
public async execute(
|
||||
id_tournament: string,
|
||||
flag: string,
|
||||
): Promise<TournamentColumns> {
|
||||
const tournamentsRepository = getRepository(Tournament);
|
||||
const tournamentColumnsRepository = getRepository(TournamentColumns);
|
||||
|
||||
const tournament = await tournamentsRepository.findOne({
|
||||
where: { id_tournament },
|
||||
});
|
||||
|
||||
if (!tournament) {
|
||||
throw new AppError('Tournament does not exist.');
|
||||
}
|
||||
|
||||
const tournamentColumns = await tournamentColumnsRepository.findOne({
|
||||
where: { tournament },
|
||||
});
|
||||
|
||||
if (!tournamentColumns) {
|
||||
throw new AppError('Tournament does not have columns.');
|
||||
}
|
||||
|
||||
if (flag) tournamentColumns.tournament_initialized = true;
|
||||
else tournamentColumns.tournament_initialized = false;
|
||||
|
||||
await tournamentColumnsRepository.save(tournamentColumns);
|
||||
|
||||
return tournamentColumns;
|
||||
}
|
||||
}
|
||||
|
||||
export default UpdateTournamentColumnsInitializedFlagService;
|
||||
50
src/services/UpdateTournamentColumnsService.ts
Normal file
50
src/services/UpdateTournamentColumnsService.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import Tournament from '../models/Tournament';
|
||||
import TournamentColumns from '../models/TournamentColumns';
|
||||
|
||||
interface Columns {
|
||||
column1: string;
|
||||
column2: string;
|
||||
column3: string;
|
||||
column4: string;
|
||||
}
|
||||
|
||||
class UpdateTournamentColumnsService {
|
||||
public async execute(
|
||||
id_tournament: string,
|
||||
{ column1, column2, column3, column4 }: Columns,
|
||||
): Promise<TournamentColumns> {
|
||||
const tournamentsRepository = getRepository(Tournament);
|
||||
const tournamentColumnsRepository = getRepository(TournamentColumns);
|
||||
|
||||
const tournament = await tournamentsRepository.findOne({
|
||||
where: { id_tournament },
|
||||
});
|
||||
|
||||
if (!tournament) {
|
||||
throw new AppError('Tournament does not exist.');
|
||||
}
|
||||
|
||||
const tournamentColumns = await tournamentColumnsRepository.findOne({
|
||||
where: { tournament },
|
||||
});
|
||||
|
||||
if (!tournamentColumns) {
|
||||
throw new AppError('Tournament does not have columns.');
|
||||
}
|
||||
|
||||
tournamentColumns.column1 = column1;
|
||||
tournamentColumns.column2 = column2;
|
||||
tournamentColumns.column3 = column3;
|
||||
tournamentColumns.column4 = column4;
|
||||
|
||||
await tournamentColumnsRepository.save(tournamentColumns);
|
||||
|
||||
return tournamentColumns;
|
||||
}
|
||||
}
|
||||
|
||||
export default UpdateTournamentColumnsService;
|
||||
44
src/services/UpdateTournamentService.ts
Normal file
44
src/services/UpdateTournamentService.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import Tournament from '../models/Tournament';
|
||||
|
||||
interface Request {
|
||||
id: string;
|
||||
name: string;
|
||||
game: string;
|
||||
description: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
class UpdateTournamentService {
|
||||
public async execute({
|
||||
id,
|
||||
name,
|
||||
game,
|
||||
description,
|
||||
password,
|
||||
}: Request): Promise<Tournament> {
|
||||
const tournamentsRepository = getRepository(Tournament);
|
||||
|
||||
const tournament = await tournamentsRepository.findOne({
|
||||
where: { id_tournament: id },
|
||||
});
|
||||
|
||||
if (!tournament) {
|
||||
throw new AppError('Tournament does not exist.');
|
||||
}
|
||||
|
||||
tournament.name = name;
|
||||
tournament.game = game;
|
||||
tournament.description = description;
|
||||
if (!!password) tournament.password = password;
|
||||
|
||||
await tournamentsRepository.save(tournament);
|
||||
|
||||
return tournament;
|
||||
}
|
||||
}
|
||||
|
||||
export default UpdateTournamentService;
|
||||
57
src/services/UpdateTournamentsInviteUserAcceptedService.ts
Normal file
57
src/services/UpdateTournamentsInviteUserAcceptedService.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Tournament from '../models/Tournament';
|
||||
import TournamentParticipant from '../models/TournamentParticipant';
|
||||
|
||||
interface Request {
|
||||
id_tournament: string;
|
||||
id_user: string;
|
||||
}
|
||||
|
||||
class UpdateTournamentsInviteUserAcceptedService {
|
||||
public async execute({
|
||||
id_tournament,
|
||||
id_user,
|
||||
}: Request): Promise<TournamentParticipant> {
|
||||
const usersRepository = getRepository(User);
|
||||
const tournamentsRepository = getRepository(Tournament);
|
||||
const TournamentParticipantsRepository = getRepository(
|
||||
TournamentParticipant,
|
||||
);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
}
|
||||
|
||||
const tournament = await tournamentsRepository.findOne({
|
||||
where: { id_tournament },
|
||||
});
|
||||
|
||||
if (!tournament) {
|
||||
throw new AppError('Tournament does not exist.');
|
||||
}
|
||||
|
||||
const tournamentInvite = await TournamentParticipantsRepository.findOne({
|
||||
where: { tournament, user, user_accepted_invite: false },
|
||||
});
|
||||
|
||||
if (!tournamentInvite) {
|
||||
throw new AppError('User is not invited to this tournament.');
|
||||
}
|
||||
|
||||
tournamentInvite.user_accepted_invite = true;
|
||||
|
||||
await TournamentParticipantsRepository.save(tournamentInvite);
|
||||
|
||||
return tournamentInvite;
|
||||
}
|
||||
}
|
||||
|
||||
export default UpdateTournamentsInviteUserAcceptedService;
|
||||
57
src/services/UpdateTournamentsInviteUserRefusedService.ts
Normal file
57
src/services/UpdateTournamentsInviteUserRefusedService.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Tournament from '../models/Tournament';
|
||||
import TournamentParticipant from '../models/TournamentParticipant';
|
||||
|
||||
interface Request {
|
||||
id_tournament: string;
|
||||
id_user: string;
|
||||
}
|
||||
|
||||
class UpdateTournamentsInviteUserRefusedService {
|
||||
public async execute({
|
||||
id_tournament,
|
||||
id_user,
|
||||
}: Request): Promise<TournamentParticipant> {
|
||||
const usersRepository = getRepository(User);
|
||||
const tournamentsRepository = getRepository(Tournament);
|
||||
const TournamentParticipantsRepository = getRepository(
|
||||
TournamentParticipant,
|
||||
);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
}
|
||||
|
||||
const tournament = await tournamentsRepository.findOne({
|
||||
where: { id_tournament },
|
||||
});
|
||||
|
||||
if (!tournament) {
|
||||
throw new AppError('Tournament does not exist.');
|
||||
}
|
||||
|
||||
const tournamentInvite = await TournamentParticipantsRepository.findOne({
|
||||
where: { tournament, user, user_accepted_invite: false },
|
||||
});
|
||||
|
||||
if (!tournamentInvite) {
|
||||
throw new AppError('User is not invited to this tournament.');
|
||||
}
|
||||
|
||||
tournamentInvite.invite_refused = true;
|
||||
|
||||
await TournamentParticipantsRepository.save(tournamentInvite);
|
||||
|
||||
return tournamentInvite;
|
||||
}
|
||||
}
|
||||
|
||||
export default UpdateTournamentsInviteUserRefusedService;
|
||||
34
src/services/UpdateUserAvatarService.ts
Normal file
34
src/services/UpdateUserAvatarService.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Social from '../models/Social';
|
||||
|
||||
interface Request {
|
||||
id_user: string;
|
||||
avatar_image: string;
|
||||
}
|
||||
|
||||
class UpdateUserAvatarService {
|
||||
public async execute({ id_user, avatar_image }: Request): Promise<User> {
|
||||
const usersRepository = getRepository(User);
|
||||
const socialRepository = getRepository(Social);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user: id_user }
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
};
|
||||
|
||||
user.avatar_image = avatar_image;
|
||||
|
||||
await usersRepository.save(user);
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
export default UpdateUserAvatarService;
|
||||
34
src/services/UpdateUserBackgroundService.ts
Normal file
34
src/services/UpdateUserBackgroundService.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Social from '../models/Social';
|
||||
|
||||
interface Request {
|
||||
id_user: string;
|
||||
background_image: string;
|
||||
}
|
||||
|
||||
class UpdateUserBackgroundService {
|
||||
public async execute({ id_user, background_image }: Request): Promise<User> {
|
||||
const usersRepository = getRepository(User);
|
||||
const socialRepository = getRepository(Social);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user: id_user }
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
};
|
||||
|
||||
user.background_image = background_image;
|
||||
|
||||
await usersRepository.save(user);
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
export default UpdateUserBackgroundService;
|
||||
43
src/services/UpdateUserCoinsService.ts
Normal file
43
src/services/UpdateUserCoinsService.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
import { hash, compare } from 'bcryptjs';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Social from '../models/Social';
|
||||
|
||||
interface Request {
|
||||
id_user: string;
|
||||
quantity: number;
|
||||
operation: 'add' | 'remove';
|
||||
}
|
||||
|
||||
class UpdateUserCoinsService {
|
||||
public async execute({
|
||||
id_user,
|
||||
quantity,
|
||||
operation,
|
||||
}: Request): Promise<User> {
|
||||
const usersRepository = getRepository(User);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user: id_user },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
}
|
||||
|
||||
const userCoinsAsNumber = parseInt(user.coins);
|
||||
|
||||
if (operation == 'add')
|
||||
user.coins = (userCoinsAsNumber + quantity).toString();
|
||||
else user.coins = (userCoinsAsNumber - quantity).toString();
|
||||
|
||||
await usersRepository.save(user);
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
export default UpdateUserCoinsService;
|
||||
44
src/services/UpdateUserPasswordService.ts
Normal file
44
src/services/UpdateUserPasswordService.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
import { hash, compare } from 'bcryptjs';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Social from '../models/Social';
|
||||
|
||||
interface Request {
|
||||
id_user: string;
|
||||
password_old: string;
|
||||
password_new: string;
|
||||
}
|
||||
|
||||
class UpdateUserService {
|
||||
public async execute({ id_user, password_old, password_new }: Request): Promise<User> {
|
||||
const usersRepository = getRepository(User);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user: id_user }
|
||||
});
|
||||
|
||||
// TODO, notar que isso é necessário
|
||||
// tentar remover que o TypeScript apontará como erro
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
};
|
||||
|
||||
// TODO, padronizar mensagens em PT-BR
|
||||
const passwordMatched = await compare(password_old, user.password);
|
||||
|
||||
if (!passwordMatched) throw new AppError("Senha atual incorreta.", 200);
|
||||
|
||||
const hashedNewPassword = await hash(password_new, 8);
|
||||
|
||||
user.password = hashedNewPassword;
|
||||
|
||||
await usersRepository.save(user);
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
export default UpdateUserService;
|
||||
41
src/services/UpdateUserService.ts
Normal file
41
src/services/UpdateUserService.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Social from '../models/Social';
|
||||
|
||||
interface Request {
|
||||
id_user: string;
|
||||
name: string;
|
||||
username: string;
|
||||
bio: string;
|
||||
email: string;
|
||||
birth_date: string;
|
||||
}
|
||||
|
||||
class UpdateUserService {
|
||||
public async execute({ id_user, name, username, bio, email, birth_date }: Request): Promise<User> {
|
||||
const usersRepository = getRepository(User);
|
||||
const socialRepository = getRepository(Social);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user: id_user }
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
};
|
||||
|
||||
user.name = name;
|
||||
user.username = username;
|
||||
user.bio = bio;
|
||||
user.birth_date = new Date(birth_date); // TODO, funciona?
|
||||
|
||||
await usersRepository.save(user);
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
export default UpdateUserService;
|
||||
61
src/services/UpdateUserSocialService.ts
Normal file
61
src/services/UpdateUserSocialService.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import Social from '../models/Social';
|
||||
|
||||
interface Request {
|
||||
id_user: string;
|
||||
social_network: string;
|
||||
username: string;
|
||||
}
|
||||
|
||||
class UpdateUserSocialService {
|
||||
public async execute({
|
||||
id_user,
|
||||
social_network,
|
||||
username,
|
||||
}: Request): Promise<Social> {
|
||||
const usersRepository = getRepository(User);
|
||||
const socialRepository = getRepository(Social);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user: id_user },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('User does not exist.');
|
||||
}
|
||||
|
||||
const social = await socialRepository.findOne({
|
||||
where: { user: user },
|
||||
});
|
||||
|
||||
if (!social) throw new AppError('User does not exist.');
|
||||
|
||||
switch (social_network) {
|
||||
case 'telegram':
|
||||
social.telegram = username;
|
||||
break;
|
||||
case 'facebook':
|
||||
social.facebook = username;
|
||||
break;
|
||||
case 'twitter':
|
||||
social.twitter = username;
|
||||
break;
|
||||
case 'twitch':
|
||||
social.twitch = username;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
await socialRepository.save(social);
|
||||
|
||||
return social;
|
||||
}
|
||||
}
|
||||
|
||||
export default UpdateUserSocialService;
|
||||
Reference in New Issue
Block a user