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
179
180
181
182
183
184
185import { db, event, graphqlQuery } from "#src/utils";
import { DateTime } from "luxon";
import { SendEmailCommand } from "@aws-sdk/client-ses";
import { SESClient } from "@aws-sdk/client-ses";
const REGION = "us-east-2";
const sesClient = new SESClient({ region: REGION });
const EMAIL_NOTIFICATION_FROM_ADDRESS = "support@equalifyapp.com";
// interfaces copied from /frontend/src/components/AuditEmailSubscriptionList.tsx
interface EmailSubscriptionList {
emails: EmailSubscriptionEmail[];
}
interface EmailSubscriptionEmail {
id: string;
email: string;
frequency: string; // daily|weekly|monthly
lastSent: string; // UTC date string
}
interface graphResponse {
email_notifications: EmailSubscriptionList;
response: JSON;
id: string;
name: string;
}
//"{\"emails\":[{\"id\":\"742170ae-37a2-4c22-b732-af19029130e3\",\"email\":\"sdanie28@uic.edu\",\"frequency\":\"Weekly\",\"lastSent\":\"2025-10-17T12:53:00.180Z\"},{\"id\":\"cd0bbc97-8e1c-4e2c-b3a3-3306a864dd61\",\"email\":\"negatia@gmail.com\",\"frequency\":\"Daily\",\"lastSent\":\"2025-10-17T12:53:00.180Z\"}]}",
export const processScheduledAuditEmails = async () => {
console.log("Started processing scheduled audit emails...");
// fetch the email_notification fields (when email_notifications and response are not null)
const query = {
query: `
query GetAudits {
audits(where: {email_notifications: {_is_null: false}, response: {_is_null: false}}) {
id
name
response
email_notifications
}
}`
};
//console.log(JSON.stringify({ query }));
const response = await graphqlQuery(query);
//console.log(JSON.stringify({ response }));
let sentCount = 0;
let subscriptionsCount = 0;
// extract the email_notifications column
const emailListArray: graphResponse[] = response.audits.map((email) => {
return {
email_notifications: JSON.parse(
email.email_notifications
) as EmailSubscriptionList,
reponse: email.reponse,
};
});
//console.log(emailListArray);
const currentTime = new Date();
emailListArray.forEach(async (audit) => {
if (
audit.email_notifications.emails &&
audit.email_notifications.emails.length > 0
) {
let newEmailNotificationField: EmailSubscriptionList =
audit.email_notifications;
audit.email_notifications.emails.forEach((email, index) => {
subscriptionsCount++;
const lastSent = DateTime.fromISO(email.lastSent);
let intervalDays = null;
switch (email.frequency.toLowerCase()) {
case "daily":
intervalDays = 1;
break;
case "weekly":
intervalDays = 7;
break;
case "monthly":
intervalDays = 30;
}
const dateLimit = lastSent.plus({ days: intervalDays });
if (DateTime.now() > dateLimit) {
// time to send email!
console.log(
`Sending email to ${email.email}. Last sent ${email.lastSent}, frequency: ${email.frequency}.`
);
sentCount++;
sendEmail(email.email, JSON.stringify(audit.response), `${email.frequency} Equalify Report for Audit ${audit.name}`)
newEmailNotificationField.emails[index].lastSent =
new Date().toISOString();
}else{
console.log(
`Skipping email to ${email.email}, last sent ${email.lastSent}, frequency: ${email.frequency}.`
);
}
});
// check if we need to update the email_notifications field in the database
if (
JSON.stringify(newEmailNotificationField) !=
JSON.stringify(audit.email_notifications)
) {
console.log(
"Updating lastSent in database...",
newEmailNotificationField
);
// update audit in database with new email_notification field
await db.query({
text: `UPDATE "audits" SET "email_notifications"=$1 WHERE "id"=$2`,
values: [JSON.stringify(newEmailNotificationField), audit.id]
});
}
}
});
await db.clean();
console.log(
`Finished processing ${subscriptionsCount} scheduled audit emails, ${sentCount} emails sent.`
);
return;
};
async function sendEmail(
address: string,
content: string,
subjectLine: string
) {
const sendEmailCommand = createSendEmailCommand(
address,
EMAIL_NOTIFICATION_FROM_ADDRESS,
content,
subjectLine
);
try {
//return await sesClient.send(sendEmailCommand);
console.log(
"Email data to be sent:",
address,
EMAIL_NOTIFICATION_FROM_ADDRESS,
content,
subjectLine
);
} catch (caught) {
if (caught instanceof Error && caught.name === "MessageRejected") {
/** @type { import('@aws-sdk/client-ses').MessageRejected} */
const messageRejectedError = caught;
console.error("Email rejected:", messageRejectedError);
return messageRejectedError;
}
//throw caught;
console.error("Error sending email!", caught);
}
}
const createSendEmailCommand = (
toAddress,
fromAddress,
content,
subjectLine
) => {
return new SendEmailCommand({
Destination: {
CcAddresses: [],
ToAddresses: [toAddress],
},
Message: {
Body: {
Html: {
Charset: "UTF-8",
Data: content,
},
Text: {
Charset: "UTF-8",
Data: content,
},
},
Subject: {
Charset: "UTF-8",
Data: subjectLine,
},
},
Source: fromAddress,
ReplyToAddresses: [],
});
};