fix: don't crash on customer page with bad dates

This commit is contained in:
siiky 2024-09-19 15:23:10 +01:00
parent 7e87a3e911
commit 39e78a37a2
2 changed files with 19 additions and 17 deletions

View file

@ -1,6 +1,5 @@
import Grid from '@material-ui/core/Grid' import Grid from '@material-ui/core/Grid'
import { makeStyles } from '@material-ui/core/styles' import { makeStyles } from '@material-ui/core/styles'
import { parse, format } from 'date-fns/fp'
import * as R from 'ramda' import * as R from 'ramda'
import { useState, React } from 'react' import { useState, React } from 'react'
import * as Yup from 'yup' import * as Yup from 'yup'
@ -30,6 +29,7 @@ import {
customerDataElements, customerDataElements,
customerDataSchemas, customerDataSchemas,
formatDates, formatDates,
tryFormatDate,
getFormattedPhone getFormattedPhone
} from './helper.js' } from './helper.js'
@ -113,18 +113,10 @@ const CustomerData = ({
firstName: R.path(['firstName'])(idData) ?? '', firstName: R.path(['firstName'])(idData) ?? '',
lastName: R.path(['lastName'])(idData) ?? '', lastName: R.path(['lastName'])(idData) ?? '',
documentNumber: R.path(['documentNumber'])(idData) ?? '', documentNumber: R.path(['documentNumber'])(idData) ?? '',
dateOfBirth: dateOfBirth: tryFormatDate(rawDob),
(rawDob &&
format('yyyy-MM-dd')(parse(new Date(), 'yyyyMMdd', rawDob))) ??
'',
gender: R.path(['gender'])(idData) ?? '', gender: R.path(['gender'])(idData) ?? '',
country: R.path(['country'])(idData) ?? '', country: R.path(['country'])(idData) ?? '',
expirationDate: expirationDate: tryFormatDate(rawExpirationDate)
(rawExpirationDate &&
format('yyyy-MM-dd')(
parse(new Date(), 'yyyyMMdd', rawExpirationDate)
)) ??
''
}, },
usSsn: { usSsn: {
usSsn: customer.usSsn ?? '' usSsn: customer.usSsn ?? ''

View file

@ -528,13 +528,22 @@ const requirementElements = {
} }
} }
const tryFormatDate = rawDate => {
try {
return (
(rawDate &&
format('yyyy-MM-dd')(parse(new Date(), 'yyyyMMdd', rawDate))) ??
''
)
} catch (err) {
return ''
}
}
const formatDates = values => { const formatDates = values => {
R.map( R.forEach(elem => {
elem => values[elem] = tryFormatDate(values[elem])
(values[elem] = format('yyyyMMdd')( })(['dateOfBirth', 'expirationDate'])
parse(new Date(), 'yyyy-MM-dd', values[elem])
))
)(['dateOfBirth', 'expirationDate'])
return values return values
} }
@ -579,6 +588,7 @@ export {
customerDataElements, customerDataElements,
customerDataSchemas, customerDataSchemas,
formatDates, formatDates,
tryFormatDate,
REQUIREMENT, REQUIREMENT,
CUSTOM, CUSTOM,
ID_CARD_DATA ID_CARD_DATA