From 019b477d2d0b9888e3c082939bdb25e0d9c636a3 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Tue, 24 Oct 2023 05:16:04 +0400 Subject: [PATCH] Reminder helpers for creating and deleting crons --- backend/src/helpers/reminder.ts | 55 +++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 backend/src/helpers/reminder.ts diff --git a/backend/src/helpers/reminder.ts b/backend/src/helpers/reminder.ts new file mode 100644 index 000000000..ba4cb9fed --- /dev/null +++ b/backend/src/helpers/reminder.ts @@ -0,0 +1,55 @@ + +import { ISecret } from "../models" +import { createSecretReminderCron, deleteSecretReminderCron, updateSecretReminderCron } from "../queues/reminders/sendSecretReminders" + + +type TPartialSecret = Pick +type TPartialSecretDeleteReminder = Pick + + + +export const createReminder = async (oldSecret: TPartialSecret, newSecret: TPartialSecret) => { + if(oldSecret._id !== newSecret._id) { + throw new Error("Secret id's don't match") + } + + if(!newSecret.secretReminderCron) { + throw new Error("No cron provided") + } + + const secretId = oldSecret._id.toString() + const workspaceId = oldSecret.workspace.toString() + + if(oldSecret.secretReminderCron) { + // This will first delete the existing cron job, and then create a new one. + await updateSecretReminderCron({ + workspaceId, + secretId, + cron: newSecret.secretReminderCron, + note: newSecret.secretReminderNote + }) + } else { + // This will create a new cron job. + await createSecretReminderCron({ + workspaceId, + secretId, + cron: newSecret.secretReminderCron, + note: newSecret.secretReminderNote + }) + } +} + +export const deleteReminder = async (secret: TPartialSecretDeleteReminder) => { + if(!secret._id) { + throw new Error("No secret id provided") + } + + if(!secret.secretReminderCron) { + throw new Error("No cron provided") + } + + await deleteSecretReminderCron({ + secretId: secret._id.toString(), + cron: secret.secretReminderCron, + }) +} \ No newline at end of file