|
| 1 | +# Enterprise API - Automation Versioning Endpoints |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Implement versioning for automation scripts. The Next.js app handles database operations (storing version metadata), while the Enterprise API handles S3 operations (copying/managing script files) and Redis operations (chat history). |
| 6 | + |
| 7 | +## Context |
| 8 | + |
| 9 | +### Current S3 Structure |
| 10 | + |
| 11 | +- **Draft script**: `{orgId}/{taskId}/{automationId}.automation.js` |
| 12 | +- Scripts are stored in S3 via the enterprise API |
| 13 | + |
| 14 | +### New S3 Structure for Versions |
| 15 | + |
| 16 | +- **Draft script**: `{orgId}/{taskId}/{automationId}.draft.js` |
| 17 | +- **Published versions**: `{orgId}/{taskId}/{automationId}.v{version}.js` |
| 18 | + |
| 19 | +**Migration Note**: Existing scripts at `{automationId}.automation.js` should be moved to `{automationId}.draft.js` |
| 20 | + |
| 21 | +### Database (handled by Next.js app) |
| 22 | + |
| 23 | +- `EvidenceAutomationVersion` table stores version metadata |
| 24 | +- Next.js app creates version records after enterprise API copies files |
| 25 | + |
| 26 | +## Endpoints to Implement |
| 27 | + |
| 28 | +### 1. Publish Draft Script |
| 29 | + |
| 30 | +**Endpoint**: `POST /api/tasks-automations/publish` |
| 31 | + |
| 32 | +**Purpose**: Create a new version by copying current draft script to a versioned S3 key. |
| 33 | + |
| 34 | +**Request Body**: |
| 35 | + |
| 36 | +```typescript |
| 37 | +{ |
| 38 | + orgId: string; |
| 39 | + taskId: string; |
| 40 | + automationId: string; |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +**Process**: |
| 45 | + |
| 46 | +1. Construct draft S3 key: `{orgId}/{taskId}/{automationId}.draft.js` |
| 47 | +2. Check if draft script exists in S3 |
| 48 | +3. If not found, return error: `{ success: false, error: 'No draft script found to publish' }` |
| 49 | +4. Query database to get the next version number: |
| 50 | + - Find highest existing version for this `automationId` |
| 51 | + - Increment by 1 (or start at 1 if no versions exist) |
| 52 | +5. Construct version S3 key: `{orgId}/{taskId}/{automationId}.v{nextVersion}.js` |
| 53 | +6. Copy draft script to version key in S3 |
| 54 | +7. Return success with the version number and scriptKey |
| 55 | + |
| 56 | +**Response**: |
| 57 | + |
| 58 | +```typescript |
| 59 | +{ |
| 60 | + success: boolean; |
| 61 | + version?: number; // e.g., 1, 2, 3 |
| 62 | + scriptKey?: string; // e.g., "org_xxx/tsk_xxx/aut_xxx.v1.js" |
| 63 | + error?: string; |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +**Note**: Enterprise API determines the version number server-side by querying the database, not from client input. This prevents version conflicts. |
| 68 | + |
| 69 | +**Error Cases**: |
| 70 | + |
| 71 | +- Draft script not found in S3 |
| 72 | +- S3 copy operation fails |
| 73 | +- Invalid orgId/taskId/automationId |
| 74 | + |
| 75 | +--- |
| 76 | + |
| 77 | +### 2. Restore Version to Draft |
| 78 | + |
| 79 | +**Endpoint**: `POST /api/tasks-automations/restore-version` |
| 80 | + |
| 81 | +**Purpose**: Replace current draft script with a published version's script. Chat history is preserved. |
| 82 | + |
| 83 | +**Request Body**: |
| 84 | + |
| 85 | +```typescript |
| 86 | +{ |
| 87 | + orgId: string; |
| 88 | + taskId: string; |
| 89 | + automationId: string; |
| 90 | + version: number; // Which version to restore (e.g., 1, 2, 3) |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +**Process**: |
| 95 | + |
| 96 | +1. Construct version S3 key: `{orgId}/{taskId}/{automationId}.v{version}.js` |
| 97 | +2. Check if version script exists in S3 |
| 98 | +3. If not found, return error: `{ success: false, error: 'Version not found' }` |
| 99 | +4. Construct draft S3 key: `{orgId}/{taskId}/{automationId}.draft.js` |
| 100 | +5. Copy version script to draft key in S3 (overwrites current draft) |
| 101 | +6. Do NOT touch Redis chat history - it should persist |
| 102 | +7. Return success |
| 103 | + |
| 104 | +**Response**: |
| 105 | + |
| 106 | +```typescript |
| 107 | +{ |
| 108 | + success: boolean; |
| 109 | + error?: string; |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +**Error Cases**: |
| 114 | + |
| 115 | +- Version script not found in S3 |
| 116 | +- S3 copy operation fails |
| 117 | +- Invalid version number |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +## Implementation Notes |
| 122 | + |
| 123 | +### S3 Operations |
| 124 | + |
| 125 | +- Use AWS S3 SDK's `copyObject` method to copy between keys |
| 126 | +- Bucket name should come from environment variables |
| 127 | +- Ensure proper error handling for S3 operations |
| 128 | + |
| 129 | +### Authentication |
| 130 | + |
| 131 | +- These endpoints should require authentication (API key or session) |
| 132 | +- Validate that the user has access to the organization/task/automation |
| 133 | + |
| 134 | +### Redis Chat History |
| 135 | + |
| 136 | +- **Important**: Do NOT clear or modify chat history when restoring versions |
| 137 | +- Chat history key format: `automation:{automationId}:chat` |
| 138 | +- Chat history persists regardless of which version is in the draft |
| 139 | + |
| 140 | +### Example S3 Keys |
| 141 | + |
| 142 | +For automation `aut_68e6a70803cf925eac17896a` in task `tsk_68e6a5c1e0b762e741c2e020`: |
| 143 | + |
| 144 | +- **Draft**: `org_68e6a5c1d30338b3981c2104/tsk_68e6a5c1e0b762e741c2e020/aut_68e6a70803cf925eac17896a.draft.js` |
| 145 | +- **Version 1**: `org_68e6a5c1d30338b3981c2104/tsk_68e6a5c1e0b762e741c2e020/aut_68e6a70803cf925eac17896a.v1.js` |
| 146 | +- **Version 2**: `org_68e6a5c1d30338b3981c2104/tsk_68e6a5c1e0b762e741c2e020/aut_68e6a70803cf925eac17896a.v2.js` |
| 147 | + |
| 148 | +### Integration Flow |
| 149 | + |
| 150 | +#### Publishing a Version |
| 151 | + |
| 152 | +1. User clicks "Publish" in Next.js UI with optional changelog |
| 153 | +2. Next.js calls `POST /api/tasks-automations/publish` (no version number in request) |
| 154 | +3. Enterprise API: |
| 155 | + - Queries database to get next version number |
| 156 | + - Copies draft → versioned S3 key |
| 157 | + - Returns version number and scriptKey |
| 158 | +4. Next.js saves version record to database with returned version number, scriptKey, and changelog |
| 159 | + |
| 160 | +#### Restoring a Version |
| 161 | + |
| 162 | +1. User clicks "Restore Version X" in Next.js UI |
| 163 | +2. Shows confirmation dialog warning current draft will be lost |
| 164 | +3. Next.js calls `POST /api/tasks-automations/restore-version` |
| 165 | +4. Enterprise API copies version script → draft S3 key |
| 166 | +5. Enterprise API returns success |
| 167 | +6. Next.js shows success message |
| 168 | +7. User can continue editing in builder with restored script |
| 169 | + |
| 170 | +### Error Handling |
| 171 | + |
| 172 | +- Return proper HTTP status codes (404 for not found, 400 for bad request, 500 for S3 errors) |
| 173 | +- Include descriptive error messages in response body |
| 174 | +- Log errors for debugging |
| 175 | + |
| 176 | +### Testing Checklist |
| 177 | + |
| 178 | +- [ ] Can publish a draft script as version 1 |
| 179 | +- [ ] Can publish multiple versions (1, 2, 3...) |
| 180 | +- [ ] Cannot publish if no draft exists |
| 181 | +- [ ] Can restore version 1 to draft |
| 182 | +- [ ] Restoring doesn't affect chat history |
| 183 | +- [ ] S3 keys follow correct naming convention |
| 184 | +- [ ] Proper error messages when scripts don't exist |
0 commit comments