fix: triggers wizard allowing empty custom requirement field

fix: issue with custom requirement filtering when in presence of older triggers

refactor: pull up methods

fix: remove log
This commit is contained in:
Sérgio Salgado 2021-12-22 18:31:32 +00:00
parent 633e0656c1
commit 807c5bfc85
3 changed files with 67 additions and 25 deletions

View file

@ -41,8 +41,8 @@ const createTerms = terms => (terms.active && terms.text) ? ({
const buildTriggers = (allTriggers) => {
const normalTriggers = []
const customTriggers = _.filter(o => {
if (o.customInfoRequestId === '') normalTriggers.push(o)
return o.customInfoRequestId !== ''
if (o.customInfoRequestId === '' || _.isNil(o.customInfoRequestId)) normalTriggers.push(o)
return !_.isNil(o.customInfoRequestId)
}, allTriggers)
return _.flow([_.map(_.get('customInfoRequestId')), customRequestQueries.batchGetCustomInfoRequest])(customTriggers)

View file

@ -230,12 +230,18 @@ const Wizard = ({ onClose, save, error, currency, customInfoRequests }) => {
const triggerType = values?.triggerType
const containsType = R.contains(triggerType)
const isSuspend = values?.requirement?.requirement === 'suspend'
const isCustom = values?.requirement?.requirement === 'custom'
const hasRequirementError =
!!errors.requirement &&
!!touched.requirement?.suspensionDays &&
(!values.requirement?.suspensionDays ||
values.requirement?.suspensionDays < 0)
const hasRequirementError = requirements.hasRequirementError(
errors,
touched,
values
)
const hasCustomRequirementError = requirements.hasCustomRequirementError(
errors,
touched,
values
)
const hasAmountError =
!!errors.threshold &&
@ -258,7 +264,11 @@ const Wizard = ({ onClose, save, error, currency, customInfoRequests }) => {
)
return errors.threshold
if (isSuspend && hasRequirementError) return errors.requirement
if (
(isSuspend && hasRequirementError) ||
(isCustom && hasCustomRequirementError)
)
return errors.requirement
}
return (

View file

@ -477,21 +477,43 @@ const requirementSchema = Yup.object()
otherwise: Yup.number()
.nullable()
.transform(() => null)
}),
customInfoRequestId: Yup.string().when('requirement', {
is: value => value === 'custom',
then: Yup.string(),
otherwise: Yup.string()
.nullable()
.transform(() => '')
})
}).required()
})
.test(({ requirement }, context) => {
const requirementValidator = requirement =>
requirement.requirement === 'suspend'
? requirement.suspensionDays > 0
: true
const requirementValidator = (requirement, type) => {
switch (type) {
case 'suspend':
return requirement.requirement === type
? requirement.suspensionDays > 0
: true
case 'custom':
return requirement.requirement === type
? !R.isNil(requirement.customInfoRequestId)
: true
default:
return true
}
}
if (requirement && requirementValidator(requirement)) return
if (requirement && !requirementValidator(requirement, 'suspend'))
return context.createError({
path: 'requirement',
message: 'Suspension days must be greater than 0'
})
return context.createError({
path: 'requirement',
message: 'Suspension days must be greater than 0'
})
if (requirement && !requirementValidator(requirement, 'custom'))
return context.createError({
path: 'requirement',
message: 'You must select an item'
})
})
const requirementOptions = [
@ -506,6 +528,18 @@ const requirementOptions = [
{ display: 'Block', code: 'block' }
]
const hasRequirementError = (errors, touched, values) =>
!!errors.requirement &&
!!touched.requirement?.suspensionDays &&
(!values.requirement?.suspensionDays ||
values.requirement?.suspensionDays < 0)
const hasCustomRequirementError = (errors, touched, values) =>
!!errors.requirement &&
!!touched.requirement?.customInfoRequestId &&
(!values.requirement?.customInfoRequestId ||
!R.isNil(values.requirement?.customInfoRequestId))
const Requirement = ({ customInfoRequests }) => {
const classes = useStyles()
const {
@ -524,12 +558,6 @@ const Requirement = ({ customInfoRequests }) => {
display: it.customRequest.name
}))
const hasRequirementError =
!!errors.requirement &&
!!touched.requirement?.suspensionDays &&
(!values.requirement?.suspensionDays ||
values.requirement?.suspensionDays < 0)
const enableCustomRequirement = customInfoRequests?.length > 0
const customInfoOption = {
display: 'Custom information requirement',
@ -540,7 +568,9 @@ const Requirement = ({ customInfoRequests }) => {
: [...requirementOptions, { ...customInfoOption, disabled: true }]
const titleClass = {
[classes.error]:
(!!errors.requirement && !isSuspend) || (isSuspend && hasRequirementError)
(!!errors.requirement && !isSuspend && !isCustom) ||
(isSuspend && hasRequirementError(errors, touched, values)) ||
(isCustom && hasCustomRequirementError(errors, touched, values))
}
return (
@ -569,7 +599,7 @@ const Requirement = ({ customInfoRequests }) => {
label="Days"
size="lg"
name="requirement.suspensionDays"
error={hasRequirementError}
error={hasRequirementError(errors, touched, values)}
/>
)}
{isCustom && (
@ -592,6 +622,8 @@ const requirements = customInfoRequests => ({
options: requirementOptions,
Component: Requirement,
props: { customInfoRequests },
hasRequirementError: hasRequirementError,
hasCustomRequirementError: hasCustomRequirementError,
initialValues: {
requirement: {
requirement: '',