generated from CSES-UCSD/nextjs-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Notification Schema #5
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
emmanishikawa
wants to merge
8
commits into
main
Choose a base branch
from
notifications-schema
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fc18709
created notification schema
emmanishikawa 5a7cc4f
Create watchUpdates.ts
AlexMeng0831 893affc
Create index.ts
AlexMeng0831 04f1290
fixed formatting
emmanishikawa de8b1d2
remove console.log, changed products to items, added useful comments
emmanishikawa 3f2be20
deleted id field, move labId to top, add roles field to recipients
emmanishikawa c659621
updated comments
emmanishikawa 8248fe0
change recipients to list with role instead of type String
emmanishikawa 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 |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import mongoose from 'mongoose'; | ||
| const { Schema } = mongoose; | ||
|
|
||
| const notificationSchema = new Schema({ | ||
| labId: { type: String, required: true }, | ||
| type: { type: String, required: true }, | ||
| resourceId: { type: String, required: true }, | ||
| recipients: [ | ||
| { | ||
| role: { type: String }, | ||
| required: true | ||
| } | ||
| ], | ||
| createdAt: { type: Date, required: true, default: Date.now } | ||
| }); | ||
|
|
||
| const Notification = mongoose.models.Notification || | ||
| mongoose.model('Notification', notificationSchema); | ||
|
|
||
| export default Notification; | ||
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 |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { startNotificationWatcher } from "./watchUpdates"; | ||
|
|
||
| // Entry point for notification service | ||
| startNotificationWatcher(); |
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 |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import mongoose from "mongoose"; | ||
| import { connectToDatabase } from "@/lib/mongoose"; | ||
| import Notification from "@/models/Notification"; | ||
|
|
||
| /** | ||
| * Starts a Change Stream watcher on the "products" collection. | ||
| * Inserts a DB_UPDATE notification whenever a product is updated. | ||
| */ | ||
| export async function startNotificationWatcher() { | ||
| await connectToDatabase(); | ||
|
|
||
| const collection = mongoose.connection.collection("items"); | ||
|
|
||
| const changeStream = collection.watch([], { | ||
| fullDocument: "updateLookup", | ||
| }); | ||
|
|
||
| for await (const change of changeStream) { | ||
| if (change.operationType !== "update") continue; | ||
|
|
||
| // Get the updated document from the change stream | ||
| const updatedDoc = change.fullDocument; | ||
|
|
||
| // If there’s no document, skip this iteration | ||
| if (!updatedDoc) continue; | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can add a comment here where we can check the quantity and compare it to the specified threshold. If it is less we continue else we will send a noti |
||
| // TODO: Check the quantity in updatedDoc | ||
| // If quantity is below the threshold, continue | ||
| // Otherwise, send a notification | ||
| await Notification.create({ | ||
| _id: `notif_${Date.now()}`, | ||
| type: "DB_UPDATE", | ||
| labId: updatedDoc.labId ?? "unknown", | ||
| resourceId: String(updatedDoc._id), | ||
| recipients: [], | ||
| }); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
this defines an array of objects with a role, we want to have a list of strings roles like so:
{
type: [String],
enum: ["PI", LAB_MANAGER, RESEARCHER],
required: true
}