Skip to content

Commit

Permalink
feat: add proposal count endpoint (#1626)
Browse files Browse the repository at this point in the history
* feat: add proposal count endpoint

* remove unused import
  • Loading branch information
martin-trajanovski authored Jan 17, 2025
1 parent 55fb2fd commit ad3c129
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/proposals/proposals.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ import {
import { plainToInstance } from "class-transformer";
import { validate, ValidatorOptions } from "class-validator";
import {
CountApiResponse,
filterDescription,
filterExample,
FullFacetFilters,
FullFacetResponse,
fullQueryExampleLimits,
FullQueryFilters,
Expand Down Expand Up @@ -368,6 +370,38 @@ export class ProposalsController {
return this.proposalsService.findAll(proposalFilters);
}

// GET /proposals/count
@UseGuards(PoliciesGuard)
@CheckPolicies("proposals", (ability: AppAbility) =>
ability.can(Action.ProposalsRead, ProposalClass),
)
@Get("/count")
@ApiOperation({
summary: "It returns the number of proposals.",
description:
"It returns a number of proposals matching the where filter if provided.",
})
@ApiQuery({
name: "filters",
description:
"Database filters to apply when retrieving count for proposals",
required: false,
type: String,
example: '{"where": {"proposalId": "189691"}}',
})
@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 ?? "{}"));

return this.proposalsService.count(proposalFilters);
}

// GET /proposals/fullquery
@UseGuards(PoliciesGuard)
@CheckPolicies("proposals", (ability: AppAbility) =>
Expand Down Expand Up @@ -454,7 +488,7 @@ export class ProposalsController {
"Full facet query filters to apply when retrieving proposals\n" +
proposalsFullQueryDescriptionFields,
required: false,
type: String,
type: FullFacetFilters,
example: proposalsFullQueryExampleFields,
})
@ApiResponse({
Expand Down
10 changes: 10 additions & 0 deletions src/proposals/proposals.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ export class ProposalsService {
.exec();
}

async count(
filter: IFilters<ProposalDocument, IProposalFields>,
): Promise<{ count: number }> {
const whereFilter: FilterQuery<ProposalDocument> = filter.where ?? {};

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

return { count };
}

async fullquery(
filter: IFilters<ProposalDocument, IProposalFields>,
): Promise<ProposalClass[]> {
Expand Down
26 changes: 26 additions & 0 deletions test/Proposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,32 @@ describe("1500: Proposal: Simple Proposal", () => {
});
});

it("0091: should get proposal count", async () => {
return request(appUrl)
.get("/api/v3/Proposals/count")
.set({ Authorization: `Bearer ${accessTokenProposalIngestor}` })
.set("Accept", "application/json")
.expect(TestData.SuccessfulGetStatusCode)
.expect("Content-Type", /json/)
.then((res) => {
res.body["count"].should.be.greaterThan(0);
});
});

it("0091: should get proposal count using filters", async () => {
const query = { where: { proposalId: { $in: [proposalId] } } };
return request(appUrl)
.get("/api/v3/Proposals/count")
.set({ Authorization: `Bearer ${accessTokenProposalIngestor}` })
.query("filter=" + encodeURIComponent(JSON.stringify(query)))
.set("Accept", "application/json")
.expect(TestData.SuccessfulGetStatusCode)
.expect("Content-Type", /json/)
.then((res) => {
res.body["count"].should.be.equal(1);
});
});

it("0100: should add a new attachment to this proposal", async () => {
let testAttachment = { ...TestData.AttachmentCorrect };
testAttachment.proposalId = defaultProposalId;
Expand Down

0 comments on commit ad3c129

Please sign in to comment.