From 3183569461488396a4ddfe858bc136ceb0044f99 Mon Sep 17 00:00:00 2001
From: Diego Dario <25825145+diegodario88@users.noreply.github.com>
Date: Sun, 19 May 2024 19:27:57 -0300
Subject: [PATCH] feat: add multi option for priority queryParam

Tenta resolver a issue #145
---
 src/shelter/ShelterSearch.ts      | 28 ++++++++++++++++++++++++----
 src/shelter/types/search.types.ts | 10 ++++++++--
 2 files changed, 32 insertions(+), 6 deletions(-)

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/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<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(),