Improve merge on update
This commit is contained in:
10
server/package-lock.json
generated
10
server/package-lock.json
generated
@@ -4892,16 +4892,16 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"mongoose-doc-merge": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/mongoose-doc-merge/-/mongoose-doc-merge-1.0.0.tgz",
|
||||
"integrity": "sha512-rci5BJyqc/0uuQtvXVMh4jJqHi0DdpM8u1s5oAysc28mznPqZ+KfqcCVjTR1RA/4Maii8qC9QRkZfI0UCrAh6g=="
|
||||
},
|
||||
"mongoose-legacy-pluralize": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz",
|
||||
"integrity": "sha512-Yo/7qQU4/EyIS8YDFSeenIvXxZN+ld7YdV9LqFVQJzTLye8unujAWPZ4NWKfFA+RNjh+wvTWKY9Z3E5XM6ZZiQ=="
|
||||
},
|
||||
"mongoose-merge-plugin": {
|
||||
"version": "0.0.5",
|
||||
"resolved": "https://registry.npmjs.org/mongoose-merge-plugin/-/mongoose-merge-plugin-0.0.5.tgz",
|
||||
"integrity": "sha1-AQDPzO2vaUzmNt8o7r6K1RuLKVs="
|
||||
},
|
||||
"monzilla": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/monzilla/-/monzilla-1.1.1.tgz",
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
"jsonwebtoken": "^7.4.0",
|
||||
"mongodb": "^2.2.35",
|
||||
"mongoose": "^5.0.13",
|
||||
"mongoose-merge-plugin": "0.0.5",
|
||||
"mongoose-doc-merge": "^1.0.0",
|
||||
"node-fetch": "^2.1.2",
|
||||
"nodemailer": "^4.0.1",
|
||||
"passport": "^0.3.2",
|
||||
|
||||
@@ -111,23 +111,19 @@ export class ActivityRoutes {
|
||||
}
|
||||
|
||||
let Activity = this.db.Activity
|
||||
let activityUpdates = req.body
|
||||
let activity = await Activity.findById(req.body._id)
|
||||
|
||||
const foundActivity = await Activity.findById(activityUpdates._id)
|
||||
|
||||
if (!foundActivity) {
|
||||
if (!activity) {
|
||||
return next(
|
||||
createError.NotFound(`Activity with _id ${_id} was not found`)
|
||||
createError.NotFound(`Activity with _id ${req.body_id} was not found`)
|
||||
)
|
||||
}
|
||||
|
||||
// Strip off all BSON types
|
||||
foundActivity = JSON.parse(JSON.stringify(foundActivity))
|
||||
let activityUpdates = new Activity(req.body)
|
||||
|
||||
const mergedActivity = Activity.hydrate(
|
||||
merge(foundActivity, activityUpdates)
|
||||
)
|
||||
const savedActivity = await mergedActivity.save()
|
||||
// Strip off all BSON types
|
||||
activity.merge(activityUpdates)
|
||||
const savedActivity = await activity.save()
|
||||
|
||||
res.json(savedActivity.toClient())
|
||||
}
|
||||
|
||||
@@ -107,21 +107,17 @@ export class TeamRoutes {
|
||||
}
|
||||
|
||||
let Team = this.db.Team
|
||||
let teamUpdates = null
|
||||
let team = await Team.findById(req.body._id)
|
||||
|
||||
try {
|
||||
teamUpdates = new Team(req.body)
|
||||
} catch (err) {
|
||||
throw createError.BadRequest("Invalid data")
|
||||
if (!team) {
|
||||
throw createError.NotFound(`Team with _id ${req.body_id} was not found`)
|
||||
}
|
||||
|
||||
const foundTeam = await Team.findById(teamUpdates._id)
|
||||
let teamUpdates = new Team(req.body)
|
||||
|
||||
if (!foundTeam) {
|
||||
throw createError.NotFound(`Team with _id ${_id} was not found`)
|
||||
}
|
||||
foundTeam.merge(teamUpdates)
|
||||
const savedTeam = await foundTeam.save()
|
||||
team.merge(teamUpdates)
|
||||
|
||||
const savedTeam = await team.save()
|
||||
|
||||
res.json(savedTeam.toClient())
|
||||
}
|
||||
|
||||
@@ -183,30 +183,24 @@ export class UserRoutes {
|
||||
throw createError.Forbidden()
|
||||
}
|
||||
|
||||
const User = this.db.User
|
||||
let userUpdates = null
|
||||
|
||||
try {
|
||||
userUpdates = new User(req.body)
|
||||
} catch (err) {
|
||||
throw createError.BadRequest("Invalid data")
|
||||
}
|
||||
|
||||
if (isSelf && !isAdmin) {
|
||||
throw createError.BadRequest("Cannot modify own administrator level")
|
||||
}
|
||||
|
||||
const foundUser = await User.findById(userUpdates._id)
|
||||
const User = this.db.User
|
||||
const user = await User.findById(req.body._id)
|
||||
|
||||
if (!foundUser) {
|
||||
throw createError.NotFound(`User with _id ${user._id} was not found`)
|
||||
if (!user) {
|
||||
throw createError.NotFound(`User with _id ${req.body._id} was not found`)
|
||||
}
|
||||
|
||||
// We don't allow direct updates to the email field so remove it if present
|
||||
const userUpdates = new User(req.body)
|
||||
|
||||
delete userUpdates.email
|
||||
|
||||
foundUser.merge(userUpdates)
|
||||
const savedUser = await foundUser.save()
|
||||
user.merge(userUpdates)
|
||||
const savedUser = await user.save()
|
||||
|
||||
res.json(savedUser.toClient(req.user))
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import mongoose from "mongoose"
|
||||
import mongodb from "mongodb"
|
||||
import Grid from "gridfs-stream"
|
||||
import merge from "mongoose-merge-plugin"
|
||||
import mergePlugin from "mongoose-doc-merge"
|
||||
import autobind from "autobind-decorator"
|
||||
import * as Schemas from "./schemas"
|
||||
import util from "util"
|
||||
@@ -9,7 +9,7 @@ import util from "util"
|
||||
@autobind
|
||||
export class DB {
|
||||
constructor() {
|
||||
mongoose.plugin(merge)
|
||||
mongoose.plugin(mergePlugin)
|
||||
}
|
||||
|
||||
async connect(mongoUri, isProduction) {
|
||||
|
||||
Reference in New Issue
Block a user