diff --git a/src/modules/restaurants/controllers/schedule.controller.ts b/src/modules/restaurants/controllers/schedule.controller.ts index ffc994a..6b2959f 100644 --- a/src/modules/restaurants/controllers/schedule.controller.ts +++ b/src/modules/restaurants/controllers/schedule.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get, Post, Body, Patch, Param, Delete, Query, ValidationPipe } from '@nestjs/common'; +import { Controller, Get, Post, Body, Patch, Param, Delete, Query, ValidationPipe, UseGuards } from '@nestjs/common'; import { ApiTags, ApiOperation, @@ -13,9 +13,12 @@ import { ScheduleService } from '../providers/schedule.service'; import { CreateScheduleDto } from '../dto/create-schedule.dto'; import { UpdateScheduleDto } from '../dto/update-schedule.dto'; import { Schedule } from '../entities/schedule.entity'; +import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard'; +import { RestId } from 'src/common/decorators/rest-id.decorator'; @ApiTags('schedules') @Controller('schedules') +@UseGuards(AdminAuthGuard) export class ScheduleController { constructor(private readonly scheduleService: ScheduleService) {} @@ -23,15 +26,15 @@ export class ScheduleController { @ApiOperation({ summary: 'ایجاد برنامه جدید' }) @ApiBody({ type: CreateScheduleDto }) @ApiCreatedResponse({ description: 'برنامه ایجاد شد', type: Schedule }) - create(@Body(new ValidationPipe({ transform: true, whitelist: true })) createScheduleDto: CreateScheduleDto) { - return this.scheduleService.create(createScheduleDto); + create(@Body() createScheduleDto: CreateScheduleDto, @RestId() restId: string) { + return this.scheduleService.create(restId, createScheduleDto); } @Get() @ApiOperation({ summary: 'لیست همه برنامه‌ها' }) @ApiQuery({ name: 'restId', required: false, type: String, description: 'شناسه رستوران برای فیلتر کردن' }) @ApiOkResponse({ description: 'لیست برنامه‌ها', type: [Schedule] }) - findAll(@Query('restId') restId?: string) { + findAll(@RestId() restId: string) { return this.scheduleService.findAll(restId); } @@ -40,8 +43,8 @@ export class ScheduleController { @ApiParam({ name: 'id', description: 'شناسه برنامه' }) @ApiOkResponse({ description: 'جزئیات برنامه', type: Schedule }) @ApiNotFoundResponse({ description: 'برنامه یافت نشد' }) - findOne(@Param('id') id: string) { - return this.scheduleService.findOne(id); + findOne(@Param('id') id: string, @RestId() restId: string) { + return this.scheduleService.findOne(id, restId); } @Patch(':id') @@ -50,11 +53,8 @@ export class ScheduleController { @ApiBody({ type: UpdateScheduleDto }) @ApiOkResponse({ description: 'برنامه به‌روز شد', type: Schedule }) @ApiNotFoundResponse({ description: 'برنامه یافت نشد' }) - update( - @Param('id') id: string, - @Body(new ValidationPipe({ transform: true, whitelist: true })) updateScheduleDto: UpdateScheduleDto, - ) { - return this.scheduleService.update(id, updateScheduleDto); + update(@Param('id') id: string, @Body() updateScheduleDto: UpdateScheduleDto, @RestId() restId: string) { + return this.scheduleService.update(id, restId, updateScheduleDto); } @Delete(':id') @@ -62,7 +62,7 @@ export class ScheduleController { @ApiParam({ name: 'id', description: 'شناسه برنامه' }) @ApiOkResponse({ description: 'برنامه حذف شد' }) @ApiNotFoundResponse({ description: 'برنامه یافت نشد' }) - remove(@Param('id') id: string) { - return this.scheduleService.remove(id); + remove(@Param('id') id: string, @RestId() restId: string) { + return this.scheduleService.remove(id, restId); } } diff --git a/src/modules/restaurants/dto/create-schedule.dto.ts b/src/modules/restaurants/dto/create-schedule.dto.ts index 41357c3..cac8dd7 100644 --- a/src/modules/restaurants/dto/create-schedule.dto.ts +++ b/src/modules/restaurants/dto/create-schedule.dto.ts @@ -30,7 +30,4 @@ export class CreateScheduleDto { @Type(() => Boolean) isActive?: boolean; - @ApiProperty({ example: 'rest-ulid-123', description: 'شناسه رستوران' }) - @IsString() - restId!: string; } diff --git a/src/modules/restaurants/providers/schedule.service.ts b/src/modules/restaurants/providers/schedule.service.ts index 4e0b05a..3880dea 100644 --- a/src/modules/restaurants/providers/schedule.service.ts +++ b/src/modules/restaurants/providers/schedule.service.ts @@ -1,4 +1,4 @@ -import { Injectable, NotFoundException } from '@nestjs/common'; +import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common'; import { EntityManager, RequiredEntityData } from '@mikro-orm/postgresql'; import { Schedule } from '../entities/schedule.entity'; import { ScheduleRepository } from '../repositories/schedule.repository'; @@ -15,13 +15,23 @@ export class ScheduleService { private readonly em: EntityManager, ) {} - async create(createScheduleDto: CreateScheduleDto): Promise { + async create(restId: string, createScheduleDto: CreateScheduleDto): Promise { + const rest = await this.restRepository.findOne({ id: restId }); + if (!rest) { + throw new NotFoundException(`رستوران با شناسه ${restId} یافت نشد.`); + } + + const existingSchedule = await this.scheduleRepository.findOne({ restId, weekDay: createScheduleDto.weekDay }); + if (existingSchedule) { + throw new BadRequestException(`برنامه برای این روز هفته و رستوران قبلا ایجاد شده است.`); + } + const data: RequiredEntityData = { weekDay: createScheduleDto.weekDay, openTime: createScheduleDto.openTime, closeTime: createScheduleDto.closeTime, isActive: createScheduleDto.isActive ?? true, - restId: '01KA35CFFN0F6VA04DF0W6KCE3', + restId, }; const schedule = this.scheduleRepository.create(data); @@ -34,16 +44,16 @@ export class ScheduleService { return this.scheduleRepository.find(where); } - async findOne(id: string): Promise { - const schedule = await this.scheduleRepository.findOne({ id }); + async findOne(id: string, restId: string): Promise { + const schedule = await this.scheduleRepository.findOne({ id, restId }); if (!schedule) { throw new NotFoundException(`برنامه با شناسه ${id} یافت نشد.`); } return schedule; } - async update(id: string, updateScheduleDto: UpdateScheduleDto): Promise { - const schedule = await this.scheduleRepository.findOne({ id }); + async update(id: string, restId: string, updateScheduleDto: UpdateScheduleDto): Promise { + const schedule = await this.scheduleRepository.findOne({ id, restId }); if (!schedule) { throw new NotFoundException(`برنامه با شناسه ${id} یافت نشد.`); } @@ -61,17 +71,14 @@ export class ScheduleService { if (updateScheduleDto.isActive !== undefined) { updateData.isActive = updateScheduleDto.isActive; } - // if (updateScheduleDto.restId) { - // updateData.restId = updateScheduleDto.restId; - // } this.em.assign(schedule, updateData); await this.em.persistAndFlush(schedule); return schedule; } - async remove(id: string): Promise { - const schedule = await this.scheduleRepository.findOne({ id }); + async remove(id: string, restId: string): Promise { + const schedule = await this.scheduleRepository.findOne({ id, restId }); if (!schedule) { throw new NotFoundException(`برنامه با شناسه ${id} یافت نشد.`); }