Skip to content
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

Merged
merged 31 commits into from
Nov 4, 2024

Conversation

ETLaurent
Copy link
Contributor

@ETLaurent ETLaurent commented Oct 18, 2024

Summary

image

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)

  • Bug fix
  • New feature
  • Refactor
  • Documentation
  • Build-related changes
  • Other

Make sure the PR fulfills these requirements:

  • It includes a) the existing issue ID being resolved, b) a convincing reason for adding this feature, or c) a clear description of the bug it resolves
  • The changelog is updated
  • Related documentation has been updated
  • Related tests have been updated

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:

lib/methods/import.js Outdated Show resolved Hide resolved
@ETLaurent ETLaurent changed the title ui add option to import documents only as drafts Oct 22, 2024
Comment on lines +9 to +10
this.timeout(t.timeout);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for slow computers...

@ETLaurent ETLaurent marked this pull request as ready for review October 25, 2024 12:53
@ETLaurent ETLaurent requested a review from ValJed October 25, 2024 13:40
@ETLaurent
Copy link
Contributor Author

cypress no regression test: https://github.com/apostrophecms/testbed/actions/runs/11531890614/

Copy link
Contributor

@ValJed ValJed left a 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) when modified: true to allow users to review and update to remove the modified flag and the associated label.

// i.e "is draft without a published version"
const isDraftAlone =
doc.aposMode === 'draft' &&
!docs.find(item => item.aposDocId === doc.aposDocId && item.aposMode === 'published');
Copy link
Contributor

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;
}
Copy link
Contributor

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;
      }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Contributor

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)
}
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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;
}
Copy link
Contributor

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.

Copy link
Contributor

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

@@ -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));
Copy link
Contributor

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));

Copy link
Contributor Author

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

@boutell
Copy link
Member

boutell commented Oct 29, 2024 via email

@ETLaurent
Copy link
Contributor Author

Copy link
Contributor

@ValJed ValJed left a 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)
}
Copy link
Contributor

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;
}
Copy link
Contributor

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;
}
Copy link
Contributor

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;
}
Copy link
Contributor

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.

@ETLaurent ETLaurent merged commit 34a2f5e into main Nov 4, 2024
9 checks passed
@ETLaurent ETLaurent deleted the pro-6696-import-as-draft branch November 4, 2024 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants