|
7 | 7 | * @memberof forge.routes.api |
8 | 8 | */ |
9 | 9 |
|
| 10 | +const { UpdatesCollection } = require('../../auditLog/formatters.js') |
| 11 | + |
10 | 12 | module.exports = async function (app) { |
11 | 13 | /** @type {typeof import('../../db/controllers/Snapshot.js')} */ |
12 | 14 | const snapshotController = app.db.controllers.Snapshot |
@@ -160,6 +162,54 @@ module.exports = async function (app) { |
160 | 162 | reply.send({ status: 'okay' }) |
161 | 163 | }) |
162 | 164 |
|
| 165 | + /** |
| 166 | + * Update a snapshot |
| 167 | + */ |
| 168 | + app.put('/:id', { |
| 169 | + preHandler: app.needsPermission('snapshot:edit'), |
| 170 | + schema: { |
| 171 | + summary: 'Update a snapshot', |
| 172 | + tags: ['Snapshots'], |
| 173 | + params: { |
| 174 | + type: 'object', |
| 175 | + properties: { |
| 176 | + id: { type: 'string' } |
| 177 | + } |
| 178 | + }, |
| 179 | + body: { |
| 180 | + type: 'object', |
| 181 | + properties: { |
| 182 | + name: { type: 'string' }, |
| 183 | + description: { type: 'string' } |
| 184 | + } |
| 185 | + }, |
| 186 | + response: { |
| 187 | + 200: { |
| 188 | + $ref: 'Snapshot' |
| 189 | + }, |
| 190 | + '4xx': { |
| 191 | + $ref: 'APIError' |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + }, async (request, reply) => { |
| 196 | + // capture the original name/description for the audit log |
| 197 | + const snapshotBefore = { name: request.snapshot.name, description: request.snapshot.description } |
| 198 | + // perform the update |
| 199 | + const snapshot = await snapshotController.updateSnapshot(request.snapshot, request.body) |
| 200 | + // log the update |
| 201 | + const snapshotAfter = { name: snapshot.name, description: snapshot.description } |
| 202 | + const updates = new UpdatesCollection() |
| 203 | + updates.pushDifferences(snapshotBefore, snapshotAfter) |
| 204 | + if (request.ownerType === 'device') { |
| 205 | + const application = await request.owner.getApplication() |
| 206 | + await applicationLogger.application.device.snapshot.updated(request.session.User, null, application, request.owner, request.snapshot, updates) |
| 207 | + } else if (request.ownerType === 'instance') { |
| 208 | + await projectLogger.project.snapshot.updated(request.session.User, null, request.owner, request.snapshot, updates) |
| 209 | + } |
| 210 | + reply.send(projectSnapshotView.snapshot(snapshot)) |
| 211 | + }) |
| 212 | + |
163 | 213 | /** |
164 | 214 | * Export a snapshot for later import in another project or platform |
165 | 215 | */ |
|
0 commit comments