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

Fix desks template filter #4376

Merged
Show file tree
Hide file tree
Changes from 2 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
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 {terms: {exists: {[field]: value}}};
case '$notExists':
return {terms: {must_not: {exists: {[field]: value}}}};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's remove notExists if we can do the same via $exists: false

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: value}};
case '$notExists':
return {[field]: {$exists: value}};
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 ISortOption = 'asc' | 'desc';
export type ISortOptions = Array<{[field: string]: ISortOption}>;

/**
* 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
Loading