refactor: centralize wechat notification dispatch
This commit is contained in:
@@ -1,12 +1,18 @@
|
||||
export function startScheduleReminderWorker({
|
||||
scheduleService,
|
||||
sendWechatTextToUser,
|
||||
notificationDispatcher,
|
||||
logger = console,
|
||||
intervalMs = Number(process.env.H5_REMINDER_SCAN_INTERVAL_MS ?? 30_000),
|
||||
maxAttempts = Number(process.env.H5_REMINDER_MAX_ATTEMPTS ?? 5),
|
||||
runOnStart = true,
|
||||
} = {}) {
|
||||
if (!scheduleService || typeof sendWechatTextToUser !== 'function') {
|
||||
const sendScheduleNotification =
|
||||
notificationDispatcher?.sendScheduleNotification?.bind(notificationDispatcher) ??
|
||||
(typeof sendWechatTextToUser === 'function'
|
||||
? async ({ userId, text }) => sendWechatTextToUser(userId, text)
|
||||
: null);
|
||||
if (!scheduleService || typeof sendScheduleNotification !== 'function') {
|
||||
return { stop() {} };
|
||||
}
|
||||
|
||||
@@ -39,7 +45,7 @@ export function startScheduleReminderWorker({
|
||||
},
|
||||
});
|
||||
if (reminder.channel === 'wechat') {
|
||||
await sendWechatTextToUser(reminder.userId, text);
|
||||
await sendScheduleNotification({ userId: reminder.userId, text });
|
||||
}
|
||||
await scheduleService.logDelivery({
|
||||
reminderId: reminder.id,
|
||||
@@ -81,7 +87,7 @@ export function startScheduleReminderWorker({
|
||||
timezone: subscription.timezone,
|
||||
},
|
||||
});
|
||||
await sendWechatTextToUser(subscription.userId, text);
|
||||
await sendScheduleNotification({ userId: subscription.userId, text });
|
||||
await scheduleService.logDelivery({
|
||||
subscriptionId: subscription.id,
|
||||
userId: subscription.userId,
|
||||
@@ -128,7 +134,7 @@ export function startScheduleReminderWorker({
|
||||
balanceCents,
|
||||
},
|
||||
});
|
||||
await sendWechatTextToUser(subscription.userId, text);
|
||||
await sendScheduleNotification({ userId: subscription.userId, text });
|
||||
await scheduleService.logDelivery({
|
||||
subscriptionId: subscription.id,
|
||||
userId: subscription.userId,
|
||||
|
||||
Reference in New Issue
Block a user