Skip to content
Snippets Groups Projects
Commit 217da943 authored by Timothee P's avatar Timothee P :sunflower:
Browse files

improve delimiter detection, encoding pattern & striing conversion to json compatible syntax

parent 6f3d5fdc
Branches
Tags
1 merge request!728REDMINE_ISSUE-19251 | Listes de valeurs pré-enr. - Vérification que l'option existe bien dans la liste ne fonctionne pas dans le cas où j'ai un string
......@@ -518,7 +518,7 @@ export default {
const hasCoordValues = checkLonLatValues(headers, rows);
if (headersCoord.length === 2 && hasCoordValues) {
this.csvError = null;
csv()
csv({ delimiter })
.fromString(fr.result)
.then((jsonObj)=>{
this.csvImport = jsonObj;
......
......
......@@ -384,7 +384,6 @@
<script>
import { csv } from 'csvtojson';
import { escape as _escape } from 'lodash';
import { mapActions, mapMutations, mapGetters, mapState } from 'vuex';
import { formatStringDate, transformProperties } from '@/utils';
......@@ -561,8 +560,10 @@ export default {
async checkPreRecordedValue(fieldValue, listName) {
const fieldLabel = fieldValue.label || fieldValue;
// encode special characters like apostrophe or white space
const encodedPattern = encodeURIComponent(fieldLabel);
// query existing prerecorded list values (with label to limit results in response, there could be many) and escape special characters, since single quote causes error in backend
await this.GET_SELECTED_PRERECORDED_LIST_VALUES({ name: listName, pattern: _escape(fieldLabel) });
await this.GET_SELECTED_PRERECORDED_LIST_VALUES({ name: listName, pattern: encodedPattern });
// check if the value exist in available prerecorded list values
return this.selectedPrerecordedListValues[listName].some((el) => el.label === fieldLabel);
},
......@@ -576,8 +577,10 @@ export default {
options: el.options,
};
});
for (const feature of data.features) {
for (const { name, field_type, options } of fields) {
const properties = feature.properties || feature;
if (name in properties) {
let fieldInFeature = properties[name];
......@@ -598,9 +601,13 @@ export default {
} else if (field_type === 'pre_recorded_list') {
if (typeof fieldInFeature === 'string' && fieldInFeature.charAt(0) === '{') { // data from CSV come as string, if it doesn't start with bracket then it should not be converted to an object and stay as a string, since the structure has been simplified: https://redmine.neogeo.fr/issues/18740
try {
fieldInFeature = JSON.parse(fieldInFeature.replaceAll('\'', '"'));
} catch {
this.DISPLAY_MESSAGE({ comment: `La valeur [ ${fieldInFeature} ] n'a pas pu être vérifiée pour l'import du signalement [${feature.properties.title}]` });
const jsonStr = fieldInFeature.replace(/['‘’"]\s*label\s*['‘’"]\s*:/g, '"label":')
.replace(/:\s*['‘’"](.+?)['‘’"]\s*(?=[,}])/g, ':"$1"');
// Parse la chaîne en JSON
fieldInFeature = JSON.parse(jsonStr);
} catch (e) {
console.error(e);
this.DISPLAY_MESSAGE({ comment: `La valeur [ ${fieldInFeature} ] n'a pas pu être vérifiée pour l'import du signalement [${properties.title}]` });
}
}
let fieldLabel = fieldInFeature.label || fieldInFeature;
......@@ -615,8 +622,9 @@ export default {
if (typeof fieldInFeature === 'string' && fieldInFeature.charAt(0) === '[') { // data from CSV come as string, if it doesn't start with bracket then there's no need to convert it to an array
try {
fieldInFeature = JSON.parse(fieldInFeature.replaceAll('\'', '"'));
} catch {
this.DISPLAY_MESSAGE({ comment: `La valeur [ ${fieldInFeature.label} ] n'a pas pu être vérifiée pour l'import du signalement [${feature.properties.title}]` });
} catch (e) {
console.error(e);
this.DISPLAY_MESSAGE({ comment: `La valeur [ ${fieldInFeature.label} ] n'a pas pu être vérifiée pour l'import du signalement [${properties.title}]` });
}
}
const unvalidValues = fieldInFeature.filter((el) => !options.includes(el));
......@@ -697,7 +705,7 @@ export default {
}
// Convert the CSV string to a JSON object for further processing
const jsonFromCsv = await csv().fromString(csvString);
const jsonFromCsv = await csv({ delimiter }).fromString(csvString);
// Validate the types of values in the JSON object
const validity = await this.isValidTypes({ features: jsonFromCsv });
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment