Source: models/production/searchall.js

define('application/models/production/searchall', [
    'rofl/models/api/abstract',
    'rofl/lib/utils',
    'application/models/broadcast',
    'application/models/configuration',
    'application/models/configuration'
], function (
    Abstract,
    Utils,
    Broadcast,
    KPNConfig,
    Configuration
) {
    'use strict';

    var SEARCH_TYPES = {
            program: 'program',
            vod: 'vod'
        },
        endPoints = {
            program: '101/1.2.0/A/nld/smarttv/kpn/TRAY/SEARCH/PROGRAM',
            vod: '101/1.2.0/A/nld/smarttv/kpn/TRAY/SEARCH/VOD/'
        };

    return Abstract.extend({

        /**
         * Resolves the endpoint.
         *
         * @param {Object} params - Search parameters.
         * @param {string} params.query - Text to be searched for in the backend.
         * @returns {string} Resolved endpoint.
         */
        resolveEndpoint: function (params) {
            var sanitizedQuery = encodeURIComponent(params.query),
                searchContentType = params._searchContentType || SEARCH_TYPES.program,
                endpoint = endPoints[searchContentType],
                dfilterChannels = params.dfilter_channels,
                dfilterPackages = params.dfilter_packages,
                filterStartTime = params.filter_startTime,
                filterEndTime = params.filter_endTime,
                filterIsCatchup = params.filter_isCatchup,
                filterFuzzy = params.filter_fuzzy,
                filterIsTvPremiere = params.filter_isTvPremiere,
                sortOrder = params.sortOrder,
                orderBy = params.orderBy,
                from = params.from,
                to = params.to,
                filterQuality = params.filter_quality,
                filterContentType = params.filter_contentType,
                filterContentSubtype = params.filter_contentSubtype,
                filterAiringStartTime = params.filter_airingStartTime,
                filterChannelType = params.filter_channelType,
                filterContentIds = params.filter_contentIds;

            endpoint +=
                '?'
                + (sanitizedQuery ? 'query=' + sanitizedQuery : '')
                + (filterChannelType ? '&filter_channelType=' + filterChannelType : '')
                + (dfilterChannels ? '&dfilter_channels=' + dfilterChannels : '')
                + (dfilterPackages ? '&dfilter_packages=' + dfilterPackages : '')
                + (filterStartTime ? '&filter_startTime=' + filterStartTime : '')
                + (filterAiringStartTime ? '&filter_airingStartTime=' + filterAiringStartTime : '')
                + (filterEndTime ? '&filter_endTime=' + filterEndTime : '')
                + (filterIsCatchup ? '&filter_isCatchUp=' + filterIsCatchup : '')
                + (filterIsTvPremiere ? '&filter_isTvPremiere=' + filterIsTvPremiere : '')
                + (orderBy ? '&orderBy=' + orderBy : '')
                + (sortOrder ? '&sortOrder=' + sortOrder : '')
                + (filterQuality ? '&filter_quality=' + filterQuality : '')
                + (filterContentType ? '&filter_contentType=' + filterContentType : '')
                + (filterContentSubtype ? '&filter_contentSubtype=' + filterContentSubtype : '')
                + (filterContentIds ? '&filter_contentIds=' + filterContentIds : '')
                + (filterFuzzy ? '&filter_fuzzy=' + filterFuzzy : '')
                + (!isNaN(from) ? '&from=' + from : '')
                + (!isNaN(to) ? '&to=' + to : '')
                + '&filter_excludedGenres=erotiek';

            return endpoint;
        },

        /**
         * Transforms API data into the abstract model data.
         *
         * @param  {Object} response - The data object gotten from the API.
         * @returns {Object} The filled model instance.
         */
        transformFrom: function (response) {
            var results = Utils.getNested(response, 'resultObj', 'containers') || [],
                channels = {};

            if (this._formatByChannel) {

                Utils.each(results, function (entry) {

                    if (!channels[entry.channel.channelId]) {
                        channels[entry.channel.channelId] = [];
                    }

                    channels[entry.channel.channelId].push(new Broadcast(entry));
                });

                return channels;
            }

            return Utils.map(results, function (entry) {
                return new Broadcast(entry);
            });
        },

        /**
         * Validates the read response.
         *
         * @param {Object} response - The read response.
         * @returns {boolean} - True if valid.
         */
        validateReadResponse: function (response) {
            return response.resultCode === Configuration.RESPONSE_CODES.OK;
        }
    });
});