diff --git a/prisma/migrations/20240602144148_delete_creator_from_queue/migration.sql b/prisma/migrations/20240602144148_delete_creator_from_queue/migration.sql new file mode 100644 index 0000000..a556b16 --- /dev/null +++ b/prisma/migrations/20240602144148_delete_creator_from_queue/migration.sql @@ -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"; diff --git a/prisma/migrations/20240602160356_add_constraint_for_lab_id/migration.sql b/prisma/migrations/20240602160356_add_constraint_for_lab_id/migration.sql new file mode 100644 index 0000000..d23ee15 --- /dev/null +++ b/prisma/migrations/20240602160356_add_constraint_for_lab_id/migration.sql @@ -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"); diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 49b38ed..d61ad7e 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -26,7 +26,6 @@ model User { refreshToken String? @unique verifiedToken String? @unique - createdQueues Queue[] joinedQueues UserQueue[] @@map("users") @@ -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") diff --git a/src/auth/dto/register.dto.ts b/src/auth/dto/register.dto.ts index 6f1cd7f..b6c9438 100644 --- a/src/auth/dto/register.dto.ts +++ b/src/auth/dto/register.dto.ts @@ -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() diff --git a/src/main.ts b/src/main.ts index 0428b12..7d99c4a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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))); diff --git a/src/queues/queue.controller.ts b/src/queues/queue.controller.ts index c98c9db..4e1623d 100644 --- a/src/queues/queue.controller.ts +++ b/src/queues/queue.controller.ts @@ -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); diff --git a/src/queues/queues.service.ts b/src/queues/queues.service.ts index dc3a257..5477ed3 100644 --- a/src/queues/queues.service.ts +++ b/src/queues/queues.service.ts @@ -17,8 +17,10 @@ export class QueuesService { } async findQueueById (id: string): Promise { - 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; } @@ -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'); } @@ -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); } @@ -80,5 +82,4 @@ export class QueuesService { return await this.queueRepository.updateQueueStatus(queueId, 'PENDING'); } - } \ No newline at end of file diff --git a/src/repositories/queues.repository.ts b/src/repositories/queues.repository.ts index d9f1cfc..656a420 100644 --- a/src/repositories/queues.repository.ts +++ b/src/repositories/queues.repository.ts @@ -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, + } + } + } }); }