diff --git a/i18n/en/settings.js b/i18n/en/settings.js index 937f87abd5ad12ad635623a585aa3288b7ab2a21..ad6dd471f66a7134605317a250b7bc8c8fbe043f 100644 --- a/i18n/en/settings.js +++ b/i18n/en/settings.js @@ -47,7 +47,8 @@ export default { deleteOption: 'Import and delete existing', errors: { couldNotOpenFile: 'Could not open file', - postFix: 'No data was imported or changed' + postFix: 'No data was imported or changed', + futureEdit: 'Future dates may only contain a note, no other symptoms' }, success: { message: 'Data successfully imported' diff --git a/lib/import-export/import-from-csv.js b/lib/import-export/import-from-csv.js index 4731eeadb28e1e5fb0cab07bb60bd8165ef26b2f..1bdc8d80fd85e2aaf66baa98cfa53b37e6306fde 100644 --- a/lib/import-export/import-from-csv.js +++ b/lib/import-export/import-from-csv.js @@ -7,6 +7,8 @@ import { updateCycleStartsForAllCycleDays } from '../../db' import getColumnNamesForCsv from './get-csv-column-names' +import { LocalDate } from 'js-joda' +import labels from '../../i18n/en/settings' export default async function importCsv(csv, deleteFirst) { const parseFuncs = { @@ -48,6 +50,7 @@ export default async function importCsv(csv, deleteFirst) { //remove symptoms where all fields are null putNullForEmptySymptoms(cycleDays) + throwIfFutureData(cycleDays) if (deleteFirst) { tryToImportWithDelete(cycleDays) @@ -87,4 +90,15 @@ function getDbType(modelProperties, path) { if (path.length === 1) return modelProperties[path[0]].type const modelName = modelProperties[path[0]].objectType return getDbType(schema[modelName], path.slice(1)) +} + +function throwIfFutureData(cycleDays) { + const today = LocalDate.now().toString() + for (const i in cycleDays) { + const day = cycleDays[i] + // notes are allowed for future dates but everything else isn't + if (day.date > today && Object.keys(day).some(symptom => symptom != 'date' && symptom != 'note')) { + throw new Error(labels.import.errors.futureEdit) + } + } } \ No newline at end of file