-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7d22fd
commit 17041be
Showing
1 changed file
with
54 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<api.SearchUsers> { | ||
return await withPermission<PageOf<api.UserSearchResult>, KnownErrorsOf<api.SearchUsers>>( | ||
export function SearchUsers( | ||
userRepo: UserRepository, | ||
permissions: api.UsersPermissionService | ||
): api.SearchUsers { | ||
return async function searchUsers( | ||
req: api.UserSearchRequest | ||
): ReturnType<api.SearchUsers> { | ||
// 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<api.UserSearchResult>, | ||
KnownErrorsOf<api.SearchUsers> | ||
>( | ||
permissions.ensureReadUsersPermission(req.context), | ||
async (): Promise<PageOf<api.UserSearchResult>> => { | ||
const page = await userRepo.find<api.UserSearchResult>(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<api.UserSearchResult>( | ||
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; | ||
} | ||
) | ||
} | ||
} | ||
); | ||
}; | ||
} | ||
|
||
// 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; | ||
} |