Skip to content

Commit 8f745d3

Browse files
fix: filter agency by jurisdiction
1 parent a2b0f87 commit 8f745d3

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

api/src/services/agency.service.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import {
33
Injectable,
44
NotFoundException,
55
} from '@nestjs/common';
6+
import { Prisma } from '@prisma/client';
67
import { PrismaService } from './prisma.service';
78
import { AgencyQueryParams } from '../dtos/agency/agency-query-params.dto';
9+
import { AgencyFilterKeys } from '../enums/agency/filter-key-enum';
810
import AgencyCreate from '../dtos/agency/agency-create.dto';
911
import { AgencyUpdate } from '../dtos/agency/agency-update.dto';
1012
import {
@@ -44,6 +46,7 @@ export class AgencyService {
4446
include: {
4547
jurisdictions: true,
4648
},
49+
where: this.buildWhereClause(params),
4750
});
4851

4952
const agencies = mapTo(Agency, agenciesRaw);
@@ -54,6 +57,28 @@ export class AgencyService {
5457
};
5558
}
5659

60+
buildWhereClause(params: AgencyQueryParams): Prisma.AgencyWhereInput {
61+
const filters: Prisma.AgencyWhereInput[] = [];
62+
63+
const jurisdictionId = params?.filter?.find(
64+
(filter) => filter[AgencyFilterKeys.jurisdiction],
65+
)?.[AgencyFilterKeys.jurisdiction];
66+
67+
if (jurisdictionId) {
68+
filters.push({
69+
jurisdictions: {
70+
is: {
71+
id: jurisdictionId,
72+
},
73+
},
74+
});
75+
}
76+
77+
return {
78+
AND: filters,
79+
};
80+
}
81+
5782
/**
5883
* Returns a single agency entry matching the provided ID.
5984
*

api/test/unit/services/agencies.service.spec.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { AgencyService } from '../../../src/services/agency.service';
66
import { PrismaService } from '../../../src/services/prisma.service';
77
import { PermissionService } from '../../../src/services/permission.service';
88
import { BadRequestException, NotFoundException } from '@nestjs/common';
9+
import { Compare } from '../../../src/dtos/shared/base-filter.dto';
910

1011
describe('Testing agency service', () => {
1112
let service: AgencyService;
@@ -110,6 +111,9 @@ describe('Testing agency service', () => {
110111
include: {
111112
jurisdictions: true,
112113
},
114+
where: {
115+
AND: [],
116+
},
113117
});
114118
});
115119

@@ -144,6 +148,9 @@ describe('Testing agency service', () => {
144148
include: {
145149
jurisdictions: true,
146150
},
151+
where: {
152+
AND: [],
153+
},
147154
});
148155
});
149156

@@ -178,6 +185,9 @@ describe('Testing agency service', () => {
178185
include: {
179186
jurisdictions: true,
180187
},
188+
where: {
189+
AND: [],
190+
},
181191
});
182192
});
183193

@@ -202,6 +212,82 @@ describe('Testing agency service', () => {
202212
include: {
203213
jurisdictions: true,
204214
},
215+
where: {
216+
AND: [],
217+
},
218+
});
219+
});
220+
221+
it('should apply jurisdiction filter when provided', async () => {
222+
const date = new Date();
223+
const jurisdictionId = randomUUID();
224+
const mockedValue = mockAgencySet(2, date, jurisdictionId);
225+
prisma.agency.findMany = jest.fn().mockResolvedValue(mockedValue);
226+
prisma.agency.count = jest.fn().mockResolvedValue(2);
227+
228+
const params: AgencyQueryParams = {
229+
filter: [
230+
{
231+
$comparison: Compare['='],
232+
jurisdiction: jurisdictionId,
233+
},
234+
],
235+
};
236+
237+
await service.list(params);
238+
239+
expect(prisma.agency.findMany).toHaveBeenCalledWith({
240+
skip: 0,
241+
take: undefined,
242+
include: {
243+
jurisdictions: true,
244+
},
245+
where: {
246+
AND: [
247+
{
248+
jurisdictions: {
249+
is: {
250+
id: jurisdictionId,
251+
},
252+
},
253+
},
254+
],
255+
},
256+
});
257+
});
258+
});
259+
260+
describe('buildWhereClause', () => {
261+
it('should return empty where when jurisdiction filter is not provided', () => {
262+
const where = service.buildWhereClause({});
263+
264+
expect(where).toEqual({
265+
AND: [],
266+
});
267+
});
268+
269+
it('should build jurisdiction where clause from filter array', () => {
270+
const jurisdictionId = randomUUID();
271+
272+
const where = service.buildWhereClause({
273+
filter: [
274+
{
275+
$comparison: Compare['='],
276+
jurisdiction: jurisdictionId,
277+
},
278+
],
279+
});
280+
281+
expect(where).toEqual({
282+
AND: [
283+
{
284+
jurisdictions: {
285+
is: {
286+
id: jurisdictionId,
287+
},
288+
},
289+
},
290+
],
205291
});
206292
});
207293
});

sites/public/src/lib/hooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ export async function fetchAgencies(req: any, jurisdictionId: string) {
374374
} = {
375375
filter: [
376376
{
377-
$comparison: EnumAgencyFilterParamsComparison["IN"],
377+
$comparison: EnumAgencyFilterParamsComparison["="],
378378
jurisdiction: jurisdictionId && jurisdictionId !== "" ? jurisdictionId : undefined,
379379
},
380380
],

0 commit comments

Comments
 (0)