reminder.ts

1import cron from 'node-cron';
2import { EventEmitter } from 'events';
3import IReminders from '../interfaces/Reminders';
4
5export const eventEmitter = new EventEmitter();
6
7const list: IReminders[] = [
8	{
9		slug: 'update-master-key',
10		title: 'Update the master key',
11		message:
12			'To keep the system secure, the master key should be updated every 2 weeks.',
13		schedule: '0 0 */14 * *',
14		type: 'warning',
15		role: 'owner',
16	},
17];
18
19export const reminders = {
20	reminder: (slug: string) => {
21		eventEmitter.emit(
22			'reminder',
23			list.find((reminder) => reminder.slug === slug),
24		);
25	},
26	start: () => {
27		console.log('Starting reminders...');
28		list.forEach((reminder) => {
29			console.log(`Reminder started for ${reminder.title}! (${reminder.slug})`);
30			cron.schedule(reminder.schedule, () => {
31				reminders.reminder(reminder.slug);
32			});
33		});
34	},
35};
36