Skip to content

Commit

Permalink
update edilog API better answer formating relates #429
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgirard committed Aug 28, 2017
1 parent a3ce0f8 commit f945d00
Showing 1 changed file with 60 additions and 2 deletions.
62 changes: 60 additions & 2 deletions server/routes/editLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ const debug = require('debug')('isari:export');

const ObjectId = mongoose.Types.ObjectId;

// UTILS

function formatKind(kind){
if (kind === "E")
return "update"
if (kind === "D")
return "delete"
if (kind === "N")
return "create"
//by default fall back to update
return "update"
}


module.exports = Router().get('/:model/:itemId?', requiresAuthentication, getEditLog)
Expand Down Expand Up @@ -46,14 +58,60 @@ function getEditLog(req, res){
mongoQuery.item = itemId



EditLog.find(mongoQuery)
// skip and limit
.skip(query.skip ? +query.skip : 0)
.limit(query.limit ? +query.limit : 50).then(data => {
return res.status(200).send(data)
const edits = []
data.forEach(d => {
const edit = {}
edit.who = d.who
edit.date = d.date
edit.item = d.item
edit.action = d.action

if (edit.action === 'update'){

edit.diff = d.diff.filter(d => d[0].path[0] !== 'latestChangeBy')
.map(d => {
d = d[0]
if (d.path.includes("id"))
return undefined
const diff = {path: d.path.filter(e => typeof e !== 'number')}

if (d.kind === 'A'){
//array case...
if (d.item.lhs)
diff.valueBefore = d.item.lhs
if(d.item.rhs)
diff.valueAfter = d.item.rhs
diff.editType = formatKind(d.item.kind)
}
else {
if (d.lhs)
diff.valueBefore = d.lhs
if( d.rhs)
diff.valueAfter = d.rhs
diff.editType = formatKind(d.kind)
}
return diff
}).filter(d => d)
}
else{
edit.diff = d.data
}

edits.push(edit)
})


return res.status(200).send(edits)
})


}

function formatEditLogs(editLogs){


}

0 comments on commit f945d00

Please sign in to comment.