Skip to content

Commit

Permalink
Fix desks template filter (#4376)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecalcc authored Nov 15, 2023
1 parent 5532d0a commit 04772e9
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 31 deletions.
24 changes: 20 additions & 4 deletions scripts/core/query-formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ function toElasticFilter(q: ILogicalOperator | IComparison) {
return {range: {[field]: {'lte': value}}};
case '$in':
return {terms: {[field]: value}};
case '$exists':
return {query: {exists: {field: field}}};
case '$notExists':
return {query: {bool: {must_not: [{exists: {field: field}}]}}};
case '$stringContains':
return {query: {simple_query_string: {query: value.val, fields: [field]}}};
}

throw new Error(`Conversion for operator ${operator} is not defined.`);
Expand Down Expand Up @@ -79,6 +85,15 @@ function toPyEveFilter(q: ILogicalOperator | IComparison) {
return {[field]: {$lte: value}};
case '$in':
return {[field]: {$in: value}};
case '$exists':
return {[field]: {$exists: true}};
case '$notExists':
return {[field]: {$exists: false}};
case '$stringContains':
return {[field]: {
$regex: value.val,
$options: '-i',
}};
}

throw new Error(`Conversion for operator ${operator} is not defined.`);
Expand Down Expand Up @@ -138,9 +153,7 @@ export function toElasticQuery(q: ISuperdeskQuery): {q?: string; source: string}
}

if (Object.keys(filtered).length > 0) {
query['query'] = {
filtered: filtered,
};
query['query'] = {filtered: filtered};
}

if (q.fullTextSearch) {
Expand Down Expand Up @@ -172,7 +185,10 @@ function objectToTuple<T>(obj: {[key: string]: T}): [string, T] {
return [key, obj[key]];
}

export function toPyEveQuery(filter: ISuperdeskQuery['filter'], sort: ISuperdeskQuery['sort']): IPyEveQuery {
export function toPyEveQuery(
filter: ISuperdeskQuery['filter'],
sort: ISuperdeskQuery['sort'],
): IPyEveQuery {
const result: IPyEveQuery = {
sort: `[${
sort.map((sortOption) => {
Expand Down
12 changes: 8 additions & 4 deletions scripts/core/superdesk-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1621,21 +1621,25 @@ declare module 'superdesk-api' {
| {$gte: any}
| {$lt: any}
| {$lte: any}
| {$in: any};
// consider adding $inString for matching substrings
| {$in: any}
| {$exists: any} // not null
| {$notExists: any} // is null
| {$stringContains: any}; // options for the future

export type IComparison = {[field: string]: IComparisonOptions};
export type IAndOperator = {$and: Array<IComparison | ILogicalOperator>};
export type IOrOperator = {$or: Array<IComparison | ILogicalOperator>};
export type ILogicalOperator = IAndOperator | IOrOperator;
export type ISortDirection = 'asc' | 'desc';
export type ISortOptions = Array<{[field: string]: ISortDirection}>;

/**
* Universal query format that works with both - Elasticsearch and pyeve endpoints
*/
export interface ISuperdeskQuery {
filter: ILogicalOperator;
fullTextSearch?: string;
sort: Array<{[field: string]: 'asc' | 'desc'}>;
sort: ISortOptions;
page: number;
max_results: number;
}
Expand All @@ -1644,7 +1648,7 @@ declare module 'superdesk-api' {
endpoint: string,
filter: ILogicalOperator;
fullTextSearch?: string;
sort: Array<{[field: string]: 'asc' | 'desc'}>;
sort: ISortOptions;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ import React from 'react';
import {IconButton, Input, WithPagination} from 'superdesk-ui-framework/react';
import {gettext} from 'core/utils';
import {Spacer, SpacerBlock} from '../Spacer';
import {IRestApiResponse, ITemplate} from 'superdesk-api';
import {
IComparison,
ILogicalOperator,
IRestApiResponse,
ISortOptions,
ISuperdeskQuery,
ITemplate,
} from 'superdesk-api';
import {httpRequestJsonLocal} from 'core/helpers/network';
import {DropdownOption} from './dropdown-option';
import {nameof} from 'core/helpers/typescript-helpers';
import {sdApi} from 'api';
import {prepareSuperdeskQuery} from 'core/helpers/universal-query';

interface IProps {
onSelect(template: ITemplate): void;
Expand All @@ -29,39 +37,55 @@ export class MoreTemplates extends React.PureComponent<IProps, IState> {
}

fetchData(pageToFetch: number, pageSize: number, abortSignal?: AbortSignal): Promise<IRestApiResponse<ITemplate>> {
const templateDesks = nameof<ITemplate>('template_desks');
const template_desks = nameof<ITemplate>('template_desks');
const currentDeskId = sdApi.desks.getCurrentDeskId();

const deskCriteria: any = [
{[templateDesks]: {$exists: false}},
{[templateDesks]: []},
const templateDesks: Array<IComparison | ILogicalOperator> = [
{[template_desks]: {$exists: false}},
{[template_desks]: {$eq: []}},
];

const criteria: ILogicalOperator = {
$or: [
{
$and: [
{is_public: {$eq: false}},
{user: {$eq: sdApi.user.getCurrentUserId()}},
],
},
{
$and: [
{is_public: {$eq: true}},
{$or: templateDesks},
],
},
],
};

if (currentDeskId != null) {
deskCriteria.push({[templateDesks]: {$in: [currentDeskId]}});
templateDesks.push({$and: [{[template_desks]: {$in: [currentDeskId]}}, {is_public: {$eq: true}}]});
}

const criteria = {$or: [{$or: deskCriteria}, {user: sdApi.user.getCurrentUserId()}]};
const templateName = nameof<ITemplate>('template_name');
const where = {$and: [criteria]};
const sort: ISortOptions = [{[templateName]: 'desc'}];
const filtered: ILogicalOperator = {
$and: [
criteria,
{[templateName]: {$stringContains: {val: this.state.searchString, options: null}}},
],
};
const maybeFiltered: ILogicalOperator = this.state.searchString.length < 1 ? criteria : filtered;

if (this.state.searchString.length < 1) {
where[templateName] = {
$regex: this.state.searchString,
$options: '-i',
};
}
const query: ISuperdeskQuery = {
filter: maybeFiltered,
page: pageToFetch,
max_results: pageSize,
sort: sort,
};

return httpRequestJsonLocal<IRestApiResponse<ITemplate>>({
method: 'GET',
path: '/content_templates',
urlParams: {
max_results: pageSize,
page: pageToFetch,
sort: templateName,
where: where,
},
abortSignal,
...prepareSuperdeskQuery('/content_templates', query),
abortSignal: abortSignal,
});
}

Expand Down

0 comments on commit 04772e9

Please sign in to comment.