diff --git a/ormconfig.json b/ormconfig.json index 011f8a5..7b139a8 100644 --- a/ormconfig.json +++ b/ormconfig.json @@ -3,7 +3,7 @@ "host": "localhost", "port": 5432, "username": "postgres", - "password": "Cara@2012", + "password": "", "database": "postgres", "entities": [ "./src/models/*.ts" diff --git a/src/services/AddItemService.ts b/src/services/AddItemService.ts deleted file mode 100644 index ff5f833..0000000 --- a/src/services/AddItemService.ts +++ /dev/null @@ -1,48 +0,0 @@ -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 { - 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; diff --git a/src/services/CheckUserIsFollowingUserService.ts b/src/services/CheckUserIsFollowingUserService.ts deleted file mode 100644 index 8ff117e..0000000 --- a/src/services/CheckUserIsFollowingUserService.ts +++ /dev/null @@ -1,48 +0,0 @@ -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 { - 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; diff --git a/src/services/CheckUserIsKickedFromTournamentService.ts b/src/services/CheckUserIsKickedFromTournamentService.ts deleted file mode 100644 index a21a5b2..0000000 --- a/src/services/CheckUserIsKickedFromTournamentService.ts +++ /dev/null @@ -1,51 +0,0 @@ -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 { - 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; diff --git a/src/services/CreateFollowService.ts b/src/services/CreateFollowService.ts deleted file mode 100644 index 1956f3e..0000000 --- a/src/services/CreateFollowService.ts +++ /dev/null @@ -1,59 +0,0 @@ -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 { - 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; diff --git a/src/services/CreateItemService.ts b/src/services/CreateItemService.ts deleted file mode 100644 index 0df8eff..0000000 --- a/src/services/CreateItemService.ts +++ /dev/null @@ -1,35 +0,0 @@ -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 { - const itemsRepository = getRepository(Item); - - const item = itemsRepository.create({ - nome, - tipo, - asset, - preco - }); - - await itemsRepository.save(item); - - return item; - } -} - -export default CreateItemService; diff --git a/src/services/CreatePublicationService.ts b/src/services/CreatePublicationService.ts deleted file mode 100644 index 245d72e..0000000 --- a/src/services/CreatePublicationService.ts +++ /dev/null @@ -1,48 +0,0 @@ -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 { - 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; diff --git a/src/services/CreateTournamentParticipantService.ts b/src/services/CreateTournamentParticipantService.ts deleted file mode 100644 index b152421..0000000 --- a/src/services/CreateTournamentParticipantService.ts +++ /dev/null @@ -1,70 +0,0 @@ -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 { - 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; diff --git a/src/services/CreateTournamentService.ts b/src/services/CreateTournamentService.ts deleted file mode 100644 index 018b9e4..0000000 --- a/src/services/CreateTournamentService.ts +++ /dev/null @@ -1,60 +0,0 @@ -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 { - 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; diff --git a/src/services/DeleteFollowService.ts b/src/services/DeleteFollowService.ts deleted file mode 100644 index d606c19..0000000 --- a/src/services/DeleteFollowService.ts +++ /dev/null @@ -1,62 +0,0 @@ -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 { - 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; diff --git a/src/services/FindAcceptedTournamentParticipantsService.ts b/src/services/FindAcceptedTournamentParticipantsService.ts deleted file mode 100644 index 5978469..0000000 --- a/src/services/FindAcceptedTournamentParticipantsService.ts +++ /dev/null @@ -1,56 +0,0 @@ -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 { - 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; diff --git a/src/services/FindInventarioUserService.ts b/src/services/FindInventarioUserService.ts deleted file mode 100644 index eb1de27..0000000 --- a/src/services/FindInventarioUserService.ts +++ /dev/null @@ -1,33 +0,0 @@ -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 { - 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; diff --git a/src/services/FindItensAtivosService.ts b/src/services/FindItensAtivosService.ts deleted file mode 100644 index d43b058..0000000 --- a/src/services/FindItensAtivosService.ts +++ /dev/null @@ -1,33 +0,0 @@ -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 { - 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; diff --git a/src/services/FindKickedParticipantsFromTournamentService.ts b/src/services/FindKickedParticipantsFromTournamentService.ts deleted file mode 100644 index 4a62d66..0000000 --- a/src/services/FindKickedParticipantsFromTournamentService.ts +++ /dev/null @@ -1,44 +0,0 @@ -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 { - 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; diff --git a/src/services/FindPendingTournamentInvitesService.ts b/src/services/FindPendingTournamentInvitesService.ts deleted file mode 100644 index 97ca251..0000000 --- a/src/services/FindPendingTournamentInvitesService.ts +++ /dev/null @@ -1,33 +0,0 @@ -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 { - 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; diff --git a/src/services/FindTournamentColumnsService.ts b/src/services/FindTournamentColumnsService.ts deleted file mode 100644 index c526a08..0000000 --- a/src/services/FindTournamentColumnsService.ts +++ /dev/null @@ -1,33 +0,0 @@ -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 { - 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; diff --git a/src/services/FindTournamentService.ts b/src/services/FindTournamentService.ts deleted file mode 100644 index 5dd1356..0000000 --- a/src/services/FindTournamentService.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { getRepository } from 'typeorm'; - -import AppError from '../errors/AppError'; - -import Tournament from '../models/Tournament'; - -class FindTournamentService { - public async execute(id: string): Promise { - 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; diff --git a/src/services/FindTournamentsByUserService.ts b/src/services/FindTournamentsByUserService.ts deleted file mode 100644 index 908a525..0000000 --- a/src/services/FindTournamentsByUserService.ts +++ /dev/null @@ -1,29 +0,0 @@ -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 { - 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; diff --git a/src/services/FindTournamentsPublicatedService.ts b/src/services/FindTournamentsPublicatedService.ts deleted file mode 100644 index 2f455af..0000000 --- a/src/services/FindTournamentsPublicatedService.ts +++ /dev/null @@ -1,33 +0,0 @@ -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 { - 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; diff --git a/src/services/FindTournamentsUserIsParticipatingService.ts b/src/services/FindTournamentsUserIsParticipatingService.ts deleted file mode 100644 index 2e3b263..0000000 --- a/src/services/FindTournamentsUserIsParticipatingService.ts +++ /dev/null @@ -1,35 +0,0 @@ -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 { - 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; diff --git a/src/services/FindUserColocationsService.ts b/src/services/FindUserColocationsService.ts deleted file mode 100644 index 7f416a5..0000000 --- a/src/services/FindUserColocationsService.ts +++ /dev/null @@ -1,113 +0,0 @@ -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 { - 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; diff --git a/src/services/FindUserFollowersService.ts b/src/services/FindUserFollowersService.ts deleted file mode 100644 index a31a5ed..0000000 --- a/src/services/FindUserFollowersService.ts +++ /dev/null @@ -1,29 +0,0 @@ -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 { - 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; diff --git a/src/services/KickTournamentParticipantService.ts b/src/services/KickTournamentParticipantService.ts deleted file mode 100644 index 0694122..0000000 --- a/src/services/KickTournamentParticipantService.ts +++ /dev/null @@ -1,56 +0,0 @@ -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 { - 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; diff --git a/src/services/UpdateAtivaItemService.ts b/src/services/UpdateAtivaItemService.ts deleted file mode 100644 index e3b3dcb..0000000 --- a/src/services/UpdateAtivaItemService.ts +++ /dev/null @@ -1,52 +0,0 @@ -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 { - 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; diff --git a/src/services/UpdateDesativaItemService.ts b/src/services/UpdateDesativaItemService.ts deleted file mode 100644 index c6804fa..0000000 --- a/src/services/UpdateDesativaItemService.ts +++ /dev/null @@ -1,52 +0,0 @@ -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 { - 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; diff --git a/src/services/UpdateFollowCounterService.ts b/src/services/UpdateFollowCounterService.ts deleted file mode 100644 index 7368afe..0000000 --- a/src/services/UpdateFollowCounterService.ts +++ /dev/null @@ -1,37 +0,0 @@ -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 { - 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; diff --git a/src/services/UpdateTournamentAsEnded.ts b/src/services/UpdateTournamentAsEnded.ts deleted file mode 100644 index e627865..0000000 --- a/src/services/UpdateTournamentAsEnded.ts +++ /dev/null @@ -1,41 +0,0 @@ -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 { - 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; diff --git a/src/services/UpdateTournamentColumnsInitializedFlagService.ts b/src/services/UpdateTournamentColumnsInitializedFlagService.ts deleted file mode 100644 index 1301024..0000000 --- a/src/services/UpdateTournamentColumnsInitializedFlagService.ts +++ /dev/null @@ -1,48 +0,0 @@ -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 { - 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; diff --git a/src/services/UpdateTournamentColumnsService.ts b/src/services/UpdateTournamentColumnsService.ts deleted file mode 100644 index 8e75ce2..0000000 --- a/src/services/UpdateTournamentColumnsService.ts +++ /dev/null @@ -1,50 +0,0 @@ -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 { - 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; diff --git a/src/services/UpdateTournamentService.ts b/src/services/UpdateTournamentService.ts deleted file mode 100644 index a1d8fbc..0000000 --- a/src/services/UpdateTournamentService.ts +++ /dev/null @@ -1,44 +0,0 @@ -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 { - 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; diff --git a/src/services/UpdateTournamentsInviteUserAcceptedService.ts b/src/services/UpdateTournamentsInviteUserAcceptedService.ts deleted file mode 100644 index b5afffa..0000000 --- a/src/services/UpdateTournamentsInviteUserAcceptedService.ts +++ /dev/null @@ -1,57 +0,0 @@ -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 { - 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; diff --git a/src/services/UpdateTournamentsInviteUserRefusedService.ts b/src/services/UpdateTournamentsInviteUserRefusedService.ts deleted file mode 100644 index 56cd309..0000000 --- a/src/services/UpdateTournamentsInviteUserRefusedService.ts +++ /dev/null @@ -1,57 +0,0 @@ -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 { - 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; diff --git a/src/services/UpdateUserBackgroundService.ts b/src/services/UpdateUserBackgroundService.ts deleted file mode 100644 index 05925d2..0000000 --- a/src/services/UpdateUserBackgroundService.ts +++ /dev/null @@ -1,34 +0,0 @@ -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 { - 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; diff --git a/src/services/UpdateUserCoinsService.ts b/src/services/UpdateUserCoinsService.ts deleted file mode 100644 index 9335e5d..0000000 --- a/src/services/UpdateUserCoinsService.ts +++ /dev/null @@ -1,43 +0,0 @@ -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 { - 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;