2024-03-18 13:58:05 +01:00
|
|
|
import type { Logger } from '../../logger';
|
|
|
|
import type {
|
2023-12-14 14:45:36 +01:00
|
|
|
IFeatureSearchStore,
|
2023-10-25 15:18:52 +02:00
|
|
|
IUnleashConfig,
|
|
|
|
IUnleashStores,
|
|
|
|
} from '../../types';
|
2024-03-18 13:58:05 +01:00
|
|
|
import type {
|
2023-11-24 09:45:44 +01:00
|
|
|
IFeatureSearchParams,
|
|
|
|
IQueryParam,
|
|
|
|
} from '../feature-toggle/types/feature-toggle-strategies-store-type';
|
2024-08-02 09:56:42 +02:00
|
|
|
import { parseSearchOperatorValue } from './search-utils';
|
2023-10-25 15:18:52 +02:00
|
|
|
|
|
|
|
export class FeatureSearchService {
|
2023-12-14 14:45:36 +01:00
|
|
|
private featureSearchStore: IFeatureSearchStore;
|
2023-10-25 15:18:52 +02:00
|
|
|
private logger: Logger;
|
|
|
|
constructor(
|
2023-12-14 14:45:36 +01:00
|
|
|
{ featureSearchStore }: Pick<IUnleashStores, 'featureSearchStore'>,
|
2023-10-25 15:18:52 +02:00
|
|
|
{ getLogger }: Pick<IUnleashConfig, 'getLogger'>,
|
|
|
|
) {
|
2023-12-14 14:45:36 +01:00
|
|
|
this.featureSearchStore = featureSearchStore;
|
2023-10-25 15:18:52 +02:00
|
|
|
this.logger = getLogger('services/feature-search-service.ts');
|
|
|
|
}
|
|
|
|
|
2023-10-26 15:29:30 +02:00
|
|
|
async search(params: IFeatureSearchParams) {
|
2023-11-24 09:45:44 +01:00
|
|
|
const queryParams = this.convertToQueryParams(params);
|
2023-11-01 09:19:42 +01:00
|
|
|
const { features, total } =
|
2023-12-14 14:45:36 +01:00
|
|
|
await this.featureSearchStore.searchFeatures(
|
2023-11-24 09:45:44 +01:00
|
|
|
{
|
|
|
|
...params,
|
|
|
|
limit: params.limit,
|
2024-08-02 09:56:42 +02:00
|
|
|
sortBy: params.sortBy || 'createdAt',
|
2023-11-24 09:45:44 +01:00
|
|
|
},
|
|
|
|
queryParams,
|
|
|
|
);
|
2023-10-25 15:18:52 +02:00
|
|
|
|
2023-10-31 14:10:31 +01:00
|
|
|
return {
|
2023-11-08 10:12:42 +01:00
|
|
|
features,
|
2023-11-01 09:19:42 +01:00
|
|
|
total,
|
2023-10-31 14:10:31 +01:00
|
|
|
};
|
2023-10-25 15:18:52 +02:00
|
|
|
}
|
2023-11-24 09:45:44 +01:00
|
|
|
|
|
|
|
convertToQueryParams = (params: IFeatureSearchParams): IQueryParam[] => {
|
|
|
|
const queryParams: IQueryParam[] = [];
|
|
|
|
|
2023-11-29 14:22:42 +01:00
|
|
|
if (params.state) {
|
2024-08-02 09:56:42 +02:00
|
|
|
const parsedState = parseSearchOperatorValue('stale', params.state);
|
2023-11-29 14:22:42 +01:00
|
|
|
if (parsedState) {
|
|
|
|
parsedState.values = parsedState.values.map((value) =>
|
|
|
|
value === 'active' ? 'false' : 'true',
|
|
|
|
);
|
|
|
|
queryParams.push(parsedState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-30 11:00:39 +01:00
|
|
|
if (params.createdAt) {
|
2024-08-02 09:56:42 +02:00
|
|
|
const parsed = parseSearchOperatorValue(
|
2023-11-30 11:00:39 +01:00
|
|
|
'features.created_at',
|
|
|
|
params.createdAt,
|
|
|
|
);
|
|
|
|
if (parsed) queryParams.push(parsed);
|
|
|
|
}
|
|
|
|
|
2024-06-06 12:59:11 +02:00
|
|
|
if (params.createdBy) {
|
2024-08-02 09:56:42 +02:00
|
|
|
const parsed = parseSearchOperatorValue(
|
2024-06-06 12:59:11 +02:00
|
|
|
'users.id',
|
|
|
|
params.createdBy,
|
|
|
|
);
|
|
|
|
if (parsed) queryParams.push(parsed);
|
|
|
|
}
|
|
|
|
|
2024-06-04 15:13:11 +02:00
|
|
|
if (params.type) {
|
2024-08-02 09:56:42 +02:00
|
|
|
const parsed = parseSearchOperatorValue(
|
2024-06-04 15:13:11 +02:00
|
|
|
'features.type',
|
|
|
|
params.type,
|
|
|
|
);
|
|
|
|
if (parsed) queryParams.push(parsed);
|
|
|
|
}
|
|
|
|
|
2023-11-29 09:40:25 +01:00
|
|
|
['tag', 'segment', 'project'].forEach((field) => {
|
2023-11-27 14:48:41 +01:00
|
|
|
if (params[field]) {
|
2024-08-02 09:56:42 +02:00
|
|
|
const parsed = parseSearchOperatorValue(field, params[field]);
|
2023-11-29 09:40:25 +01:00
|
|
|
if (parsed) queryParams.push(parsed);
|
2023-11-27 14:48:41 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-11-24 09:45:44 +01:00
|
|
|
return queryParams;
|
|
|
|
};
|
2023-10-25 15:18:52 +02:00
|
|
|
}
|