seed prefrences
This commit is contained in:
@@ -12,6 +12,7 @@ import { FoodsSeeder } from './foods.seeder';
|
|||||||
import { AdminsSeeder } from './admins.seeder';
|
import { AdminsSeeder } from './admins.seeder';
|
||||||
import { UsersSeeder } from './users.seeder';
|
import { UsersSeeder } from './users.seeder';
|
||||||
import { CouponsSeeder } from './coupons.seeder';
|
import { CouponsSeeder } from './coupons.seeder';
|
||||||
|
import { NotificationPreferencesSeeder } from './notification-preferences.seeder';
|
||||||
|
|
||||||
export class DatabaseSeeder extends Seeder {
|
export class DatabaseSeeder extends Seeder {
|
||||||
async run(em: EntityManager) {
|
async run(em: EntityManager) {
|
||||||
@@ -40,23 +41,27 @@ export class DatabaseSeeder extends Seeder {
|
|||||||
const schedulesSeeder = new SchedulesSeeder();
|
const schedulesSeeder = new SchedulesSeeder();
|
||||||
await schedulesSeeder.run(em, allRestaurants);
|
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 categoriesSeeder = new CategoriesSeeder();
|
||||||
const categoriesMap = await categoriesSeeder.run(em, restaurantsMap);
|
const categoriesMap = await categoriesSeeder.run(em, restaurantsMap);
|
||||||
|
|
||||||
// 8. Create Foods
|
// 9. Create Foods
|
||||||
const foodsSeeder = new FoodsSeeder();
|
const foodsSeeder = new FoodsSeeder();
|
||||||
await foodsSeeder.run(em, restaurantsMap, categoriesMap);
|
await foodsSeeder.run(em, restaurantsMap, categoriesMap);
|
||||||
|
|
||||||
// 9. Create Coupons
|
// 10. Create Coupons
|
||||||
const couponsSeeder = new CouponsSeeder();
|
const couponsSeeder = new CouponsSeeder();
|
||||||
await couponsSeeder.run(em, restaurantsMap);
|
await couponsSeeder.run(em, restaurantsMap);
|
||||||
|
|
||||||
// 10. Create Admins
|
// 11. Create Admins
|
||||||
const adminsSeeder = new AdminsSeeder();
|
const adminsSeeder = new AdminsSeeder();
|
||||||
await adminsSeeder.run(em, rolesMap, restaurantsMap);
|
await adminsSeeder.run(em, rolesMap, restaurantsMap);
|
||||||
|
|
||||||
// 11. Create Users
|
// 12. Create Users
|
||||||
const usersSeeder = new UsersSeeder();
|
const usersSeeder = new UsersSeeder();
|
||||||
await usersSeeder.run(em);
|
await usersSeeder.run(em);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,
|
||||||
|
},
|
||||||
|
];
|
||||||
@@ -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