Skip to content

Commit 29868c4

Browse files
SharpDevOps10thebladehit
authored andcommitted
feature: added endpoint for finding all queues by labId
1 parent da54e17 commit 29868c4

File tree

5 files changed

+40
-61
lines changed

5 files changed

+40
-61
lines changed

src/queues/dto/create-queue.dto.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ import { IsEnum, IsNotEmpty, IsOptional, IsString } from 'class-validator';
22
import { QueueStatus } from '@prisma/client';
33

44
export class CreateQueueDto {
5-
@IsString()
6-
@IsNotEmpty()
7-
creatorId: string;
8-
95
@IsString()
106
@IsNotEmpty()
117
labId: string;

src/queues/queue.controller.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ export class QueuesController {
99
constructor (private readonly queuesService: QueuesService) {}
1010

1111
@Post()
12-
async createQueue (@Body() createQueueDto: CreateQueueDto) {
13-
return this.queuesService.createQueue(createQueueDto);
12+
async createQueue (@Body() createQueueDto: CreateQueueDto, @Req() req: Request) {
13+
const creatorId = req['user'].userId;
14+
return this.queuesService.createQueue(createQueueDto, creatorId);
1415
}
1516

1617
@Public()
@@ -19,6 +20,12 @@ export class QueuesController {
1920
return await this.queuesService.findQueueById(id);
2021
}
2122

23+
@Public()
24+
@Get('lab/:labId')
25+
async findAllQueuesByLabId (@Param('labId') labId: string) {
26+
return await this.queuesService.findAllQueuesByLabId(labId);
27+
}
28+
2229
@Delete(':id')
2330
async deleteQueueById (@Param('id') id: string, @Req() req: Request) {
2431
return this.queuesService.deleteQueueById(id, req);

src/queues/queues.service.spec.ts

Lines changed: 16 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,9 @@ describe('QueuesService', () => {
5050
});
5151

5252
const queueDto: CreateQueueDto = {
53-
creatorId: user.id,
5453
labId: 'lab1',
5554
};
56-
const queue = await queuesService.createQueue(queueDto);
55+
const queue = await queuesService.createQueue(queueDto, user.id);
5756

5857
expect(queue).toHaveProperty('id');
5958
expect(queue.creatorId).toBe(user.id);
@@ -74,10 +73,7 @@ describe('QueuesService', () => {
7473
},
7574
});
7675

77-
const queue = await queuesService.createQueue({
78-
creatorId: user.id,
79-
labId: 'lab2',
80-
});
76+
const queue = await queuesService.createQueue({ labId: 'lab2' }, user.id);
8177

8278
const foundQueue = await queuesService.findQueueById(queue.id);
8379

@@ -110,10 +106,7 @@ describe('QueuesService', () => {
110106
},
111107
} as unknown as Request;
112108

113-
const queue = await queuesService.createQueue({
114-
creatorId: admin.id,
115-
labId: 'lab3',
116-
});
109+
const queue = await queuesService.createQueue({ labId: 'lab3' }, admin.id);
117110

118111
const deletedQueue = await queuesService.deleteQueueById(queue.id, req);
119112
expect(deletedQueue).toStrictEqual(queue);
@@ -137,10 +130,7 @@ describe('QueuesService', () => {
137130
},
138131
} as unknown as Request;
139132

140-
const queue = await queuesService.createQueue({
141-
creatorId: creator.id,
142-
labId: 'lab4',
143-
});
133+
const queue = await queuesService.createQueue({ labId: 'lab4' }, creator.id);
144134

145135
const deletedQueue = await queuesService.deleteQueueById(queue.id, req);
146136
expect(deletedQueue).toStrictEqual(queue);
@@ -175,10 +165,7 @@ describe('QueuesService', () => {
175165
},
176166
} as unknown as Request;
177167

178-
const queue = await queuesService.createQueue({
179-
creatorId: creator.id,
180-
labId: 'lab5',
181-
});
168+
const queue = await queuesService.createQueue({ labId: 'lab5' }, creator.id);
182169

183170
await expect(queuesService.deleteQueueById(queue.id, req)).rejects.toThrow(ForbiddenException);
184171
});
@@ -204,10 +191,9 @@ describe('QueuesService', () => {
204191
} as unknown as Request;
205192

206193
const queue = await queuesService.createQueue({
207-
creatorId: user.id,
208194
labId: 'lab6',
209195
status: 'PENDING',
210-
});
196+
}, user.id);
211197

212198
const joinedQueue = await queuesService.joinQueue(queue.id, req);
213199
expect(joinedQueue).toStrictEqual(
@@ -239,10 +225,9 @@ describe('QueuesService', () => {
239225
} as unknown as Request;
240226

241227
const queue = await queuesService.createQueue({
242-
creatorId: user.id,
243228
labId: 'lab7',
244229
status: 'SKIPPED',
245-
});
230+
}, user.id);
246231

247232
await expect(queuesService.joinQueue(queue.id, req)).rejects.toThrow(ForbiddenException);
248233
});
@@ -266,10 +251,9 @@ describe('QueuesService', () => {
266251
} as unknown as Request;
267252

268253
const queue = await queuesService.createQueue({
269-
creatorId: user.id,
270254
labId: 'lab8',
271255
status: 'PENDING',
272-
});
256+
}, user.id);
273257

274258
await queuesService.joinQueue(queue.id, req);
275259
await expect(queuesService.joinQueue(queue.id, req)).rejects.toThrow(ConflictException);
@@ -295,10 +279,7 @@ describe('QueuesService', () => {
295279
},
296280
} as unknown as Request;
297281

298-
const queue = await queuesService.createQueue({
299-
creatorId: user.id,
300-
labId: 'lab9',
301-
});
282+
const queue = await queuesService.createQueue({ labId: 'lab9' }, user.id);
302283

303284
await queuesService.joinQueue(queue.id, req);
304285

@@ -331,10 +312,7 @@ describe('QueuesService', () => {
331312
},
332313
} as unknown as Request;
333314

334-
const queue = await queuesService.createQueue({
335-
creatorId: user.id,
336-
labId: 'lab10',
337-
});
315+
const queue = await queuesService.createQueue({ labId: 'lab10' }, user.id);
338316

339317
await expect(queuesService.leaveQueue(queue.id, req)).rejects.toThrow(NotFoundException);
340318
});
@@ -370,10 +348,7 @@ describe('QueuesService', () => {
370348
},
371349
} as unknown as Request;
372350

373-
const queue = await queuesService.createQueue({
374-
creatorId: user.id,
375-
labId: 'lab11',
376-
});
351+
const queue = await queuesService.createQueue({ labId: 'lab11' }, user.id);
377352

378353
await queuesService.joinQueue(queue.id, {
379354
user: {
@@ -421,10 +396,7 @@ describe('QueuesService', () => {
421396
},
422397
} as unknown as Request;
423398

424-
const queue = await queuesService.createQueue({
425-
creatorId: user.id,
426-
labId: 'lab12',
427-
});
399+
const queue = await queuesService.createQueue({ labId: 'lab12' }, user.id);
428400

429401
await queuesService.joinQueue(queue.id, {
430402
user: {
@@ -464,10 +436,7 @@ describe('QueuesService', () => {
464436
},
465437
} as unknown as Request;
466438

467-
const queue = await queuesService.createQueue({
468-
creatorId: user.id,
469-
labId: 'lab13',
470-
});
439+
const queue = await queuesService.createQueue({ labId: 'lab13' }, user.id);
471440

472441
await expect(queuesService.leaveQueue(queue.id, req)).rejects.toThrow(NotFoundException);
473442
});
@@ -493,10 +462,9 @@ describe('QueuesService', () => {
493462
} as unknown as Request;
494463

495464
const queue = await queuesService.createQueue({
496-
creatorId: admin.id,
497465
labId: 'lab14',
498466
status: 'COMPLETED',
499-
});
467+
}, admin.id);
500468

501469
await queuesService.resumeQueueStatus(queue.id, req);
502470
const updatedQueue = await queuesService.findQueueById(queue.id);
@@ -522,10 +490,9 @@ describe('QueuesService', () => {
522490
} as unknown as Request;
523491

524492
const queue = await queuesService.createQueue({
525-
creatorId: user.id,
526493
labId: 'lab15',
527494
status: 'SKIPPED',
528-
});
495+
}, user.id);
529496

530497
await expect(queuesService.resumeQueueStatus(queue.id, req)).rejects.toThrow(ForbiddenException);
531498
});
@@ -549,10 +516,9 @@ describe('QueuesService', () => {
549516
} as unknown as Request;
550517

551518
const queue = await queuesService.createQueue({
552-
creatorId: admin.id,
553519
labId: 'lab16',
554520
status: 'PENDING',
555-
});
521+
}, admin.id);
556522

557523
await expect(queuesService.resumeQueueStatus(queue.id, req)).rejects.toThrow(ConflictException);
558524
});

src/queues/queues.service.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export class QueuesService {
1212
private readonly userService: UsersService,
1313
) {}
1414

15-
async createQueue (queue: CreateQueueDto): Promise<Queue> {
16-
return this.queueRepository.create(queue);
15+
async createQueue (queue: CreateQueueDto, creatorId: string): Promise<Queue> {
16+
return this.queueRepository.create(queue, creatorId);
1717
}
1818

1919
async findQueueById (id: string): Promise<Queue> {
@@ -22,6 +22,10 @@ export class QueuesService {
2222
return queue;
2323
}
2424

25+
async findAllQueuesByLabId (labId: string): Promise<Queue[]> {
26+
return this.queueRepository.findAllByLabId(labId);
27+
}
28+
2529
async deleteQueueById (id: string, req: Request): Promise<Queue> {
2630
const userId = req['user'].userId;
2731
const isAdmin = await this.userService.isAdmin(userId);

src/repositories/queues.repository.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { Injectable } from '@nestjs/common';
22
import { PrismaService } from '../prisma/prisma.service';
3-
import { CreateQueueDto } from '../queues/dto/create-queue.dto';
43
import { QueueStatus } from '@prisma/client';
4+
import { CreateQueueDto } from '../queues/dto/create-queue.dto';
55

66
@Injectable()
77

88
export class QueuesRepository {
99
constructor (private readonly prisma: PrismaService) {}
1010

11-
async create (createQueueDto: CreateQueueDto) {
12-
const { creatorId, labId, status } = createQueueDto;
11+
async create (createQueueDto: CreateQueueDto, creatorId: string) {
12+
const { labId, status } = createQueueDto;
1313
return this.prisma.queue.create({
1414
data: {
1515
creatorId,
@@ -25,6 +25,12 @@ export class QueuesRepository {
2525
});
2626
}
2727

28+
async findAllByLabId (labId: string) {
29+
return this.prisma.queue.findMany({
30+
where: { labId },
31+
});
32+
}
33+
2834
async deleteById (id: string) {
2935
return this.prisma.queue.delete({
3036
where: { id },

0 commit comments

Comments
 (0)