Skip to content

Commit

Permalink
Code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MikkoKauhanen committed Jan 20, 2025
1 parent 0dfaff0 commit 628019a
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions aoe-web-backend/src/services/mailService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ const sendEmail = async (email: {
*/
export const sendSystemNotification = async (content: string): Promise<void> => {
try {
// If environment variable SEND_MAIL is true (1), not false (0).
if (parseInt(process.env.SEND_SYSTEM_NOTIFICATION_EMAIL, 10)) {
if (isEnabled('SEND_SYSTEM_NOTIFICATION_EMAIL')) {
await sendEmail({
to: process.env.ADMIN_EMAIL,
from: process.env.EMAIL_FROM,
Expand All @@ -80,9 +79,7 @@ export async function sendExpirationMail() {
try {
const materials = await getExpiredMaterials();
const emails = materials.filter((m) => m.email != undefined).map((m) => m.email);
if (!(process.env.SEND_EXPIRATION_NOTIFICATION_EMAIL === '1')) {
winstonLogger.debug('Material expiration email sending disabled');
} else {
if (isEnabled('SEND_EXPIRATION_NOTIFICATION_EMAIL')) {
for (const email of emails) {
const info = await sendEmail({
to: email,
Expand All @@ -93,6 +90,8 @@ export async function sendExpirationMail() {

winstonLogger.debug('Message sent: %s', info.MessageId);
}
} else {
winstonLogger.debug('Material expiration email sending disabled');
}
} catch (err) {
winstonLogger.error('Error in sendExpirationMail(): %o', err);
Expand All @@ -111,9 +110,8 @@ export async function sendRatingNotificationMail() {
holder[d.email] = d.materialname;
}
});
if (!(process.env.SEND_RATING_NOTIFICATION === '1')) {
winstonLogger.debug('Rating notification email sending disabled');
} else {

if (isEnabled('SEND_RATING_NOTIFICATION')) {
for (const element of emailArray) {
const mailOptions = {
from: process.env.EMAIL_FROM,
Expand All @@ -135,6 +133,8 @@ export async function sendRatingNotificationMail() {
winstonLogger.error(error);
}
}
} else {
winstonLogger.debug('Rating notification email sending disabled');
}
} catch (error) {
winstonLogger.debug('Error in sendRatingNotificationMail(): %o', error);
Expand Down Expand Up @@ -176,7 +176,7 @@ export async function sendVerificationEmail(user: string, email: string) {
const url = process.env.BASE_URL + 'verify?id=' + token_mail_verification;
const content = await verificationEmailText(url);

if (process.env.SEND_VERIFICATION_EMAIL === '1') {
if (isEnabled('SEND_VERIFICATION_EMAIL')) {
await sendEmail({
to: email,
from: process.env.EMAIL_FROM,
Expand Down Expand Up @@ -266,3 +266,7 @@ AOE-team
Detta är ett automatiskt meddelande. Om du vill inte få dessa meddelandena, kan di förändra dina inställningar i vyn Mitt konto på Biblioteket för öppna lärresurser.`;
return ratingNotificationText;
}

const isEnabled = (prop: string): boolean => {
return process.env[prop] === '1';
};

0 comments on commit 628019a

Please sign in to comment.