import { Router } from "express"; import type { Db } from "@paperclipai/db"; import { updateExecutionWorkspaceSchema } from "@paperclipai/shared"; import { validate } from "../middleware/validate.js"; import { executionWorkspaceService, logActivity } from "../services/index.js"; import { assertCompanyAccess, getActorInfo } from "./authz.js"; export function executionWorkspaceRoutes(db: Db) { const router = Router(); const svc = executionWorkspaceService(db); router.get("/companies/:companyId/execution-workspaces", async (req, res) => { const companyId = req.params.companyId as string; assertCompanyAccess(req, companyId); const workspaces = await svc.list(companyId, { projectId: req.query.projectId as string | undefined, projectWorkspaceId: req.query.projectWorkspaceId as string | undefined, issueId: req.query.issueId as string | undefined, status: req.query.status as string | undefined, reuseEligible: req.query.reuseEligible === "true", }); res.json(workspaces); }); router.get("/execution-workspaces/:id", async (req, res) => { const id = req.params.id as string; const workspace = await svc.getById(id); if (!workspace) { res.status(404).json({ error: "Execution workspace not found" }); return; } assertCompanyAccess(req, workspace.companyId); res.json(workspace); }); router.patch("/execution-workspaces/:id", validate(updateExecutionWorkspaceSchema), async (req, res) => { const id = req.params.id as string; const existing = await svc.getById(id); if (!existing) { res.status(404).json({ error: "Execution workspace not found" }); return; } assertCompanyAccess(req, existing.companyId); const workspace = await svc.update(id, { ...req.body, ...(req.body.cleanupEligibleAt ? { cleanupEligibleAt: new Date(req.body.cleanupEligibleAt) } : {}), }); if (!workspace) { res.status(404).json({ error: "Execution workspace not found" }); return; } const actor = getActorInfo(req); await logActivity(db, { companyId: existing.companyId, actorType: actor.actorType, actorId: actor.actorId, agentId: actor.agentId, runId: actor.runId, action: "execution_workspace.updated", entityType: "execution_workspace", entityId: workspace.id, details: { changedKeys: Object.keys(req.body).sort() }, }); res.json(workspace); }); return router; }