|
24 | 24 |
|
25 | 25 | import { Router } from "express"; |
26 | 26 | import { z } from "zod"; |
27 | | -import { eq, desc } from "drizzle-orm"; |
| 27 | +import { eq, desc, and } from "drizzle-orm"; |
28 | 28 | import { db } from "../../db"; |
29 | 29 | import { scheduledReports } from "../../db/schema"; |
30 | 30 | import { RouteErrorFactory } from "../../errors"; |
@@ -182,6 +182,20 @@ const listQuerySchema = z.object({ |
182 | 182 | .refine((val) => val > 0 && val <= 100, { |
183 | 183 | message: "pageSize must be between 1 and 100", |
184 | 184 | }), |
| 185 | + active: z |
| 186 | + .string() |
| 187 | + .optional() |
| 188 | + .refine( |
| 189 | + (val) => { |
| 190 | + if (val === undefined || val === "") return true; |
| 191 | + return ["true", "false", "1", "0"].includes(val); |
| 192 | + }, |
| 193 | + { message: "active must be 'true', 'false', '1', or '0'" }, |
| 194 | + ) |
| 195 | + .transform((val) => { |
| 196 | + if (val === undefined || val === "") return undefined; |
| 197 | + return val === "true" || val === "1"; |
| 198 | + }), |
185 | 199 | }); |
186 | 200 |
|
187 | 201 | // --------------------------------------------------------------------------- |
@@ -293,22 +307,27 @@ const listQuerySchema = z.object({ |
293 | 307 | throw RouteErrorFactory.badRequest("Invalid query parameters"); |
294 | 308 | } |
295 | 309 |
|
296 | | - const { page, pageSize } = parsed.data; |
| 310 | + const { page, pageSize, active } = parsed.data; |
297 | 311 | const offset = (page - 1) * pageSize; |
298 | 312 |
|
| 313 | + const whereConditions = [eq(scheduledReports.userId, userId)]; |
| 314 | + if (active !== undefined) { |
| 315 | + whereConditions.push(eq(scheduledReports.active, active)); |
| 316 | + } |
| 317 | + |
299 | 318 | // Fetch total count for pagination metadata |
300 | 319 | const [countResult] = await db |
301 | 320 | .select({ count: db.$count(scheduledReports) }) |
302 | 321 | .from(scheduledReports) |
303 | | - .where(eq(scheduledReports.userId, userId)); |
| 322 | + .where(and(...whereConditions)); |
304 | 323 |
|
305 | 324 | const total = Number(countResult?.count ?? 0); |
306 | 325 |
|
307 | 326 | // Fetch paginated results |
308 | 327 | const results = await db |
309 | 328 | .select() |
310 | 329 | .from(scheduledReports) |
311 | | - .where(eq(scheduledReports.userId, userId)) |
| 330 | + .where(and(...whereConditions)) |
312 | 331 | .orderBy(desc(scheduledReports.createdAt)) |
313 | 332 | .limit(pageSize) |
314 | 333 | .offset(offset); |
|
0 commit comments