Home Manual Reference Source Test Repository

src/searching/DeviceSearch.js

import Search from './Search';
import q from 'q';

/** 
 * This extends Search and allow make request to any available resource into Opengate North API.
 */
export default class DeviceSearch extends Search {
    /**
     * Constructor
     * @param {!InternalOpenGateAPI} ogapi - this is configuration about Opengate North API.
     * @param {!string} url - this define a specific resource to make the search
     * @param {object} filter - this is the filter
     * @param {object} limit - this is the pagination about the search
     * @param {object} sort - this defined parameters to order the result of search
     * @param {object} group - this defined the group by
     */
    constructor(ogapi, url, filter, limit, sort, group, select, timeout, urlParams) {
        super(ogapi, url, filter, limit, sort, group, select, timeout, urlParams);
    }

    /**
     * This invoke a request to OpenGate North API and the callback is managed by promises
     * @return {Promise}
     * @property {function (result:object, statusCode:number)} then - When request it is OK
     * @property {function (error:string)} catch - When request it is NOK
     */
    execute() {
        var defered = q.defer();
        var promise = defered.promise;
        var parameters = this._getUrlParameters();
        this._ogapi.Napi
            .post(this._resource, this._filter(), this._timeout, this._getExtraHeaders(), parameters)
            .then((response) => {
                let resultQuery = response.body;
                let statusCode = response.statusCode;

                if (statusCode === 200 && resultQuery.entities) {
                    resultQuery.devices = resultQuery.entities;

                    // OUW-944
                    if (resultQuery.devices.length > 0) {

                        var ele = false;
                        var flattened = (parameters && parameters.flattened) || false;

                        for (ele = 0; ele < resultQuery.devices.length; ele++) {
                            if (flattened) {
                                if (resultQuery.devices[ele]['device.identifier']) {
                                    var dato = resultQuery.devices[ele]['device.identifier'];
                                    if (!dato._value || (dato._value && !dato._value._current)) {
                                        delete resultQuery.devices[ele]['device.identifier'];
                                    }
                                }

                            } else {
                                if (resultQuery.devices[ele].device && resultQuery.devices[ele].device.identifier && !resultQuery.devices[ele].device.identifier._current) {
                                    delete resultQuery.devices[ele].device.identifier;
                                }
                            }
                        }
                    }


                    delete resultQuery.entities;
                }
                defered.resolve({
                    data: resultQuery,
                    statusCode: statusCode
                });
            })
            .catch((error) => {
                defered.reject(error);
            });
        return promise;
    }


}