-
Notifications
You must be signed in to change notification settings - Fork 24
feat: Add Email Notifications for Limit Warnings (#47) #77
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
ayash911
wants to merge
1
commit into
yash-pouranik:main
Choose a base branch
from
ayash911:feature/limit-notifications
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
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
103 changes: 103 additions & 0 deletions
103
apps/dashboard-api/src/__tests__/project.controller.test.js
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,103 @@ | ||
| 'use strict'; | ||
|
|
||
| const mongoose = require('mongoose'); | ||
|
|
||
| // Mock @urbackend/common first to mock Project and cache utilities | ||
| const mockProjectSave = jest.fn(); | ||
| const mockProjectFindOne = jest.fn(); | ||
|
|
||
| jest.mock('@urbackend/common', () => ({ | ||
| Project: { | ||
| findOne: mockProjectFindOne | ||
| }, | ||
| deleteProjectById: jest.fn().mockResolvedValue(true) | ||
| })); | ||
|
|
||
| const { updateNotificationSettings } = require('../controllers/project.controller'); | ||
|
|
||
| describe('project.controller - updateNotificationSettings', () => { | ||
| let mockReq; | ||
| let mockRes; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
|
|
||
| mockReq = { | ||
| params: { projectId: '60c72b2f9b1d8b001c8e4b52' }, | ||
| user: { _id: 'ownerId123' }, | ||
| body: {} | ||
| }; | ||
|
|
||
| mockRes = { | ||
| status: jest.fn().mockReturnThis(), | ||
| json: jest.fn() | ||
| }; | ||
| }); | ||
|
|
||
| test('should return 400 if email payload is missing', async () => { | ||
| mockReq.body = {}; // no 'email' property | ||
| await updateNotificationSettings(mockReq, mockRes); | ||
|
|
||
| expect(mockRes.status).toHaveBeenCalledWith(400); | ||
| expect(mockRes.json).toHaveBeenCalledWith({ error: "Missing 'email' settings in body" }); | ||
| }); | ||
|
|
||
| test('should return 404 if project is not found or not owned by user', async () => { | ||
| mockReq.body = { email: { enabled: true } }; | ||
| mockProjectFindOne.mockResolvedValueOnce(null); | ||
|
|
||
| await updateNotificationSettings(mockReq, mockRes); | ||
|
|
||
| expect(mockRes.status).toHaveBeenCalledWith(404); | ||
| expect(mockRes.json).toHaveBeenCalledWith({ error: "Project not found or access denied." }); | ||
| }); | ||
|
|
||
| test('should successfully update notification settings and save the project', async () => { | ||
| const incomingEmailSettings = { | ||
| enabled: true, | ||
| storage: { type: 'absolute', absoluteLimit: 1000 }, | ||
| database: { type: 'percentage', thresholds: [50, 75] } | ||
| }; | ||
|
|
||
| mockReq.body = { email: incomingEmailSettings }; | ||
|
|
||
| const mockProjectDoc = { | ||
| _id: '60c72b2f9b1d8b001c8e4b52', | ||
| notificationSettings: { | ||
| email: { | ||
| enabled: false | ||
| } | ||
| }, | ||
| markModified: jest.fn(), | ||
| save: mockProjectSave | ||
| }; | ||
|
|
||
| mockProjectFindOne.mockResolvedValueOnce(mockProjectDoc); | ||
| mockProjectSave.mockResolvedValueOnce(mockProjectDoc); | ||
|
|
||
| await updateNotificationSettings(mockReq, mockRes); | ||
|
|
||
| // Expect the object to be updated with merged settings | ||
| expect(mockProjectDoc.notificationSettings.email).toMatchObject(incomingEmailSettings); | ||
|
|
||
| // Since we provided storage/database, markModified should be called | ||
| expect(mockProjectDoc.markModified).toHaveBeenCalledWith('notificationSettings'); | ||
| expect(mockProjectSave).toHaveBeenCalled(); | ||
|
|
||
| expect(mockRes.json).toHaveBeenCalledWith({ | ||
| success: true, | ||
| settings: mockProjectDoc.notificationSettings | ||
| }); | ||
| }); | ||
|
|
||
| test('should handle validation errors or save failures gracefully', async () => { | ||
| mockReq.body = { email: { enabled: true } }; | ||
|
|
||
| mockProjectFindOne.mockRejectedValueOnce(new Error('DB Connection Failed')); | ||
|
|
||
| await updateNotificationSettings(mockReq, mockRes); | ||
|
|
||
| expect(mockRes.status).toHaveBeenCalledWith(500); | ||
| expect(mockRes.json).toHaveBeenCalledWith({ error: 'DB Connection Failed' }); | ||
| }); | ||
| }); |
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
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
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.
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.
Validate and deep-merge notification settings payload.
Line 1176-1182 performs a shallow merge. Partial updates like
email.storage = { thresholds: [...] }can overwrite sibling storage fields (type,absoluteLimit) unintentionally, and there’s no schema validation to prevent invalid payload shapes.🧩 Proposed fix
module.exports.updateNotificationSettings = async (req, res) => { try { const { projectId } = req.params; - const { email } = req.body; // { enabled: true, storage: {...}, database: {...} } + const { email } = req.body; if (!email) { return res.status(400).json({ error: "Missing 'email' settings in body" }); } const project = await Project.findOne({ _id: projectId, owner: req.user._id }); if (!project) return res.status(404).json({ error: "Project not found or access denied." }); + const currentEmail = project.notificationSettings?.email || {}; project.notificationSettings = { ...project.notificationSettings, email: { - ...project.notificationSettings?.email, + ...currentEmail, ...email, + storage: email.storage + ? { ...(currentEmail.storage || {}), ...email.storage } + : currentEmail.storage, + database: email.database + ? { ...(currentEmail.database || {}), ...email.database } + : currentEmail.database, } }; - if (email.storage || email.database) { - project.markModified('notificationSettings'); - } + project.markModified('notificationSettings'); await project.save(); await deleteProjectById(projectId); res.json({ success: true, settings: project.notificationSettings });🤖 Prompt for AI Agents