Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 109 additions & 2 deletions modules/@apostrophecms/doc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const _ = require('lodash');
const { createId } = require('@paralleldrive/cuid2');
const { SemanticAttributes } = require('@opentelemetry/semantic-conventions');
const { klona } = require('klona');
const legacyMigrations = require('./lib/legacy-migrations.js');
const migrations = require('./lib/migrations.js');

// This module is responsible for managing all of the documents (apostrophe
// "docs") in the `aposDocs` mongodb collection.
Expand Down Expand Up @@ -1843,8 +1845,113 @@ module.exports = {
return existing?.aposDocId || self.apos.util.generateId();
},

...require('./lib/legacy-migrations')(self),
...require('./lib/migrations')(self)
async getAposDocId({
id, slug, locale
}) {
if (!id && !slug) {
throw self.apos.error('invalid', 'Either id or slug must be provided');
}
if (!locale) {
throw self.apos.error('invalid', 'Missing locale');
}

const req = self.apos.task.getReq({ mode: 'draft' });

const criteria = id
? { _id: self.apos.i18n.inferIdLocaleAndMode(req, id) }
: { slug };

const doc = await self.find(
req,
{
...criteria,
aposLocale: new RegExp(`^${self.apos.util.regExpQuote(locale)}:`)
}
)
.project({ aposDocId: 1 })
.toObject();
if (!doc || !doc.aposDocId) {
throw self.apos.error('notfound');
}

return doc.aposDocId;
},
async setAposDocId({
newId, oldId, slug, locale
}) {
if (!newId) {
throw self.apos.error('invalid', 'Missing newId');
}
if (!oldId && !slug) {
throw self.apos.error('invalid', 'Either oldId or slug must be provided');
}
if (!locale) {
throw self.apos.error('invalid', 'Missing locale');
}

const originalId = (slug && !oldId)
? await self.getAposDocId({
slug,
locale
})
: oldId;

const modes = [ 'draft', 'published' ];
const pairs = modes.map(mode =>
[
`${originalId}:${locale}:${mode}`,
`${newId}:${locale}:${mode}`
]
);
const { renamed } = await self.changeDocIds(pairs, { keep: false });

return {
oldId: originalId,
newId,
locale,
renamed
};
},

...legacyMigrations(self),
...migrations(self)
};
},
tasks(self) {
return {
'get-apos-doc-id': {
usage: '',
task: async (argv) => {
const {
id, slug, locale
} = argv;

const aposDocId = await self.getAposDocId({
id,
slug,
locale
});

self.apos.util.info(aposDocId);
}
},
'set-apos-doc-id': {
usage: '',
task: async (argv) => {
const {
'new-id': newId, 'old-id': oldId, slug, locale
} = argv;

const result = await self.setAposDocId({
newId,
oldId,
slug,
locale
});

self.apos.util.info(`"${result.oldId}" has been changed to "${result.newId}" for locale "${result.locale}", ${result.renamed} documents changed.`);
}
}
};
}
};