Skip to content

Commit

Permalink
feat: Add a project count API (#9333)
Browse files Browse the repository at this point in the history
  • Loading branch information
valya committed May 8, 2024
1 parent afffa36 commit 2c3553f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/cli/src/controllers/project.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export class ProjectController {
return await this.projectsService.getAccessibleProjects(req.user);
}

@Get('/count')
async getProjectCounts() {
return await this.projectsService.getProjectCounts();
}

@Post('/')
@GlobalScope('project:create')
@Licensed('feat:advancedPermissions')
Expand Down
9 changes: 8 additions & 1 deletion packages/cli/src/services/project.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Project } from '@/databases/entities/Project';
import { Project, type ProjectType } from '@/databases/entities/Project';
import { ProjectRelation } from '@/databases/entities/ProjectRelation';
import type { ProjectRole } from '@/databases/entities/ProjectRelation';
import type { User } from '@/databases/entities/User';
Expand Down Expand Up @@ -336,4 +336,11 @@ export class ProjectService {
},
});
}

async getProjectCounts(): Promise<Record<ProjectType, number>> {
return {
personal: await this.projectRepository.count({ where: { type: 'personal' } }),
team: await this.projectRepository.count({ where: { type: 'team' } }),
};
}
}
19 changes: 19 additions & 0 deletions packages/cli/test/integration/project.api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,25 @@ describe('GET /projects/', () => {
});
});

describe('GET /projects/count', () => {
test('should return correct number of projects', async () => {
const [firstUser] = await Promise.all([
createUser(),
createUser(),
createUser(),
createUser(),
createTeamProject(),
createTeamProject(),
createTeamProject(),
]);

const resp = await testServer.authAgentFor(firstUser).get('/projects/count');

expect(resp.body.data.personal).toBe(4);
expect(resp.body.data.team).toBe(3);
});
});

describe('GET /projects/my-projects', () => {
test('member should get all projects they are apart of', async () => {
//
Expand Down

0 comments on commit 2c3553f

Please sign in to comment.