From efa930a31a7c3854da50ab314f412ff69380fcaa Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Thu, 18 Apr 2024 17:46:06 +0800 Subject: [PATCH] send push notifications when digest is ready --- packages/api/src/jobs/ai/create_digest.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/api/src/jobs/ai/create_digest.ts b/packages/api/src/jobs/ai/create_digest.ts index 90260cf4c..1bc85be4c 100644 --- a/packages/api/src/jobs/ai/create_digest.ts +++ b/packages/api/src/jobs/ai/create_digest.ts @@ -17,8 +17,10 @@ import { findLibraryItemsByIds, searchLibraryItems, } from '../../services/library_item' +import { findDeviceTokensByUserId } from '../../services/user_device_tokens' import { logger } from '../../utils/logger' import { htmlToMarkdown } from '../../utils/parser' +import { sendMulticastPushNotifications } from '../../utils/sendNotification' export type CreateDigestJobSchedule = 'daily' | 'weekly' @@ -490,5 +492,19 @@ export const createDigestJob = async (jobData: CreateDigestJobData) => { id: jobData.id, jobState: TaskState.Failed, }) + } finally { + // send notification + const tokens = await findDeviceTokensByUserId(jobData.userId) + if (tokens.length > 0) { + const message = { + notification: { + title: 'Digest ready', + body: 'Your digest is ready to listen', + }, + tokens: tokens.map((token) => token.token), + } + + await sendMulticastPushNotifications(jobData.userId, message, 'reminder') + } } }