-
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
Conversation
this.timeout(t.timeout); | ||
|
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.
for slow computers...
cypress no regression test: https://github.com/apostrophecms/testbed/actions/runs/11531890614/ |
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.
Globally good, few style feedback.
One tiny issue noticed, when importing draft that has no changes with the existing document, it's marked pending update
but it cannot be updated (can discard draft but maybe unclear to users).
Two solutions:
- Running
detectDocChange
to each imported document with existing one (if existing one). Might be heavy to add this step. - Updating
detectDocChange
to consider there are difference between draft and live (even if there is not) whenmodified: true
to allow users to review and update to remove the modified flag and the associated label.
lib/methods/import.js
Outdated
// i.e "is draft without a published version" | ||
const isDraftAlone = | ||
doc.aposMode === 'draft' && | ||
!docs.find(item => item.aposDocId === doc.aposDocId && item.aposMode === 'published'); |
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.
You could use some
here that return a boolean instead of a document like find
.
@@ -491,6 +533,10 @@ module.exports = self => { | |||
fetchRelationships: false | |||
}); | |||
|
|||
if (importDraftsOnly) { | |||
delete docToInsert.lastPublishedAt; | |||
} |
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?
function convertToDraft(doc) {
if (doc.aposMode === 'published') {
doc._id = doc._id?.replace('published', 'draft');
doc.aposLocale = doc.aposLocale?.replace('published', 'draft');
doc.aposMode = 'draft';
}
delete doc.lastPublishedAt;
return doc;
}
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":
function convertToDraft({ lastPublishedAt, ...doc }) {
if (doc.aposMode === 'published') {
return {
...doc,
_id: doc._id?.replace(':published', ':draft'),
aposLocale: doc.aposLocale?.replace(':published', ':draft'),
aposMode: 'draft'
};
}
return doc;
}
I think these lines should exist in the function convertToDraft
.
await self.editDocMongoFields(matchingDraft, { | ||
$set: { | ||
modified: Boolean(importDraftsOnly) | ||
} |
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.
Would have put the $set
part into the function. Or just have renamed setDocModified
to take a boolean.
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.
I removed the function, as it was not really necessary.
Using the mongo connector directly is way clearer I think.
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.
Agree
@@ -647,6 +710,10 @@ module.exports = self => { | |||
fetchRelationships: false | |||
}); | |||
|
|||
if (importDraftsOnly) { | |||
delete docToInsert.lastPublishedAt; | |||
} |
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.
Here too, could be done inside getPublishedDocsAsDraft
.
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.
? See my comment above to make it functional inside convertToDraft
lib/methods/import.js
Outdated
@@ -792,7 +861,7 @@ module.exports = self => { | |||
|
|||
const { docs, attachmentsInfo = [] } = await format.input(exportPath); | |||
|
|||
const filterDocs = docs.filter(doc => docIds.includes(doc.aposDocId)); | |||
let filterDocs = docs.filter(doc => docIds.includes(doc.aposDocId)); |
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.
Could avoid reassigning:
const filterDocs = importDraftsOnly
? self.getPublishedDocsAsDraft(
docs.filter(doc => docIds.includes(doc.aposDocId))
)
: docs.filter(doc => docIds.includes(doc.aposDocId));
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.
done, and filtering now happens before locale re-writing
detectDocChange is pretty fast. I think we do need to do it for the reason
you mentioned. We can't leave this in a weird state or expect the user to
disentangle it. It must "just work" in the most sensible way.
…On Mon, Oct 28, 2024 at 12:46 PM Jed ***@***.***> wrote:
***@***.**** requested changes on this pull request.
Globally good, few style feedback.
One tiny issue noticed, when importing draft that has no changes with the
existing document, it's marked pending update but it cannot be updated
(can discard draft but maybe unclear to users).
Two solutions:
- Running detectDocChange to each imported document with existing one
(if existing one). Might be heavy to add this step.
- Updating detectDocChange to consider there are difference between
draft and live (even if there is not) and allow user to review and update
to remove the modified flag and the associated label.
------------------------------
In lib/methods/import.js
<#95 (comment)>
:
> @@ -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.find(item => item.aposDocId === doc.aposDocId && item.aposMode === 'published');
You could use some here that return a boolean instead of a document like
find.
------------------------------
In lib/methods/import.js
<#95 (comment)>
:
> @@ -491,6 +533,10 @@ module.exports = self => {
fetchRelationships: false
});
+ if (importDraftsOnly) {
+ delete docToInsert.lastPublishedAt;
+ }
Maybe this could be done in the function getPublishedDocsAsDraft to avoid
spreading the logic?
function convertToDraft(doc) {
if (doc.aposMode === 'published') {
doc._id = doc._id?.replace('published', 'draft');
doc.aposLocale = doc.aposLocale?.replace('published', 'draft');
doc.aposMode = 'draft';
}
delete doc.lastPublishedAt;
return doc;
}
------------------------------
In lib/methods/import.js
<#95 (comment)>
:
> @@ -547,7 +603,13 @@ module.exports = self => {
: await manager.update(_req, docToUpdate);
}
- await self.setDocAsNotModified(matchingDraft);
+ if (matchingDraft) {
+ await self.editDocMongoFields(matchingDraft, {
+ $set: {
+ modified: Boolean(importDraftsOnly)
+ }
Would have put the $set part into the function. Or just have renamed
setDocModified to take a boolean.
------------------------------
In lib/methods/import.js
<#95 (comment)>
:
> @@ -647,6 +710,10 @@ module.exports = self => {
fetchRelationships: false
});
+ if (importDraftsOnly) {
+ delete docToInsert.lastPublishedAt;
+ }
Here too, could be done inside getPublishedDocsAsDraft.
------------------------------
In lib/methods/import.js
<#95 (comment)>
:
> @@ -792,7 +861,7 @@ module.exports = self => {
const { docs, attachmentsInfo = [] } = await format.input(exportPath);
- const filterDocs = docs.filter(doc => docIds.includes(doc.aposDocId));
+ let filterDocs = docs.filter(doc => docIds.includes(doc.aposDocId));
Could avoid reassigning:
const filterDocs = importDraftsOnly
? self.getPublishedDocsAsDraft(
docs.filter(doc => docIds.includes(doc.aposDocId))
)
: docs.filter(doc => docIds.includes(doc.aposDocId));
—
Reply to this email directly, view it on GitHub
<#95 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAH27O46ZIRPOGRJSEG7S3Z5ZL65AVCNFSM6AAAAABQF7FBYGVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDGOJZGQ4TKNZZHA>
.
You are receiving this because your review was requested.Message ID:
***@***.***>
--
THOMAS BOUTELL | CHIEF TECHNOLOGY OFFICER
APOSTROPHECMS | apostrophecms.com | he/him/his
|
cypress tests: https://github.com/apostrophecms/testbed/pull/304 |
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.
Tested, seems working, just a tiny comment about moving the logic that removes lastPublishedAt
in the function that converts to draft.
Otherwise 👍🏼
await self.editDocMongoFields(matchingDraft, { | ||
$set: { | ||
modified: Boolean(importDraftsOnly) | ||
} |
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.
Agree
@@ -491,6 +533,10 @@ module.exports = self => { | |||
fetchRelationships: false | |||
}); | |||
|
|||
if (importDraftsOnly) { | |||
delete docToInsert.lastPublishedAt; | |||
} |
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.
?
@@ -647,6 +710,10 @@ module.exports = self => { | |||
fetchRelationships: false | |||
}); | |||
|
|||
if (importDraftsOnly) { | |||
delete docToInsert.lastPublishedAt; | |||
} |
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.
? See my comment above to make it functional inside convertToDraft
@@ -491,6 +533,10 @@ module.exports = self => { | |||
fetchRelationships: false | |||
}); | |||
|
|||
if (importDraftsOnly) { | |||
delete docToInsert.lastPublishedAt; | |||
} |
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":
function convertToDraft({ lastPublishedAt, ...doc }) {
if (doc.aposMode === 'published') {
return {
...doc,
_id: doc._id?.replace(':published', ':draft'),
aposLocale: doc.aposLocale?.replace(':published', ':draft'),
aposMode: 'draft'
};
}
return doc;
}
I think these lines should exist in the function convertToDraft
.
Summary
What are the specific steps to test this change?
Check the option to import everything in draft only.
Verify that new documents should only be imported as draft, and existing documents should only have their draft version updated with the published imported version.
What kind of change does this PR introduce?
(Check at least one)
Make sure the PR fulfills these requirements:
If adding a new feature without an already open issue, it's best to open a feature request issue first and wait for approval before working on it.
Other information: