Skip to content

Commit

Permalink
♻️refactor(CoI): adding initiative parameter to all results endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
German Martinez committed Dec 11, 2024
1 parent f961c6c commit 246961c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
14 changes: 11 additions & 3 deletions onecgiar-pr-server/src/api/results/result.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,11 @@ WHERE
}
}*/

async AllResultsByRoleUsers(userid: number, excludeType = [10, 11]) {
async AllResultsByRoleUserAndInitiative(
userid: number,
excludeType = [10, 11],
initiativeCode?: string,
) {
const queryData = `
SELECT
r.id,
Expand Down Expand Up @@ -533,11 +537,15 @@ WHERE
AND rbi.is_active > 0
AND rbi.initiative_role_id = 1
AND ci.active > 0
AND rt.id not in (${excludeType.toString()});
AND rt.id not in (${excludeType.toString()})
${initiativeCode ? 'AND ci.official_code = ?' : ''};
`;

try {
const results = await this.query(queryData, [userid]);
const results = await this.query(
queryData,
([userid] as any[]).concat(initiativeCode ? [initiativeCode] : []),
);
return results;
} catch (error) {
throw {
Expand Down
7 changes: 5 additions & 2 deletions onecgiar-pr-server/src/api/results/results.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ export class ResultsController {
}

@Get('get/all/roles/:userId')
findAllResultRoles(@Param('userId') userId: number) {
return this.resultsService.findAllByRole(userId);
findAllResultRoles(
@Param('userId') userId: number,
@Query('initiative') init?: string,
) {
return this.resultsService.findAllByRole(userId, init);
}

@Get('get/depth-search/:title')
Expand Down
8 changes: 6 additions & 2 deletions onecgiar-pr-server/src/api/results/results.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -885,10 +885,14 @@ export class ResultsService {
}
}

async findAllByRole(userId: number) {
async findAllByRole(userId: number, initiativeCode?: string) {
try {
const result: any[] =
await this._customResultRepository.AllResultsByRoleUsers(userId);
await this._customResultRepository.AllResultsByRoleUserAndInitiative(
userId,
undefined,
initiativeCode,
);

if (!result.length) {
throw {
Expand Down

0 comments on commit 246961c

Please sign in to comment.