39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import type { EntityManager } from '@mikro-orm/core';
|
|
import { Schedule } from ../../../shops/entities/schedule.entity';
|
|
import type { Shop } from ../../../shops/entities/shop.entity';
|
|
import { timeSlots } from './data/schedules.data';
|
|
|
|
export class SchedulesSeeder {
|
|
async run(em: EntityManager, shops: Shop[]): Promise<void> {
|
|
for (const shop of shops) {
|
|
if (!shop) continue;
|
|
|
|
// Create schedules for each day (0 = Sunday, 6 = Saturday)
|
|
for (let weekDay = 0; weekDay <= 6; weekDay++) {
|
|
// Create 3 schedules per day
|
|
for (const timeSlot of timeSlots) {
|
|
const existing = await em.findOne(Schedule, {
|
|
restId: shop.id,
|
|
weekDay,
|
|
openTime: timeSlot.openTime,
|
|
closeTime: timeSlot.closeTime,
|
|
});
|
|
|
|
if (!existing) {
|
|
const schedule = em.create(Schedule, {
|
|
restId: shop.id,
|
|
weekDay,
|
|
openTime: timeSlot.openTime,
|
|
closeTime: timeSlot.closeTime,
|
|
isActive: true,
|
|
});
|
|
em.persist(schedule);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
await em.flush();
|
|
}
|
|
}
|