{"version":3,"file":"reorder.mjs","names":[],"sources":["../../../../../../src/astro/routes/api/widget-areas/[name]/reorder.ts"],"sourcesContent":["/**\n * Reorder widgets endpoint\n *\n * POST /_emdash/api/widget-areas/:name/reorder\n */\n\nimport type { APIRoute } from \"astro\";\n\nimport { requirePerm } from \"#api/authorize.js\";\nimport { apiError, apiSuccess, handleError } from \"#api/error.js\";\nimport { isParseError, parseBody } from \"#api/parse.js\";\nimport { reorderWidgetsBody } from \"#api/schemas.js\";\n\nexport const prerender = false;\n\nexport const POST: APIRoute = async ({ params, request, locals }) => {\n\tconst { emdash, user } = locals;\n\tconst db = emdash.db;\n\tconst { name } = params;\n\n\tconst denied = requirePerm(user, \"widgets:manage\");\n\tif (denied) return denied;\n\n\tif (!name) {\n\t\treturn apiError(\"VALIDATION_ERROR\", \"name is required\", 400);\n\t}\n\n\ttry {\n\t\t// Get the area\n\t\tconst area = await db\n\t\t\t.selectFrom(\"_emdash_widget_areas\")\n\t\t\t.select(\"id\")\n\t\t\t.where(\"name\", \"=\", name)\n\t\t\t.executeTakeFirst();\n\n\t\tif (!area) {\n\t\t\treturn apiError(\"NOT_FOUND\", `Widget area \"${name}\" not found`, 404);\n\t\t}\n\n\t\tconst body = await parseBody(request, reorderWidgetsBody);\n\t\tif (isParseError(body)) return body;\n\n\t\t// Verify all widget IDs belong to this area\n\t\tconst existingWidgets = await db\n\t\t\t.selectFrom(\"_emdash_widgets\")\n\t\t\t.select(\"id\")\n\t\t\t.where(\"area_id\", \"=\", area.id)\n\t\t\t.execute();\n\n\t\tconst existingIds = new Set(existingWidgets.map((w) => w.id));\n\t\tfor (const id of body.widgetIds) {\n\t\t\tif (!existingIds.has(id)) {\n\t\t\t\treturn apiError(\"VALIDATION_ERROR\", `Widget \"${id}\" not found in area \"${name}\"`, 400);\n\t\t\t}\n\t\t}\n\n\t\t// Update sort_order for each widget\n\t\tawait Promise.all(\n\t\t\tbody.widgetIds.map((id, index) =>\n\t\t\t\tdb.updateTable(\"_emdash_widgets\").set({ sort_order: index }).where(\"id\", \"=\", id).execute(),\n\t\t\t),\n\t\t);\n\n\t\treturn apiSuccess({ success: true });\n\t} catch (error) {\n\t\treturn handleError(error, \"Failed to reorder widgets\", \"WIDGET_REORDER_ERROR\");\n\t}\n};\n"],"mappings":";;;;;;;;;;;;;AAaA,MAAa,YAAY;AAEzB,MAAa,OAAiB,OAAO,EAAE,QAAQ,SAAS,aAAa;CACpE,MAAM,EAAE,QAAQ,SAAS;CACzB,MAAM,KAAK,OAAO;CAClB,MAAM,EAAE,SAAS;CAEjB,MAAM,SAAS,YAAY,MAAM,iBAAiB;AAClD,KAAI,OAAQ,QAAO;AAEnB,KAAI,CAAC,KACJ,QAAO,SAAS,oBAAoB,oBAAoB,IAAI;AAG7D,KAAI;EAEH,MAAM,OAAO,MAAM,GACjB,WAAW,uBAAuB,CAClC,OAAO,KAAK,CACZ,MAAM,QAAQ,KAAK,KAAK,CACxB,kBAAkB;AAEpB,MAAI,CAAC,KACJ,QAAO,SAAS,aAAa,gBAAgB,KAAK,cAAc,IAAI;EAGrE,MAAM,OAAO,MAAM,UAAU,SAAS,mBAAmB;AACzD,MAAI,aAAa,KAAK,CAAE,QAAO;EAG/B,MAAM,kBAAkB,MAAM,GAC5B,WAAW,kBAAkB,CAC7B,OAAO,KAAK,CACZ,MAAM,WAAW,KAAK,KAAK,GAAG,CAC9B,SAAS;EAEX,MAAM,cAAc,IAAI,IAAI,gBAAgB,KAAK,MAAM,EAAE,GAAG,CAAC;AAC7D,OAAK,MAAM,MAAM,KAAK,UACrB,KAAI,CAAC,YAAY,IAAI,GAAG,CACvB,QAAO,SAAS,oBAAoB,WAAW,GAAG,uBAAuB,KAAK,IAAI,IAAI;AAKxF,QAAM,QAAQ,IACb,KAAK,UAAU,KAAK,IAAI,UACvB,GAAG,YAAY,kBAAkB,CAAC,IAAI,EAAE,YAAY,OAAO,CAAC,CAAC,MAAM,MAAM,KAAK,GAAG,CAAC,SAAS,CAC3F,CACD;AAED,SAAO,WAAW,EAAE,SAAS,MAAM,CAAC;UAC5B,OAAO;AACf,SAAO,YAAY,OAAO,6BAA6B,uBAAuB"}