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

Template filtering #4353

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
7 changes: 7 additions & 0 deletions scripts/api/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ function hasPrivilege(privilege: string): boolean {
return privileges.userHasPrivileges({[privilege]: 1});
}

function getCurrentUserId(): string {
thecalcc marked this conversation as resolved.
Show resolved Hide resolved
const session = ng.get('session');

return session?.identity?._id;
}

function isLoggedIn() {
const session = ng.get('session');

Expand All @@ -15,4 +21,5 @@ function isLoggedIn() {
export const user = {
hasPrivilege,
isLoggedIn,
getCurrentUserId,
};
2 changes: 1 addition & 1 deletion scripts/core/get-superdesk-api-implementation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ export function getSuperdeskApiImplementation(
getToken: () => session.token,
getCurrentUser: () => session.getIdentity(),
getSessionId: () => session.sessionId,
getCurrentUserId: () => session.identity._id,
getCurrentUserId: () => sdApi.user.getCurrentUserId(),
},
browser: {
location: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {IRestApiResponse, 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';

interface IProps {
onSelect(template: ITemplate): void;
Expand All @@ -28,19 +29,37 @@ 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 currentDeskId = sdApi.desks.getCurrentDeskId();

const deskCriteria: any = [
{[templateDesks]: {$exists: false}},
{[templateDesks]: []},
];

if (currentDeskId != null) {
deskCriteria.push({[templateDesks]: {$in: [currentDeskId]}});
}

const criteria = {$or: [{$or: deskCriteria}, {user: sdApi.user.getCurrentUserId()}]};
const templateName = nameof<ITemplate>('template_name');
const where = {$and: [criteria]};

if (this.state.searchString.length < 1) {
where[templateName] = {
$regex: this.state.searchString,
$options: '-i',
};
}

return httpRequestJsonLocal<IRestApiResponse<ITemplate>>({
method: 'GET',
path: '/content_templates',
urlParams: {
max_results: pageSize,
page: pageToFetch,
sort: nameof<ITemplate>('template_name'),
where: this.state.searchString.length < 1 ? undefined : {
[nameof<ITemplate>('template_name')]: {
$regex: this.state.searchString,
$options: '-i',
},
},
sort: templateName,
where: where,
},
abortSignal,
});
Expand Down
Loading