Skip to content

Commit d75fe42

Browse files
committed
feat: add multi option for priority queryParam
Tenta resolver a issue SOS-RS#145
1 parent 0249985 commit d75fe42

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

src/shelter/ShelterSearch.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ class ShelterSearch {
3131
}
3232

3333
priority(supplyIds: string[] = []): Prisma.ShelterWhereInput {
34-
if (!this.formProps.priority) return {};
34+
if (!this.formProps.priority?.length) return {};
3535

3636
return {
3737
shelterSupplies: {
3838
some: {
39-
priority: +this.formProps.priority,
39+
priority: {
40+
in: this.formProps.priority,
41+
},
4042
supplyId:
4143
supplyIds.length > 0
4244
? {
@@ -71,13 +73,18 @@ class ShelterSearch {
7173
}
7274

7375
supplyCategoryIds(
74-
priority?: SupplyPriority | null,
76+
priority?: SupplyPriority[] | null,
7577
): Prisma.ShelterWhereInput {
7678
if (!this.formProps.supplyCategoryIds) return {};
79+
80+
const inPriority: Prisma.IntFilter<'ShelterSupply'> = {
81+
in: priority?.length ? priority : [],
82+
};
83+
7784
return {
7885
shelterSupplies: {
7986
some: {
80-
priority: priority ? +priority : undefined,
87+
priority: priority?.length ? inPriority : undefined,
8188
supply: {
8289
supplyCategoryId: {
8390
in: this.formProps.supplyCategoryIds,

src/shelter/shelter.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as qs from 'qs';
66
import { z } from 'zod';
77

88
import { PrismaService } from '../prisma/prisma.service';
9-
import { SupplyPriority } from '../supply/types';
9+
import { SupplyPriority } from 'src/supply/types';
1010
import { SearchSchema } from '../types';
1111
import { ShelterSearch, parseTagResponse } from './ShelterSearch';
1212
import { ShelterSearchPropsSchema } from './types/search.types';
@@ -127,6 +127,7 @@ export class ShelterService implements OnModuleInit {
127127
search: searchQuery,
128128
} = SearchSchema.parse(query);
129129
const queryData = ShelterSearchPropsSchema.parse(qs.parse(searchQuery));
130+
130131
const { getQuery } = new ShelterSearch(this.prismaService, queryData);
131132
const where = await getQuery();
132133

src/shelter/types/search.types.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,14 @@ export type GeolocationFilter = z.infer<typeof GeolocationFilterSchema>;
3232
export const ShelterSearchPropsSchema = z.object({
3333
search: z.string().optional(),
3434
priority: z.preprocess(
35-
(value) => Number(value) || undefined,
36-
z.nativeEnum(SupplyPriority).optional(),
35+
(value) =>
36+
typeof value === 'string'
37+
? value
38+
.split(',')
39+
.map((v) => Number(v))
40+
.filter((v) => !isNaN(v))
41+
: [],
42+
z.array(z.nativeEnum(SupplyPriority)).optional(),
3743
),
3844
supplyCategoryIds: z.array(z.string()).optional(),
3945
supplyIds: z.array(z.string()).optional(),

0 commit comments

Comments
 (0)