-
Notifications
You must be signed in to change notification settings - Fork 65
Standarized the API Response in Dashboard API-Middleware #299
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
yash-pouranik
merged 4 commits into
geturbackend:main
from
Ayush4958:pr-dashboard-middleware
Jun 11, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
38e5864
feat(dashboard-api): update middlewares for AppError and ApiResponse
Ayush4958 651a4ea
fix(dashboard-api): address code rabbit review for loadProjectForAdmiβ¦
Ayush4958 d6f77a1
fix(test): mock @urbackend/common before require to prevent CI redis β¦
Ayush4958 348a298
added Utility to loadProjectForAdmin
Ayush4958 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
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
71 changes: 71 additions & 0 deletions
71
apps/dashboard-api/src/__tests__/loadProjectForAdmin.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,71 @@ | ||
| 'use strict'; | ||
|
|
||
| class AppError extends Error { | ||
| constructor(statusCode, message) { | ||
| super(message); | ||
| this.statusCode = statusCode; | ||
| } | ||
| } | ||
|
|
||
| jest.mock('@urbackend/common', () => ({ | ||
| AppError, | ||
| Project: { | ||
| findOne: jest.fn() | ||
| } | ||
| })); | ||
|
|
||
| const { Project } = require('@urbackend/common'); | ||
| const loadProjectForAdmin = require('../middlewares/loadProjectForAdmin'); | ||
|
|
||
|
|
||
| describe('loadProjectForAdmin Middleware', () => { | ||
| let req, res, next; | ||
|
|
||
| beforeEach(() => { | ||
| req = { | ||
| params: {}, | ||
| user: { _id: 'user123' } | ||
| }; | ||
| res = {}; | ||
| next = jest.fn(); | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should call next with AppError(400) if projectId is missing', async () => { | ||
| await loadProjectForAdmin(req, res, next); | ||
|
|
||
| expect(next).toHaveBeenCalledTimes(1); | ||
| expect(next).toHaveBeenCalledWith(expect.any(AppError)); | ||
| const error = next.mock.calls[0][0]; | ||
| expect(error.statusCode).toBe(400); | ||
| expect(error.message).toBe("Project ID is required"); | ||
| expect(Project.findOne).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should call next with AppError(404) if project is not found', async () => { | ||
| req.params.projectId = 'proj123'; | ||
| Project.findOne.mockResolvedValueOnce(null); | ||
|
|
||
| await loadProjectForAdmin(req, res, next); | ||
|
|
||
| expect(Project.findOne).toHaveBeenCalledWith({ _id: 'proj123', owner: 'user123' }); | ||
| expect(next).toHaveBeenCalledTimes(1); | ||
| expect(next).toHaveBeenCalledWith(expect.any(AppError)); | ||
| const error = next.mock.calls[0][0]; | ||
| expect(error.statusCode).toBe(404); | ||
| expect(error.message).toBe("Project not found or access denied"); | ||
| }); | ||
|
|
||
| it('should set req.project and call next without error if project is found', async () => { | ||
| req.params.projectId = 'proj123'; | ||
| const mockProject = { _id: 'proj123', name: 'Test Project' }; | ||
| Project.findOne.mockResolvedValueOnce(mockProject); | ||
|
|
||
| await loadProjectForAdmin(req, res, next); | ||
|
|
||
| expect(Project.findOne).toHaveBeenCalledWith({ _id: 'proj123', owner: 'user123' }); | ||
| expect(req.project).toEqual(mockProject); | ||
| expect(next).toHaveBeenCalledTimes(1); | ||
| expect(next).toHaveBeenCalledWith(); | ||
| }); | ||
| }); |
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
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,19 +1,20 @@ | ||
| // FUNCTION - LOAD PROJECT FOR ADMIN (MIDDLEWARE) | ||
| const Project = require('../models/Project'); | ||
|
|
||
| const { Project, AppError } = require('@urbackend/common'); | ||
|
|
||
| module.exports = async (req, res, next) => { | ||
| try { | ||
| const { projectId } = req.params; | ||
| if (!projectId) return res.status(400).json({ error: "Project ID is required" }); | ||
| if (!projectId) return next(new AppError(400, "Project ID is required")); | ||
|
|
||
| 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" }); | ||
| return next(new AppError(404, "Project not found or access denied")); | ||
| } | ||
|
|
||
| req.project = project; | ||
| next(); | ||
| } catch (err) { | ||
| res.status(500).json({ error: err.message }); | ||
| next(err); | ||
| } | ||
| }; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
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.