-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
33 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,42 @@ | ||
import ving from '#ving/index.mjs'; | ||
import { sendMail } from '#ving/email/send.mjs'; | ||
import { useKind } from '#ving/record/utils.mjs'; | ||
import { eq } from '#ving/drizzle/orm.mjs'; | ||
|
||
/** | ||
* This handler looks up all users with a specified role and sends them an email. | ||
* @param {Object} job A `BullMQ` job. | ||
* @param {Object} job.data An object with data needed for this job. | ||
* @param {string} job.data.role The name of a role on the User record that if true will be sent this email. | ||
* @param {string} job.data.subject The subject line of the email. | ||
* @param {string} job.data.summary A very short summary of the message. | ||
* @param {string} job.data.message The message body of the email. | ||
* @returns {boolean} `true` | ||
*/ | ||
export default async function (job) { | ||
ving.log('jobs').debug(`Test ran with data: ${JSON.stringify(job.data)} which is a ${typeof job.data}`); | ||
if (job.data.error) { | ||
throw new Error(job.data.error); | ||
const users = await useKind('User'); | ||
const roleUsers = await users.findMany(eq(users.table[job.data.role], true)); | ||
ving.log('jobs').info(`${job.id} found ${roleUsers.length} users to send emails to.`); | ||
let succeeded = 0; | ||
let failures = 0; | ||
for (const user of roleUsers) { | ||
const vars = { | ||
subject: job.data.subject, | ||
summary: job.data.summary, | ||
message: job.data.message, | ||
}; | ||
try { | ||
await sendMail('generic', { | ||
options: { to: user.get('email') }, | ||
vars | ||
}); | ||
succeeded++; | ||
} | ||
catch (err) { | ||
failures++; | ||
ving.log('jobs').error(`${job.id} failed to send email to user ${user.get('id')} ${user.get('email')} because ${err.message} with message ${JSON.stringify(vars)}`); | ||
} | ||
} | ||
ving.log('jobs').info(`${job.id} sent ${succeeded} emails with ${failures} failures.`) | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import Test from "#ving/jobs/handlers/Test.mjs"; | ||
import DeleteUnusedS3File from "#ving/jobs/handlers/DeleteUnusedS3File.mjs"; | ||
import CronJob from "#ving/jobs/handlers/CronJob.mjs"; | ||
import EmailRole from "#ving/jobs/handlers/EmailRole.mjs"; | ||
|
||
export const jobHandlers = { | ||
Test, | ||
DeleteUnusedS3File, | ||
CronJob | ||
CronJob, | ||
EmailRole | ||
} |