fix: Minor fixes in backlinks service (#1240)

This commit is contained in:
Tom Moor 2020-04-24 05:29:48 -07:00 committed by GitHub
parent 9f8e7be755
commit c41e6e0423
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 7 deletions

View File

@ -72,17 +72,24 @@ export default class Backlinks {
// delete any backlinks that were removed
await Promise.all(
removedLinkIds.map(async linkId => {
const document = await Document.findByPk(linkId);
await Backlink.destroy({
where: {
documentId: document.id,
reverseDocumentId: event.documentId,
},
const document = await Document.findByPk(linkId, {
paranoid: false,
});
if (document) {
await Backlink.destroy({
where: {
documentId: document.id,
reverseDocumentId: event.documentId,
},
});
}
})
);
if (currentRevision.title === previousRevision.title) {
if (
!previousRevision ||
currentRevision.title === previousRevision.title
) {
break;
}

View File

@ -10,6 +10,28 @@ beforeEach(flushdb);
beforeEach(jest.resetAllMocks);
describe('documents.update', () => {
test('should not fail on a document with no previous revisions', async () => {
const otherDocument = await buildDocument();
const document = await buildDocument({
text: `[this is a link](${otherDocument.url})`,
});
await Backlinks.on({
name: 'documents.update',
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
actorId: document.createdById,
data: { autosave: false },
});
const backlinks = await Backlink.findAll({
where: { reverseDocumentId: document.id },
});
expect(backlinks.length).toBe(1);
});
test('should create new backlink records', async () => {
const otherDocument = await buildDocument();
const document = await buildDocument();