Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add proposal count endpoint #1626

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading