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 { 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} یافت نشد.`);
}