Files
dmail-api/database/seeders/notif-setting.seeder.ts
T
2025-07-20 10:15:13 +03:30

34 lines
1.5 KiB
TypeScript
Executable File

import { EntityManager, RequiredEntityData } from "@mikro-orm/postgresql";
import { Seeder } from "@mikro-orm/seeder";
import { Logger } from "@nestjs/common";
import { NotificationSetting } from "../../src/modules/settings/entities/notification-setting.entity";
import { NotifDescriptions, NotifType } from "../../src/modules/settings/enums/notif-settings.enum";
export class NotificationSettingSeeder extends Seeder {
private readonly logger = new Logger(NotificationSettingSeeder.name);
async run(em: EntityManager): Promise<void> {
const notifSettingsData: RequiredEntityData<NotificationSetting>[] = Object.entries(NotifDescriptions).map(([type, { category, fa }]) => ({
type: type as NotifType,
category,
description: fa,
createdAt: new Date(),
updatedAt: new Date(),
}));
for (const notifSettingData of notifSettingsData) {
const existingSetting = await em.findOne(NotificationSetting, { type: notifSettingData.type });
if (!existingSetting) {
const notifSetting = em.create(NotificationSetting, notifSettingData);
await em.persistAndFlush(notifSetting);
this.logger.log(`[NotificationSettingSeeder] Created notification setting: ${notifSettingData.type}`);
} else {
this.logger.log(`[NotificationSettingSeeder] Notification setting already exists: ${notifSettingData.type}`);
}
}
this.logger.log(`[NotificationSettingSeeder] Seeding completed for ${notifSettingsData.length} notification settings.`);
}
}