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

100% Statements 35/35
100% Branches 0/0
100% Functions 8/8
100% Lines 35/35

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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178                                54x 54x   54x   54x 54x         7x   7x   7x                   14x       14x         14x     14x     14x                 7x       7x         7x     7x     7x                 13x       13x         13x     13x     13x                 10x     10x     10x                 4x     4x     4x                 4x       4x         4x     4x     4x                     59x                
import { NotificationTask } from "server/utils/notificationQueue";
import { getDateAndTimeWithLocale } from "shared/utils/timeFormatter";
 
export interface EmailMessage {
  from: string;
  to: string;
  subject: string;
  text: string;
  html?: string;
}
 
interface EmailTemplate {
  subject: string;
  text: string;
}
 
const SUBJECT = "Konsti-arvonnan tulos / Results for Konsti lottery sign-up";
const CANCELLED_DELETED_SUBJECT = "Ohjelma peruttu / Program cancelled";
const SIGNUP_CHANGED_SUBJECT =
  "Ohjelman ilmoittautuminen muuttunut / Program sign-up method changed";
const PROGRAM_STARTING_TIME_CHANGED_SUBJECT =
  "Ohjelman aika muuttunut / Program time changed";
const SIGNATURE = "Terveisin / Sincerely Konsti";
 
export function getRejectedEmailTemplate(
  notification: NotificationTask,
): EmailTemplate {
  const bodyFi = `Hei ${notification.username}!
Et valitettavasti päässyt arvonnassa yhteenkään ohjelmaan johon ilmoittauduit.`;
  const bodyEn = `Hi ${notification.username}!
Unfortunately you did not get spot in the lottery sign-up.`;
  return {
    subject: SUBJECT,
    text: `${bodyFi}\n\n${bodyEn}\n\n${SIGNATURE}`,
  };
}
 
export function getAcceptedEmailTemplate(
  programItemTitle: string,
  notification: NotificationTask,
): EmailTemplate {
  const programStartTimeFi = getDateAndTimeWithLocale(
    notification.programItemStartTime,
    "fi",
  );
  const programStartTimeEn = getDateAndTimeWithLocale(
    notification.programItemStartTime,
    "en",
  );
 
  const bodyFi = `Hei ${notification.username}!
Olet ollut onnekas ja pääsit ohjelmaan ${programItemTitle}.
Ohjelma alkaa ${programStartTimeFi}.`;
  const bodyEn = `Hi ${notification.username}!
You got a spot in the program ${programItemTitle}.
The program will start at ${programStartTimeEn}.`;
  return {
    subject: SUBJECT,
    text: `${bodyFi}\n\n${bodyEn}\n\n${SIGNATURE}`,
  };
}
 
export function getProgramItemCancelledEmailTemplate(
  notification: NotificationTask,
): EmailTemplate {
  const programStartTimeFi = getDateAndTimeWithLocale(
    notification.programItemStartTime,
    "fi",
  );
  const programStartTimeEn = getDateAndTimeWithLocale(
    notification.programItemStartTime,
    "en",
  );
 
  const bodyFi = `Hei ${notification.username}!
Ohjelma ${notification.programItemTitle} on peruttu.
Ohjelman piti alkaa ${programStartTimeFi}.`;
  const bodyEn = `Hi ${notification.username}!
Program ${notification.programItemTitle} has been cancelled.
Program was supposed to start at ${programStartTimeEn}.`;
  return {
    subject: CANCELLED_DELETED_SUBJECT,
    text: `${bodyFi}\n\n${bodyEn}\n\n${SIGNATURE}`,
  };
}
 
export function getProgramItemDeletedEmailTemplate(
  notification: NotificationTask,
): EmailTemplate {
  const programStartTimeFi = getDateAndTimeWithLocale(
    notification.programItemStartTime,
    "fi",
  );
  const programStartTimeEn = getDateAndTimeWithLocale(
    notification.programItemStartTime,
    "en",
  );
 
  const bodyFi = `Hei ${notification.username}!
Ohjelma ${notification.programItemTitle} on poistettu ohjelmistosta.
Ohjelman piti alkaa ${programStartTimeFi}.`;
  const bodyEn = `Hi ${notification.username}!
Program ${notification.programItemTitle} has been removed from the program.
Program was supposed to start at ${programStartTimeEn}.`;
  return {
    subject: CANCELLED_DELETED_SUBJECT,
    text: `${bodyFi}\n\n${bodyEn}\n\n${SIGNATURE}`,
  };
}
 
export function getProgramItemNoKonstiSignupEmailTemplate(
  notification: NotificationTask,
): EmailTemplate {
  const bodyFi = `Hei ${notification.username}!
Ohjelma ${notification.programItemTitle} ei enää käytä Konsti-ilmoittautumista.
Ilmoittautumisesi ohjelmaan on poistettu.`;
  const bodyEn = `Hi ${notification.username}!
Program ${notification.programItemTitle} no longer uses Konsti sign-up.
Your sign-up for the program has been removed.`;
  return {
    subject: SIGNUP_CHANGED_SUBJECT,
    text: `${bodyFi}\n\n${bodyEn}\n\n${SIGNATURE}`,
  };
}
 
export function getProgramItemNoLotteryEmailTemplate(
  notification: NotificationTask,
): EmailTemplate {
  const bodyFi = `Hei ${notification.username}!
Ohjelma ${notification.programItemTitle} ei enää käytä arvontailmoittautumista.
Arvontailmoittautumisesi ohjelmaan on poistettu.`;
  const bodyEn = `Hi ${notification.username}!
Program ${notification.programItemTitle} no longer uses lottery sign-up.
Your lottery sign-up for the program has been removed.`;
  return {
    subject: SIGNUP_CHANGED_SUBJECT,
    text: `${bodyFi}\n\n${bodyEn}\n\n${SIGNATURE}`,
  };
}
 
export function getProgramItemTimeChangedEmailTemplate(
  notification: NotificationTask,
): EmailTemplate {
  const programStartTimeFi = getDateAndTimeWithLocale(
    notification.programItemStartTime,
    "fi",
  );
  const programStartTimeEn = getDateAndTimeWithLocale(
    notification.programItemStartTime,
    "en",
  );
 
  const bodyFi = `Hei ${notification.username}!
Ohjelman ${notification.programItemTitle} aikataulu on muuttunut.
Ohjelma alkaa nyt ${programStartTimeFi}.`;
  const bodyEn = `Hi ${notification.username}!
Program ${notification.programItemTitle} start time has changed.
The program will now start at ${programStartTimeEn}.`;
  return {
    subject: PROGRAM_STARTING_TIME_CHANGED_SUBJECT,
    text: `${bodyFi}\n\n${bodyEn}\n\n${SIGNATURE}`,
  };
}
 
export function buildEmail(
  template: EmailTemplate,
  to: string,
  from: string,
): EmailMessage {
  return {
    from,
    to,
    subject: template.subject,
    text: template.text,
    html: `<p>${template.text.replaceAll("\n", "<br />")}</p>`,
  };
}