Skip to content

Commit

Permalink
fix(logs): fixes wrong diffs for Array<FK> (fixes #462)
Browse files Browse the repository at this point in the history
  • Loading branch information
naholyr committed Sep 25, 2017
1 parent 3b22a11 commit 7704311
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions server/lib/edit-logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ const isEmptyDiff = changes =>

const flattenDiff = diffs => diffs.map(diff => Array.isArray(diff) && diff.length === 1 ? diff[0] : diff)

const cleanupData = (data, subDoc = false, returnNullIfNotModified = false) => {
const cleanupData = (data, inArray = false, returnNullIfNotModified = false) => {
if (!data) {
return returnNullIfNotModified ? null : data
}
Expand All @@ -159,9 +159,19 @@ const cleanupData = (data, subDoc = false, returnNullIfNotModified = false) => {
return data
}

// Sub-document: if it has an id, the whole object should be replace with this value
if (subDoc && data._id instanceof mongoose.mongo.ObjectID) {
return getID(data)
// Handle Array<FK>
if (inArray) {
// Case 1: the FK is a mongo.ObjectID
if (data instanceof mongoose.mongo.ObjectID) {
return asID(data)
}
// Case 2: the FK has been populated
else if (typeof data === 'object' && data._id instanceof mongoose.mongo.ObjectID) {
return getID(data)
}
// Case 3: the FK is a string: use it as-is (should not happen, but if it does it's like it's been already cleaned up)
// Other cases: there are not other forms for a FK
// And if it's not a FK? It's a raw value, just handle it normally
}

// Standard case, just cleanup object
Expand Down

0 comments on commit 7704311

Please sign in to comment.