@@ -58,13 +58,29 @@ function openPolicyTravelPage(policyID: string, workspaceAccountID: number) {
5858function setTravelInvoicingSettlementAccount ( policyID : string , workspaceAccountID : number , settlementBankAccountID : number , previousPaymentBankAccountID ?: number ) {
5959 const cardSettingsKey = getTravelInvoicingCardSettingsKey ( workspaceAccountID ) ;
6060
61+ // Determine if we need to set the default frequency:
62+ // - When enabling for the first time (no previous account): default to monthly
63+ // - When disabling (zero bank account): clear the frequency
64+ // - When changing accounts (previous account exists): don't touch frequency (undefined = no change)
65+ const isFirstEnable = settlementBankAccountID !== 0 && ! previousPaymentBankAccountID ;
66+ const isDisabling = settlementBankAccountID === 0 ;
67+
68+ let monthlySettlementDate : Date | null | undefined ;
69+ if ( isFirstEnable ) {
70+ monthlySettlementDate = new Date ( ) ;
71+ } else if ( isDisabling ) {
72+ monthlySettlementDate = null ;
73+ }
74+ // Otherwise leave undefined - Onyx.merge will not overwrite existing value
75+
6176 const optimisticData : Array < OnyxUpdate < typeof ONYXKEYS . COLLECTION . PRIVATE_EXPENSIFY_CARD_SETTINGS > > = [
6277 {
6378 onyxMethod : Onyx . METHOD . MERGE ,
6479 key : cardSettingsKey ,
6580 value : {
6681 paymentBankAccountID : settlementBankAccountID ,
6782 previousPaymentBankAccountID,
83+ monthlySettlementDate,
6884 isLoading : true ,
6985 pendingFields : {
7086 paymentBankAccountID : CONST . RED_BRICK_ROAD_PENDING_ACTION . UPDATE ,
@@ -83,6 +99,7 @@ function setTravelInvoicingSettlementAccount(policyID: string, workspaceAccountI
8399 value : {
84100 paymentBankAccountID : settlementBankAccountID ,
85101 previousPaymentBankAccountID : null ,
102+ monthlySettlementDate,
86103 isLoading : false ,
87104 pendingFields : {
88105 paymentBankAccountID : null ,
@@ -102,6 +119,7 @@ function setTravelInvoicingSettlementAccount(policyID: string, workspaceAccountI
102119 // Keep the attempted value visible (grayed out) until error is dismissed
103120 paymentBankAccountID : settlementBankAccountID ,
104121 previousPaymentBankAccountID,
122+ monthlySettlementDate,
105123 isLoading : false ,
106124 pendingFields : {
107125 paymentBankAccountID : CONST . RED_BRICK_ROAD_PENDING_ACTION . UPDATE ,
@@ -127,10 +145,14 @@ function setTravelInvoicingSettlementAccount(policyID: string, workspaceAccountI
127145 */
128146function clearTravelInvoicingSettlementAccountErrors ( workspaceAccountID : number , paymentBankAccountID : number | null ) {
129147 Onyx . merge ( getTravelInvoicingCardSettingsKey ( workspaceAccountID ) , {
130- errors : null ,
131- pendingAction : null ,
132148 paymentBankAccountID,
133149 previousPaymentBankAccountID : null ,
150+ pendingFields : {
151+ paymentBankAccountID : null ,
152+ } ,
153+ errorFields : {
154+ paymentBankAccountID : null ,
155+ } ,
134156 } ) ;
135157}
136158
@@ -145,8 +167,7 @@ function updateTravelInvoiceSettlementFrequency(
145167 frequency : ValueOf < typeof CONST . EXPENSIFY_CARD . FREQUENCY_SETTING > ,
146168 currentMonthlySettlementDate ?: Date ,
147169) {
148- const cardSettingsKey =
149- `${ ONYXKEYS . COLLECTION . PRIVATE_EXPENSIFY_CARD_SETTINGS } ${ workspaceAccountID } _${ CONST . TRAVEL . PROGRAM_TRAVEL_US } ` as `${typeof ONYXKEYS . COLLECTION . PRIVATE_EXPENSIFY_CARD_SETTINGS } ${string } `;
170+ const cardSettingsKey = getTravelInvoicingCardSettingsKey ( workspaceAccountID ) ;
150171
151172 // If Monthly, set date (optimistically today). If Daily, set null.
152173 const monthlySettlementDate = frequency === CONST . EXPENSIFY_CARD . FREQUENCY_SETTING . MONTHLY ? new Date ( ) : null ;
@@ -161,7 +182,6 @@ function updateTravelInvoiceSettlementFrequency(
161182 pendingFields : {
162183 monthlySettlementDate : CONST . RED_BRICK_ROAD_PENDING_ACTION . UPDATE ,
163184 } ,
164- errors : null ,
165185 errorFields : {
166186 monthlySettlementDate : null ,
167187 } ,
@@ -179,7 +199,6 @@ function updateTravelInvoiceSettlementFrequency(
179199 pendingFields : {
180200 monthlySettlementDate : null ,
181201 } ,
182- errors : null ,
183202 errorFields : {
184203 monthlySettlementDate : null ,
185204 } ,
@@ -197,7 +216,6 @@ function updateTravelInvoiceSettlementFrequency(
197216 pendingFields : {
198217 monthlySettlementDate : null ,
199218 } ,
200- errors : null ,
201219 errorFields : {
202220 monthlySettlementDate : ErrorUtils . getMicroSecondOnyxErrorWithTranslationKey ( 'common.genericErrorMessage' ) ,
203221 } ,
@@ -219,13 +237,14 @@ function updateTravelInvoiceSettlementFrequency(
219237 */
220238function clearTravelInvoicingSettlementFrequencyErrors ( workspaceAccountID : number , monthlySettlementDate : Date | null | undefined ) {
221239 Onyx . merge ( getTravelInvoicingCardSettingsKey ( workspaceAccountID ) , {
222- errors : null ,
240+ monthlySettlementDate : monthlySettlementDate ?? null ,
241+ previousMonthlySettlementDate : null ,
242+ pendingFields : {
243+ monthlySettlementDate : null ,
244+ } ,
223245 errorFields : {
224246 monthlySettlementDate : null ,
225247 } ,
226- pendingAction : null ,
227- monthlySettlementDate : monthlySettlementDate ?? null ,
228- previousMonthlySettlementDate : null ,
229248 } ) ;
230249}
231250
0 commit comments