Skip to content

Commit

Permalink
adds method to sort all documents and pages
Browse files Browse the repository at this point in the history
  • Loading branch information
ValJed committed Jan 28, 2025
1 parent d406584 commit 47cdd53
Showing 1 changed file with 73 additions and 16 deletions.
89 changes: 73 additions & 16 deletions lib/methods/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { uniqBy, uniqueId } = require('lodash');

const MAX_RECURSION = 10;

module.exports = self => {
module.exports = (self) => {
return {
async export(req, manager, reporting = null) {
if (!req.user) {
Expand All @@ -33,9 +33,11 @@ module.exports = self => {

const hasRelatedTypes = !!relatedTypes.length;

const docs = (await self.getDocs(req, ids, hasRelatedTypes, manager, reporting))
const docs = (await self.getDocs(req, ids, manager, reporting))
.map((doc) => self.apos.util.clonePermanent(doc));

if (!hasRelatedTypes) {
self.sortDocs(docs);
return self.exportFile(
req,
reporting,
Expand All @@ -55,6 +57,8 @@ module.exports = self => {
});
}

self.sortDocs(allDocs);

if (!format.includeAttachments) {
return self.exportFile(
req,
Expand Down Expand Up @@ -88,6 +92,18 @@ module.exports = self => {
})
);

for (const {
_id, title, aposMode, level, rank
} of allDocs) {
console.log('doc:', {
_id,
title,
aposMode,
level,
rank
});
}

return self.exportFile(
req,
reporting,
Expand All @@ -101,28 +117,69 @@ module.exports = self => {
);
},

sortDocs(docs) {
docs.sort((a, b) => {
if (!self.apos.page.isPage(a) && !self.apos.page.isPage(b)) {
return 0;
}
if (self.apos.page.isPage(a) && !self.apos.page.isPage(b)) {
return -1;
}
if (!self.apos.page.isPage(a) && self.apos.page.isPage(b)) {
return 1;
}
if (a.aposMode === 'draft' && b.aposMode === 'published') {
return -1;
}
if (a.aposMode === 'published' && b.aposMode === 'draft') {
return 1;
}
if (a.level > b.level) {
return 1;
}
if (a.level < b.level) {
return -1;
}
if (a.rank < b.rank) {
return -1;
}
if (a.rank > b.rank) {
return 1;
}
return 0;
});
},

// Get docs via their manager in order to populate them
// so that we can retrieve their relationships IDs later,
// and to let the manager handle permissions.
async getDocs(req, docsIds, includeRelationships, manager, reporting) {
async getDocs(req, docsIds, manager, reporting) {
if (!docsIds.length) {
return [];
}

/* const isPage = self.apos.instanceOf(manager, '@apostrophecms/page'); */
const { draftIds, publishedIds } = self.getAllModesIds(docsIds);
const isReqDraft = req.mode === 'draft';

const docs = [];

const draftReq = isReqDraft ? req : req.clone({ mode: 'draft' });
const draftDocs = await manager
const cursor = await manager
.findForEditing(draftReq, {
_id: {
$in: draftIds
}
})
.relationships(includeRelationships)
.toArray();
.relationships(false);

/* if (isPage) { */
/* cursor.sort({ */
/* level: 1, */
/* rank: 1 */
/* }); */
/* } */
const draftDocs = await cursor.toArray();

docs.push(...draftDocs);

Expand All @@ -133,7 +190,7 @@ module.exports = self => {
$in: publishedIds
}
})
.relationships(includeRelationships)
.relationships(false)
.toArray();

docs.push(...publishedDocs);
Expand Down Expand Up @@ -225,7 +282,7 @@ module.exports = self => {
if (!field.withType && !fieldValue) {
continue;
}
if (field.withType && relatedTypes && !relatedTypesIncludes(field.withType)) {
if (field.withType && relatedTypes && !self.relatedTypesIncludes(field.withType, relatedTypes)) {
continue;
}
if (field.withType && !self.canExport(req, field.withType)) {
Expand Down Expand Up @@ -290,16 +347,16 @@ module.exports = self => {
});
}
}
},

function relatedTypesIncludes(name) {
if ([ '@apostrophecms/any-page-type', '@apostrophecms/page' ].includes(name)) {
return relatedTypes.some(type => {
const module = self.apos.modules[type];
return self.apos.instanceOf(module, '@apostrophecms/page-type');
});
}
return relatedTypes.includes(name);
relatedTypesIncludes(name, relatedTypes) {
if ([ '@apostrophecms/any-page-type', '@apostrophecms/page' ].includes(name)) {
return relatedTypes.some(type => {
const module = self.apos.modules[type];
return self.apos.instanceOf(module, '@apostrophecms/page-type');
});
}
return relatedTypes.includes(name);
},

async getRelatedDocsFromRichTextWidget(req, {
Expand Down

0 comments on commit 47cdd53

Please sign in to comment.