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

Add search exclude syntax #387

Merged
merged 2 commits into from
Aug 2, 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
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,23 @@ describe('Basesnippet', function (this: Suite) {
});
});

it('Search with token should return 200 and exclude matching body.tags', async () => {
const includeTags = {include: ['subsnippets']};
await client
.get(
`/basesnippets/search=${encodeURIComponent(
'-#aSearchableTag',
)}?filter=${JSON.stringify(includeTags)}`,
)
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
.expect(200)
.then(result => expect(result.body.length).to.be.eql(0))
.catch(err => {
throw err;
});
});

it('Search with token should return 200 and matching body.tags + body.textcontent', async () => {
const includeTags = {include: ['subsnippets']};
await client
Expand Down Expand Up @@ -300,6 +317,18 @@ describe('Basesnippet', function (this: Suite) {
});
});

it('Search with token should return 200 and exclude matching readACL', async () => {
await client
.get(`/basesnippets/search=-@basesnippetAcceptance`)
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
.expect(200)
.then(result => expect(result.body.length).to.be.eql(0))
.catch(err => {
throw err;
});
});

it('patch snippet by id without token should return 401', async () => {
await client
.patch(`/basesnippets/${baseSnippetId}`)
Expand Down
65 changes: 36 additions & 29 deletions sci-log-db/src/mixins/basesnippet.repository-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,26 +346,23 @@ function FindWithSearchRepositoryMixin<
}

private buildAdditionalConditions(search: string) {
const additionalConditions: {
tags?: {inq: string[]};
readACL?: {inq: string[]};
or?: {
textcontent?: {regexp: RegExp};
name?: {regexp: RegExp};
description?: {regexp: RegExp};
}[];
} & Condition<M> = {};
const additionalConditions: Where<M> & {or?: Condition<M>[]} = {};
let searchText = '';
search.split(' ').map(searchTerms => {
if (searchTerms.startsWith('#')) {
additionalConditions.tags = additionalConditions.tags ?? {inq: []};
additionalConditions.tags.inq.push(searchTerms.slice(1));
} else if (searchTerms.startsWith('@')) {
additionalConditions.readACL = additionalConditions.readACL ?? {
inq: [],
};
additionalConditions.readACL.inq.push(searchTerms.slice(1));
} else searchText += ` ${searchTerms}`;
searchTerms = this._searchPrefixToIncludeCondition(
additionalConditions,
searchTerms,
'#',
'tags' as keyof Where<M>,
);
searchTerms = this._searchPrefixToIncludeCondition(
additionalConditions,
searchTerms,
'@',
'readACL' as keyof Where<M>,
);
searchText += ` ${searchTerms}`;
searchText = searchText.trim();
});
this.addSearchOr(searchText, additionalConditions);
if (
Expand All @@ -377,19 +374,29 @@ function FindWithSearchRepositoryMixin<
return additionalConditions;
}

private _searchPrefixToIncludeCondition(
additionalCondition: Where<M>,
searchTerm: string,
prefix: string,
field: keyof Where<M>,
) {
if (!searchTerm) return '';
let term = searchTerm;
const condition = searchTerm.startsWith('-')
? (term = searchTerm.slice(1)) && 'nin'
: 'inq';
if (!term.startsWith(prefix)) return searchTerm;
if (!additionalCondition[field])
(additionalCondition[field] as object) = {};
if (!additionalCondition[field][condition])
(additionalCondition[field][condition] as string[]) = [];
(additionalCondition[field][condition] as string[]).push(term.slice(1));
return '';
}

private addSearchOr(
searchText: string,
additionalConditions: {
tags?: {inq: string[]} | undefined;
readACL?: {inq: string[]} | undefined;
or?:
| {
textcontent?: {regexp: RegExp} | undefined;
name?: {regexp: RegExp} | undefined;
description?: {regexp: RegExp} | undefined;
}[]
| undefined;
} & Condition<M>,
additionalConditions: Where<M> & {or?: {}[]},
) {
if (!searchText) return;
searchText = searchText.trimStart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,19 @@
<div class="header">
Suggestions...
<div>
Created by:
Created by (prepend "-" to negate):
<button class="suggestion-button" (click)="addToSearch('@' + _sample_user)">
@{{_sample_user}}
</button>
</div>
<div *ngIf="tags[0]">
Tag:
Tag (prepend "-" to negate):
<span *ngFor="let tag of tags.slice(0,3)">
<button class="suggestion-button" (click)="addToSearch('#' + tag)">
#{{tag}}
</button>
</span>


</div>
<div>
</div>
</div>
<mat-divider></mat-divider>
<div class="header">
Examples:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ export class SearchWindowComponent implements OnInit {
addToSearch(val: string) {
let _stringParts = val.split(" ");
_stringParts.forEach((subVal) => {
if (!this.searchString.includes(subVal)) {
this.searchString = subVal + " " + this.searchString;
if (!this.searchString?.includes(subVal)) {
this.searchString = `${subVal} ${this.searchString ?? ''}`;
}
}
);
Expand Down
Loading