-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 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 8969893
fix recipients structure
emmanishikawa 5fdd7fd
fix recipients structure
emmanishikawa 963a41e
added unit tests
emmanishikawa 63e754a
Merge remote-tracking branch 'origin/main' into notifications-schema
emmanishikawa c308385
co-purchase popups front end work components
emmanishikawa 3ca5911
fixing unit tests
emmanishikawa 1ddf6f1
fixing unit tests
emmanishikawa 3fe4156
fixing unit tests
emmanishikawa 57655a7
front end copurchase popups
emmanishikawa 26e8c55
front-end copurchase popup
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
Some comments aren't visible on the classic Files Changed page.
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,120 @@ | ||
| .modal { | ||
| background: #ffffff; | ||
| width: 456px; | ||
| border-radius: 10px; | ||
| border: 1px solid #d8d8d8; | ||
| box-shadow: 0 4px 24px rgba(0, 0, 0, 0.08); | ||
| padding: 25px; | ||
| position: relative; | ||
| } | ||
|
|
||
| .closeBtn { | ||
| position: absolute; | ||
| top: 17px; | ||
| right: 17px; | ||
| width: 16px; | ||
| height: 16px; | ||
| border-radius: 2px; | ||
| border: none; | ||
| background: transparent; | ||
| cursor: pointer; | ||
| opacity: 0.7; | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| padding: 0; | ||
| transition: opacity 0.15s; | ||
| } | ||
|
|
||
| .closeBtn:hover { | ||
| opacity: 1; | ||
| } | ||
|
|
||
| .header { | ||
| display: flex; | ||
| flex-direction: column; | ||
| gap: 8px; | ||
| margin-bottom: 20px; | ||
| padding-right: 24px; | ||
| } | ||
|
|
||
| .title { | ||
| font-size: 17px; | ||
| font-weight: 700; | ||
| color: #1a1a1a; | ||
| line-height: 1.2; | ||
| } | ||
|
|
||
| .subtitle { | ||
| font-size: 13px; | ||
| color: #666; | ||
| line-height: 1.4; | ||
| } | ||
|
|
||
| .input { | ||
| width: 100%; | ||
| height: 36px; | ||
| border-radius: 8px; | ||
| border: 1px solid #d8d8d8; | ||
| padding: 4px 12px; | ||
| font-size: 13px; | ||
| color: #444; | ||
| background: #fafafa; | ||
| outline: none; | ||
| margin-bottom: 14px; | ||
| box-sizing: border-box; | ||
| transition: border-color 0.15s, box-shadow 0.15s; | ||
| } | ||
|
|
||
| .input:focus { | ||
| border-color: #2e6b8a; | ||
| box-shadow: 0 0 0 3px rgba(46, 107, 138, 0.12); | ||
| } | ||
|
|
||
| .actions { | ||
| display: flex; | ||
| justify-content: flex-end; | ||
| gap: 8px; | ||
| } | ||
|
|
||
| .btnPrimary { | ||
| height: 36px; | ||
| border-radius: 8px; | ||
| border: none; | ||
| cursor: pointer; | ||
| font-size: 13px; | ||
| font-weight: 600; | ||
| padding: 0 20px; | ||
| background: #2e6b8a; | ||
| color: #fff; | ||
| transition: opacity 0.15s, transform 0.1s; | ||
| } | ||
|
|
||
| .btnPrimary:hover { | ||
| opacity: 0.88; | ||
| } | ||
|
|
||
| .btnPrimary:active { | ||
| transform: scale(0.97); | ||
| } | ||
|
|
||
| .btnSecondary { | ||
| height: 36px; | ||
| border-radius: 8px; | ||
| border: none; | ||
| cursor: pointer; | ||
| font-size: 13px; | ||
| font-weight: 600; | ||
| padding: 0 20px; | ||
| background: #f0f0f0; | ||
| color: #444; | ||
| transition: opacity 0.15s, transform 0.1s; | ||
| } | ||
|
|
||
| .btnSecondary:hover { | ||
| opacity: 0.75; | ||
| } | ||
|
|
||
| .btnSecondary:active { | ||
| transform: scale(0.97); | ||
| } |
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,48 @@ | ||
| import { useState } from "react"; | ||
| import styles from "./CoPurchase.module.css"; | ||
|
|
||
| interface InviteModalProps { | ||
| onClose: () => void; | ||
| onSend: (email: string) => void; | ||
| } | ||
|
|
||
| export default function CopurchaseInvite({ onClose, onSend }: InviteModalProps) { | ||
| const [email, setEmail] = useState(""); | ||
|
|
||
| const handleSend = () => { | ||
| onSend(email); | ||
| onClose(); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className={styles.modal}> | ||
| <button className={styles.closeBtn} aria-label="Close" onClick={onClose}> | ||
| × | ||
| </button> | ||
|
|
||
| <div className={styles.header}> | ||
| <div className={styles.title}>Invite to Co-purchase</div> | ||
| <div className={styles.subtitle}> | ||
| Invite other labs or colleagues to split the cost of this item. | ||
| </div> | ||
| </div> | ||
|
|
||
| <input | ||
| className={styles.input} | ||
| type="email" | ||
| value={email} | ||
| placeholder="colleague@ucsd.edu" | ||
| onChange={(e) => setEmail(e.target.value)} | ||
| /> | ||
|
|
||
| <div className={styles.actions}> | ||
| <button className={styles.btnSecondary} onClick={onClose}> | ||
| Cancel | ||
| </button> | ||
| <button className={styles.btnPrimary} onClick={handleSend}> | ||
| Send Invites | ||
| </button> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } |
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,45 @@ | ||
| import { useState } from "react"; | ||
| import styles from "./CoPurchase.module.css"; | ||
|
|
||
| interface InviteModalProps { | ||
| onClose: () => void; | ||
| onSend: (email: string) => void; | ||
| } | ||
|
|
||
| export default function CopurchaseInvite({ onClose, onSend }: InviteModalProps) { | ||
| const [email, setEmail] = useState(""); | ||
|
|
||
| const handleSend = () => { | ||
| onSend(email); | ||
| onClose(); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className={styles.modal}> | ||
| <button className={styles.closeBtn} aria-label="Close" onClick={onClose}> | ||
| × | ||
| </button> | ||
|
|
||
| <div className={styles.header}> | ||
| <div className={styles.title}>Enter your lab</div> | ||
| <div className={styles.subtitle}> | ||
| Enter the name of your desired lab | ||
| </div> | ||
| </div> | ||
|
|
||
| <input | ||
| className={styles.input} | ||
| type="email" | ||
| value={email} | ||
| placeholder="Dr. Xu" | ||
| onChange={(e) => setEmail(e.target.value)} | ||
| /> | ||
|
|
||
| <div className={styles.actions}> | ||
| <button className={styles.btnPrimary} onClick={handleSend}> | ||
| Enter | ||
| </button> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } |
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,26 @@ | ||
| 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, | ||
| enum: ["PI", "LAB_MANAGER", "RESEARCHER"], | ||
| }], | ||
| channel: [{ | ||
| type: String, | ||
| }], | ||
| } | ||
| ], | ||
| 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,32 @@ | ||
| import Notification from "../Notification"; | ||
|
|
||
|
|
||
| describe("Notification Schema", () => { | ||
| test("valid user passes", async () => { | ||
| const notification = new Notification({ | ||
| labId: "lab123", | ||
| type: "DB_UPDATE", | ||
| resourceId: "item123", | ||
| recipients: [ | ||
| { | ||
| role: ["PI", "RESEARCHER"], | ||
| channel: ["EMAIL"], | ||
| }, | ||
| ], | ||
| }); | ||
| await expect(notification.validate()).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| test("missing required field fails", async () => { | ||
| const notification = new Notification({ | ||
| type: "DB_UPDATE", | ||
| recipients: [ | ||
| { | ||
| role: ["PI", "RESEARCHER"], | ||
| channel: ["EMAIL"], | ||
| }, | ||
| ], | ||
| }); | ||
| await expect(notification.validate()).rejects.toThrow(); | ||
| }); | ||
| }) |
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,55 @@ | ||
| import mongoose from "mongoose"; | ||
| import { startNotificationWatcher } from "../notifications/watchUpdates"; | ||
| import Notification from "@/models/Notification"; | ||
|
|
||
| jest.mock("mongoose", () => ({ | ||
| ...jest.requireActual("mongoose"), | ||
| connection: { | ||
| collection: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| jest.mock("@/models/Notification", () => ({ | ||
| __esModule: true, | ||
| default: { | ||
| create: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| jest.mock("../../lib/mongoose", () => ({ | ||
| connectToDatabase: jest.fn(), | ||
| })); | ||
|
|
||
| const mockCollection = { | ||
| watch: jest.fn(), | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| (mongoose.connection.collection as jest.Mock).mockReturnValue(mockCollection); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe("startNotificationWatcher", () => { | ||
| test("creates notification on update event", async () => { | ||
| const fakeStream = { | ||
| async *[Symbol.asyncIterator]() { | ||
| yield { | ||
| operationType: "update", | ||
| fullDocument: { | ||
| _id: "item1", | ||
| labId: "lab1", | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
|
|
||
| mockCollection.watch.mockReturnValue(fakeStream); | ||
|
|
||
| await startNotificationWatcher(); | ||
|
|
||
| expect(Notification.create).toHaveBeenCalled(); | ||
| }); | ||
| }); |
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,37 @@ | ||
| 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; | ||
|
|
||
|
arnavjk007 marked this conversation as resolved.
|
||
| // TODO: Check the quantity in updatedDoc | ||
| // If quantity is below the threshold, continue | ||
| // Otherwise, send a notification | ||
| await Notification.create({ | ||
| type: "DB_UPDATE", | ||
| labId: updatedDoc.labId ?? "unknown", | ||
| resourceId: String(updatedDoc._id), | ||
| recipients: [], | ||
| }); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.