37 lines
1.3 KiB
TypeScript
37 lines
1.3 KiB
TypeScript
import type { EntityManager } from '@mikro-orm/core';
|
|
import { NotificationPreference } from '../modules/notification/entities/notification-preference.entity';
|
|
import type { Restaurant } from '../modules/restaurants/entities/restaurant.entity';
|
|
import { notificationPreferencesData } from './data/notification-preferences.data';
|
|
|
|
export class NotificationPreferencesSeeder {
|
|
async run(em: EntityManager, restaurants: Restaurant[]): Promise<void> {
|
|
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,
|
|
channels: preferenceData.channels,
|
|
});
|
|
em.persist(preference);
|
|
} else {
|
|
// Update existing preference if notification type changed
|
|
if (existing.channels.length !== preferenceData.channels.length) {
|
|
existing.channels = preferenceData.channels;
|
|
em.persist(existing);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
await em.flush();
|
|
}
|
|
}
|