Skip to content

Commit

Permalink
refactor: proposal count endpoint (#1672)
Browse files Browse the repository at this point in the history
<!--
Follow semantic-release guidelines for the PR title, which is used in
the changelog.

Title should follow the format `<type>(<scope>): <subject>`, where
- Type is one of:
build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test|BREAKING
CHANGE
- Scope (optional) describes the place of the change (eg a particular
milestone) and is usually omitted
- subject should be a non-capitalized one-line description in present
imperative tense and not ending with a period

See
https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#-git-commit-guidelines
for more details.
-->

## Description
Refactor the filters input for the proposal count endpoint

## Motivation
After the proposals count endpoint is used in the frontend there were
some improvements needed

* Bug fixed (#X)

## Changes:
<!-- Please provide a list of the changes implemented by this PR -->

* Changes are only in the proposals controller and service where we just
change the filters input and how we parse it.

## Tests included

- [x] Included for each change/fix?
- [ ] Passing? <!-- Merge will not be approved unless tests pass -->

## Documentation
- [ ] swagger documentation updated (required for API changes)
- [ ] official documentation updated

### official documentation info
<!-- If you have updated the official documentation, please provide PR #
and URL of the updated pages -->
  • Loading branch information
nitrosx authored Jan 30, 2025
2 parents 8ac01ba + 819e123 commit 67d1627
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export class FullFacetResponse implements IFullFacets {
[key: string]: object;
}

export class ProposalCountFilters {
@ApiPropertyOptional()
fields?: string;
}

export class CountApiResponse {
@ApiProperty({ type: Number })
count: number;
Expand Down
43 changes: 37 additions & 6 deletions src/proposals/proposals.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import {
FullQueryFilters,
CountApiResponse,
FullFacetFilters,
ProposalCountFilters,
} from "src/common/types";

@ApiBearerAuth()
Expand Down Expand Up @@ -388,20 +389,50 @@ export class ProposalsController {
description:
"Database filters to apply when retrieving count for proposals",
required: false,
type: String,
example: '{"where": {"proposalId": "189691"}}',
type: ProposalCountFilters,
example: `{ fields: ${proposalsFullQueryExampleFields}}`,
})
@ApiResponse({
status: 200,
type: CountApiResponse,
description:
"Return the number of proposals in the following format: { count: integer }",
})
async count(@Req() request: Request, @Query("filters") filters?: string) {
const proposalFilters: IFilters<ProposalDocument, IProposalFields> =
this.updateFiltersForList(request, JSON.parse(filters ?? "{}"));
async count(@Req() request: Request, @Query() filters: { fields?: string }) {
const user: JWTUser = request.user as JWTUser;
const fields: IProposalFields = JSON.parse(filters.fields ?? "{}");

if (user) {
const ability = this.caslAbilityFactory.proposalsInstanceAccess(user);
const canViewAll = ability.can(Action.ProposalsReadAny, ProposalClass);

if (!canViewAll) {
const canViewAccess = ability.can(
Action.ProposalsReadManyAccess,
ProposalClass,
);
const canViewOwner = ability.can(
Action.ProposalsReadManyOwner,
ProposalClass,
);
const canViewPublic = ability.can(
Action.ProposalsReadManyPublic,
ProposalClass,
);
if (canViewAccess) {
fields.userGroups = fields.userGroups ?? [];
fields.userGroups.push(...user.currentGroups);
// fields.sharedWith = user.email;
} else if (canViewOwner) {
fields.ownerGroup = fields.ownerGroup ?? [];
fields.ownerGroup.push(...user.currentGroups);
} else if (canViewPublic) {
fields.isPublished = true;
}
}
}

return this.proposalsService.count(proposalFilters);
return this.proposalsService.count({ fields });
}

// GET /proposals/fullquery
Expand Down
9 changes: 7 additions & 2 deletions src/proposals/proposals.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,14 @@ export class ProposalsService {
async count(
filter: IFilters<ProposalDocument, IProposalFields>,
): Promise<{ count: number }> {
const whereFilter: FilterQuery<ProposalDocument> = filter.where ?? {};
const filterQuery: FilterQuery<ProposalDocument> =
createFullqueryFilter<ProposalDocument>(
this.proposalModel,
"proposalId",
filter.fields,
);

const count = await this.proposalModel.countDocuments(whereFilter).exec();
const count = await this.proposalModel.countDocuments(filterQuery).exec();

return { count };
}
Expand Down

0 comments on commit 67d1627

Please sign in to comment.