-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add option to import documents only as drafts #95
Changes from all commits
5c571ca
93c0e7e
b2d029b
5a27d3a
c60341e
81668eb
f0d4099
cd662fe
5a25ea0
c146506
728102b
09cb7e3
b868628
9caff0b
6c0e096
b2f139b
c640a1e
31c71ed
a280ffa
d716aba
b104d4e
b2f02f8
8f0f546
48bc9e4
99ca648
5105866
3491452
95c83d6
5dfcd5c
e97ffdd
9665fc2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ module.exports = self => { | |
} | ||
|
||
const { file } = req.files || {}; | ||
const importDraftsOnly = self.apos.launder.boolean(req.body.importDraftsOnly); | ||
const overrideLocale = self.apos.launder.boolean(req.body.overrideLocale); | ||
const formatLabel = self.apos.launder.string(req.body.formatLabel); | ||
let exportPath = await self.getExportPathById(self.apos.launder.string(req.body.exportPathId)); | ||
|
@@ -59,6 +60,10 @@ module.exports = self => { | |
throw self.apos.error(error.message); | ||
} | ||
|
||
if (importDraftsOnly) { | ||
docs = self.getPublishedDocsAsDraft(docs); | ||
} | ||
|
||
const differentDocsLocale = self.getFirstDifferentLocale(req, docs); | ||
const siteHasMultipleLocales = Object.keys(self.apos.i18n.locales).length > 1; | ||
|
||
|
@@ -74,6 +79,7 @@ module.exports = self => { | |
moduleName, | ||
exportPathId: await self.getExportPathId(exportPath), | ||
formatLabel: format.label, | ||
importDraftsOnly, | ||
content: { | ||
heading: req.t('aposImportExport:importWithCurrentLocaleHeading'), | ||
description: req.t('aposImportExport:importWithCurrentLocaleDescription', { | ||
|
@@ -129,7 +135,8 @@ module.exports = self => { | |
reporting, | ||
duplicatedIds, | ||
duplicatedDocs, | ||
failedIds | ||
failedIds, | ||
importDraftsOnly | ||
}); | ||
|
||
if (!duplicatedDocs.length) { | ||
|
@@ -174,6 +181,7 @@ module.exports = self => { | |
await self.apos.attachment.recomputeAllDocReferences(); | ||
|
||
const results = { | ||
importDraftsOnly, | ||
overrideLocale, | ||
duplicatedDocs, | ||
importedAttachments, | ||
|
@@ -248,6 +256,37 @@ module.exports = self => { | |
} | ||
}, | ||
|
||
// Get only the published docs and convert them into draft. | ||
// If a doc is a draft and has no published version, we keep it. | ||
// If a doc has no aposMode, we keep it. | ||
getPublishedDocsAsDraft(docs) { | ||
return docs | ||
.filter(isPublishedOrDraftAlone) | ||
.map(convertToDraft); | ||
|
||
function isPublishedOrDraftAlone(doc) { | ||
// i.e "is draft without a published version" | ||
const isDraftAlone = | ||
doc.aposMode === 'draft' && | ||
!docs.some(item => item.aposDocId === doc.aposDocId && item.aposMode === 'published'); | ||
|
||
return !doc.aposMode || doc.aposMode === 'published' || isDraftAlone; | ||
} | ||
|
||
function convertToDraft(doc) { | ||
if (doc.aposMode === 'published') { | ||
return { | ||
...doc, | ||
_id: doc._id?.replace(':published', ':draft'), | ||
aposLocale: doc.aposLocale?.replace(':published', ':draft'), | ||
aposMode: 'draft' | ||
}; | ||
} | ||
|
||
return doc; | ||
} | ||
}, | ||
|
||
isLocaleDifferent(req, doc) { | ||
return doc.aposLocale && self.extractLocale(doc.aposLocale) !== req.locale; | ||
}, | ||
|
@@ -385,7 +424,7 @@ module.exports = self => { | |
}, | ||
|
||
async insertDocs(req, { | ||
docs, reporting, duplicatedIds, duplicatedDocs, failedIds | ||
docs, reporting, duplicatedIds, duplicatedDocs, failedIds, importDraftsOnly | ||
}) { | ||
for (const doc of docs) { | ||
if (duplicatedIds.has(doc.aposDocId) || failedIds.includes(doc.aposDocId)) { | ||
|
@@ -402,7 +441,8 @@ module.exports = self => { | |
doc, | ||
updateKey, | ||
updateField, | ||
duplicatedDocs | ||
duplicatedDocs, | ||
importDraftsOnly | ||
}); | ||
reporting.success(); | ||
} catch (error) { | ||
|
@@ -420,7 +460,8 @@ module.exports = self => { | |
const inserted = await self.insertOrUpdateDoc(req, { | ||
doc, | ||
failedIds, | ||
duplicatedDocs | ||
duplicatedDocs, | ||
importDraftsOnly | ||
}); | ||
if (inserted) { | ||
reporting.success(); | ||
|
@@ -437,7 +478,8 @@ module.exports = self => { | |
doc, | ||
updateKey, | ||
updateField, | ||
duplicatedDocs = [] | ||
duplicatedDocs = [], | ||
importDraftsOnly | ||
}) { | ||
const manager = self.apos.doc.getManager(doc.type); | ||
if (!self.canImport(req, doc.type)) { | ||
|
@@ -481,7 +523,7 @@ module.exports = self => { | |
async function insert() { | ||
// Insert as "published" to insert | ||
// in both draft and published versions: | ||
const _req = req.clone({ mode: 'published' }); | ||
const _req = req.clone({ mode: importDraftsOnly ? 'draft' : 'published' }); | ||
|
||
const type = doc.type; | ||
const docToInsert = {}; | ||
|
@@ -491,6 +533,10 @@ module.exports = self => { | |
fetchRelationships: false | ||
}); | ||
|
||
if (importDraftsOnly) { | ||
delete docToInsert.lastPublishedAt; | ||
} | ||
|
||
if (self.isPage(manager)) { | ||
// TODO: check if this is still true | ||
// `convert` sets the type to `@apostrophecms/home-page`, | ||
|
@@ -528,7 +574,16 @@ module.exports = self => { | |
.toObject(); | ||
} | ||
|
||
const docsToUpdate = [ matchingDraft, matchingPublished ].filter(Boolean); | ||
const docsToUpdate = [ matchingDraft, matchingPublished ].filter(doc => { | ||
if (!doc) { | ||
return false; | ||
} | ||
// If `importDraftsOnly` is true, we only update the existing draft version. | ||
if (importDraftsOnly && doc.aposMode === 'published' && matchingDraft) { | ||
return false; | ||
} | ||
return true; | ||
}); | ||
|
||
for (const docToUpdate of docsToUpdate) { | ||
const _req = req.clone({ mode: docToUpdate.aposMode }); | ||
|
@@ -537,6 +592,7 @@ module.exports = self => { | |
presentFieldsOnly: true, | ||
fetchRelationships: false | ||
}); | ||
|
||
self.isPage(manager) | ||
? await importPage.update({ | ||
manager: self.apos.page, | ||
|
@@ -547,7 +603,14 @@ module.exports = self => { | |
: await manager.update(_req, docToUpdate); | ||
} | ||
|
||
await self.setDocAsNotModified(matchingDraft); | ||
// Set the `modified` property to true if the draft version is different from the published one | ||
if (matchingDraft) { | ||
await self.apos.doc.db.updateOne({ _id: matchingDraft._id }, { | ||
$set: { | ||
modified: !self.apos.schema.isEqual(req, manager.schema, matchingDraft, matchingPublished) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would have put the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed the function, as it was not really necessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree |
||
}); | ||
} | ||
} | ||
}, | ||
|
||
|
@@ -599,7 +662,7 @@ module.exports = self => { | |
}, | ||
|
||
async insertOrUpdateDoc(req, { | ||
doc, method = 'insert', failedIds = [], duplicatedDocs = [], existingAposDocId | ||
doc, method = 'insert', failedIds = [], duplicatedDocs = [], existingAposDocId, importDraftsOnly | ||
}) { | ||
const manager = self.apos.doc.getManager(doc.type); | ||
if (existingAposDocId) { | ||
|
@@ -637,6 +700,7 @@ module.exports = self => { | |
|
||
async function insert() { | ||
const _req = req.clone({ mode: doc.aposMode }); | ||
|
||
self.apos.schema.simulateRelationshipsFromStorage(req, doc, manager.schema); | ||
|
||
const type = doc.type; | ||
|
@@ -647,6 +711,10 @@ module.exports = self => { | |
fetchRelationships: false | ||
}); | ||
|
||
if (importDraftsOnly) { | ||
delete docToInsert.lastPublishedAt; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here too, could be done inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? See my comment above to make it functional inside |
||
|
||
if (self.isPage(manager)) { | ||
// TODO: check if this is still true | ||
// `convert` sets the type to `@apostrophecms/home-page`, | ||
|
@@ -668,6 +736,9 @@ module.exports = self => { | |
const _req = req.clone({ mode: doc.aposMode }); | ||
self.apos.schema.simulateRelationshipsFromStorage(req, doc, manager.schema); | ||
const docToUpdate = await self.apos.doc.db.findOne({ _id: doc._id }); | ||
if (!docToUpdate) { | ||
return; | ||
} | ||
await manager.convert(_req, doc, docToUpdate, { | ||
presentFieldsOnly: true, | ||
fetchRelationships: false | ||
|
@@ -682,9 +753,24 @@ module.exports = self => { | |
} else { | ||
await manager.update(_req, docToUpdate); | ||
} | ||
if (doc.aposMode === 'draft') { | ||
await self.setDocAsNotModified(docToUpdate); | ||
|
||
const docToCompare = doc.aposMode === 'draft' | ||
? await self.apos.doc.db.findOne({ _id: docToUpdate._id.replace(':draft', ':published') }) | ||
: await self.apos.doc.db.findOne({ _id: docToUpdate._id.replace(':published', ':draft') }); | ||
|
||
if (!docToCompare) { | ||
return; | ||
} | ||
|
||
// Set the `modified` property to true if the draft version is different from the published one | ||
await self.apos.doc.db.updateOne( | ||
{ _id: docToUpdate._id.replace(':published', ':draft') }, | ||
{ | ||
$set: { | ||
modified: !self.apos.schema.isEqual(req, manager.schema, docToUpdate, docToCompare) | ||
} | ||
} | ||
); | ||
} | ||
}, | ||
|
||
|
@@ -699,16 +785,6 @@ module.exports = self => { | |
self.apos.instanceOf(manager, '@apostrophecms/any-page-type'); | ||
}, | ||
|
||
// Manually set `modified: false` because `setModified` | ||
// option is not taken into account in the `update` method. | ||
setDocAsNotModified(doc) { | ||
return self.apos.doc.db.updateOne({ _id: doc._id }, { | ||
$set: { | ||
modified: false | ||
} | ||
}); | ||
}, | ||
|
||
async insertOrUpdateAttachment(req, { | ||
attachmentInfo: { attachment, file }, | ||
duplicatedIds = new Set(), | ||
|
@@ -760,6 +836,7 @@ module.exports = self => { | |
}, | ||
|
||
async overrideDuplicates(req) { | ||
const importDraftsOnly = self.apos.launder.boolean(req.body.importDraftsOnly); | ||
const overrideLocale = self.apos.launder.boolean(req.body.overrideLocale); | ||
const exportPath = await self.getExportPathById(self.apos.launder.string(req.body.exportPathId)); | ||
const docIds = self.apos.launder.strings(req.body.docIds); | ||
|
@@ -792,7 +869,11 @@ module.exports = self => { | |
|
||
const { docs, attachmentsInfo = [] } = await format.input(exportPath); | ||
|
||
const filterDocs = docs.filter(doc => docIds.includes(doc.aposDocId)); | ||
const filterDocs = importDraftsOnly | ||
? self.getPublishedDocsAsDraft( | ||
docs.filter(doc => docIds.includes(doc.aposDocId)) | ||
) | ||
: docs.filter(doc => docIds.includes(doc.aposDocId)); | ||
|
||
const differentDocsLocale = self.getFirstDifferentLocale(req, filterDocs); | ||
const siteHasMultipleLocales = Object.keys(self.apos.i18n.locales).length > 1; | ||
|
@@ -840,7 +921,8 @@ module.exports = self => { | |
method: 'update', | ||
failedIds, | ||
existingAposDocId, | ||
duplicatedDocs | ||
duplicatedDocs, | ||
importDraftsOnly | ||
}); | ||
|
||
if (existingAposDocId) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this could be done in the function
getPublishedDocsAsDraft
to avoid spreading the logic?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to stay "functional":
I think these lines should exist in the function
convertToDraft
.