( event: H3Event, toolId: string, collection: string, userEmail: string, )
| 235 | } |
| 236 | |
| 237 | async function handleToolDataList( |
| 238 | event: H3Event, |
| 239 | toolId: string, |
| 240 | collection: string, |
| 241 | userEmail: string, |
| 242 | ): Promise<unknown> { |
| 243 | await ensureToolsTables(); |
| 244 | const tool = await getTool(toolId); |
| 245 | if (!tool) { |
| 246 | setResponseStatus(event, 404); |
| 247 | return { error: "Tool not found" }; |
| 248 | } |
| 249 | const client = getDbExec(); |
| 250 | const url = event.url; |
| 251 | const limitParam = url?.searchParams?.get("limit"); |
| 252 | const limit = limitParam |
| 253 | ? Math.min(Math.max(1, Number(limitParam)), 1000) |
| 254 | : 100; |
| 255 | const scope = url?.searchParams?.get("scope") || "user"; |
| 256 | const orgId = getRequestOrgId(); |
| 257 | |
| 258 | if (scope === "org") { |
| 259 | if (!orgId) { |
| 260 | setResponseStatus(event, 400); |
| 261 | return { error: "Org context required for scope=org" }; |
| 262 | } |
| 263 | const result = await client.execute({ |
| 264 | sql: `SELECT COALESCE(item_id, id) AS id, tool_id, collection, data, owner_email, scope, org_id, created_at, updated_at |
| 265 | FROM tool_data |
| 266 | WHERE tool_id = ? AND collection = ? AND scope = 'org' AND org_id = ? |
| 267 | ORDER BY created_at DESC |
| 268 | LIMIT ?`, |
| 269 | args: [toolId, collection, orgId, limit], |
| 270 | }); |
| 271 | return result.rows ?? []; |
| 272 | } |
| 273 | |
| 274 | if (scope === "all") { |
| 275 | const result = await client.execute({ |
| 276 | sql: `SELECT COALESCE(item_id, id) AS id, tool_id, collection, data, owner_email, scope, org_id, created_at, updated_at |
| 277 | FROM tool_data |
| 278 | WHERE tool_id = ? AND collection = ? |
| 279 | AND ((scope = 'user' AND owner_email = ?) OR (scope = 'org' AND org_id = ?)) |
| 280 | ORDER BY created_at DESC |
| 281 | LIMIT ?`, |
| 282 | args: [toolId, collection, userEmail, orgId ?? "", limit], |
| 283 | }); |
| 284 | return result.rows ?? []; |
| 285 | } |
| 286 | |
| 287 | const result = await client.execute({ |
| 288 | sql: `SELECT COALESCE(item_id, id) AS id, tool_id, collection, data, owner_email, scope, org_id, created_at, updated_at |
| 289 | FROM tool_data |
| 290 | WHERE tool_id = ? AND collection = ? AND scope = 'user' AND owner_email = ? |
| 291 | ORDER BY updated_at DESC |
| 292 | LIMIT ?`, |
| 293 | args: [toolId, collection, userEmail, limit], |
| 294 | }); |
no test coverage detected