Skip to content

Commit 62fd563

Browse files
Copilotcubapthehabes
authored
Return 200 with empty array for /my/projects when user has no projects (#428)
* Initial plan * Fix my/projects endpoint to return 200 with empty array for users with no projects Co-authored-by: cubap <1119165+cubap@users.noreply.github.com> * Update API documentation to reflect new empty projects response * Revert unintended package-lock.json changes Co-authored-by: thehabes <3287006+thehabes@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: cubap <1119165+cubap@users.noreply.github.com> Co-authored-by: thehabes <3287006+thehabes@users.noreply.github.com>
1 parent 9f5c4f3 commit 62fd563

3 files changed

Lines changed: 57 additions & 12 deletions

File tree

API.md

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -248,20 +248,35 @@ Users can always see and manipulate their own profile. The profile object is a p
248248
- **Description**: Retrieve a list of projects the authenticated user is a member of.
249249
- **Responses**:
250250

251-
- **200**: Projects found
251+
- **200**: Projects found (or empty list if user has no projects)
252252
```json
253-
[
254-
{
255-
"_id": "hexstring",
256-
"label": "string",
257-
"roles": ["string"]
258-
}, ...
259-
]
253+
{
254+
"metrics": {
255+
"newest": "project:hexstring/page:hexstring",
256+
"lastModified": "project:hexstring/page:hexstring",
257+
"myRecent": "hexstring"
258+
},
259+
"projects": [
260+
{
261+
"_id": "hexstring",
262+
"label": "string",
263+
"roles": ["string"]
264+
}, ...
265+
]
266+
}
267+
```
268+
269+
When the user has no projects, returns:
270+
```json
271+
{
272+
"metrics": null,
273+
"projects": []
274+
}
260275
```
261276
- **401**: Unauthorized
262277
- **500**: Server error
263278

264-
The response is a list of projects the user is a member of regardless of the permissions afforded to them in each project.
279+
The response includes a list of projects the user is a member of regardless of the permissions afforded to them in each project, along with metrics about the newest and most recently modified projects.
265280

266281
#### `GET /user/:id`
267282

classes/User/__tests__/unit.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,28 @@ describe("GET /my/projects #user_class", () => {
154154
expect(response.status).toBe(401)
155155
})
156156
})
157+
158+
describe("GET /my/projects with no projects #user_class", () => {
159+
beforeAll(() => {
160+
jest.spyOn(User.prototype, "getProjects").mockResolvedValue([])
161+
jest.spyOn(User.prototype, "getSelf").mockResolvedValue({
162+
_id: "123456",
163+
_lastModified: null
164+
})
165+
})
166+
167+
afterAll(() => {
168+
jest.restoreAllMocks()
169+
})
170+
171+
it.skip("should return 200 with empty projects array when user has no projects", async () => {
172+
const response = await request(app)
173+
.get("/my/projects")
174+
.set("Authorization", `Bearer ${token}`)
175+
expect(response.status).toBe(200)
176+
expect(response.body).toHaveProperty("projects")
177+
expect(Array.isArray(response.body.projects)).toBe(true)
178+
expect(response.body.projects.length).toBe(0)
179+
expect(response.body.metrics).toBeNull()
180+
})
181+
})

userProfile/privateProfile.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,15 @@ router.route("/projects").get(auth0Middleware(), async (req, res) => {
4444
const userObj = await new User(user._id)
4545
const userProjects = await userObj.getProjects()
4646
const validMetrics = userProjects.filter((proj) => proj._createdAt && proj._modifiedAt && proj._lastModified)
47+
48+
res.set("Content-Type", "application/json; charset=utf-8")
49+
4750
if (validMetrics.length === 0) {
48-
return respondWithError(res, 404, "No valid projects found")
51+
// Return empty response when user has no projects
52+
return res.status(200).json({
53+
metrics: null,
54+
projects: []
55+
})
4956
}
5057

5158
// TODO: When the projects are all formatted correctly, we will not need this
@@ -59,8 +66,6 @@ router.route("/projects").get(auth0Middleware(), async (req, res) => {
5966
const userData = await userObj.getSelf()
6067
const myRecent = userData._lastModified
6168

62-
res.set("Content-Type", "application/json; charset=utf-8")
63-
6469
res.status(200).json({
6570
metrics: {
6671
newest,

0 commit comments

Comments
 (0)