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

feat(PRO-5623): fix issue in import-export when publishing then unpublishing #53

Merged
merged 7 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions lib/methods/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,15 @@ module.exports = self => {
}
}

if (method === 'update' && doc.aposMode === 'draft') {
const existingDoc = await self.apos.doc.db.findOne({ _id: doc._id }, {
lastPublishedAt: 1
});
if (existingDoc) {
doc.lastPublishedAt = existingDoc.lastPublishedAt;
}
}

if (isPage) {
return method === 'update'
? manager[method](req, doc, { setModified: false })
Expand Down
74 changes: 74 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,80 @@ describe('@apostrophecms/import-export', function () {
await cleanFile(pageTgzPath.replace(gzip.allowedExtension, ''));
});

it('should preserve lastPublishedAt property on import for existing drafts', async function() {
const req = apos.task.getReq();
const manager = apos.doc.getManager('default-page');
const pageInstance = manager.newInstance();

// PUBLISH a new page
await apos.page.insert(req, '_home', 'lastChild', {
...pageInstance,
title: 'page2',
type: 'default-page',
_articles: [],
main: {
_id: 'areaId',
items: [],
metaType: 'area'
}
});

const page2 = await apos.page.find(req, { title: 'page2' }).toObject();

// UNPUBLISH (draft) the page
const draftPage = await apos.page.unpublish(req, page2);

// EXPORT the page (as draft)
req.body = {
_ids: [ draftPage._id ],
extension: 'gzip',
type: draftPage.type
};

const { url } = await importExportManager.export(req, apos.page);
const fileName = path.basename(url);

pageTgzPath = path.join(exportsPath, fileName);

// Now that it's exported as draft, PUBLISH the page again
const { lastPublishedAt } = await apos.page.publish(req, draftPage);

// Finally, IMPORT the previously exported draft page
req.body = {};
req.files = {
file: {
path: pageTgzPath,
type: mimeType
}
};

const {
duplicatedDocs,
importedAttachments,
exportPathId,
jobId,
notificationId
} = await importExportManager.import(req);

req.body = {
docIds: duplicatedDocs.map(doc => doc.aposDocId),
importedAttachments,
exportPathId,
jobId,
notificationId
};

await importExportManager.overrideDuplicates(req);

const updatedPage = await apos.doc.db
.find({ title: 'page2' })
.toArray();

assert.deepEqual(updatedPage.every((doc) => {
return String(doc.lastPublishedAt) === String(lastPublishedAt);
}), true, `expected imported docs 'lastPublishedAt' value to be of '${lastPublishedAt}'`);
});

describe('#getFirstDifferentLocale', function() {
it('should find among the docs the first locale that is different from the req one', async function() {
const req = {
Expand Down
Loading