diff --git a/src/shelter/ShelterSearch.ts b/src/shelter/ShelterSearch.ts index 1b9c76cf..6b4ca02c 100644 --- a/src/shelter/ShelterSearch.ts +++ b/src/shelter/ShelterSearch.ts @@ -31,12 +31,14 @@ class ShelterSearch { } priority(supplyIds: string[] = []): Prisma.ShelterWhereInput { - if (!this.formProps.priority) return {}; + if (!this.formProps.priority?.length) return {}; return { shelterSupplies: { some: { - priority: +this.formProps.priority, + priority: { + in: this.formProps.priority, + }, supplyId: supplyIds.length > 0 ? { @@ -71,13 +73,31 @@ class ShelterSearch { } supplyCategoryIds( - priority?: SupplyPriority | null, + priority?: SupplyPriority[] | null, ): Prisma.ShelterWhereInput { if (!this.formProps.supplyCategoryIds) return {}; + + if (!priority || !priority.length) { + return { + shelterSupplies: { + some: { + priority: undefined, + supply: { + supplyCategoryId: { + in: this.formProps.supplyCategoryIds, + }, + }, + }, + }, + }; + } + return { shelterSupplies: { some: { - priority: priority ? +priority : undefined, + priority: { + in: priority, + }, supply: { supplyCategoryId: { in: this.formProps.supplyCategoryIds, diff --git a/src/shelter/shelter.service.ts b/src/shelter/shelter.service.ts index f79ac367..d6a7c3f9 100644 --- a/src/shelter/shelter.service.ts +++ b/src/shelter/shelter.service.ts @@ -6,7 +6,7 @@ import * as qs from 'qs'; import { z } from 'zod'; import { PrismaService } from '../prisma/prisma.service'; -import { SupplyPriority } from '../supply/types'; +import { SupplyPriority } from 'src/supply/types'; import { SearchSchema } from '../types'; import { ShelterSearch, parseTagResponse } from './ShelterSearch'; import { ShelterSearchPropsSchema } from './types/search.types'; @@ -127,6 +127,7 @@ export class ShelterService implements OnModuleInit { search: searchQuery, } = SearchSchema.parse(query); const queryData = ShelterSearchPropsSchema.parse(qs.parse(searchQuery)); + const { getQuery } = new ShelterSearch(this.prismaService, queryData); const where = await getQuery(); diff --git a/src/shelter/types/search.types.ts b/src/shelter/types/search.types.ts index b87e653a..64dd1a3a 100644 --- a/src/shelter/types/search.types.ts +++ b/src/shelter/types/search.types.ts @@ -32,8 +32,14 @@ export type GeolocationFilter = z.infer; export const ShelterSearchPropsSchema = z.object({ search: z.string().optional(), priority: z.preprocess( - (value) => Number(value) || undefined, - z.nativeEnum(SupplyPriority).optional(), + (value) => + typeof value === 'string' + ? value + .split(',') + .map((v) => Number(v)) + .filter((v) => !isNaN(v)) + : [], + z.array(z.nativeEnum(SupplyPriority)).optional(), ), supplyCategoryIds: z.array(z.string()).optional(), supplyIds: z.array(z.string()).optional(),