Skip to content

Commit

Permalink
Move check community district id to community district domain
Browse files Browse the repository at this point in the history
  • Loading branch information
pratishta committed Jun 26, 2024
1 parent a2a9cd8 commit 233b621
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 40 deletions.
3 changes: 2 additions & 1 deletion src/borough/borough.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Module } from "@nestjs/common";
import { BoroughService } from "./borough.service";
import { BoroughController } from "./borough.controller";
import { BoroughRepository } from "./borough.repository";
import { CommunityDistrictRepository } from "src/community-district/community-district.repository";

@Module({
exports: [BoroughService],
providers: [BoroughService, BoroughRepository],
providers: [BoroughService, BoroughRepository, CommunityDistrictRepository],
controllers: [BoroughController],
})
export class BoroughModule {}
9 changes: 0 additions & 9 deletions src/borough/borough.repository.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@ export const checkByIdRepoSchema = boroughEntitySchema.pick({

export type CheckByIdRepo = z.infer<typeof checkByIdRepoSchema>;

export const checkByCommunityDistrictIdRepoSchema =
communityDistrictEntitySchema.pick({
id: true,
});

export type CheckByCommunityDistrictIdRepo = z.infer<
typeof checkByCommunityDistrictIdRepoSchema
>;

export const findCommunityDistrictsByBoroughIdRepoSchema = z.array(
communityDistrictEntitySchema,
);
Expand Down
22 changes: 0 additions & 22 deletions src/borough/borough.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,6 @@ export class BoroughRepository {
}
}

#checkCommunityDistrictById = this.db.query.communityDistrict
.findFirst({
columns: {
id: true,
},
where: (communityDistrict, { eq, sql }) =>
eq(communityDistrict.id, sql.placeholder("id")),
})
.prepare("checkCommunityDistrictId");

async checkCommunityDistrictById(
id: string,
): Promise<CheckByIdRepo | undefined> {
try {
return await this.#checkCommunityDistrictById.execute({
id,
});
} catch {
throw new DataRetrievalException();
}
}

async findMany(): Promise<FindManyRepo> {
try {
return await this.db.query.borough.findMany();
Expand Down
5 changes: 4 additions & 1 deletion src/borough/borough.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Inject, Injectable } from "@nestjs/common";
import { BoroughRepository } from "./borough.repository";
import { CommunityDistrictRepository } from "src/community-district/community-district.repository";
import { ResourceNotFoundException } from "src/exception";
import {
FindCapitalProjectsByBoroughIdCommunityDistrictIdPathParams,
Expand All @@ -11,6 +12,8 @@ export class BoroughService {
constructor(
@Inject(BoroughRepository)
private readonly boroughRepository: BoroughRepository,
@Inject(CommunityDistrictRepository)
private readonly communityDistrictRepository: CommunityDistrictRepository,
) {}

async findMany() {
Expand Down Expand Up @@ -44,7 +47,7 @@ export class BoroughService {
if (boroughCheck === undefined) throw new ResourceNotFoundException();

const communityDistrictCheck =
await this.boroughRepository.checkCommunityDistrictById(
await this.communityDistrictRepository.checkCommunityDistrictById(
communityDistrictId,
);
if (communityDistrictCheck === undefined)
Expand Down
11 changes: 7 additions & 4 deletions src/community-district/community-district.repository.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ export const findTilesRepoSchema = mvtEntitySchema;

export type FindTilesRepo = z.infer<typeof findTilesRepoSchema>;

export const checkByIdRepoSchema = communityDistrictEntitySchema.pick({
id: true,
});
export const checkByCommunityDistrictIdRepoSchema =
communityDistrictEntitySchema.pick({
id: true,
});

export type CheckByIdRepo = z.infer<typeof checkByIdRepoSchema>;
export type CheckByCommunityDistrictIdRepo = z.infer<
typeof checkByCommunityDistrictIdRepoSchema
>;
27 changes: 26 additions & 1 deletion src/community-district/community-district.repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Inject } from "@nestjs/common";
import { DB, DbType } from "src/global/providers/db.provider";
import { FindCommunityDistrictTilesPathParams } from "src/gen";
import { FindTilesRepo } from "./community-district.repository.schema";
import {
FindTilesRepo,
CheckByCommunityDistrictIdRepo,
} from "./community-district.repository.schema";
import { borough, communityDistrict } from "src/schema";
import { sql, isNotNull, eq } from "drizzle-orm";
import { DataRetrievalException } from "src/exception";
Expand All @@ -12,6 +15,28 @@ export class CommunityDistrictRepository {
private readonly db: DbType,
) {}

#checkCommunityDistrictById = this.db.query.communityDistrict
.findFirst({
columns: {
id: true,
},
where: (communityDistrict, { eq, sql }) =>
eq(communityDistrict.id, sql.placeholder("id")),
})
.prepare("checkCommunityDistrictId");

async checkCommunityDistrictById(
id: string,
): Promise<CheckByCommunityDistrictIdRepo | undefined> {
try {
return await this.#checkCommunityDistrictById.execute({
id,
});
} catch {
throw new DataRetrievalException();
}
}

async findTiles(
params: FindCommunityDistrictTilesPathParams,
): Promise<FindTilesRepo> {
Expand Down
5 changes: 5 additions & 0 deletions test/borough/borough.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import * as request from "supertest";
import { INestApplication } from "@nestjs/common";
import { Test } from "@nestjs/testing";
import { BoroughRepository } from "src/borough/borough.repository";
import { CommunityDistrictRepository } from "src/community-district/community-district.repository";
import { BoroughRepositoryMock } from "./borough.repository.mock";
import { CommunityDistrictRepositoryMock } from "../community-district/community-district.repository.mock";
import { BoroughModule } from "src/borough/borough.module";
import {
findBoroughsQueryResponseSchema,
Expand All @@ -16,13 +18,16 @@ describe("Borough e2e", () => {
let app: INestApplication;

const boroughRepositoryMock = new BoroughRepositoryMock();
const communityDistrictRepositoryMock = new CommunityDistrictRepositoryMock();

beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [BoroughModule],
})
.overrideProvider(BoroughRepository)
.useValue(boroughRepositoryMock)
.overrideProvider(CommunityDistrictRepository)
.useValue(communityDistrictRepositoryMock)
.compile();
app = moduleRef.createNestApplication();
await app.init();
Expand Down
3 changes: 2 additions & 1 deletion test/borough/borough.repository.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
checkByIdRepoSchema,
findCommunityDistrictsByBoroughIdRepoSchema,
findCapitalProjectsByBoroughIdCommunityDistrictIdRepoSchema,
checkByCommunityDistrictIdRepoSchema,
} from "src/borough/borough.repository.schema";
import { checkByCommunityDistrictIdRepoSchema } from "src/community-district/community-district.repository.schema";
import { generateMock } from "@anatine/zod-mock";

export class BoroughRepositoryMock {
Expand All @@ -25,6 +25,7 @@ export class BoroughRepositoryMock {
);

async checkCommunityDistrictById(id: string) {
console.log("checking communitydistrictbyid");
return this.checkCommunityDistrictByIdMocks.find((row) => row.id === id);
}

Expand Down
18 changes: 17 additions & 1 deletion test/community-district/community-district.repository.mock.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
import { generateMock } from "@anatine/zod-mock";
import { findTilesRepoSchema } from "src/community-district/community-district.repository.schema";
import {
findTilesRepoSchema,
checkByCommunityDistrictIdRepoSchema,
} from "src/community-district/community-district.repository.schema";

export class CommunityDistrictRepositoryMock {
numberOfMocks = 1;

checkCommunityDistrictByIdMocks = Array.from(
Array(this.numberOfMocks),
(_, seed) =>
generateMock(checkByCommunityDistrictIdRepoSchema, { seed: seed + 1 }),
);

async checkCommunityDistrictById(id: string) {
console.log("checking communitydistrictbyid");
return this.checkCommunityDistrictByIdMocks.find((row) => row.id === id);
}

findTilesMock = generateMock(findTilesRepoSchema);

/**
Expand Down

0 comments on commit 233b621

Please sign in to comment.