|
| 1 | +/** |
| 2 | + * project-archival.ts — automated project archival with data retention. |
| 3 | + * |
| 4 | + * Mounted at /api/v1/project-archival. Kept separate from the projects |
| 5 | + * router so the collection paths do not collide with its "/:id" routes. |
| 6 | + */ |
| 7 | + |
| 8 | +import { Router } from 'express'; |
| 9 | +import { z } from 'zod'; |
| 10 | +import { validate } from '../middleware/validate.js'; |
| 11 | +import { requireEnhancedPermission } from '../middleware/permissions.js'; |
| 12 | +import { projectArchivalService } from '../services/project-archival/index.js'; |
| 13 | + |
| 14 | +export const projectArchivalRouter = Router(); |
| 15 | + |
| 16 | +const projectStatus = z.enum(['active', 'completed', 'archived', 'disputed', 'abandoned']); |
| 17 | + |
| 18 | +const policySchema = z.object({ |
| 19 | + id: z.string().min(1).optional(), |
| 20 | + name: z.string().min(1), |
| 21 | + scope: z.enum(['global', 'client', 'owner']).optional(), |
| 22 | + scopeId: z.string().min(1).nullable().optional(), |
| 23 | + archiveAfterDays: z.number().int().min(0), |
| 24 | + purgeAfterDays: z.number().int().min(0), |
| 25 | + eligibleStatuses: z.array(projectStatus).optional(), |
| 26 | + enabled: z.boolean().optional(), |
| 27 | + notify: z.boolean().optional(), |
| 28 | +}); |
| 29 | + |
| 30 | +const runSchema = z.object({ |
| 31 | + dryRun: z.boolean().optional(), |
| 32 | +}); |
| 33 | + |
| 34 | +const restoreSchema = z.object({ |
| 35 | + restoredBy: z.string().min(1).optional(), |
| 36 | +}); |
| 37 | + |
| 38 | +const archiveSchema = z.object({ |
| 39 | + projectId: z.string().min(1), |
| 40 | + actor: z.string().min(1).optional(), |
| 41 | +}); |
| 42 | + |
| 43 | +function actorOf(req: unknown): string | undefined { |
| 44 | + return (req as { user?: { id?: string } }).user?.id; |
| 45 | +} |
| 46 | + |
| 47 | +// ── Retention policies ────────────────────────────────────────────────────── |
| 48 | + |
| 49 | +projectArchivalRouter.get( |
| 50 | + '/policies', |
| 51 | + requireEnhancedPermission('projects', 'read'), |
| 52 | + (_req, res, next) => { |
| 53 | + try { |
| 54 | + const policies = projectArchivalService.listPolicies(); |
| 55 | + res.json({ policies, count: policies.length }); |
| 56 | + } catch (err) { next(err); } |
| 57 | + }, |
| 58 | +); |
| 59 | + |
| 60 | +projectArchivalRouter.post( |
| 61 | + '/policies', |
| 62 | + requireEnhancedPermission('projects', 'write'), |
| 63 | + validate(policySchema), |
| 64 | + (req, res) => { |
| 65 | + try { |
| 66 | + res.status(201).json(projectArchivalService.configurePolicy(req.body)); |
| 67 | + } catch (err) { |
| 68 | + res.status(400).json({ error: (err as Error).message }); |
| 69 | + } |
| 70 | + }, |
| 71 | +); |
| 72 | + |
| 73 | +projectArchivalRouter.delete( |
| 74 | + '/policies/:policyId', |
| 75 | + requireEnhancedPermission('projects', 'delete'), |
| 76 | + (req, res, next) => { |
| 77 | + try { |
| 78 | + const deleted = projectArchivalService.deletePolicy(String(req.params.policyId)); |
| 79 | + if (!deleted) { |
| 80 | + res.status(400).json({ error: 'Policy not found or cannot be deleted' }); |
| 81 | + return; |
| 82 | + } |
| 83 | + res.json({ deleted: true, policyId: req.params.policyId }); |
| 84 | + } catch (err) { next(err); } |
| 85 | + }, |
| 86 | +); |
| 87 | + |
| 88 | +// ── Archival sweep ────────────────────────────────────────────────────────── |
| 89 | + |
| 90 | +projectArchivalRouter.get( |
| 91 | + '/candidates', |
| 92 | + requireEnhancedPermission('projects', 'read'), |
| 93 | + (_req, res, next) => { |
| 94 | + try { |
| 95 | + const candidates = projectArchivalService.previewArchival(); |
| 96 | + res.json({ candidates, count: candidates.length }); |
| 97 | + } catch (err) { next(err); } |
| 98 | + }, |
| 99 | +); |
| 100 | + |
| 101 | +projectArchivalRouter.post( |
| 102 | + '/run', |
| 103 | + requireEnhancedPermission('projects', 'write'), |
| 104 | + validate(runSchema), |
| 105 | + (req, res, next) => { |
| 106 | + try { |
| 107 | + res.json(projectArchivalService.runArchival({ dryRun: Boolean(req.body?.dryRun) })); |
| 108 | + } catch (err) { next(err); } |
| 109 | + }, |
| 110 | +); |
| 111 | + |
| 112 | +projectArchivalRouter.post( |
| 113 | + '/archive', |
| 114 | + requireEnhancedPermission('projects', 'write'), |
| 115 | + validate(archiveSchema), |
| 116 | + (req, res, next) => { |
| 117 | + try { |
| 118 | + const record = projectArchivalService.archiveNow( |
| 119 | + req.body.projectId, |
| 120 | + req.body.actor ?? actorOf(req), |
| 121 | + ); |
| 122 | + if (!record) { |
| 123 | + res.status(404).json({ error: 'Project not found or already archived' }); |
| 124 | + return; |
| 125 | + } |
| 126 | + res.status(201).json(record); |
| 127 | + } catch (err) { next(err); } |
| 128 | + }, |
| 129 | +); |
| 130 | + |
| 131 | +// ── Archives, restoration, analytics, notifications ───────────────────────── |
| 132 | + |
| 133 | +projectArchivalRouter.get( |
| 134 | + '/archives', |
| 135 | + requireEnhancedPermission('projects', 'read'), |
| 136 | + (req, res, next) => { |
| 137 | + try { |
| 138 | + const archives = projectArchivalService.listArchives({ |
| 139 | + clientId: typeof req.query.clientId === 'string' ? req.query.clientId : undefined, |
| 140 | + ownerId: typeof req.query.ownerId === 'string' ? req.query.ownerId : undefined, |
| 141 | + policyId: typeof req.query.policyId === 'string' ? req.query.policyId : undefined, |
| 142 | + includeRestored: req.query.includeRestored === 'true', |
| 143 | + includePurged: req.query.includePurged === 'true', |
| 144 | + }); |
| 145 | + res.json({ archives, count: archives.length }); |
| 146 | + } catch (err) { next(err); } |
| 147 | + }, |
| 148 | +); |
| 149 | + |
| 150 | +projectArchivalRouter.post( |
| 151 | + '/restore/:projectId', |
| 152 | + requireEnhancedPermission('projects', 'write'), |
| 153 | + validate(restoreSchema), |
| 154 | + (req, res, next) => { |
| 155 | + try { |
| 156 | + const restored = projectArchivalService.restoreProject( |
| 157 | + String(req.params.projectId), |
| 158 | + req.body?.restoredBy ?? actorOf(req), |
| 159 | + ); |
| 160 | + if (!restored) { |
| 161 | + res.status(404).json({ error: 'No restorable archive found for this project' }); |
| 162 | + return; |
| 163 | + } |
| 164 | + res.json(restored); |
| 165 | + } catch (err) { next(err); } |
| 166 | + }, |
| 167 | +); |
| 168 | + |
| 169 | +projectArchivalRouter.get( |
| 170 | + '/analytics', |
| 171 | + requireEnhancedPermission('projects', 'read'), |
| 172 | + (_req, res, next) => { |
| 173 | + try { |
| 174 | + res.json(projectArchivalService.getAnalytics()); |
| 175 | + } catch (err) { next(err); } |
| 176 | + }, |
| 177 | +); |
| 178 | + |
| 179 | +projectArchivalRouter.get( |
| 180 | + '/notifications', |
| 181 | + requireEnhancedPermission('projects', 'read'), |
| 182 | + (req, res, next) => { |
| 183 | + try { |
| 184 | + const limit = typeof req.query.limit === 'string' ? parseInt(req.query.limit, 10) : 50; |
| 185 | + const projectId = typeof req.query.projectId === 'string' ? req.query.projectId : undefined; |
| 186 | + const notifications = projectArchivalService.listNotifications( |
| 187 | + Number.isFinite(limit) ? limit : 50, |
| 188 | + projectId, |
| 189 | + ); |
| 190 | + res.json({ notifications, count: notifications.length }); |
| 191 | + } catch (err) { next(err); } |
| 192 | + }, |
| 193 | +); |
0 commit comments