import type { EntityManager } from '@mikro-orm/core'; import { Notification } from '../modules/notification/entities/notification.entity'; import { Admin } from '../modules/admin/entities/admin.entity'; import { User } from '../modules/user/entities/user.entity'; import { Restaurant } from '../modules/restaurants/entities/restaurant.entity'; import { NotifTitleEnum } from '../modules/notification/interfaces/notification.interface'; export class NotificationsSeeder { /** * Seeds notifications for all admins and users for every restaurant. * Ensures up to 33 notifications exist per (restaurant, admin) and (restaurant, user). */ async run(em: EntityManager): Promise { const restaurants = await em.find(Restaurant, {}); const admins = await em.find(Admin, {}); const users = await em.find(User, {}); const titles = Object.values(NotifTitleEnum); for (const restaurant of restaurants) { if (!restaurant) continue; // Admin notifications for (const admin of admins) { if (!admin) continue; const existingCount = await em.count(Notification, { restaurant: { id: restaurant.id }, admin: { id: admin.id }, }); const toCreate = Math.max(0, 33 - (existingCount || 0)); for (let i = 0; i < toCreate; i++) { const notif = em.create(Notification, { restaurant, admin, title: titles[i % titles.length] as NotifTitleEnum, content: `Seeded notification ${i + 1} for admin ${admin.id} at ${restaurant.slug}`, }); em.persist(notif); } } // User notifications for (const user of users) { if (!user) continue; const existingCount = await em.count(Notification, { restaurant: { id: restaurant.id }, user: { id: user.id }, }); const toCreate = Math.max(0, 33 - (existingCount || 0)); for (let i = 0; i < toCreate; i++) { const notif = em.create(Notification, { restaurant, user, title: titles[i % titles.length] as NotifTitleEnum, content: `Seeded notification ${i + 1} for user ${user.id} at ${restaurant.slug}`, }); em.persist(notif); } } } await em.flush(); } }