Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/pokemon player #150

Merged
merged 11 commits into from
Oct 13, 2023
26 changes: 13 additions & 13 deletions schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,20 @@ model Setting {
}

model Team {
id String @id
name String
tournamentId String
captainId String @unique
lockedAt DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
discordRoleId String?
captain User @relation(fields: [captainId], references: [id])
tournament Tournament @relation(fields: [tournamentId], references: [id])
askingUsers User[] @relation("teamAskingUsers")
users User[] @relation("teamUsers")
id String @id
name String
tournamentId String
captainId String @unique
lockedAt DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
discordRoleId String?
pokemonPlayerId String?
captain User @relation(fields: [captainId], references: [id])
tournament Tournament @relation(fields: [tournamentId], references: [id])
askingUsers User[] @relation("teamAskingUsers")
users User[] @relation("teamUsers")
enteredQueueAt DateTime?

@@unique([name, tournamentId])
@@index([id])
@@index([tournamentId], map: "teams_tournamentId_fkey")
Expand Down
11 changes: 8 additions & 3 deletions src/controllers/teams/createTeam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { fetchTournament } from '../../operations/tournament';
import { hasUserAlreadyPaidForAnotherTicket } from '../../operations/user';
import { Error as ResponseError, UserType } from '../../types';
import { filterTeam } from '../../utils/filters';
import { conflict, created, forbidden, gone, notFound } from '../../utils/responses';
import { badRequest, conflict, created, forbidden, gone, notFound } from '../../utils/responses';
import { getRequestInfo } from '../../utils/users';
import * as validators from '../../utils/validators';

Expand All @@ -22,6 +22,7 @@ export default [
Joi.object({
name: validators.teamName.required(),
tournamentId: Joi.string().required(),
pokemonPlayerId: Joi.string().pattern(/^\d+$/),
userType: Joi.string()
.valid(UserType.player, UserType.coach)
.required()
Expand All @@ -32,7 +33,7 @@ export default [
// Controller
async (request: Request, response: Response, next: NextFunction) => {
try {
const { name, tournamentId, userType } = request.body;
const { name, tournamentId, pokemonPlayerId, userType } = request.body;
const { user } = getRequestInfo(response);

const tournament = await fetchTournament(tournamentId);
Expand All @@ -50,8 +51,12 @@ export default [
if (await hasUserAlreadyPaidForAnotherTicket(user, tournamentId, userType))
return forbidden(response, ResponseError.HasAlreadyPaidForAnotherTicket);

if (tournamentId === 'pokemon' && !pokemonPlayerId) {
return badRequest(response, ResponseError.NoPokemonIdProvided);
}

try {
const team = await createTeam(name, tournamentId, response.locals.user.id, userType);
const team = await createTeam(name, tournamentId, response.locals.user.id, pokemonPlayerId, userType);
return created(response, filterTeam(team));
} catch (error) {
// If the email already exists in the database, throw a bad request
Expand Down
5 changes: 5 additions & 0 deletions src/controllers/teams/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
description: Type d'utilisateur
type: string
enum: [player, coach]
pokemonPlayerId:
description: Identifiant du Pokémon du joueur (obligatoire si tournamentId est pokemon)
optional: true
type: string
example: "1"
responses:
201:
description: L'équipe a bien été créée. Ses informations sont retournées.
Expand Down
2 changes: 2 additions & 0 deletions src/operations/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export const createTeam = async (
name: string,
tournamentId: string,
captainId: string,
pokemonPlayerId: string | undefined,
userType: UserType,
): Promise<Team> => {
// Update the user to create a transaction update (update the user AND create the team)
Expand All @@ -150,6 +151,7 @@ export const createTeam = async (
create: {
id: nanoid(),
name,
pokemonPlayerId,
captain: {
connect: {
id: captainId,
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ export const enum Error {
MalformedMailBody = 'Structure du mail incorrecte',
InvalidMailOptions = "Paramètres d'envoi incorrects",

NoPokemonIdProvided = "L'ID Pokémon n'a pas été spécifié",

// 401
// The user credentials were refused or not provided
Unauthenticated = "Tu n'es pas authentifié",
Expand Down
39 changes: 39 additions & 0 deletions tests/teams/createTeam.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ describe('POST /teams', () => {
userType: UserType.player,
};

const teamBodyPokemon = {
name: 'ZeBest2',
tournamentId: 'pokemon',
userType: UserType.player,
};

before(async () => {
user = await createFakeUser();
token = generateToken(user);
Expand Down Expand Up @@ -98,6 +104,39 @@ describe('POST /teams', () => {
.expect(500, { error: Error.InternalServerError });
});

it('should fail as the pokemonId is required for pokemon tournament', async () => {
const pokemonUser = await createFakeUser();
const pokemonToken = generateToken(pokemonUser);

return request(app)
.post('/teams')
.send({ ...teamBodyPokemon })
.set('Authorization', `Bearer ${pokemonToken}`)
.expect(400, { error: Error.NoPokemonIdProvided });
});

it('should fail as the pokemonId is not a number', async () => {
const pokemonUser = await createFakeUser();
const pokemonToken = generateToken(pokemonUser);

return request(app)
.post('/teams')
.send({ ...teamBodyPokemon, pokemonPlayerId: 'test' })
.set('Authorization', `Bearer ${pokemonToken}`)
.expect(400);
});

it('should successfully create a pokemon team', async () => {
const pokemonUser = await createFakeUser();
const pokemonToken = generateToken(pokemonUser);

return request(app)
.post('/teams')
.send({ ...teamBodyPokemon, pokemonPlayerId: '1' })
.set('Authorization', `Bearer ${pokemonToken}`)
.expect(201);
});

it('should successfully create a team (as a player)', async () => {
const response = await request(app)
.post('/teams')
Expand Down
Loading