Improve merge on update

This commit is contained in:
John Lyon-Smith
2018-05-09 15:32:53 -07:00
parent 57355088f0
commit d087da2ce7
8 changed files with 40 additions and 58 deletions

View File

@@ -147,22 +147,19 @@ export class WorkItemRoutes {
}
let WorkItem = this.db.WorkItem
let workItemUpdates = req.body
let foundWorkItem = await WorkItem.findById(workItemUpdates._id)
let workItem = await WorkItem.findById(req.body._id)
if (!foundWorkItem) {
if (!workItem) {
return next(
createError.NotFound(`WorkItem with _id ${_id} was not found`)
createError.NotFound(`WorkItem with _id ${req.body_id} was not found`)
)
}
// Strip off all BSON types
foundWorkItem = JSON.parse(JSON.stringify(foundWorkItem))
const workItemUpdates = new WorkItem(req.body)
const mergedWorkItem = WorkItem.hydrate(
merge(foundWorkItem, workItemUpdates)
)
const savedWorkItem = await mergedWorkItem.save()
workItem.merge(workItemUpdates)
const savedWorkItem = await workItem.save()
res.json(savedWorkItem.toClient())
}