Skip to content

Commit

Permalink
buildFilter func
Browse files Browse the repository at this point in the history
  • Loading branch information
kimura-developer committed Sep 13, 2023
1 parent c7d22fd commit 17041be
Showing 1 changed file with 54 additions and 23 deletions.
77 changes: 54 additions & 23 deletions service/src/app.impl/users/app.impl.users.ts
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;
}

0 comments on commit 17041be

Please sign in to comment.