Skip to content

Commit

Permalink
Feat/position in items (#160)
Browse files Browse the repository at this point in the history
* test: a tournament was modified in this test

* feat: items now have a position

* fix: forgot some commented code

---------

Co-authored-by: Teddy Roncin <[email protected]>
  • Loading branch information
TeddyRoncin and Teddy Roncin authored Oct 17, 2023
1 parent 0d3956c commit 225aacf
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 63 deletions.
25 changes: 13 additions & 12 deletions schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,19 @@ model CartItem {
}

model Item {
id String @id
name String
category ItemCategory
attribute String?
price Int
reducedPrice Int?
infos String? @db.VarChar(300)
image String?
stock Int?
availableFrom DateTime?
availableUntil DateTime?
cartItems CartItem[]
id String @id
name String
category ItemCategory
attribute String?
price Int
reducedPrice Int?
infos String? @db.VarChar(300)
image String?
stock Int?
availableFrom DateTime?
availableUntil DateTime?
position Int @default(0)
cartItems CartItem[]
@@map("items")
}
Expand Down
30 changes: 15 additions & 15 deletions seed.sql
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
INSERT INTO `items` (`id`, `name`, `category`, `attribute`, `price`, `reducedPrice`, `infos`, `image`, `stock`) VALUES
('ticket-player', 'Place joueur', 'ticket', NULL, 2500, 2000, NULL, NULL, NULL),
('ticket-player-ssbu', 'Place joueur (SSBU)', 'ticket', NULL, 2200, 1700, NULL, NULL, NULL),
('ticket-coach', 'Place coach/manager', 'ticket', NULL, 1500, NULL, NULL, NULL, NULL),
('ticket-attendant', 'Place accompagnateur', 'ticket', NULL, 1500, NULL, NULL, NULL, NULL),
('ticket-spectator', 'Place spectateur', 'ticket', NULL, 1000, NULL, NULL, NULL, 100),
('discount-switch-ssbu', 'Réduction si tu amènes ta propre Nintendo Switch', 'supplement', NULL, -300, NULL, 'Une réduction applicable si tu amènes ta propre Nintendo Switch pendant le weekend. Il faut que tu aies le jeu SSBU avec tous les personnages et que tu prennes un cable HDMI.', NULL, 30),
('ethernet-7', 'Câble ethernet (7m)', 'supplement', NULL, 1000, NULL, 'Un câble ethernet est requis pour se brancher aux switchs des tables', NULL, NULL),
('multi-socket', 'Multiprise 3 trous', 'supplement', NULL, 500, NULL, 'Une multiprise 3 trous pour brancher tout ton setup', NULL, NULL),
('pin', "Pin's", 'supplement', NULL, 200, NULL, "Un pin's argenté, souvenir de cette LAN de folie", 'pin.png', 100),
('tshirt-s', 'T-shirt UA 2022 (Unixese)', 'supplement', 's', 1500, NULL, 'Un t-shirt souvenir de cette LAN de folie', 'tshirt.png', 30),
('tshirt-m', 'T-shirt UA 2022 (Unixese)', 'supplement', 'm', 1500, NULL, 'Un t-shirt souvenir de cette LAN de folie', 'tshirt.png', 30),
('tshirt-l', 'T-shirt UA 2022 (Unixese)', 'supplement', 'l', 1500, NULL, 'Un t-shirt souvenir de cette LAN de folie', 'tshirt.png', 30),
('tshirt-xl', 'T-shirt UA 2022 (Unixese)', 'supplement', 'xl', 1500, NULL, 'Un t-shirt souvenir de cette LAN de folie', 'tshirt.png', 30),
('pc', 'Location de PC', 'rent', NULL, 14000, NULL, 'Location de PC si tu ne peux pas amener le tien pendant l''UTT Arena. L''un des PC suivants sera mis à ta disposition :->AMD Ryzen 5 2600,16GB RAM SSD 500G° et 2060 ou 6600XT->INTEL I5 8500 16GB RAM SSD 500G° et 2060 ou 6600XT->AMD RYZEN 5 5600X 500G° SSD et 2060 ou 6600XT', NULL, 7);
INSERT INTO `items` (`id`, `name`, `category`, `attribute`, `price`, `reducedPrice`, `infos`, `image`, `stock`, `position`) VALUES
('ticket-player', 'Place joueur', 'ticket', NULL, 2500, 2000, NULL, NULL, NULL, 0),
('ticket-player-ssbu', 'Place joueur (SSBU)', 'ticket', NULL, 2200, 1700, NULL, NULL, NULL, 1),
('ticket-coach', 'Place coach/manager', 'ticket', NULL, 1500, NULL, NULL, NULL, NULL, 2),
('ticket-attendant', 'Place accompagnateur', 'ticket', NULL, 1500, NULL, NULL, NULL, NULL, 3),
('ticket-spectator', 'Place spectateur', 'ticket', NULL, 1000, NULL, NULL, NULL, 100, 4),
('discount-switch-ssbu', 'Réduction si tu amènes ta propre Nintendo Switch', 'supplement', NULL, -300, NULL, 'Une réduction applicable si tu amènes ta propre Nintendo Switch pendant le weekend. Il faut que tu aies le jeu SSBU avec tous les personnages et que tu prennes un cable HDMI.', NULL, 30, 5),
('ethernet-7', 'Câble ethernet (7m)', 'supplement', NULL, 1000, NULL, 'Un câble ethernet est requis pour se brancher aux switchs des tables', NULL, NULL, 11),
('multi-socket', 'Multiprise 3 trous', 'supplement', NULL, 500, NULL, 'Une multiprise 3 trous pour brancher tout ton setup', NULL, NULL, 12),
('pin', "Pin's", 'supplement', NULL, 200, NULL, "Un pin's argenté, souvenir de cette LAN de folie", 'pin.png', 100, 10),
('tshirt-s', 'T-shirt UA 2022 (Unixese)', 'supplement', 's', 1500, NULL, 'Un t-shirt souvenir de cette LAN de folie', 'tshirt.png', 30, 6),
('tshirt-m', 'T-shirt UA 2022 (Unixese)', 'supplement', 'm', 1500, NULL, 'Un t-shirt souvenir de cette LAN de folie', 'tshirt.png', 30, 7),
('tshirt-l', 'T-shirt UA 2022 (Unixese)', 'supplement', 'l', 1500, NULL, 'Un t-shirt souvenir de cette LAN de folie', 'tshirt.png', 30, 8),
('tshirt-xl', 'T-shirt UA 2022 (Unixese)', 'supplement', 'xl', 1500, NULL, 'Un t-shirt souvenir de cette LAN de folie', 'tshirt.png', 30, 9),
('pc', 'Location de PC', 'rent', NULL, 14000, NULL, 'Location de PC si tu ne peux pas amener le tien pendant l''UTT Arena. L''un des PC suivants sera mis à ta disposition :->AMD Ryzen 5 2600,16GB RAM SSD 500G° et 2060 ou 6600XT->INTEL I5 8500 16GB RAM SSD 500G° et 2060 ou 6600XT->AMD RYZEN 5 5600X 500G° SSD et 2060 ou 6600XT', NULL, 7, 13);

INSERT INTO `settings` (`id`, `value`) VALUES
('login', 0),
Expand Down
16 changes: 5 additions & 11 deletions src/operations/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import { isPartnerSchool } from '../utils/helpers';

export const fetchAllItems = async (): Promise<Item[]> => {
// fetches the items
let items = await database.item.findMany();
const order = ['tshirt-s', 'tshirt-m', 'tshirt-l', 'tshirt-xl'];
// sort items according to size attribute
items = items.sort((a, b) => order.indexOf(a.id) - order.indexOf(b.id));
const items = await database.item.findMany({ orderBy: [{ position: 'asc' }] });

// Add a left property which tells how many items are there left
return Promise.all(
Expand Down Expand Up @@ -75,13 +72,10 @@ export const fetchUserItems = async (team?: Team, user?: User) => {
const currentTicket =
items.find((item) => item.id === `ticket-player-${team?.tournamentId}`) ??
items.find((item) => item.id === 'ticket-player');
items = [
...items.filter((item) => !item.id.startsWith('ticket-player')),
{
...currentTicket,
id: 'ticket-player',
},
];
// Remove every ticket-player* item except the currentTicket
items = items.filter((item) => !item.id.startsWith('ticket-player') || item.id === currentTicket.id);
// Update the currentTicket id
currentTicket.id = 'ticket-player';

return items;
};
Expand Down
44 changes: 22 additions & 22 deletions tests/admin/tournaments/updateTournament.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import { sandbox } from '../../setup';
import * as tournamentOperations from '../../../src/operations/tournament';
import database from '../../../src/services/database';
import { Error, Permission, Tournament, User, UserType } from '../../../src/types';
import { createFakeUser } from '../../utils';
import { createFakeTournament, createFakeUser } from '../../utils';
import { generateToken } from '../../../src/utils/users';
import { fetchTournaments } from '../../../src/operations/tournament';

describe('PATCH /admin/tournaments/{tournamentId}', () => {
let nonAdminUser: User;
let admin: User;
let adminToken: string;
let tournaments: Tournament[];
let tournament: Tournament;

const validBody = {
name: 'test',
Expand All @@ -32,26 +31,27 @@ describe('PATCH /admin/tournaments/{tournamentId}', () => {

after(async () => {
await database.user.deleteMany();
await database.tournament.delete({ where: { id: tournament.id } });
});

before(async () => {
admin = await createFakeUser({ type: UserType.orga, permissions: [Permission.admin] });
nonAdminUser = await createFakeUser();
adminToken = generateToken(admin);

tournaments = await fetchTournaments();
tournament = await createFakeTournament({ id: 'test', name: 'Dragibus', maxTeams: 1, playersPerTeam: 1 });
});

it('should error as the user is not authenticated', () =>
request(app)
.patch(`/admin/tournaments/${tournaments[0].id}`)
.patch(`/admin/tournaments/${tournament.id}`)
.send(validBody)
.expect(401, { error: Error.Unauthenticated }));

it('should error as the user is not an administrator', () => {
const userToken = generateToken(nonAdminUser);
return request(app)
.patch(`/admin/tournaments/${tournaments[0].id}`)
.patch(`/admin/tournaments/${tournament.id}`)
.send(validBody)
.set('Authorization', `Bearer ${userToken}`)
.expect(403, { error: Error.NoPermission });
Expand All @@ -61,7 +61,7 @@ describe('PATCH /admin/tournaments/{tournamentId}', () => {
sandbox.stub(tournamentOperations, 'updateTournament').throws('Unexpected error');

await request(app)
.patch(`/admin/tournaments/${tournaments[0].id}`)
.patch(`/admin/tournaments/${tournament.id}`)
.send(validBody)
.set('Authorization', `Bearer ${adminToken}`)
.expect(500, { error: Error.InternalServerError });
Expand All @@ -77,25 +77,25 @@ describe('PATCH /admin/tournaments/{tournamentId}', () => {

it('should successfully update the tournament', async () => {
await request(app)
.patch(`/admin/tournaments/${tournaments[0].id}`)
.patch(`/admin/tournaments/${tournament.id}`)
.send(validBody)
.set('Authorization', `Bearer ${adminToken}`)
.expect(200);

const tournament = await tournamentOperations.fetchTournament(tournaments[0].id);
const tournamentDatabase = await tournamentOperations.fetchTournament(tournament.id);

expect(tournament.name).to.equal(validBody.name);
expect(tournament.maxPlayers).to.equal(validBody.maxPlayers);
expect(tournament.playersPerTeam).to.equal(validBody.playersPerTeam);
expect(tournament.cashprize).to.equal(validBody.cashprize);
expect(tournament.cashprizeDetails).to.equal(validBody.cashprizeDetails);
expect(tournament.displayCashprize).to.equal(validBody.displayCashprize);
expect(tournament.format).to.equal(validBody.format);
expect(tournament.infos).to.equal(validBody.infos);
expect(tournament.casters).to.be.an('array');
expect(tournament.casters[0].name).to.be.equal(validBody.casters[0]);
expect(tournament.displayCasters).to.equal(validBody.displayCasters);
expect(tournament.display).to.equal(validBody.display);
expect(tournament.position).to.equal(150);
expect(tournamentDatabase.name).to.equal(validBody.name);
expect(tournamentDatabase.maxPlayers).to.equal(validBody.maxPlayers);
expect(tournamentDatabase.playersPerTeam).to.equal(validBody.playersPerTeam);
expect(tournamentDatabase.cashprize).to.equal(validBody.cashprize);
expect(tournamentDatabase.cashprizeDetails).to.equal(validBody.cashprizeDetails);
expect(tournamentDatabase.displayCashprize).to.equal(validBody.displayCashprize);
expect(tournamentDatabase.format).to.equal(validBody.format);
expect(tournamentDatabase.infos).to.equal(validBody.infos);
expect(tournamentDatabase.casters).to.be.an('array');
expect(tournamentDatabase.casters[0].name).to.be.equal(validBody.casters[0]);
expect(tournamentDatabase.displayCasters).to.equal(validBody.displayCasters);
expect(tournamentDatabase.display).to.equal(validBody.display);
expect(tournamentDatabase.position).to.equal(150);
});
});
12 changes: 9 additions & 3 deletions tests/items/getItems.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,16 @@ describe('GET /items', () => {
});

it('should return items with their stock', async () => {
const items = await database.item.findMany();
// Fetch all items, order them by their position, and remove special ticket players and ssbu discount
const items = await database.item.findMany({
where: { NOT: { OR: [{ id: { startsWith: 'ticket-player-' } }, { id: 'discount-switch-ssbu' }] } },
orderBy: [{ position: 'asc' }],
});
const response = await request(app).get('/items').expect(200);

for (const responseItem of response.body)
expect(responseItem.left ?? null).to.be.equal(items.find((item) => item.name === responseItem.name).stock);
for (let index = 0; index < response.body.length; index++) {
expect(response.body[index].id).to.be.equal(items[index].id);
expect(response.body[index].left ?? null).to.be.equal(items[index].stock);
}
});
});

0 comments on commit 225aacf

Please sign in to comment.