Skip to content

Commit

Permalink
chore: unify the workpad update handler (#31218) (#31823)
Browse files Browse the repository at this point in the history
  • Loading branch information
w33ble authored Feb 22, 2019
1 parent 7930a3f commit 5d59537
Show file tree
Hide file tree
Showing 2 changed files with 567 additions and 47 deletions.
55 changes: 8 additions & 47 deletions x-pack/plugins/canvas/server/routes/workpad.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,10 @@ export function workpad(server) {
);
}

function updateWorkpad(req) {
const savedObjectsClient = req.getSavedObjectsClient();
const { id } = req.params;

const now = new Date().toISOString();

return savedObjectsClient.get(CANVAS_TYPE, id).then(workpad => {
// TODO: Using create with force over-write because of version conflict issues with update
return savedObjectsClient.create(
CANVAS_TYPE,
{
...req.payload,
'@timestamp': now,
'@created': workpad.attributes['@created'],
},
{ overwrite: true, id }
);
});
}

function updateWorkpadAssets(req) {
function updateWorkpad(req, newPayload) {
const savedObjectsClient = req.getSavedObjectsClient();
const { id } = req.params;
const payload = newPayload ? newPayload : req.payload;

const now = new Date().toISOString();

Expand All @@ -95,30 +76,9 @@ export function workpad(server) {
CANVAS_TYPE,
{
...workpad.attributes,
assets: req.payload,
'@timestamp': now,
'@created': workpad.attributes['@created'],
},
{ overwrite: true, id }
);
});
}

function updateWorkpadStructures(req) {
const savedObjectsClient = req.getSavedObjectsClient();
const { id } = req.params;

const now = new Date().toISOString();

return savedObjectsClient.get(CANVAS_TYPE, id).then(workpad => {
// TODO: Using create with force over-write because of version conflict issues with update
return savedObjectsClient.create(
CANVAS_TYPE,
{
...workpad.attributes, // retain preexisting assets and prop order (or maybe better to call out the `assets` prop?)
...req.payload,
'@timestamp': now,
'@created': workpad.attributes['@created'],
...payload,
'@timestamp': now, // always update the modified time
'@created': workpad.attributes['@created'], // ensure created is not modified
},
{ overwrite: true, id }
);
Expand Down Expand Up @@ -194,7 +154,8 @@ export function workpad(server) {
path: `${routePrefixAssets}/{id}`,
config: { payload: { allow: 'application/json', maxBytes: 26214400 } }, // 25MB payload limit
handler: function(request) {
return updateWorkpadAssets(request)
const payload = { assets: request.payload };
return updateWorkpad(request, payload)
.then(() => ({ ok: true }))
.catch(formatResponse);
},
Expand All @@ -206,7 +167,7 @@ export function workpad(server) {
path: `${routePrefixStructures}/{id}`,
config: { payload: { allow: 'application/json', maxBytes: 26214400 } }, // 25MB payload limit
handler: function(request) {
return updateWorkpadStructures(request)
return updateWorkpad(request)
.then(() => ({ ok: true }))
.catch(formatResponse);
},
Expand Down
Loading

0 comments on commit 5d59537

Please sign in to comment.