@@ -11,7 +11,10 @@ import {
1111 isValidTime ,
1212 transformNumber ,
1313} from '../../utils/validationUtils' ;
14- import { VALIDATION_MESSAGE_KEYS } from '../app/i18n/constants' ;
14+ import {
15+ REGISTRATION_ACCOUNT_VALIDATION_MESSAGE_KEYS ,
16+ VALIDATION_MESSAGE_KEYS ,
17+ } from '../app/i18n/constants' ;
1518import { ACCOUNT_TEXT_FIELD_MAX_LENGTH } from '../organization/constants' ;
1619import { PriceGroupOption } from '../priceGroup/types' ;
1720import {
@@ -62,6 +65,37 @@ const priceGroupsSchema = (
6265 : schema ;
6366} ;
6467
68+ const hasNonEmptyValue = ( value ?: string | null ) : boolean =>
69+ Boolean ( value ?. trim ( ) ) ;
70+
71+ // Prevent submitting both internal order and profit center at the same time.
72+ const internalOrderOrProfitCenterTest = {
73+ name : 'internalOrderOrProfitCenter' ,
74+ message :
75+ REGISTRATION_ACCOUNT_VALIDATION_MESSAGE_KEYS . INTERNAL_ORDER_OR_PROFIT_CENTER ,
76+ test ( this : Yup . TestContext ) {
77+ const { internalOrder, profitCenter } = this . parent ;
78+ return ! ( hasNonEmptyValue ( internalOrder ) && hasNonEmptyValue ( profitCenter ) ) ;
79+ } ,
80+ } ;
81+
82+ // If neither internal order nor profit center is given, project must be provided.
83+ const projectRequiredWhenNoInternalOrderOrProfitCenterTest = {
84+ name : 'projectRequiredWhenNoInternalOrderOrProfitCenter' ,
85+ message : REGISTRATION_ACCOUNT_VALIDATION_MESSAGE_KEYS . PROJECT_REQUIRED ,
86+ test ( this : Yup . TestContext ) {
87+ const { internalOrder, profitCenter, project } = this . parent ;
88+ return (
89+ hasNonEmptyValue ( internalOrder ) ||
90+ hasNonEmptyValue ( profitCenter ) ||
91+ hasNonEmptyValue ( project )
92+ ) ;
93+ } ,
94+ } ;
95+
96+ // Registration account business rules:
97+ // 1) internal order and profit center are mutually exclusive
98+ // 2) project is required when both internal order and profit center are empty
6599const registrationAccountSchema = Yup . object ( ) . shape ( {
66100 [ REGISTRATION_ACCOUNT_FIELDS . ACCOUNT ] : Yup . string ( ) . required (
67101 VALIDATION_MESSAGE_KEYS . STRING_REQUIRED
@@ -88,18 +122,24 @@ const registrationAccountSchema = Yup.object().shape({
88122 ] ,
89123 createStringMaxErrorMessage
90124 ) ,
91- [ REGISTRATION_ACCOUNT_FIELDS . INTERNAL_ORDER ] : Yup . string ( ) . max (
92- ACCOUNT_TEXT_FIELD_MAX_LENGTH [ REGISTRATION_ACCOUNT_FIELDS . INTERNAL_ORDER ] ,
93- createStringMaxErrorMessage
94- ) ,
95- [ REGISTRATION_ACCOUNT_FIELDS . PROFIT_CENTER ] : Yup . string ( ) . max (
96- ACCOUNT_TEXT_FIELD_MAX_LENGTH [ REGISTRATION_ACCOUNT_FIELDS . PROFIT_CENTER ] ,
97- createStringMaxErrorMessage
98- ) ,
99- [ REGISTRATION_ACCOUNT_FIELDS . PROJECT ] : Yup . string ( ) . max (
100- ACCOUNT_TEXT_FIELD_MAX_LENGTH [ REGISTRATION_ACCOUNT_FIELDS . PROJECT ] ,
101- createStringMaxErrorMessage
102- ) ,
125+ [ REGISTRATION_ACCOUNT_FIELDS . INTERNAL_ORDER ] : Yup . string ( )
126+ . max (
127+ ACCOUNT_TEXT_FIELD_MAX_LENGTH [ REGISTRATION_ACCOUNT_FIELDS . INTERNAL_ORDER ] ,
128+ createStringMaxErrorMessage
129+ )
130+ . test ( internalOrderOrProfitCenterTest ) ,
131+ [ REGISTRATION_ACCOUNT_FIELDS . PROFIT_CENTER ] : Yup . string ( )
132+ . max (
133+ ACCOUNT_TEXT_FIELD_MAX_LENGTH [ REGISTRATION_ACCOUNT_FIELDS . PROFIT_CENTER ] ,
134+ createStringMaxErrorMessage
135+ )
136+ . test ( internalOrderOrProfitCenterTest ) ,
137+ [ REGISTRATION_ACCOUNT_FIELDS . PROJECT ] : Yup . string ( )
138+ . max (
139+ ACCOUNT_TEXT_FIELD_MAX_LENGTH [ REGISTRATION_ACCOUNT_FIELDS . PROJECT ] ,
140+ createStringMaxErrorMessage
141+ )
142+ . test ( projectRequiredWhenNoInternalOrderOrProfitCenterTest ) ,
103143 [ REGISTRATION_ACCOUNT_FIELDS . OPERATION_AREA ] : Yup . string ( ) . max (
104144 ACCOUNT_TEXT_FIELD_MAX_LENGTH [ REGISTRATION_ACCOUNT_FIELDS . OPERATION_AREA ] ,
105145 createStringMaxErrorMessage
0 commit comments