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

413 Add Total Projects to capital projects endpoint #421

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions openapi/components/schemas/CapitalProjectPage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@ allOf:
type: array
items:
$ref: ./CapitalProject.yaml
totalProjects:
type: integer
description: >-
The total number of results matching the query parameters.
minimum: 0
example: 212
required:
- capitalProjects
48 changes: 47 additions & 1 deletion src/capital-project/capital-project.repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Inject } from "@nestjs/common";
import { isNotNull, sql, and, eq, sum, asc } from "drizzle-orm";
import { isNotNull, sql, and, eq, sum, asc, count } from "drizzle-orm";
import { DataRetrievalException } from "src/exception";
import {
CapitalProjectCategory,
Expand Down Expand Up @@ -91,6 +91,52 @@ export class CapitalProjectRepository {
}
}

async findCount({
cityCouncilDistrictId,
communityDistrictId,
boroughId,
}: {
cityCouncilDistrictId: string | null;
communityDistrictId: string | null;
boroughId: string | null;
}): Promise<number> {
try {
const countObject = await this.db
.select({
totalProjects: count(),
})
.from(capitalProject)
.leftJoin(
cityCouncilDistrict,
sql`
ST_Intersects(${cityCouncilDistrict.liFt}, ${capitalProject.liFtMPoly})
OR ST_Intersects(${cityCouncilDistrict.liFt}, ${capitalProject.liFtMPnt})`,
)
.leftJoin(
communityDistrict,
sql`
ST_Intersects(${communityDistrict.liFt}, ${capitalProject.liFtMPoly})
OR ST_Intersects(${communityDistrict.liFt}, ${capitalProject.liFtMPnt})`,
)
.where(
and(
cityCouncilDistrictId !== null
? eq(cityCouncilDistrict.id, cityCouncilDistrictId)
: undefined,
communityDistrictId !== null && boroughId !== null
? and(
eq(communityDistrict.boroughId, boroughId),
eq(communityDistrict.id, communityDistrictId),
)
: undefined,
),
);
return countObject[0].totalProjects;
} catch {
throw new DataRetrievalException();
}
}

#checkByManagingCodeCapitalProjectId = this.db.query.capitalProject
.findFirst({
columns: {
Expand Down
7 changes: 7 additions & 0 deletions src/capital-project/capital-project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,18 @@ export class CapitalProjectService {
offset,
});

const totalProjects = await this.capitalProjectRepository.findCount({
cityCouncilDistrictId,
boroughId,
communityDistrictId,
});

return {
capitalProjects,
limit,
offset,
total: capitalProjects.length,
totalProjects,
order: "managingCode, capitalProjectId",
};
}
Expand Down
6 changes: 6 additions & 0 deletions src/gen/schemas/CapitalProjectPage.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@
],
"x-readme-ref-name": "CapitalProject"
}
},
"totalProjects": {
"type": "integer",
"description": "The total number of results matching the query parameters.",
"minimum": 0,
"example": 212
}
},
"required": ["capitalProjects"]
Expand Down
5 changes: 5 additions & 0 deletions src/gen/types/CapitalProjectPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ export type CapitalProjectPage = Page & {
* @type array
*/
capitalProjects: CapitalProject[];
/**
* @description The total number of results matching the query parameters.
* @type integer | undefined
*/
totalProjects?: number;
};
10 changes: 9 additions & 1 deletion src/gen/zod/capitalProjectPageSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,13 @@ import { z } from "zod";
export const capitalProjectPageSchema = z
.lazy(() => pageSchema)
.and(
z.object({ capitalProjects: z.array(z.lazy(() => capitalProjectSchema)) }),
z.object({
capitalProjects: z.array(z.lazy(() => capitalProjectSchema)),
totalProjects: z.coerce
.number()
.int()
.min(0)
.describe("The total number of results matching the query parameters.")
.optional(),
}),
);
31 changes: 31 additions & 0 deletions test/capital-project/capital-project.repository.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,37 @@ export class CapitalProjectRepositoryMock {
.slice(offset, limit + offset);
}

async findCount({
boroughId,
communityDistrictId,
cityCouncilDistrictId,
}: {
cityCouncilDistrictId: string | null;
communityDistrictId: string | null;
boroughId: string | null;
}) {
return this.findManyMocks.reduce(
(acc: FindManyRepo, [criteria, capitalProjects]) => {
if (
cityCouncilDistrictId !== null &&
criteria.cityCouncilDistrictId !== cityCouncilDistrictId
)
return acc;

if (boroughId !== null && criteria.boroughId !== boroughId) return acc;

if (
communityDistrictId !== null &&
criteria.communityDistrictId !== communityDistrictId
)
return acc;

return acc.concat(capitalProjects);
},
[],
).length;
}

checkByManagingCodeCapitalProjectIdMocks = Array.from(Array(5), (_, seed) =>
generateMock(checkByManagingCodeCapitalProjectIdRepoSchema, {
seed: seed + 1,
Expand Down