Implement restaurant ID handling in schedule operations; add AdminAuthGuard for access control; enhance schedule service with validation for existing schedules and restaurant existence checks.

This commit is contained in:
2025-11-18 09:21:37 +03:30
parent dcce6cfdda
commit 8975a9fcc7
3 changed files with 32 additions and 28 deletions
@@ -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);
}
}
@@ -30,7 +30,4 @@ export class CreateScheduleDto {
@Type(() => Boolean)
isActive?: boolean;
@ApiProperty({ example: 'rest-ulid-123', description: 'شناسه رستوران' })
@IsString()
restId!: string;
}
@@ -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<Schedule> {
async create(restId: string, createScheduleDto: CreateScheduleDto): Promise<Schedule> {
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<Schedule> = {
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<Schedule> {
const schedule = await this.scheduleRepository.findOne({ id });
async findOne(id: string, restId: string): Promise<Schedule> {
const schedule = await this.scheduleRepository.findOne({ id, restId });
if (!schedule) {
throw new NotFoundException(`برنامه با شناسه ${id} یافت نشد.`);
}
return schedule;
}
async update(id: string, updateScheduleDto: UpdateScheduleDto): Promise<Schedule> {
const schedule = await this.scheduleRepository.findOne({ id });
async update(id: string, restId: string, updateScheduleDto: UpdateScheduleDto): Promise<Schedule> {
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<void> {
const schedule = await this.scheduleRepository.findOne({ id });
async remove(id: string, restId: string): Promise<void> {
const schedule = await this.scheduleRepository.findOne({ id, restId });
if (!schedule) {
throw new NotFoundException(`برنامه با شناسه ${id} یافت نشد.`);
}