Skip to content

Commit

Permalink
Refactor access query logic to use a dedicated function for user acce…
Browse files Browse the repository at this point in the history
…ss filters
  • Loading branch information
arturoliduena committed Dec 4, 2024
1 parent fee5a59 commit 90df2f7
Showing 1 changed file with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,7 @@ export function getAccessQuery({
filter: [
{
bool: {
should: [
{ term: { public: true } },
...(user
? user.id
? [
{ term: { 'user.id': user.id } },
{
bool: {
must_not: { exists: { field: 'user.id' } },
must: { term: { 'user.name': user.name } },
},
},
]
: [{ term: { 'user.name': user.name } }]
: []),
],
should: [{ term: { public: true } }, ...getUserAccessFilters(user)],
minimum_should_match: 1,
},
},
Expand Down Expand Up @@ -61,3 +46,23 @@ export function getAccessQuery({
},
];
}

function getUserAccessFilters(user?: { name: string; id?: string }) {
if (!user) {
return [];
}

if (user.id) {
return [
{ term: { 'user.id': user.id } },
{
bool: {
must_not: { exists: { field: 'user.id' } },
must: { term: { 'user.name': user.name } },
},
},
];
}

return [{ term: { 'user.name': user.name } }];
}

0 comments on commit 90df2f7

Please sign in to comment.