refactor: centralize wechat notification dispatch

This commit is contained in:
john
2026-06-30 21:26:46 +08:00
parent 9c653e9b16
commit 6e4e0e2937
6 changed files with 186 additions and 12 deletions
+10 -4
View File
@@ -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,