From c9983eabf2da62588b4d28f46eb16a1b7bda7d5e Mon Sep 17 00:00:00 2001 From: minottic Date: Tue, 27 Aug 2024 12:56:20 +0200 Subject: [PATCH] Filter subsnippets with matching tags --- .../src/app/core/remote-data.service.spec.ts | 29 +++++++++++++++++++ scilog/src/app/core/remote-data.service.ts | 15 ++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/scilog/src/app/core/remote-data.service.spec.ts b/scilog/src/app/core/remote-data.service.spec.ts index dac047e5..d071a939 100644 --- a/scilog/src/app/core/remote-data.service.spec.ts +++ b/scilog/src/app/core/remote-data.service.spec.ts @@ -131,6 +131,10 @@ describe('LogbookItemDataService', () => { relation: 'subsnippets', scope: { + where: {and: [ + {tags: {inq: ['a', 'b']}}, + {tags: {nin: ['c', 'd']}} + ]}, include: [{ relation: 'subsnippets', scope: { @@ -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', () => { diff --git a/scilog/src/app/core/remote-data.service.ts b/scilog/src/app/core/remote-data.service.ts index e0b9496e..bde67b04 100644 --- a/scilog/src/app/core/remote-data.service.ts +++ b/scilog/src/app/core/remote-data.service.ts @@ -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) { @@ -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 } }); @@ -114,7 +114,7 @@ export class RemoteDataService { return [{ parentId: { inq: parentIds } }]; } - protected addIncludeScope(): Object { + protected addIncludeScope(configFilter?: { tags?: string[], excludeTags?: string[] }): object { return { scope: { @@ -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 { return this.getSnippets(imageSnippetUrl, { responseType: 'blob' }).toPromise(); }