Skip to content
Open
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
27 changes: 27 additions & 0 deletions src/lib/features/events/event-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,33 @@ describe('filterPrivateProjectsFromParams', () => {
expect(result).toBe('IS_ANY_OF:project1,project2');
});

it('should keep the operator prefix when the first requested project is not accessible', () => {
const projectAccess: ProjectAccess = {
mode: 'limited',
projects: ['project1', 'project2'],
};

// project3 is not accessible and is listed first, so it carries the operator prefix
const projectParam = 'IS_ANY_OF:project3,project1';

const result = filterAccessibleProjects(projectParam, projectAccess);

expect(result).toBe('IS_ANY_OF:project1');
});

it('should preserve the IS operator for a single accessible project', () => {
const projectAccess: ProjectAccess = {
mode: 'limited',
projects: ['project1', 'project2'],
};

const projectParam = 'IS:project1';

const result = filterAccessibleProjects(projectParam, projectAccess);

expect(result).toBe('IS:project1');
});

it('should throw an error if no projects match', () => {
const projectAccess: ProjectAccess = {
mode: 'limited',
Expand Down
43 changes: 20 additions & 23 deletions src/lib/features/events/event-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,30 +261,27 @@ export const filterAccessibleProjects = (
projectParam: string | undefined,
projectAccess: ProjectAccess,
): string | undefined => {
if (projectAccess.mode !== 'all') {
const allowedProjects = projectAccess.projects;

if (!projectParam) {
return `IS_ANY_OF:${allowedProjects.join(',')}`;
} else {
const searchProjectList = projectParam.split(',');
const filteredProjects = searchProjectList
.filter((proj) =>
allowedProjects.includes(
proj.replace(/^(IS|IS_ANY_OF):/, ''),
),
)
.join(',');

if (!filteredProjects) {
throw new Error(
'No accessible projects in the search parameters',
);
}
if (projectAccess.mode === 'all') {
return projectParam;
}

return filteredProjects;
}
const allowedProjects = projectAccess.projects;

if (!projectParam) {
return `IS_ANY_OF:${allowedProjects.join(',')}`;
}

// Parse the operator-prefixed value so we can scope the projects to the
// accessible ones without losing the operator (it only rides on the first
// value, so naive sstring splitting drops it when the first project is filtered out).
const parsed = parseSearchOperatorValue('project', projectParam);
const filteredProjects = parsed?.values.filter((project) =>
allowedProjects.includes(project),
);

if (!parsed || !filteredProjects?.length) {
throw new Error('No accessible projects in the search parameters');
}

return projectParam;
return `${parsed.operator}:${filteredProjects.join(',')}`;
};
Loading