Skip to content

Commit

Permalink
Merge pull request #224 from ungdev/dev
Browse files Browse the repository at this point in the history
Feat: better Repo
  • Loading branch information
DevNono authored Dec 1, 2023
2 parents 8c4bea0 + 47d0a57 commit bb00cfb
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 UNG
Copyright (c) 2020-2023 UNG

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 5 additions & 1 deletion src/controllers/admin/repo/depositRepoItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ export default [
}

const isStoringPC = !!(await findItemOfType(user.id, RepoItemType.computer));
if ((isStoringPC ? 1 : 0) + items.filter((item) => item.type === RepoItemType.computer).length > 1) {
const computersInBody = items.filter((item) => item.type === RepoItemType.computer).length;
if (isStoringPC && computersInBody > 0) {
return methodNotSupported(response, ResponseError.AlreadyHaveComputer);
}
if (computersInBody > 1) {
return methodNotSupported(response, ResponseError.CantDepositMulitpleComputers);
}

await addRepoItems(
userId,
Expand Down
4 changes: 2 additions & 2 deletions src/operations/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { RepoItem, RepoLog } from '../types';
import nanoid from '../utils/nanoid';

export const fetchRepoItems = (userId: string): Promise<RepoItem[]> =>
database.repoItem.findMany({ where: { forUserId: userId, pickedUp: false } });
database.repoItem.findMany({ where: { forUserId: userId, pickedUp: false }, orderBy: { type: 'asc' } });

export const fetchRepoItem = (itemId: string): Promise<RepoItem> =>
database.repoItem.findUnique({ where: { id: itemId } });
Expand Down Expand Up @@ -34,4 +34,4 @@ export const removeRepoItem = async (itemId: string, userId: string) => {
};

export const fetchRepoLogs = (userId: string): Promise<RepoLog[]> =>
database.repoLog.findMany({ where: { forUserId: userId }, include: { item: true } });
database.repoLog.findMany({ where: { forUserId: userId }, include: { item: true }, orderBy: { timestamp: 'desc' } });
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ export const enum Error {
EtupayNoAccess = "Tu n'as pas accès à cette url",
NotYourItem = "Cet item n'est pas le tiens",
AlreadyHaveComputer = 'Tu as déjà un ordinateur stocké',
CantDepositMulitpleComputers = 'Tu ne peux pas déposer plusieurs ordinateurs',
AlreadyPickedUp = "L'objet a déjà été récupéré",
TooMuchLockedTeams = "Il y a plus d'équipes inscrites que le nombre d'équipes maximal souhaité",
TournamentFull = "Le tournoi est plein, attends qu'une place se libère pour payer un ticket",
Expand Down
2 changes: 1 addition & 1 deletion tests/admin/repo/depositRepoItem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('POST /admin/repo/user/:userId/items', () => {
{ type: 'computer', zone: 'Zone 2' },
],
})
.expect(405, { error: Error.AlreadyHaveComputer });
.expect(405, { error: Error.CantDepositMulitpleComputers });
});

it('should successfully add a new item to the repo', async () => {
Expand Down
4 changes: 3 additions & 1 deletion tests/admin/repo/getRepo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ describe('GET /admin/repo/user', () => {
});

it('should successfully return a list of items', async () => {
await database.repoItem.create({ data: { id: 'FEDCBA', type: 'monitor', forUserId: captain.id, zone: 'Zone 2' } });
await database.repoItem.create({ data: { id: 'ABCDEF', type: 'computer', forUserId: captain.id, zone: 'Zone 1' } });
const response = await request(app)
.get(`/admin/repo/user?id=${encodeURIComponent(encrypt(captain.id).toString('base64'))}`)
Expand All @@ -108,10 +109,11 @@ describe('GET /admin/repo/user', () => {
expect(response.body.lastname).to.be.equal(captain.lastname);
expect(response.body.place).to.be.equal(captain.place);
expect(response.body.id).to.be.equal(captain.id);
expect(response.body.repoItems).to.have.length(1);
expect(response.body.repoItems).to.have.length(2);
expect(response.body.repoItems[0].type).to.be.equal('computer');
expect(response.body.repoItems[0].id).to.be.equal('ABCDEF');
expect(response.body.repoItems[0].zone).to.be.equal('Zone 1');
expect(response.body.repoItems[1].type).to.be.equal('monitor');
});

it('should fail as user is a spectator', async () => {
Expand Down
15 changes: 10 additions & 5 deletions tests/admin/repo/getRepoLogs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,22 @@ describe('GET /admin/repo/user/:userId/logs', () => {
it('should successfully return a list of logs', async () => {
await lockTeam(team.id);
await database.$transaction(addRepoItem(captain.id, 'computer', 'Zone 1'));
await database.$transaction(addRepoItem(captain.id, 'computer', 'Zone 2'));
const response = await request(app)
.get(`/admin/repo/user/${captain.id}/logs`)
.set('Authorization', `Bearer ${adminToken}`)
.expect(200);
expect(response.body.logs).to.have.length(1);
expect(response.body.logs[0].itemType).to.be.equal('computer');
expect(response.body.logs[0].action).to.be.equal(RepoLogAction.added);
// Verify the first item added
expect(response.body.logs).to.have.length(2);
expect(response.body.logs[1].itemType).to.be.equal('computer');
expect(response.body.logs[1].action).to.be.equal(RepoLogAction.added);
// We can't check the exact date, so we simply verify that the timestamp is the right one at +/- 3 seconds
expect(Date.now() - Date.parse(response.body.logs[0].timestamp)).to.be.lessThanOrEqual(3000);
expect(response.body.logs[0].agent?.firstname).to.be.equal(captain.firstname);
expect(response.body.logs[0].agent?.lastname).to.be.equal(captain.lastname);
expect(response.body.logs[1].agent?.firstname).to.be.equal(captain.firstname);
expect(response.body.logs[1].agent?.lastname).to.be.equal(captain.lastname);
expect(response.body.logs[1].zone).to.be.equal('Zone 1');
// Verify the second item added
expect(response.body.logs[0].zone).to.be.equal('Zone 2');
});

it('should fail as user is a spectator', async () => {
Expand Down

0 comments on commit bb00cfb

Please sign in to comment.