feat: Add new diffing
This commit is contained in:
committed by
Khaled Garbaya
parent
fec1fa8a41
commit
4344b6ff6b
1
.gitignore
vendored
1
.gitignore
vendored
@@ -65,3 +65,4 @@ cypress
|
||||
|
||||
# e2e test directory
|
||||
test/e2e
|
||||
.vscode
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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')
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user