Skip to content

Commit

Permalink
Bugfix/invalid dates (#1619)
Browse files Browse the repository at this point in the history
* prevent throwing error in UI when date value is invalid

* fixed invalid date values imported from Collect survey

* code cleanup: use date-fns isValid when possible

Co-authored-by: Stefano Ricci <SteRiccio@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Jul 26, 2021
1 parent 75a3680 commit a62dcda
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
5 changes: 4 additions & 1 deletion core/dateUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const isValidTime = (hour = '', minutes = '') =>

export const isValidDateInFormat = (dateStr, format) => {
const parsed = parse(dateStr, format)
return !isNaN(parsed.getTime())
return fnsIsValid(parsed)
}

export const formatDateISO = (date) => (date ? format(date, formats.dateISO) : null)
Expand All @@ -118,6 +118,9 @@ export const convertDate = ({ dateStr, formatFrom = formats.dateISO, formatTo })
return null
}
const dateParsed = parse(dateStr, formatFrom)
if (!fnsIsValid(dateParsed)) {
return null
}
return format(dateParsed, formatTo)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as R from 'ramda'

import { uuidv4 } from '@core/uuid'
import * as StringUtils from '@core/stringUtils'
import * as NumberUtils from '@core/numberUtils'
import * as DateUtils from '@core/dateUtils'

import * as Survey from '@core/survey/survey'
Expand Down Expand Up @@ -71,14 +71,11 @@ const extractCoordinateValueAndMeta = (collectNode) => {
const extractDateValueAndMeta = (collectNode) => {
const { day, month, year } = CollectRecord.getTextValues(collectNode)

const date =
StringUtils.isBlank(year) || StringUtils.isBlank(month) || StringUtils.isBlank(day)
? null
: new Date(year, month - 1, day)

return {
value: DateUtils.formatDateISO(date),
if (!NumberUtils.isInteger(year) || !NumberUtils.isInteger(month) || !NumberUtils.isInteger(day)) {
return { value: null }
}
const date = new Date(year, month - 1, day)
return { value: DateUtils.formatDateISO(date) }
}

const extractFileValueAndMeta = (survey, node, collectSurveyFileZip, collectNodeDef, tx) => async (collectNode) => {
Expand Down Expand Up @@ -112,9 +109,11 @@ const extractFileValueAndMeta = (survey, node, collectSurveyFileZip, collectNode
}

const extractTaxonValueAndMeta = (survey, nodeDef) => (collectNode) => {
const { code, scientific_name: scientificName, vernacular_name: vernacularName } = CollectRecord.getTextValues(
collectNode
)
const {
code,
scientific_name: scientificName,
vernacular_name: vernacularName,
} = CollectRecord.getTextValues(collectNode)
const taxonUuid = Survey.getTaxonUuid(nodeDef, code)(survey)

if (taxonUuid) {
Expand Down

0 comments on commit a62dcda

Please sign in to comment.