Skip to content

Commit

Permalink
feat: add multi option for priority queryParam (#150)
Browse files Browse the repository at this point in the history
Tenta resolver a issue #145
  • Loading branch information
diegodario88 committed May 29, 2024
1 parent 38da306 commit adcb6cb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
28 changes: 24 additions & 4 deletions src/shelter/ShelterSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
? {
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 8 additions & 2 deletions src/shelter/types/search.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ export type GeolocationFilter = z.infer<typeof GeolocationFilterSchema>;
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(),
Expand Down

0 comments on commit adcb6cb

Please sign in to comment.