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
|
# e2e test directory
|
||||||
test/e2e
|
test/e2e
|
||||||
|
.vscode
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
const { getEntry } = require('./../services/contentful')
|
const { getEntry } = require('./../services/contentful')
|
||||||
|
const { isObject, isArray } = require('lodash')
|
||||||
|
|
||||||
async function getPublishedEntry (entry) {
|
async function getPublishedEntry (entry) {
|
||||||
try {
|
try {
|
||||||
return await getEntry(entry.sys.id)
|
return await getEntry(entry.sys.id, entry.sys.contentType.sys.id)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
@@ -12,11 +13,32 @@ module.exports = async function attachEntryState (entry) {
|
|||||||
const publishedEntry = await getPublishedEntry(entry)
|
const publishedEntry = await getPublishedEntry(entry)
|
||||||
entry.draft = false
|
entry.draft = false
|
||||||
entry.pendingChanges = false
|
entry.pendingChanges = false
|
||||||
|
|
||||||
if (!publishedEntry) {
|
if (!publishedEntry) {
|
||||||
entry.draft = true
|
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)) {
|
if (entry && publishedEntry && (entry.sys.updatedAt !== publishedEntry.sys.updatedAt)) {
|
||||||
entry.pendingChanges = true
|
entry.pendingChanges = true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,8 +52,9 @@ module.exports.getSpace = assert((api = `cda`) => {
|
|||||||
* @returns {Object}
|
* @returns {Object}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module.exports.getEntry = assert((entryId, api = `cda`) => {
|
module.exports.getEntry = assert((entryId, contentType, api = `cda`) => {
|
||||||
return getClient(api).getEntry(entryId)
|
return getClient(api).getEntries({content_type: contentType, 'sys.id': entryId})
|
||||||
|
.then((response) => response.items[0])
|
||||||
}, 'Entry')
|
}, 'Entry')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user