All files / server/src/features/notifications queueCancelledDeletedEmails.ts

96.29% Statements 26/27
86.66% Branches 26/30
100% Functions 2/2
96.29% Lines 26/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123                                    60x       105x 105x 105x 105x     105x 76x           76x           15x             70x           18x       64x           11x             59x           5x             57x           27x             22x     105x 37x     86x 86x 7x     7x     79x 79x                  
import { config } from "shared/config";
import { EmailNotificationTrigger } from "shared/types/emailNotification";
import { EventLogAction } from "shared/types/models/eventLog";
import { findOrCreateSettings } from "server/features/settings/settingsRepository";
import { logger } from "server/utils/logger";
import {
  NotificationTask,
  NotificationTaskType,
  getGlobalNotificationQueueService,
} from "server/utils/notificationQueue";
 
interface ProgramItemChangeUpdate {
  username: string;
  programItemId: string;
  programItemStartTime: string;
  action: EventLogAction;
}
 
export const queueCancelledDeletedEmails = async (
  updates: ProgramItemChangeUpdate[],
  programItemTitlesById: Map<string, string>,
): Promise<void> => {
  const settingsResult = await findOrCreateSettings();
  let emailNotificationTrigger = config.server().emailNotificationTrigger;
  Eif (settingsResult.ok) {
    emailNotificationTrigger = settingsResult.value.emailNotificationTrigger;
  }
 
  const emailTasks = updates.flatMap((update): NotificationTask[] => {
    const base = {
      username: update.username,
      programItemId: update.programItemId,
      programItemStartTime: update.programItemStartTime,
      programItemTitle: programItemTitlesById.get(update.programItemId) ?? "",
    };
    if (
      update.action === EventLogAction.PROGRAM_ITEM_CANCELLED &&
      emailNotificationTrigger.includes(
        EmailNotificationTrigger.PROGRAM_ITEM_CANCELLED,
      )
    ) {
      return [
        {
          ...base,
          type: NotificationTaskType.SEND_EMAIL_PROGRAM_ITEM_CANCELLED,
        },
      ];
    }
    if (
      update.action === EventLogAction.PROGRAM_ITEM_DELETED &&
      emailNotificationTrigger.includes(
        EmailNotificationTrigger.PROGRAM_ITEM_DELETED,
      )
    ) {
      return [
        { ...base, type: NotificationTaskType.SEND_EMAIL_PROGRAM_ITEM_DELETED },
      ];
    }
    if (
      update.action === EventLogAction.PROGRAM_ITEM_NO_KONSTI_SIGNUP_ANYMORE &&
      emailNotificationTrigger.includes(
        EmailNotificationTrigger.PROGRAM_ITEM_NO_KONSTI_SIGNUP_ANYMORE,
      )
    ) {
      return [
        {
          ...base,
          type: NotificationTaskType.SEND_EMAIL_PROGRAM_ITEM_NO_KONSTI_SIGNUP_ANYMORE,
        },
      ];
    }
    if (
      update.action === EventLogAction.PROGRAM_ITEM_NO_LOTTERY_ANYMORE &&
      emailNotificationTrigger.includes(
        EmailNotificationTrigger.PROGRAM_ITEM_NO_LOTTERY_ANYMORE,
      )
    ) {
      return [
        {
          ...base,
          type: NotificationTaskType.SEND_EMAIL_PROGRAM_ITEM_NO_LOTTERY_ANYMORE,
        },
      ];
    }
    Eif (
      update.action === EventLogAction.PROGRAM_ITEM_MOVED &&
      emailNotificationTrigger.includes(
        EmailNotificationTrigger.PROGRAM_ITEM_TIME_CHANGED,
      )
    ) {
      return [
        {
          ...base,
          type: NotificationTaskType.SEND_EMAIL_PROGRAM_ITEM_TIME_CHANGED,
        },
      ];
    }
    return [];
  });
 
  if (emailTasks.length === 0) {
    return;
  }
 
  const queueService = getGlobalNotificationQueueService();
  if (queueService === null) {
    logger.warn(
      "Notification queue not initialized, skipping program item change emails",
    );
    return;
  }
 
  const queueResult = queueService.addNotificationsBulk(emailTasks);
  Iif (!queueResult.ok) {
    logger.error(
      new Error(
        "Failed to queue cancelled/deleted program item email notifications",
        { cause: queueResult.error },
      ),
    );
  }
};