diff --git a/src/modules/restaurants/controllers/public-schedule.controller.ts b/src/modules/restaurants/controllers/public-schedule.controller.ts new file mode 100644 index 0000000..98e7d36 --- /dev/null +++ b/src/modules/restaurants/controllers/public-schedule.controller.ts @@ -0,0 +1,17 @@ +import { Controller, Get } from '@nestjs/common'; +import { ScheduleService } from '../providers/schedule.service'; +import { Schedule } from '../entities/schedule.entity'; +import { ApiTags, ApiOperation, ApiOkResponse } from '@nestjs/swagger'; + +@ApiTags('schedules') +@Controller('schedules') +export class PublicScheduleController { + constructor(private readonly scheduleService: ScheduleService) {} + + @Get('today') + @ApiOperation({ summary: 'Get all today schedules of restaurants' }) + @ApiOkResponse({ description: 'List of today schedules', type: [Schedule] }) + async getTodaySchedules(): Promise { + return await this.scheduleService.findTodaySchedules(); + } +} diff --git a/src/modules/restaurants/providers/schedule.service.ts b/src/modules/restaurants/providers/schedule.service.ts index 7c300e6..07a0888 100644 --- a/src/modules/restaurants/providers/schedule.service.ts +++ b/src/modules/restaurants/providers/schedule.service.ts @@ -1,5 +1,6 @@ import { Injectable, NotFoundException } from '@nestjs/common'; import { EntityManager, RequiredEntityData } from '@mikro-orm/postgresql'; +import { FilterQuery } from '@mikro-orm/core'; import { Schedule } from '../entities/schedule.entity'; import { ScheduleRepository } from '../repositories/schedule.repository'; import { CreateScheduleDto } from '../dto/create-schedule.dto'; @@ -36,15 +37,27 @@ export class ScheduleService { } async findAll(restId?: string, query?: FindSchedulesDto): Promise { - const where: any = restId ? { restId } : {}; + const where: FilterQuery = restId ? { restId } : {}; - if (query?.weekDay) { + if (query?.weekDay !== undefined) { where.weekDay = query.weekDay; } return this.scheduleRepository.find(where, { orderBy: { weekDay: 'asc' } }); } + async findTodaySchedules(): Promise { + const today = new Date(); + const weekDay = today.getDay(); // 0 = Sunday, 6 = Saturday + + const where: FilterQuery = { + weekDay, + isActive: true, + }; + + return this.scheduleRepository.find(where, { orderBy: { openTime: 'asc' } }); + } + async findOne(id: string, restId: string): Promise { const schedule = await this.scheduleRepository.findOne({ id, restId }); if (!schedule) { diff --git a/src/modules/restaurants/restaurants.module.ts b/src/modules/restaurants/restaurants.module.ts index af2106c..6e74309 100644 --- a/src/modules/restaurants/restaurants.module.ts +++ b/src/modules/restaurants/restaurants.module.ts @@ -9,11 +9,12 @@ import { ScheduleRepository } from './repositories/schedule.repository'; import { Schedule } from './entities/schedule.entity'; import { ScheduleService } from './providers/schedule.service'; import { ScheduleController } from './controllers/schedule.controller'; +import { PublicScheduleController } from './controllers/public-schedule.controller'; import { JwtModule } from '@nestjs/jwt'; import { AuthModule } from '../auth/auth.module'; @Module({ - controllers: [RestaurantsController, PublicRestaurantController, ScheduleController], + controllers: [RestaurantsController, PublicRestaurantController, ScheduleController, PublicScheduleController], providers: [RestaurantsService, RestRepository, ScheduleRepository, ScheduleService], imports: [MikroOrmModule.forFeature([Restaurant, Schedule]), JwtModule, forwardRef(() => AuthModule)], exports: [RestRepository, ScheduleRepository, ScheduleService],