seeder
This commit is contained in:
@@ -15,6 +15,7 @@ import { CouponsSeeder } from './coupons.seeder';
|
|||||||
import { NotificationPreferencesSeeder } from './notification-preferences.seeder';
|
import { NotificationPreferencesSeeder } from './notification-preferences.seeder';
|
||||||
import { UserWalletsSeeder } from './user-wallets.seeder';
|
import { UserWalletsSeeder } from './user-wallets.seeder';
|
||||||
import { InventorySeeder } from './inventory.seeder';
|
import { InventorySeeder } from './inventory.seeder';
|
||||||
|
import { NotificationsSeeder } from './notifications.seeder';
|
||||||
|
|
||||||
export class DatabaseSeeder extends Seeder {
|
export class DatabaseSeeder extends Seeder {
|
||||||
async run(em: EntityManager) {
|
async run(em: EntityManager) {
|
||||||
@@ -74,5 +75,9 @@ export class DatabaseSeeder extends Seeder {
|
|||||||
// 14. Create User Wallets
|
// 14. Create User Wallets
|
||||||
const userWalletsSeeder = new UserWalletsSeeder();
|
const userWalletsSeeder = new UserWalletsSeeder();
|
||||||
await userWalletsSeeder.run(em);
|
await userWalletsSeeder.run(em);
|
||||||
|
|
||||||
|
// 15. Create Notifications for admins and users for all restaurants
|
||||||
|
const notificationsSeeder = new NotificationsSeeder();
|
||||||
|
await notificationsSeeder.run(em);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
import type { EntityManager } from '@mikro-orm/core';
|
||||||
|
import { Notification } from '../modules/notifications/entities/notification.entity';
|
||||||
|
import { Admin } from '../modules/admin/entities/admin.entity';
|
||||||
|
import { User } from '../modules/users/entities/user.entity';
|
||||||
|
import { Restaurant } from '../modules/restaurants/entities/restaurant.entity';
|
||||||
|
import { NotifTitleEnum } from '../modules/notifications/interfaces/notification.interface';
|
||||||
|
|
||||||
|
export class NotificationsSeeder {
|
||||||
|
/**
|
||||||
|
* Seeds notifications for all admins and users for every restaurant.
|
||||||
|
* Ensures up to 33 notifications exist per (restaurant, admin) and (restaurant, user).
|
||||||
|
*/
|
||||||
|
async run(em: EntityManager): Promise<void> {
|
||||||
|
const restaurants = await em.find(Restaurant, {});
|
||||||
|
const admins = await em.find(Admin, {});
|
||||||
|
const users = await em.find(User, {});
|
||||||
|
|
||||||
|
const titles = Object.values(NotifTitleEnum);
|
||||||
|
|
||||||
|
for (const restaurant of restaurants) {
|
||||||
|
if (!restaurant) continue;
|
||||||
|
|
||||||
|
// Admin notifications
|
||||||
|
for (const admin of admins) {
|
||||||
|
if (!admin) continue;
|
||||||
|
|
||||||
|
const existingCount = await em.count(Notification, {
|
||||||
|
restaurant: { id: restaurant.id },
|
||||||
|
admin: { id: admin.id },
|
||||||
|
});
|
||||||
|
|
||||||
|
const toCreate = Math.max(0, 33 - (existingCount || 0));
|
||||||
|
for (let i = 0; i < toCreate; i++) {
|
||||||
|
const notif = em.create(Notification, {
|
||||||
|
restaurant,
|
||||||
|
admin,
|
||||||
|
title: titles[i % titles.length] as NotifTitleEnum,
|
||||||
|
content: `Seeded notification ${i + 1} for admin ${admin.id} at ${restaurant.slug}`,
|
||||||
|
});
|
||||||
|
em.persist(notif);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// User notifications
|
||||||
|
for (const user of users) {
|
||||||
|
if (!user) continue;
|
||||||
|
|
||||||
|
const existingCount = await em.count(Notification, {
|
||||||
|
restaurant: { id: restaurant.id },
|
||||||
|
user: { id: user.id },
|
||||||
|
});
|
||||||
|
|
||||||
|
const toCreate = Math.max(0, 33 - (existingCount || 0));
|
||||||
|
for (let i = 0; i < toCreate; i++) {
|
||||||
|
const notif = em.create(Notification, {
|
||||||
|
restaurant,
|
||||||
|
user,
|
||||||
|
title: titles[i % titles.length] as NotifTitleEnum,
|
||||||
|
content: `Seeded notification ${i + 1} for user ${user.id} at ${restaurant.slug}`,
|
||||||
|
});
|
||||||
|
em.persist(notif);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await em.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user