Skip to content
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Add `@apostrophecms/migration:add-missing-schema-fields` task. This task does not run database migrations.
* Translation strings added for the layout- and layout-column-widgets.
* Adds `@apostrophecms/doc:get-apos-doc-id` and `@apostrophecms/doc:set-apos-doc-id` tasks.
* New `box` schema field type

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How did this creep into this PR? It's not hurting anything

* 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.
* 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.

Expand Down
13 changes: 4 additions & 9 deletions modules/@apostrophecms/doc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1851,20 +1851,15 @@ module.exports = {
if (!_id && !slug) {
throw self.apos.error('invalid', 'Either _id or slug must be provided');
}
if (!locale) {
if (!_id && !locale) {
throw self.apos.error('invalid', 'Missing locale');
}

// const req = self.apos.task.getReq({ mode: 'draft', locale });
const aposLocale = new RegExp(`^${self.apos.util.regExpQuote(locale)}:`);
const criteria = _id

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One last thing, sorry... since the _id parameter has to be a full _id, which already contains the locale, there's no need to make locale mandatory or even part of the query when _id is present.

(Technically we don't need to make a query at all when _id is the argument, because _id already contains aposDocId, but I like the idea of making the query because it validates that the document actually does exist.)

? {
_id,
aposLocale
}
? { _id }
: {
slug,
aposLocale
aposLocale: new RegExp(`^${self.apos.util.regExpQuote(locale)}:`)
};

const doc = await self.apos.doc.db
Expand Down Expand Up @@ -1933,7 +1928,7 @@ module.exports = {
tasks(self) {
return {
'get-apos-doc-id': {
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 --locale=fr" or "node app.js @apostrophecms/doc:get-apos-doc-id --slug=test --locale=fr"',
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"',
task: async (argv) => {
const {
_id, slug, locale
Expand Down
28 changes: 24 additions & 4 deletions test/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,7 @@ describe('Docs: tasks', function () {
await apos.lock.db.deleteMany({});
});

it('should require the id or slug when calling @apostrophecms/doc:get-apos-doc-id task', async function() {
it('should require the _id or slug when calling @apostrophecms/doc:get-apos-doc-id task', async function() {
await insertI18nFixtures(apos);

const actual = async () => {
Expand All @@ -1328,7 +1328,27 @@ describe('Docs: tasks', function () {
await assert.rejects(actual, expected);
});

it('should require the locale when calling @apostrophecms/doc:get-apos-doc-id task', async function() {
it('should not require the locale when calling @apostrophecms/doc:get-apos-doc-id task with _id', async function() {
await insertI18nFixtures(apos);

const req = apos.task.getReq({ locale: 'en' });
const doc = await apos.doc.find(req, {
type: 'test-people',
slug: 'carl'
}).toObject();

const actual = await apos.task.invoke(
'@apostrophecms/doc:get-apos-doc-id',
{
_id: doc._id
}
);
const expected = doc.aposDocId;

assert.equal(actual, expected);
});

it('should require the locale when calling @apostrophecms/doc:get-apos-doc-id task with slug', async function() {
await insertI18nFixtures(apos);

const req = apos.task.getReq({ locale: 'en' });
Expand Down Expand Up @@ -1371,7 +1391,7 @@ describe('Docs: tasks', function () {
);
const expected = doc.aposDocId;

assert.deepEqual(actual, expected);
assert.equal(actual, expected);
});

it('should get the aposDocId when calling @apostrophecms/doc:get-apos-doc-id task with an _id', async function() {
Expand All @@ -1392,7 +1412,7 @@ describe('Docs: tasks', function () {
);
const expected = doc.aposDocId;

assert.deepEqual(actual, expected);
assert.equal(actual, expected);
});

it('should require a new ID when calling @apostrophecms/doc:set-apos-doc-id task', async function() {
Expand Down
Loading