Ajustes
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
"host": "localhost",
|
||||
"port": 5432,
|
||||
"username": "postgres",
|
||||
"password": "Cara@2012",
|
||||
"password": "",
|
||||
"database": "postgres",
|
||||
"entities": [
|
||||
"./src/models/*.ts"
|
||||
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<Item> {
|
||||
const itemsRepository = getRepository(Item);
|
||||
|
||||
const item = itemsRepository.create({
|
||||
nome,
|
||||
tipo,
|
||||
asset,
|
||||
preco
|
||||
});
|
||||
|
||||
await itemsRepository.save(item);
|
||||
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
export default CreateItemService;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
@@ -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<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;
|
||||
Reference in New Issue
Block a user