Skip to content

Commit

Permalink
fix(viirs): add a new validation to show viirs widgets on the map
Browse files Browse the repository at this point in the history
To avoid errors between viirs layers and wigets with the new options (high confidence) we added some validations to
getWhereQuery method
To the future, we will need to think a better solution
  • Loading branch information
willian-viana committed Nov 3, 2023
1 parent ed57e34 commit 2c0fa12
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions services/get-where-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const getWHEREQuery = (params = {}) => {
const allFilterOptions = forestTypes.concat(landCategories);
const allowedParams = ALLOWED_PARAMS[params.dataset || 'annual'];
const isTreeCoverDensity = dataset === 'treeCoverDensity';
const isVIIRS = dataset === 'viirs';
const comparisonString = ' = ';

let paramString = 'WHERE ';
Expand All @@ -35,26 +36,56 @@ export const getWHEREQuery = (params = {}) => {
paramKeys = paramKeys.filter((item) => item !== 'threshold');
}

/*
* Removing confidence_cat = 'false' from VIIRS request
* if the user selects 'all alerts' on the VIIRS layer,
* we don't want to add a new parameter to the query
*/
if (isVIIRS) {
if (
typeof params?.confidence === 'boolean' &&
params?.confidence === false
) {
paramKeys = paramKeys.filter((item) => item !== 'confidence');
}

if (params?.confidence === 'false' || params?.confidence === 'true') {
paramKeys = paramKeys.filter((item) => item !== 'confidence');
}
}

paramKeys.forEach((parameter, index) => {
const isLastParameter = paramKeys.length - 1 === index;
const hasFilterOption = ['forestType', 'landCategory'].includes(parameter);
const value = hasFilterOption ? 1 : params[parameter];
let value = hasFilterOption ? 1 : params[parameter];
const filterOption = allFilterOptions.find(
(pname) => pname.value === params[parameter]
);

/*
* when the user selects high confidence on VIIRS Layer
* the confidence option returns a string 'true'
* the right value should be 'h'
* Until we fix this return, we need to validate the parameter
*/
if (isVIIRS && parameter === 'confidence') {
if (
typeof params?.confidence === 'boolean' &&
params?.confidence === true
) {
value = 'h';
}
}

const tableKey =
filterOption &&
(filterOption.tableKey || filterOption.tableKeys[dataset || 'annual']);

let isNumericValue = isNumber(value);

const paramKey = translateParameterKey(parameter, params);

if (parameter === 'adm0' && type === 'wdpa') {
isNumericValue = false;
}

if (dataset === 'net_change') {
if ((parameter === 'adm0' && type === 'wdpa') || dataset === 'net_change') {
isNumericValue = false;
}

Expand Down

0 comments on commit 2c0fa12

Please sign in to comment.