Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Warnings:

- You are about to drop the column `creator_id` on the `queues` table. All the data in the column will be lost.

*/
-- DropForeignKey
ALTER TABLE "queues" DROP CONSTRAINT "queues_creator_id_fkey";

-- AlterTable
ALTER TABLE "queues" DROP COLUMN "creator_id";
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:

- A unique constraint covering the columns `[lab_id]` on the table `queues` will be added. If there are existing duplicate values, this will fail.

*/
-- CreateIndex
CREATE UNIQUE INDEX "queues_lab_id_key" ON "queues"("lab_id");
5 changes: 1 addition & 4 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ model User {
refreshToken String? @unique
verifiedToken String? @unique

createdQueues Queue[]
joinedQueues UserQueue[]

@@map("users")
Expand All @@ -40,13 +39,11 @@ enum QueueStatus {

model Queue {
id String @id @default(uuid())
creatorId String @map("creator_id")
labId String @map("lab_id")
labId String @map("lab_id") @unique
status QueueStatus @default(PENDING)
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")

creator User @relation(fields: [creatorId], references: [id], onDelete: Cascade)
participants UserQueue[]

@@map("queues")
Expand Down
2 changes: 1 addition & 1 deletion src/auth/dto/register.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class RegisterDto {
@IsString()
@IsNotEmpty()
@IsEmail()
@Matches(/^[a-zA-Z0-9._%+-]+@lll\.kpi\.ua$/, { message: 'email must be in @lll.kpu.ua domain' })
@Matches(/^[a-zA-Z0-9._%+-]+@lll\.kpi\.ua$/, { message: 'email must be in @lll.kpi.ua domain' })
email: string;

@IsString()
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AppModule } from './app.module';
import { ClassSerializerInterceptor, ValidationPipe } from '@nestjs/common';

async function bootstrap () {
const app = await NestFactory.create(AppModule);
const app = await NestFactory.create(AppModule, { cors: true });
const port = 3000;
app.useGlobalPipes(new ValidationPipe({ whitelist: true }));
app.useGlobalInterceptors(new ClassSerializerInterceptor(app.get(Reflector)));
Expand Down
7 changes: 0 additions & 7 deletions src/queues/queue.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ import { Request } from 'express';
export class QueuesController {
constructor (private readonly queuesService: QueuesService) {}

@Post()
async createQueue (@Body() createQueueDto: CreateQueueDto, @Req() req: Request) {
const creatorId = req['user'].userId;
return this.queuesService.createQueue(createQueueDto, creatorId);
}

@Public()
@Get(':id')
async findQueueById (@Param('id') id: string) {
return await this.queuesService.findQueueById(id);
Expand Down
11 changes: 6 additions & 5 deletions src/queues/queues.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ export class QueuesService {
}

async findQueueById (id: string): Promise<Queue> {
const queue = await this.queueRepository.findById(id);
if (!queue) throw new NotFoundException(`Queue with this ${id} id was not found`);
const queue = await this.queueRepository.findByLabId(id);
if (!queue) {
return this.createQueue({ labId: id } as CreateQueueDto);
}
return queue;
}

Expand All @@ -31,7 +33,7 @@ export class QueuesService {
const isAdmin = await this.userService.isAdmin(userId);

const queue = await this.findQueueById(id);
if (!isAdmin && queue.creatorId !== userId) {
if (!isAdmin) {
throw new ForbiddenException('You do not have permission to delete this queue');
}

Expand All @@ -45,7 +47,7 @@ export class QueuesService {
const userId = req['user'].userId;

const userInQueue = await this.queueRepository.findUserInQueue(queue.id, userId);
if (userInQueue) throw new ConflictException(`User with id ${userId} has already been found in queue`);
if (userInQueue) throw new ConflictException(`You have already joined this queue`);

return this.queueRepository.addUserToQueue(queue.id, userId);
}
Expand Down Expand Up @@ -80,5 +82,4 @@ export class QueuesService {

return await this.queueRepository.updateQueueStatus(queueId, 'PENDING');
}

}
21 changes: 17 additions & 4 deletions src/repositories/queues.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,33 @@ import { CreateQueueDto } from '../queues/dto/create-queue.dto';
export class QueuesRepository {
constructor (private readonly prisma: PrismaService) {}

async create (createQueueDto: CreateQueueDto, creatorId: string) {
async create (createQueueDto: CreateQueueDto) {
const { labId, status } = createQueueDto;
return this.prisma.queue.create({
data: {
creatorId,
labId,
status,
},
include: {
participants: {
include: {
user: true,
}
}
}
});
}

async findById (id: string) {
async findByLabId (labId: string) {
return this.prisma.queue.findUnique({
where: { id },
where: { labId },
include: {
participants: {
include: {
user: true,
}
}
}
});
}

Expand Down