All files / request/search-feedback search-feedback.request.ts

100% Statements 9/9
95.65% Branches 22/23
100% Functions 3/3
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68                  21x   21x       21x 20x     21x           25x               23x                                       14x     14x                      
import { customIdentity, customPickBy } from '../../utils';
import { FeedbackDocument } from '../feedback/feedback.document.type';
import { GlobalRequestOptions } from '../main/main.request.type';
import { buildBatchSearchFeedbackRequestSchema } from './search-feedback.schema';
import { BatchSearchFeedbackRequestParams, BatchSearchFeedbackRequestResult, SingleSearchFeedbackRequestParams } from './search-feedback.request.type';
 
 
export async function buildSingleSearchFeedbackRequest(this: GlobalRequestOptions, params: SingleSearchFeedbackRequestParams): Promise<BatchSearchFeedbackRequestResult> {
 
    const { documentFeedback, ...rest } = params;
 
    const options: any = {
        ...rest,
    }
 
    if (documentFeedback) {
        options.documentsFeedbacks = [documentFeedback];
    }
 
    return buildBatchSearchFeedbackRequest.bind(this)(options);
}
 
 
export async function buildBatchSearchFeedbackRequest(this: GlobalRequestOptions, params: BatchSearchFeedbackRequestParams): Promise<BatchSearchFeedbackRequestResult> {
 
    const validatedParams = await buildBatchSearchFeedbackRequestSchema
        .validate(
            customPickBy({
                ...params,
                indexName: params.indexName || this?.indexName || process.env.GLOBAL_DEFAULT_INDEX_NAME,
                platformName: params.platformName || this?.platformName,
                searchHandler: params.searchHandler || this?.searchHandler || process.env.GLOBAL_DEFAULT_SEARCH_HANDLER,
                APIKey: params.APIKey || this?.APIKey,
                documentsFeedbacks: params.documentsFeedbacks?.map(feedback => customPickBy({
                    ...feedback,
                    userCountry: feedback.userCountry || this?.userCountry,
                    userId: feedback.userId || this?.userId,
                    userIp: feedback.userIp || this?.userIp,
                    sessionId:
                        feedback.sessionId ||
                        this?.sessionId ||
                        (this.sessionIdGenerator ? this.sessionIdGenerator() : undefined),
                }, customIdentity)),
            }, customIdentity)
        );
 
 
    const {
        indexName,
        platformName,
        searchHandler,
        APIKey,
        documentsFeedbacks,
    } = validatedParams;
 
 
    return {
        method: 'POST',
        url: `${process.env.API_BASE_URL}/projects/${platformName}/indices/${indexName}/search/${searchHandler}/feedback/hits`,
        params: {
            apikey: APIKey,
        },
        headers: {},
        body: documentsFeedbacks as FeedbackDocument[],
    }
}