seed prefrences
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
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<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,
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user