diff --git a/service/src/app.impl/users/app.impl.users.ts b/service/src/app.impl/users/app.impl.users.ts index 226e6133d..c98b4c8f3 100644 --- a/service/src/app.impl/users/app.impl.users.ts +++ b/service/src/app.impl/users/app.impl.users.ts @@ -1,28 +1,59 @@ -import * as api from '../../app.api/users/app.api.users' -import { UserRepository } from '../../entities/users/entities.users' -import { withPermission, KnownErrorsOf } from '../../app.api/app.api.global' -import { PageOf } from '../../entities/entities.global' +import * as api from '../../app.api/users/app.api.users'; +import { UserRepository } from '../../entities/users/entities.users'; +import { withPermission, KnownErrorsOf } from '../../app.api/app.api.global'; +import { PageOf } from '../../entities/entities.global'; -export function SearchUsers(userRepo: UserRepository, permissions: api.UsersPermissionService): api.SearchUsers { - return async function searchUsers(req: api.UserSearchRequest): ReturnType { - return await withPermission, KnownErrorsOf>( +export function SearchUsers( + userRepo: UserRepository, + permissions: api.UsersPermissionService +): api.SearchUsers { + return async function searchUsers( + req: api.UserSearchRequest + ): ReturnType { + // Use the buildFilter function to construct the filter + const userSearch: api.UserSearchRequest['userSearch'] = buildFilter( + req.userSearch + ); + + // Continue with the rest of the logic, including the search operation + return await withPermission< + PageOf, + KnownErrorsOf + >( permissions.ensureReadUsersPermission(req.context), async (): Promise> => { - const page = await userRepo.find(req.userSearch, x => { - return { - id: x.id, - username: x.username, - displayName: x.displayName, - email: x.email, - active: x.active, - enabled: x.enabled, - allPhones: x.phones.reduce((allPhones, phone, index) => { - return index === 0 ? `${phone.number}` : `${allPhones}; ${phone.number}` - }, '') + const page = await userRepo.find( + userSearch, + x => { + return { + id: x.id, + username: x.username, + displayName: x.displayName, + email: x.email, + active: x.active, + enabled: x.enabled, + allPhones: x.phones.reduce((allPhones, phone, index) => { + return index === 0 + ? `${phone.number}` + : `${allPhones}; ${phone.number}`; + }, '') + }; } - }) - return page + ); + return page; } - ) - } -} \ No newline at end of file + ); + }; +} + +// Function to abstract parameter handling and filter building +function buildFilter(userSearch: api.UserSearchRequest['userSearch']): api.UserSearchRequest['userSearch'] { + const filter: api.UserSearchRequest['userSearch'] = { + nameOrContactTerm: userSearch.nameOrContactTerm, + pageSize: userSearch.pageSize || 250, + pageIndex: userSearch.pageIndex || 0, + includeTotalCount: userSearch.includeTotalCount, + }; + + return filter; +}