import type { EntityManager } from '@mikro-orm/core'; import { NotificationPreference } from '../modules/notifications/entities/notification-preference.entity'; import { Restaurant } from '../modules/restaurants/entities/restaurant.entity'; import { notificationPreferencesData } from './data/notification-preferences.data'; export class NotificationPreferencesSeeder { async run(em: EntityManager, restaurants: Restaurant[]): Promise { for (const restaurant of restaurants) { if (!restaurant) continue; for (const preferenceData of notificationPreferencesData) { const existing = await em.findOne(NotificationPreference, { restaurant: { id: restaurant.id }, title: preferenceData.title, }); if (!existing) { const preference = em.create(NotificationPreference, { restaurant, title: preferenceData.title, notificationType: preferenceData.notificationType, }); em.persist(preference); } else { // Update existing preference if notification type changed if (existing.notificationType !== preferenceData.notificationType) { existing.notificationType = preferenceData.notificationType; em.persist(existing); } } } } await em.flush(); } }