2017-11-08 19:18:15 -08:00
|
|
|
import Service from '@ember/service';
|
2017-08-24 11:31:54 -07:00
|
|
|
import { getUserEntities } from 'wherehows-web/utils/api/datasets/owners';
|
|
|
|
import { IPartyEntity, IPartyProps } from 'wherehows-web/typings/api/datasets/party-entities';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a userNameQuery query and find userNames that match by starting with
|
|
|
|
* the pattern
|
|
|
|
* @param {string} userNameQuery pattern to search for
|
|
|
|
* @param {Function} _syncResults callback
|
|
|
|
* @param {Function} asyncResults callback
|
|
|
|
* @return {Promise<void>}
|
|
|
|
*/
|
2017-11-10 00:08:50 -08:00
|
|
|
const ldapResolver = async (
|
|
|
|
userNameQuery: string,
|
|
|
|
_syncResults: Function,
|
|
|
|
asyncResults: (results: Array<string>) => void
|
|
|
|
): Promise<void> => {
|
2017-08-24 11:31:54 -07:00
|
|
|
const ldapRegex = new RegExp(`^${userNameQuery}.*`, 'i');
|
|
|
|
const { userEntitiesSource = [] }: IPartyProps = await getUserEntities();
|
2017-11-10 00:08:50 -08:00
|
|
|
|
2017-08-24 11:31:54 -07:00
|
|
|
asyncResults(userEntitiesSource.filter((entity: string) => ldapRegex.test(entity)));
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For a given userName, find the userEntity object that contains the userName
|
|
|
|
* @param {string} userName the unique userName
|
|
|
|
* @return {Promise<IPartyEntity>} resolves with the userEntity or null otherwise
|
|
|
|
*/
|
|
|
|
const getPartyEntityWithUserName = (userName: string): Promise<IPartyEntity | null> =>
|
|
|
|
getUserEntities().then(
|
|
|
|
({ userEntities }: IPartyProps) => userEntities.find(({ label }: { label: string }) => label === userName) || null
|
|
|
|
);
|
|
|
|
|
2017-11-08 19:18:15 -08:00
|
|
|
export default class UserLookup extends Service {
|
|
|
|
getPartyEntityWithUserName = getPartyEntityWithUserName;
|
|
|
|
userNamesResolver = ldapResolver;
|
|
|
|
fetchUserNames = getUserEntities;
|
|
|
|
}
|