Skip to content

Commit

Permalink
Template filtering (#4353)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecalcc authored Nov 7, 2023
1 parent 430c4d5 commit db6c866
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
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 {
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 @@ -477,7 +477,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

0 comments on commit db6c866

Please sign in to comment.