From c7860317170651150dc5145c917d4a804b3d121a Mon Sep 17 00:00:00 2001 From: Khaled Garbaya Date: Thu, 14 Dec 2017 11:31:48 +0100 Subject: [PATCH] refactor: Refactor draft check --- lib/entry-state.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/entry-state.js b/lib/entry-state.js index a3f8501..d8cf844 100644 --- a/lib/entry-state.js +++ b/lib/entry-state.js @@ -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 (isArray(originalItem)) { 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 return } }) // 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 return } @@ -50,8 +50,13 @@ module.exports = async function attachEntryState (entry) { return entry } +function isLinkDraft (previewLink, deliveryLink) { + return isObject(previewLink) && (!previewLink.fields || !isDateEqual(previewLink.sys.updatedAt !== previewLink.sys.updatedAt)) +} + function isDateEqual (lhs, rhs) { const lhsDate = new Date(lhs) const rhsDate = new Date(rhs) return lhsDate.setMilliseconds(0) === rhsDate.setMilliseconds(0) } +