refactor: Refactor draft check

This commit is contained in:
Khaled Garbaya
2017-12-14 11:31:48 +01:00
parent 1d8bf9c3dd
commit c786031717

View File

@@ -31,13 +31,13 @@ module.exports = async function attachEntryState (entry) {
// If the field is an array of reference we need to check its item if they have pending changes // If the field is an array of reference we need to check its item if they have pending changes
if (isArray(originalItem)) { if (isArray(originalItem)) {
originalItem.forEach((innerItem, index) => { originalItem.forEach((innerItem, index) => {
if (!isArray(innerItem) && isObject(innerItem) && (!innerItem.fields || originalItem[index].sys.updatedAt !== publishedItem[index].sys.updatedAt)) { if (!isArray(innerItem) && isLinkDraft(innerItem, publishedItem[index])) {
entry.pendingChanges = true entry.pendingChanges = true
return return
} }
}) })
// If the field is a single reference we just check if it has pending changes // If the field is a single reference we just check if it has pending changes
} else if (isObject(item[0]) && (!item[0].fields || !isDateEqual(item[0].sys.updatedAt !== item[1].sys.updatedAt))) { } else if (isLinkDraft(item[0], item[1])) {
entry.pendingChanges = true entry.pendingChanges = true
return return
} }
@@ -50,8 +50,13 @@ module.exports = async function attachEntryState (entry) {
return entry return entry
} }
function isLinkDraft (previewLink, deliveryLink) {
return isObject(previewLink) && (!previewLink.fields || !isDateEqual(previewLink.sys.updatedAt !== previewLink.sys.updatedAt))
}
function isDateEqual (lhs, rhs) { function isDateEqual (lhs, rhs) {
const lhsDate = new Date(lhs) const lhsDate = new Date(lhs)
const rhsDate = new Date(rhs) const rhsDate = new Date(rhs)
return lhsDate.setMilliseconds(0) === rhsDate.setMilliseconds(0) return lhsDate.setMilliseconds(0) === rhsDate.setMilliseconds(0)
} }