Skip to content

Commit 32194d2

Browse files
authored
add get-apos-doc-id and set-apos-doc-id tasks (#5136)
* add get-apos-doc-id and set-apos-doc-id tasks * require at the top * add tests for changing apos doc ID * test apos-doc-id get and set tasks * update changelog * remove only * filter non existing _id values + remove req usage * add missing usage * remove locale when using _id * test get-apos-doc-id without locale when using _id --------- Co-authored-by: Harouna Traoré <haroun@users.noreply.github.com>
1 parent d426115 commit 32194d2

3 files changed

Lines changed: 663 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Adds `stripUrlAccents` option in `@apostrophecms/i18n` module to globally control whether accents are stripped from URLs. When set to `true`, all URLs (slugs) will have accents from Latin characters removed on document creation and updates. No existing documents are modified automatically; this only affects new or updated documents. A new task `node app @apostrophecms/i18n:strip-slug-accents` is provided to update existing document slugs in the database when needed.
88
* Add `@apostrophecms/migration:add-missing-schema-fields` task. This task does not run database migrations.
99
* Translation strings added for the layout- and layout-column-widgets.
10+
* Adds `@apostrophecms/doc:get-apos-doc-id` and `@apostrophecms/doc:set-apos-doc-id` tasks.
1011
* New `box` schema field type
1112
* When switching locale from the doc editor, ask if the user wants to localize the current document in the target locale or want to start a blank document.
1213
* Introduced a new `longPolling: false` option for the `@apostrophecms/notification` module. This eliminates long-pending requests when logged in, but also slows down the delivery of notifications. The behavior can be tuned further via the `pollingInterval` option, which defaults to `5000` milliseconds.

‎modules/@apostrophecms/doc/index.js‎

Lines changed: 121 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const _ = require('lodash');
22
const { createId } = require('@paralleldrive/cuid2');
33
const { SemanticAttributes } = require('@opentelemetry/semantic-conventions');
44
const { klona } = require('klona');
5+
const legacyMigrations = require('./lib/legacy-migrations.js');
6+
const migrations = require('./lib/migrations.js');
57

68
// This module is responsible for managing all of the documents (apostrophe
79
// "docs") in the `aposDocs` mongodb collection.
@@ -1851,8 +1853,125 @@ module.exports = {
18511853
return existing?.aposDocId || self.apos.util.generateId();
18521854
},
18531855

1854-
...require('./lib/legacy-migrations')(self),
1855-
...require('./lib/migrations')(self)
1856+
async getAposDocId({
1857+
_id, slug, locale
1858+
}) {
1859+
if (!_id && !slug) {
1860+
throw self.apos.error('invalid', 'Either _id or slug must be provided');
1861+
}
1862+
if (!_id && !locale) {
1863+
throw self.apos.error('invalid', 'Missing locale');
1864+
}
1865+
1866+
const criteria = _id
1867+
? { _id }
1868+
: {
1869+
slug,
1870+
aposLocale: new RegExp(`^${self.apos.util.regExpQuote(locale)}:`)
1871+
};
1872+
1873+
const doc = await self.apos.doc.db
1874+
.findOne(criteria, { projection: { aposDocId: 1 } });
1875+
if (!doc || !doc.aposDocId) {
1876+
throw self.apos.error('notfound');
1877+
}
1878+
1879+
return doc.aposDocId;
1880+
},
1881+
async setAposDocId({
1882+
newId, oldId, slug, locale
1883+
}) {
1884+
if (!newId) {
1885+
throw self.apos.error('invalid', 'Missing newId');
1886+
}
1887+
if (!oldId && !slug) {
1888+
throw self.apos.error('invalid', 'Either oldId or slug must be provided');
1889+
}
1890+
if (!locale) {
1891+
throw self.apos.error('invalid', 'Missing locale');
1892+
}
1893+
1894+
const originalId = (slug && !oldId)
1895+
? await self.getAposDocId({
1896+
slug,
1897+
locale
1898+
})
1899+
: oldId;
1900+
1901+
const modes = [ 'previous', 'draft', 'published' ];
1902+
const pairs = modes.map(mode =>
1903+
[
1904+
`${originalId}:${locale}:${mode}`,
1905+
`${newId}:${locale}:${mode}`
1906+
]
1907+
);
1908+
1909+
// Filter non existing from _id from the list
1910+
const existing = (
1911+
await self.apos.doc.db
1912+
.find(
1913+
{ _id: { $in: pairs.map(([ from ]) => from) } },
1914+
{ projection: { _id: 1 } }
1915+
)
1916+
.toArray()
1917+
)
1918+
.map(({ _id }) => _id);
1919+
const { renamed } = await self.changeDocIds(
1920+
pairs.filter(([ from ]) => existing.includes(from)),
1921+
{ keep: false }
1922+
);
1923+
1924+
return {
1925+
oldId: originalId,
1926+
newId,
1927+
locale,
1928+
renamed
1929+
};
1930+
},
1931+
1932+
...legacyMigrations(self),
1933+
...migrations(self)
1934+
};
1935+
},
1936+
tasks(self) {
1937+
return {
1938+
'get-apos-doc-id': {
1939+
usage: 'Retrieve the aposDocId from the _id or slug, and the locale. Usage: "node app.js @apostrophecms/doc:get-apos-doc-id --_id=pfh0haxfpzowht3oi213cqos:fr:draft" or "node app.js @apostrophecms/doc:get-apos-doc-id --slug=test --locale=fr"',
1940+
task: async (argv) => {
1941+
const {
1942+
_id, slug, locale
1943+
} = argv;
1944+
1945+
const aposDocId = await self.getAposDocId({
1946+
_id,
1947+
slug,
1948+
locale
1949+
});
1950+
1951+
self.apos.util.info(aposDocId);
1952+
1953+
return aposDocId;
1954+
}
1955+
},
1956+
'set-apos-doc-id': {
1957+
usage: 'Change the aposDocId of an existing document. You need the new aposDocId (new-id), the old aposDocId (old-id) or the slug, and the locale. Usage: "node app.js @apostrophecms/doc:set-apos-doc-id --new-id=tz4a98xxat96iws9zmbrgj3a --old-id=pfh0haxfpzowht3oi213cqos --locale=fr" or "node app.js @apostrophecms/doc:set-apos-doc-id --new-id=tz4a98xxat96iws9zmbrgj3a --slug=test --locale=fr"',
1958+
task: async (argv) => {
1959+
const {
1960+
'new-id': newId, 'old-id': oldId, slug, locale
1961+
} = argv;
1962+
1963+
const result = await self.setAposDocId({
1964+
newId,
1965+
oldId,
1966+
slug,
1967+
locale
1968+
});
1969+
1970+
self.apos.util.info(`"${result.oldId}" has been changed to "${result.newId}" for locale "${result.locale}", ${result.renamed} documents changed.`);
1971+
1972+
return result;
1973+
}
1974+
}
18561975
};
18571976
}
18581977
};

0 commit comments

Comments
 (0)