Custom Table Example

Code

function baState(value) {
  if (value) {
    var newValor;
    var style;
    var title;
    switch (value) {
      case "0":
        newValor = "(OK)🟢";
        style = 'color:green;';
        title = "Bateria OK";
        break;
      case "1":
        newValor = "(BAJA)🟡";
        style = 'color:yellow;';
        title = "Bateria BAJA";
        break;
      case "2":
        newValor = "(MUY BAJA)🟠";
        style = 'color:orange;';
        title = "Bateria MUY BAJA";
        break;
      case "3":
        newValor = "(AGOTADA)🔴";
        style = 'color:red;';
        title = "Bateria AGOTADA";
        break;
      default:
        newValor = "Error en el campo";
        break;
    }
    return {
      _style: style,
      value: "<div title='" + title + ">" + newValor + "</div>"
    };
  } else {
    return 'N/A';
  }
}

function bpaState(value) {
  var newValor;
  var title;
  var style;
  if (value) {
    switch (value) {
      case "0":
        newValor = "(OK)🟢";
        title = "Protección no activa";
        style = 'color:green;';
        break;
      case "1":
        newValor = "(ACTIVADA)🔴";
        title = "Protección activada";
        style = 'color:red;';

        break;
      default:
        newValor = "Error en el campo";
        break;
    }
    return {
      value: "<div title='" + title + "'>" + newValor + "</div>",
      _style: style
    };
  }
}

function boolState(value) {
  var newValor;
  var title;
  var style;
  if (value === true || value === false) {
    if (value) {
      newValor = "🔴";
      title = "TRUE";
      style = 'color:red;';
    } else {
      newValor = "🟢";
      title = "FALSE";
      style = 'color:green;';
    }
    return {
      value: "<div title='" + title + "'>" + newValor + "</div>",
      _style: style
    };
  }
}


console.log("--------------------------------------------");
var builder = $api.entitiesSearchBuilder().limit(1000).flattened();

var filter = {
  and: [
    {
      neq: {
        'provision.device.specificType': 'CONCENTRATOR'
      }
    }
  ]
};

if (entityData && entityData['provision.administration.identifier']) {
  var entityKey = entityData['provision.administration.identifier']._value._current.value;
  filter.and.push({
    eq: {
      'provision.Sector': entityKey
    }
  });
}

builder.filter(filter);

const response = await builder.build().execute();

var entities = [];
if (response.statusCode === 200) {
  response.data.entities.forEach(function (entityTmp) {
    var finalData;
    if (entityTmp['ba']) {
      finalData = {
        identifier: {
          value: entityTmp['provision.administration.identifier']._value._current.value,
          _style: 'margin-left: 4px;'
        }
      };
      finalData.tipo_alerta = 'Batería';
      finalData.estado = baState(entityTmp['ba']._value._current.value);
      finalData.fecha = new Date(entityTmp['ba']._value._current.at).toLocaleString();
      entities.push(finalData);
    }

    if (entityTmp['bpA']) {
      finalData = {
        identifier: {
          value: entityTmp['provision.administration.identifier']._value._current.value,
          _style: 'margin-left: 4px;'
        }
      };
      finalData.tipo_alerta = 'Protección Batería';
      finalData.estado = bpaState(entityTmp['bpA']._value._current.value);
      finalData.fecha =  new Date(entityTmp['bpA']._value._current.at).toLocaleString();
      entities.push(finalData);
    }

    if (entityTmp['ta']) {
      finalData = {
        identifier: {
          value: entityTmp['provision.administration.identifier']._value._current.value,
          _style: 'margin-left: 4px;'
        }
      };
      finalData.tipo_alerta = 'Tampering';
      finalData.estado = boolState(entityTmp['ta']._value._current.value);
      finalData.fecha = new Date( entityTmp['ta']._value._current.at).toLocaleString();
      entities.push(finalData);
    }

    if (entityTmp['fa']) {
      finalData = {
        identifier: {
          value: entityTmp['provision.administration.identifier']._value._current.value,
          _style: 'margin-left: 4px;'
        }
      };
      finalData.tipo_alerta = 'Fuga';
      finalData.estado = boolState(entityTmp['fa']._value._current.value);
      finalData.fecha =  new Date(entityTmp['fa']._value._current.at).toLocaleString();
      entities.push(finalData);
    }

  });
}

return entities;