Skip to content

Commit

Permalink
Filter subsnippets with matching tags
Browse files Browse the repository at this point in the history
  • Loading branch information
minottic committed Aug 27, 2024
1 parent 90f5617 commit bc69078
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
29 changes: 29 additions & 0 deletions scilog/src/app/core/remote-data.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ describe('LogbookItemDataService', () => {
relation: 'subsnippets',
scope:
{
where: {and: [
{tags: {inq: ['a', 'b']}},
{tags: {nin: ['c', 'd']}}
]},
include: [{
relation: 'subsnippets',
scope: {
Expand Down Expand Up @@ -234,6 +238,31 @@ describe('LogbookItemDataService', () => {
expect(spyDeleteSnippet.calls.mostRecent().args).toEqual(["edits/paragraphs-to-delete", "1"]);
});


[
{
input: undefined,
expected: {scope:{include:[{relation:'subsnippets',scope:{where:{snippetType:'edit'}}}]}}
},
{
input: {tags: ['a', 'b'], excludeTags: ['c', 'd']},
expected: {scope:{include:[{relation:'subsnippets',scope:{where:{snippetType:'edit'}}}],where:{and:[{tags:{inq:['a','b']}},{tags:{nin:['c','d']}}]}}}
},
{
input: {tags: ['a', 'b']},
expected: {scope:{include:[{relation:'subsnippets',scope:{where:{snippetType:'edit'}}}],where:{and:[{tags:{inq:['a','b']}}]}}}
},
{
input: {excludeTags: ['c', 'd']},
expected: {scope:{include:[{relation:'subsnippets',scope:{where:{snippetType:'edit'}}}],where:{and:[{tags:{nin:['c','d']}}]}}}
}
]
.forEach((t, i) => {
it(`should addIncludeScope ${i}`, () => {
expect(service['addIncludeScope'](t.input)).toEqual(t.expected);
});
});

});

describe('LogbookDataService', () => {
Expand Down
15 changes: 12 additions & 3 deletions scilog/src/app/core/remote-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class RemoteDataService {
httpFilter["order"] = ["defaultOrder ASC"];
}

httpFilter["include"] = [{ relation: "subsnippets", ...this.addIncludeScope() }];
httpFilter["include"] = [{ relation: "subsnippets", ...this.addIncludeScope(config.filter) }];
httpFilter["where"] = { "and": [...this.staticFilters(), ...this.tagsFilter(config.filter), ...this.parentFilter(config.filter)] };

if (count < Infinity) {
Expand All @@ -97,7 +97,7 @@ export class RemoteDataService {
return [{ snippetType: { inq: ["paragraph", "image"] } }, { deleted: false }];
}

private tagsFilter(configFilter: { tags?: string[], excludeTags?: string[] }) {
protected tagsFilter(configFilter: { tags?: string[], excludeTags?: string[] }) {
const tagFilter = [];
if (configFilter?.tags?.length > 0) {
tagFilter.push({ tags: { inq: configFilter.tags } });
Expand All @@ -114,7 +114,7 @@ export class RemoteDataService {
return [{ parentId: { inq: parentIds } }];
}

protected addIncludeScope(): Object {
protected addIncludeScope(configFilter?: { tags?: string[], excludeTags?: string[] }): object {
return {
scope:
{
Expand Down Expand Up @@ -158,6 +158,15 @@ export class LogbookItemDataService extends RemoteDataService {

}

protected addIncludeScope(configFilter: { tags?: string[], excludeTags?: string[] }): Object {
const scope = super.addIncludeScope() as {scope: {where?: {}}};
const tags = this.tagsFilter(configFilter);
if (tags.length === 0)
return scope;
scope.scope.where = {and: tags};
return scope;
}

getFile(imageSnippetUrl: string): Promise<Blob> {
return this.getSnippets<Blob>(imageSnippetUrl, { responseType: 'blob' }).toPromise();
}
Expand Down

0 comments on commit bc69078

Please sign in to comment.