From 09dac379376b3601d2e2f74e5c158f23c28bde42 Mon Sep 17 00:00:00 2001 From: CJay8 Date: Tue, 10 Sep 2024 14:00:49 +0800 Subject: [PATCH] Added deployed script for send scheduled notification --- send-scheduled-notifications/index.js | 61 +++++++++++++++++++++++ send-scheduled-notifications/package.json | 9 ++++ 2 files changed, 70 insertions(+) create mode 100644 send-scheduled-notifications/index.js create mode 100644 send-scheduled-notifications/package.json diff --git a/send-scheduled-notifications/index.js b/send-scheduled-notifications/index.js new file mode 100644 index 000000000000..1532bc00903c --- /dev/null +++ b/send-scheduled-notifications/index.js @@ -0,0 +1,61 @@ +const sdk = require("node-appwrite"); +const axios = require("axios"); + +const client = new sdk.Client(); +client.setEndpoint(process.env.PUBLIC_API_URL).setProject(process.env.APPWRITE_PROJECT_ID).setKey(process.env.PUBLIC_APPWRITE_API_KEY); + +const database = new sdk.Databases(client, "event"); + +function cleanAndTrimContent(html) { + const plainText = html.replace(/<[^>]*>/g, ""); + return plainText.substring(0, 100); +} + +async function sendNotification(userId, content) { + const ONESIGNAL_APP_ID = process.env.ONESIGNAL_APP_ID; + const ONESIGNAL_API_KEY = process.env.ONESIGNAL_API_KEY; + + try { + const response = await axios.post( + "https://onesignal.com/api/v1/notifications", + { + app_id: ONESIGNAL_APP_ID, + include_external_user_ids: [userId], + contents: { en: content }, + }, + { + headers: { + Authorization: `Basic ${ONESIGNAL_API_KEY}`, + "Content-Type": "application/json", + }, + } + ); + return response.data; + } catch (error) { + console.error("Error sending notification:", error); + throw error; + } +} + +async function processNotifications() { + try { + const now = new Date().toISOString(); + + const notifications = await database.listDocuments("66dabdc8001707f3431d", [sdk.Query.equal("Confirm", true), sdk.Query.lessThanEqual("PushDate", now)]); + + for (const notification of notifications.documents) { + const recipientUserIds = notification.RecipientUserId; + const trimmedContent = cleanAndTrimContent(notification.Content); + + for (const userId of recipientUserIds) { + await sendNotification(userId, trimmedContent); + } + + console.log(`Notifications sent for: ${notification.Title}`); + } + } catch (error) { + console.error("Error processing notifications:", error); + } +} + +processNotifications(); diff --git a/send-scheduled-notifications/package.json b/send-scheduled-notifications/package.json new file mode 100644 index 000000000000..ca7da3d7191d --- /dev/null +++ b/send-scheduled-notifications/package.json @@ -0,0 +1,9 @@ +{ + "name": "send-scheduled-notifications", + "version": "1.0.0", + "main": "index.js", + "dependencies": { + "axios": "^0.21.1", + "node-appwrite": "^6.0.0" + } +}