-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
7515d82
commit b3ccfd2
Showing
9 changed files
with
143 additions
and
29 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
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 +1,2 @@ | ||
export * from './controller.js' | ||
export * from './logic.js' |
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
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 +1,2 @@ | ||
export * from './controller.js' | ||
export * from './logic.js' |
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
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
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,5 +1,10 @@ | ||
import { ioc } from './ioc/index.js' | ||
import { partial } from './ioc/partial.js' | ||
import { Server } from './server/index.js' | ||
|
||
partial(ioc, { | ||
controllers: ['task'], | ||
services: ['database', 'notifier'], | ||
}) | ||
const server = await ioc.resolve(Server) | ||
await server.listen() |
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
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,11 +1,90 @@ | ||
import { ioc } from '#root/ioc/index.js' | ||
import { Queue } from 'bullmq' | ||
import { ConnectionOptions, Queue, Worker } from 'bullmq' | ||
import * as task from '#root/components/task/index.js' | ||
import { Mailer } from './mailer.js' | ||
import * as email from '#root/components/email/index.js' | ||
|
||
export const Notifier = ioc.add([], () => { | ||
const queue = new Queue('notifications', { | ||
connection: { | ||
host: 'localhost', | ||
port: 7379, | ||
}, | ||
const queueName = 'notifications' | ||
|
||
type JobData = { | ||
userId: number | ||
} | ||
|
||
const jobIdManager = { | ||
prefix: 'task-', | ||
create(taskId: number) { | ||
return `${this.prefix}${taskId}` | ||
}, | ||
parse(jobId: string) { | ||
return Number.parseInt(jobId.substring(this.prefix.length)) | ||
}, | ||
} | ||
|
||
export const Notifier = ioc.add([Mailer, email.Logic], (mailer, emailLogic) => { | ||
const connection: ConnectionOptions = { | ||
host: 'localhost', | ||
port: 7379, | ||
} | ||
|
||
const queue = new Queue<JobData>(queueName, { | ||
connection, | ||
}) | ||
|
||
new Worker<JobData>( | ||
queueName, | ||
async (job) => { | ||
const taskLogic = await ioc.resolve(task.Logic) | ||
|
||
const taskId = jobIdManager.parse(job.id!) | ||
const { userId } = job.data | ||
|
||
const taskRecord = await taskLogic.get(userId, taskId) | ||
if (!taskRecord) { | ||
return 'Task not found' | ||
} | ||
|
||
const emailRecord = await emailLogic.get(userId) | ||
if (!emailRecord) { | ||
return 'No email' | ||
} | ||
if (!emailRecord.confirmed) { | ||
return 'Email not confirmed' | ||
} | ||
|
||
await mailer.sendTaskNotification(emailRecord.email, taskRecord) | ||
return `Sent to ${emailRecord.email}` | ||
}, | ||
{ | ||
connection, | ||
removeOnComplete: { | ||
count: 1000, | ||
}, | ||
removeOnFail: { | ||
count: 5000, | ||
}, | ||
}, | ||
) | ||
|
||
return { | ||
async set(userId: number, taskId: number, notifyDate: string | null) { | ||
const jobId = jobIdManager.create(taskId) | ||
|
||
const job = await queue.getJob(jobId) | ||
|
||
if (notifyDate === null || new Date(notifyDate) <= new Date()) { | ||
if (job) { | ||
await job.remove() | ||
} | ||
return | ||
} | ||
|
||
const delay = Number(new Date(notifyDate)) - Number(new Date()) | ||
|
||
if (job) { | ||
await job.changeDelay(delay) | ||
} else { | ||
await queue.add('Notify', { userId }, { jobId, delay }) | ||
} | ||
}, | ||
} | ||
}) |