import Service from '@ember/service'; 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} */ const ldapResolver = async ( userNameQuery: string, _syncResults: Function, asyncResults: (results: Array) => void ): Promise => { const ldapRegex = new RegExp(`^${userNameQuery}.*`, 'i'); const { userEntitiesSource = [] }: IPartyProps = await getUserEntities(); 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} resolves with the userEntity or null otherwise */ const getPartyEntityWithUserName = (userName: string): Promise => getUserEntities().then( ({ userEntities }: IPartyProps) => userEntities.find(({ label }: { label: string }) => label === userName) || null ); export default class UserLookup extends Service { getPartyEntityWithUserName = getPartyEntityWithUserName; userNamesResolver = ldapResolver; fetchUserNames = getUserEntities; }