Skip to content

Commit

Permalink
Propagate comment tags to parent
Browse files Browse the repository at this point in the history
  • Loading branch information
minottic committed Aug 27, 2024
1 parent 54f1893 commit 90f5617
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,31 @@ describe('Paragraph', function (this: Suite) {
});
});

it('posts a comment with tags and should update the parent', async () => {
await client
.post('/paragraphs')
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
.send({
...paragraphSnippet,
tags: ['another'],
linkType: 'comment',
parentId: paragraphSnippetId,
})
.expect(200);
await client
.get(`/paragraphs/${paragraphSnippetId}`)
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
.expect(200)
.then(result =>
expect(result.body.tags).to.eql(['aSearchExcludedTag', 'another']),
)
.catch(err => {
throw err;
});
});

it('delete snippet by id without token should return 401', async () => {
await client
.delete(`/paragraphs/${paragraphSnippetId}`)
Expand Down
19 changes: 17 additions & 2 deletions sci-log-db/src/mixins/basesnippet.repository-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import {AutoAddRepository} from '../repositories/autoadd.repository.base';
const fs = require('fs');

type ExpandedBasesnippet = Basesnippet & {
ownerGroup: string;
accessGroups: string[];
ownerGroup?: string;
accessGroups?: string[];
tags?: string[];
};

function UpdateAndDeleteRepositoryMixin<
Expand Down Expand Up @@ -252,6 +253,20 @@ function UpdateAndDeleteRepositoryMixin<
});
}
}

private async updateParentTagsOnComment(basesnippet: Paragraph) {
if (basesnippet.linkType !== 'comment') return;
const self = this as unknown as AutoAddRepository<M, ID, Relations>;
const parent = (await self.getParent(basesnippet)) as Paragraph;
const tags = arrayOfUniqueFrom(parent.tags, basesnippet.tags);
if (!_.isEqual(tags, parent.tags))
await this.updateByIdWithHistory(
parent.id as ID,
{tags: tags} as ExpandedBasesnippet,
{currentUser: {roles: ['admin']}},
false,
);
}
}
return Mixed;
}
Expand Down
6 changes: 5 additions & 1 deletion sci-log-db/src/repositories/autoadd.repository.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class AutoAddRepository<
);
}

private async getParent(
async getParent(
data: (Basesnippet | Logbook) & {
ownerGroup?: string | undefined;
accessGroups?: string[];
Expand Down Expand Up @@ -357,6 +357,10 @@ export class AutoAddRepository<
// ctx.instance.unsetAttribute('id')
}
}
const baseSnippetRepository = await this.baseSnippetRepository();
await baseSnippetRepository.updateParentTagsOnComment(
ctx.data ?? ctx.instance,
);
sanitizeTextContentInPlace(ctx.data ?? ctx.instance);
console.log('going to save:' + JSON.stringify(ctx, null, 3));
});
Expand Down

0 comments on commit 90f5617

Please sign in to comment.