Skip to content

Commit

Permalink
add role/title filters and fullName sort
Browse files Browse the repository at this point in the history
  • Loading branch information
rdonigian committed Feb 19, 2025
1 parent 4558689 commit c97088e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/components/user/dto/list-users.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Field, InputType, ObjectType } from '@nestjs/graphql';
import { FilterField, PaginatedList, SortablePaginationInput } from '~/common';
import {
FilterField,
PaginatedList,
Role,
SortablePaginationInput,
} from '~/common';
import { User } from './user.dto';

@InputType()
Expand All @@ -9,6 +14,16 @@ export abstract class UserFilters {
})
readonly name?: string;

@Field({
nullable: true,
})
readonly title?: string;

@Field(() => [Role], {
nullable: true,
})
readonly roles?: Role[];

@Field({
description: 'Only users that are pinned/unpinned by the requesting user',
nullable: true,
Expand Down
2 changes: 2 additions & 0 deletions src/components/user/user.edgedb.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export class UserEdgeDBRepository
return [
input.filter.pinned != null &&
e.op(user.pinned, '=', input.filter.pinned),
(input.filter.roles?.length ?? 0) > 0 &&
e.op(user.roles, 'in', e.set(...input.filter.roles!)),
// More filters here when needed...
];
}
Expand Down
29 changes: 28 additions & 1 deletion src/components/user/user.repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Injectable } from '@nestjs/common';
import { cleanJoin } from '@seedcompany/common';
import { inArray, node, Query, relation } from 'cypher-query-builder';
import { difference } from 'lodash';
import { DateTime } from 'luxon';
Expand All @@ -25,6 +26,7 @@ import {
path,
property,
requestingUser,
SortCol,
sortWith,
} from '~/core/database/query';
import {
Expand Down Expand Up @@ -406,9 +408,34 @@ export const userFilters = filter.define(() => UserFilters, {
separateQueryForEachWord: true,
minScore: 0.9,
}),
title: filter.propPartialVal(),
roles: filter.stringListProp(),
});

export const userSorters = defineSorters(User, {});
const matchNames = (query: Query) =>
query
.match([
node('node'),
relation('out', '', 'realFirstName', ACTIVE),
node('firstName', 'UserName'),
])
.match([
node('node'),
relation('out', '', 'realLastName', ACTIVE),
node('lastName', 'UserName'),
]);
const multiPropsAsSortString = (props: string[]) =>
cleanJoin(
' + ',
props.map((prop) => `coalesce(${prop}.value, "")`),
) + ' as sortValue';

export const userSorters = defineSorters(User, {
fullName: (query) =>
query
.apply(matchNames)
.return<SortCol>(multiPropsAsSortString(['firstName', 'lastName'])),
});

const NameIndex = FullTextIndex({
indexName: 'UserName',
Expand Down

0 comments on commit c97088e

Please sign in to comment.