feat: Add new diffing

This commit is contained in:
Khaled Garbaya
2017-12-13 15:48:23 +01:00
parent b5d69d71e7
commit a5c0761405
3 changed files with 28 additions and 4 deletions

1
.gitignore vendored
View File

@@ -65,3 +65,4 @@ cypress
# e2e test directory
test/e2e
.vscode

View File

@@ -1,8 +1,9 @@
const { getEntry } = require('./../services/contentful')
const { isObject, isArray } = require('lodash')
async function getPublishedEntry (entry) {
try {
return await getEntry(entry.sys.id)
return await getEntry(entry.sys.id, entry.sys.contentType.sys.id)
} catch (err) {
return null
}
@@ -12,11 +13,32 @@ module.exports = async function attachEntryState (entry) {
const publishedEntry = await getPublishedEntry(entry)
entry.draft = false
entry.pendingChanges = false
if (!publishedEntry) {
entry.draft = true
}
const entriesToCompare = Object.keys(entry.fields).map((key) => {
const field = entry.fields[key]
if (isObject(field)) {
return [entry.fields[key], publishedEntry.fields[key]]
}
}).filter(Boolean)
entriesToCompare.forEach((item) => {
const originalItem = item[0]
const publishedItem = item[1]
if (isArray(originalItem)) {
originalItem.forEach((innerItem, index) => {
if (!isArray(innerItem) && isObject(innerItem) && (!innerItem.fields || originalItem[index].sys.updatedAt !== publishedItem[index].sys.updatedAt)) {
entry.pendingChanges = true
return
}
})
} else if (isObject(item[0]) && (!item[0].fields || item[0].sys.updatedAt !== item[1].sys.updatedAt)) {
entry.pendingChanges = true
return
}
})
if (entry && publishedEntry && (entry.sys.updatedAt !== publishedEntry.sys.updatedAt)) {
entry.pendingChanges = true
}

View File

@@ -52,8 +52,9 @@ module.exports.getSpace = assert((api = `cda`) => {
* @returns {Object}
*/
module.exports.getEntry = assert((entryId, api = `cda`) => {
return getClient(api).getEntry(entryId)
module.exports.getEntry = assert((entryId, contentType, api = `cda`) => {
return getClient(api).getEntries({content_type: contentType, 'sys.id': entryId})
.then((response) => response.items[0])
}, 'Entry')
/**