From bd72d00b76cf4183bd798f219c993becd7942996 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Thu, 11 Dec 2025 08:48:44 +0330 Subject: [PATCH] seed prefrences --- src/seeders/DatabaseSeeder.ts | 15 +++++--- .../data/notification-preferences.data.ts | 26 ++++++++++++++ .../notification-preferences.seeder.ts | 36 +++++++++++++++++++ 3 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 src/seeders/data/notification-preferences.data.ts create mode 100644 src/seeders/notification-preferences.seeder.ts diff --git a/src/seeders/DatabaseSeeder.ts b/src/seeders/DatabaseSeeder.ts index 7b043d9..b1322c2 100644 --- a/src/seeders/DatabaseSeeder.ts +++ b/src/seeders/DatabaseSeeder.ts @@ -12,6 +12,7 @@ import { FoodsSeeder } from './foods.seeder'; import { AdminsSeeder } from './admins.seeder'; import { UsersSeeder } from './users.seeder'; import { CouponsSeeder } from './coupons.seeder'; +import { NotificationPreferencesSeeder } from './notification-preferences.seeder'; export class DatabaseSeeder extends Seeder { async run(em: EntityManager) { @@ -40,23 +41,27 @@ export class DatabaseSeeder extends Seeder { const schedulesSeeder = new SchedulesSeeder(); await schedulesSeeder.run(em, allRestaurants); - // 7. Create Categories + // 7. Create Notification Preferences for all Restaurants + const notificationPreferencesSeeder = new NotificationPreferencesSeeder(); + await notificationPreferencesSeeder.run(em, allRestaurants); + + // 8. Create Categories const categoriesSeeder = new CategoriesSeeder(); const categoriesMap = await categoriesSeeder.run(em, restaurantsMap); - // 8. Create Foods + // 9. Create Foods const foodsSeeder = new FoodsSeeder(); await foodsSeeder.run(em, restaurantsMap, categoriesMap); - // 9. Create Coupons + // 10. Create Coupons const couponsSeeder = new CouponsSeeder(); await couponsSeeder.run(em, restaurantsMap); - // 10. Create Admins + // 11. Create Admins const adminsSeeder = new AdminsSeeder(); await adminsSeeder.run(em, rolesMap, restaurantsMap); - // 11. Create Users + // 12. Create Users const usersSeeder = new UsersSeeder(); await usersSeeder.run(em); } diff --git a/src/seeders/data/notification-preferences.data.ts b/src/seeders/data/notification-preferences.data.ts new file mode 100644 index 0000000..6568603 --- /dev/null +++ b/src/seeders/data/notification-preferences.data.ts @@ -0,0 +1,26 @@ +import { NotificationTitleEnum } from '../../modules/notifications/interfaces/notification.interface'; +import { NotificationType } from '../../modules/notifications/interfaces/notification.interface'; + +export interface NotificationPreferenceData { + title: NotificationTitleEnum; + notificationType: NotificationType; +} + +export const notificationPreferencesData: NotificationPreferenceData[] = [ + { + title: NotificationTitleEnum.ORDER_CREATED, + notificationType: NotificationType.SMS, + }, + { + title: NotificationTitleEnum.PAYMENT_SUCCESS, + notificationType: NotificationType.SMS, + }, + { + title: NotificationTitleEnum.REVIEW_CREATED, + notificationType: NotificationType.PUSH, + }, + { + title: NotificationTitleEnum.ORDER_STATUS_CHANGED, + notificationType: NotificationType.Both, + }, +]; diff --git a/src/seeders/notification-preferences.seeder.ts b/src/seeders/notification-preferences.seeder.ts new file mode 100644 index 0000000..28f522c --- /dev/null +++ b/src/seeders/notification-preferences.seeder.ts @@ -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 { + 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(); + } +}