-
Notifications
You must be signed in to change notification settings - Fork 124
Refactor time checker and notification functions for robustness #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hmshuv
wants to merge
4
commits into
AOSSIE-Org:main
Choose a base branch
from
hmshuv:refactor/cloud-functions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fd9cec3
Refactor time checker and notification functions for robustness
hmshuv 796b12d
Apply CodeRabbit feedback: fix imports, validations, and notification…
hmshuv 2b72719
Update functions/upcomingRoom-Message-Notification/src/main.js
hmshuv d006fc3
Enhance validations and logging based on review
hmshuv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,61 +1,83 @@ | ||
| const sdk = require("node-appwrite"); | ||
|
|
||
| const admin = require('firebase-admin'); | ||
| const { getMessaging } = require('firebase-admin/messaging'); | ||
| const serviceAccount = require("./resonate-service-account.json"); | ||
|
|
||
| const app = admin.initializeApp({ | ||
| credential: admin.credential.cert(serviceAccount) | ||
| }); | ||
|
|
||
| module.exports = async function ({ req, res, log, error }) { | ||
| var subscribersTokens = []; | ||
| const subscribersTokens = []; | ||
| const client = new sdk.Client(); | ||
| const database = new sdk.Databases(client); | ||
| const query = sdk.Query; | ||
| const { roomId, payload } = JSON.parse(req.body); | ||
|
|
||
| client.setEndpoint( | ||
| process.env.APPWRITE_ENDPOINT ?? 'https://cloud.appwrite.io/v1' | ||
| ) | ||
| .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID) | ||
| .setKey(process.env.APPWRITE_API_KEY); | ||
| log(process.env.APPWRITE_FUNCTION_PROJECT_ID); | ||
|
|
||
| log("Send Notification"); | ||
| log(roomId); | ||
| log(payload); | ||
| let subscriberList = await database.listDocuments(process.env.UpcomingRoomsDataBaseID, process.env.SubscriberCollectionID, [query.equal('upcomingRoomId', [roomId])]); | ||
| log("here as well") | ||
| subscriberList.documents.forEach(subscriber => { | ||
| for (const token of subscriber["registrationTokens"]) { | ||
| subscribersTokens.push(token); | ||
| } | ||
| }); | ||
| let document = await database.getDocument(process.env.UpcomingRoomsDataBaseID, process.env.UpcomingRoomsCollectionID, roomId); | ||
| for (const creator_token of document["creator_fcm_tokens"]) { | ||
| subscribersTokens.push(creator_token); | ||
| } | ||
| log(subscribersTokens); | ||
| const message = { | ||
| notification: payload, | ||
| tokens: subscribersTokens, | ||
| priority: "high", | ||
| android: { | ||
| priority: "high" | ||
|
|
||
| try { | ||
| const { roomId, payload } = JSON.parse(req.body); | ||
|
|
||
| log(`Sending notification for room: ${roomId}`); | ||
|
|
||
| const subscriberList = await database.listDocuments( | ||
| process.env.UpcomingRoomsDataBaseID, | ||
| process.env.SubscriberCollectionID, | ||
| [query.equal('upcomingRoomId', [roomId])] | ||
| ); | ||
|
|
||
| subscriberList.documents.forEach(subscriber => { | ||
| if (subscriber["registrationTokens"] && Array.isArray(subscriber["registrationTokens"])) { | ||
| subscribersTokens.push(...subscriber["registrationTokens"]); | ||
| } | ||
| }); | ||
|
|
||
| const roomDocument = await database.getDocument( | ||
| process.env.UpcomingRoomsDataBaseID, | ||
| process.env.UpcomingRoomsCollectionID, | ||
| roomId | ||
| ); | ||
|
|
||
| if (roomDocument["creator_fcm_tokens"] && Array.isArray(roomDocument["creator_fcm_tokens"])) { | ||
| subscribersTokens.push(...roomDocument["creator_fcm_tokens"]); | ||
| } | ||
| }; | ||
| getMessaging(app).sendEachForMulticast(message) | ||
| .then((response) => { | ||
|
|
||
| // Deduplicate tokens | ||
| const uniqueTokens = [...new Set(subscribersTokens)]; | ||
|
|
||
| if (uniqueTokens.length > 0) { | ||
| const message = { | ||
| notification: payload, | ||
| tokens: uniqueTokens, | ||
| priority: "high", | ||
| android: { | ||
| priority: "high" | ||
| } | ||
| }; | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const response = await getMessaging(app).sendEachForMulticast(message); | ||
| if (response.failureCount > 0) { | ||
| log('Failed'); | ||
| log(`Failed to send ${response.failureCount} notifications`); | ||
| } else { | ||
| log('Notifications were sent successfully'); | ||
| } | ||
| } else { | ||
| log('No tokens found to send notifications.'); | ||
| } | ||
|
|
||
| return res.json({ | ||
| message: 'Notification process completed' | ||
| }); | ||
|
|
||
| return res.json({ | ||
| message: 'Notification sent' | ||
| }); | ||
| } catch (err) { | ||
| error(`Error in upcomingRoom-Message-Notification: ${err.message}`); | ||
| return res.json({ | ||
| message: 'Error occurred', | ||
| error: err.message | ||
| }, 500); | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or 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,88 +1,70 @@ | ||
| const sdk = require("node-appwrite"); | ||
|
|
||
| const admin = require('firebase-admin'); | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // const { getMessaging } = require('firebase-admin/messaging'); | ||
| // const serviceAccount = require("./resonate-service-account.json"); | ||
| // const app = admin.initializeApp({ | ||
| // credential: admin.credential.cert(serviceAccount) | ||
| // }); | ||
|
|
||
| module.exports = async function ({ req, res, log }) { | ||
| var subscribersTokens = []; | ||
| module.exports = async function ({ req, res, log, error }) { | ||
| const client = new sdk.Client(); | ||
| const database = new sdk.Databases(client); | ||
| const query = sdk.Query; | ||
| log("here"); | ||
|
|
||
| client.setEndpoint( | ||
| process.env.APPWRITE_ENDPOINT ?? 'https://cloud.appwrite.io/v1' | ||
| ) | ||
| .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID) | ||
| .setKey(process.env.APPWRITE_API_KEY); | ||
| log(process.env.APPWRITE_FUNCTION_PROJECT_ID); | ||
| log("here also"); | ||
| let upcomingRoomsList = await database.listDocuments(process.env.UpcomingRoomsDataBaseID, process.env.UpcomingRoomsCollectionID); | ||
| log("here as well"); | ||
| for (const document of upcomingRoomsList.documents) { | ||
| log("now here"); | ||
| var scheduledDateTime = document["scheduledDateTime"]; | ||
| log(scheduledDateTime); | ||
| var splittedDateTime = scheduledDateTime.split('T'); | ||
| var extractedDate = splittedDateTime[0]; | ||
| var extractedTime = splittedDateTime[1]; | ||
| var SplittingDate = extractedDate.split("-"); | ||
| const year = Number(SplittingDate[0]); | ||
| const month = Number(SplittingDate[1]); | ||
| const day = Number(SplittingDate[2]); | ||
| var SplittingTime = extractedTime.split(":"); | ||
| const hour = Number(SplittingTime[0]); | ||
| const minutes = Number(SplittingTime[1]); | ||
| const upcomingRoomDate = Date.UTC(year, month - 1, day, hour, minutes); | ||
| log(upcomingRoomDate); | ||
| const now = new Date(); | ||
| const nowTime = now.getTime(); | ||
| log(nowTime); | ||
|
|
||
| var timeLeft = upcomingRoomDate - nowTime; | ||
| var timeLeftInMinutes = timeLeft / (1000 * 60); | ||
| log(timeLeftInMinutes); | ||
| if (timeLeftInMinutes <= 5 && timeLeftInMinutes >= -5 && document["isTime"] == false) { | ||
| await database.updateDocument(process.env.UpcomingRoomsDataBaseID, process.env.UpcomingRoomsCollectionID, document.$id, { | ||
| "isTime": true | ||
| }) | ||
| // log("Send Notification"); | ||
| // let subscriberList = await database.listDocuments(process.env.UpcomingRoomsDataBaseID, process.env.SubscriberCollectionID, [query.equal('upcomingRoomId', [document.$id])]); | ||
| // log("here as well") | ||
| // subscriberList.documents.forEach(subscriber => { | ||
| // for (const token of subscriber["registrationTokens"]) { | ||
| // subscribersTokens.push(token); | ||
| // } | ||
| // }); | ||
| // for (const creator_token of document["creator_fcm_tokens"]) { | ||
| // subscribersTokens.push(creator_token); | ||
| // } | ||
| // log(subscribersTokens); | ||
| // const message = { | ||
| // notification: { | ||
| // title: 'Room Reminder', | ||
| // body: `The room ${document["name"]} will Start Soon` | ||
| // }, | ||
| // tokens: subscribersTokens, | ||
| // priority: "high", | ||
| // android: { | ||
| // priority: "high" | ||
| // } | ||
| // }; | ||
| // getMessaging(app).sendEachForMulticast(message) | ||
| // .then((response) => { | ||
| // if (response.failureCount > 0) { | ||
| // log('Failed'); | ||
| // } else { | ||
| // log('Notifications were sent successfully'); | ||
| // } | ||
| // }); | ||
| try { | ||
| const upcomingRoomsList = await database.listDocuments( | ||
| process.env.UpcomingRoomsDataBaseID, | ||
| process.env.UpcomingRoomsCollectionID | ||
| ); | ||
|
|
||
| for (const document of upcomingRoomsList.documents) { | ||
| const scheduledDateTime = document["scheduledDateTime"]; | ||
|
|
||
| // Use standard Date parsing (handles ISO 8601 correctly) | ||
| const upcomingRoomDate = new Date(scheduledDateTime).getTime(); | ||
| const nowTime = new Date().getTime(); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const timeLeft = upcomingRoomDate - nowTime; | ||
| const timeLeftInMinutes = timeLeft / (1000 * 60); | ||
|
|
||
| // Check if time is within +/- 5 minutes and not yet marked | ||
| if (timeLeftInMinutes <= 5 && timeLeftInMinutes >= -5 && document["isTime"] === false) { | ||
| await database.updateDocument( | ||
| process.env.UpcomingRoomsDataBaseID, | ||
| process.env.UpcomingRoomsCollectionID, | ||
| document.$id, | ||
| { | ||
| "isTime": true | ||
| } | ||
| ); | ||
|
|
||
| // Log explicitly when an action is taken | ||
| log(`Activated room: ${document.$id}`); | ||
|
|
||
| /* | ||
| // Notification logic (legacy) | ||
| var subscribersTokens = []; | ||
| // ... (rest of the commented notification logic kept if needed for reference, | ||
| // essentially user logic seems to be to keep it commented for now as they didn't ask to implement it) | ||
| */ | ||
| } | ||
| } | ||
|
|
||
| return res.json({ | ||
| message: 'Time check completed successfully' | ||
| }); | ||
|
|
||
| } catch (err) { | ||
| error(`Error in upcomingRoom-isTime-checker: ${err.message}`); | ||
| return res.json({ | ||
| message: 'Error occurred during time check', | ||
| error: err.message | ||
| }, 500); | ||
| } | ||
| return res.json({ | ||
| message: 'set verified' | ||
| }); | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle cases where
req.bodymay already be parsed or undefined.In Appwrite Functions,
req.bodycan be a pre-parsed object (depending on content-type headers) or undefined. CallingJSON.parse()on an object throws"Unexpected token o in JSON", which is confusing.🔎 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents