Skip to content

Commit c0ccf00

Browse files
committed
fix: adjust error clearing logic
1 parent 46d773e commit c0ccf00

2 files changed

Lines changed: 46 additions & 25 deletions

File tree

src/libs/actions/TravelInvoicing.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,29 @@ function openPolicyTravelPage(policyID: string, workspaceAccountID: number) {
5858
function 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
*/
128146
function 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
*/
220238
function 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

tests/unit/TravelInvoicingTest.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
// We need to import API because it is used in the tests
99
// eslint-disable-next-line no-restricted-syntax
1010
import * as API from '@libs/API';
11+
import {getTravelInvoicingCardSettingsKey} from '@libs/TravelInvoicingUtils';
1112
import CONST from '@src/CONST';
12-
import ONYXKEYS from '@src/ONYXKEYS';
1313

1414
describe('TravelInvoicing', () => {
1515
let spyAPIWrite: jest.SpyInstance;
@@ -29,7 +29,7 @@ describe('TravelInvoicing', () => {
2929
const workspaceAccountID = 456;
3030
const settlementBankAccountID = 789;
3131
const previousPaymentBankAccountID = 111;
32-
const cardSettingsKey = `${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}_${CONST.TRAVEL.PROGRAM_TRAVEL_US}`;
32+
const cardSettingsKey = getTravelInvoicingCardSettingsKey(workspaceAccountID);
3333

3434
setTravelInvoicingSettlementAccount(policyID, workspaceAccountID, settlementBankAccountID, previousPaymentBankAccountID);
3535

@@ -95,33 +95,38 @@ describe('TravelInvoicing', () => {
9595
it('clearTravelInvoicingSettlementAccountErrors clears errors and pendingFields', () => {
9696
const workspaceAccountID = 456;
9797
const restoredAccountID = 111;
98-
const cardSettingsKey = `${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}_${CONST.TRAVEL.PROGRAM_TRAVEL_US}`;
98+
const cardSettingsKey = getTravelInvoicingCardSettingsKey(workspaceAccountID);
9999

100100
clearTravelInvoicingSettlementAccountErrors(workspaceAccountID, restoredAccountID);
101101

102102
expect(spyOnyxMerge).toHaveBeenCalledWith(cardSettingsKey, {
103-
errors: null,
104-
pendingAction: null,
105103
paymentBankAccountID: restoredAccountID,
106104
previousPaymentBankAccountID: null,
105+
pendingFields: {
106+
paymentBankAccountID: null,
107+
},
108+
errorFields: {
109+
paymentBankAccountID: null,
110+
},
107111
});
108112
});
109113

110114
it('clearTravelInvoicingSettlementFrequencyErrors clears errors', () => {
111115
const workspaceAccountID = 456;
112-
const cardSettingsKey = `${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}_${CONST.TRAVEL.PROGRAM_TRAVEL_US}`;
116+
const cardSettingsKey = getTravelInvoicingCardSettingsKey(workspaceAccountID);
113117

114118
const monthlySettlementDate = new Date('2026-01-01');
115119
clearTravelInvoicingSettlementFrequencyErrors(workspaceAccountID, monthlySettlementDate);
116120

117121
expect(spyOnyxMerge).toHaveBeenCalledWith(cardSettingsKey, {
118-
errors: null,
122+
monthlySettlementDate: monthlySettlementDate ?? null,
123+
previousMonthlySettlementDate: null,
124+
pendingFields: {
125+
monthlySettlementDate: null,
126+
},
119127
errorFields: {
120128
monthlySettlementDate: null,
121129
},
122-
pendingAction: null,
123-
monthlySettlementDate: monthlySettlementDate ?? null,
124-
previousMonthlySettlementDate: null,
125130
});
126131
});
127132

@@ -130,7 +135,7 @@ describe('TravelInvoicing', () => {
130135
const workspaceAccountID = 456;
131136
const frequency = CONST.EXPENSIFY_CARD.FREQUENCY_SETTING.MONTHLY;
132137
const currentMonthlySettlementDate = new Date('2024-01-01');
133-
const cardSettingsKey = `${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}_${CONST.TRAVEL.PROGRAM_TRAVEL_US}`;
138+
const cardSettingsKey = getTravelInvoicingCardSettingsKey(workspaceAccountID);
134139

135140
// Set fake time to ensure deterministic optimistic data
136141
const mockDate = new Date('2024-05-20');
@@ -156,7 +161,6 @@ describe('TravelInvoicing', () => {
156161
pendingFields: expect.objectContaining({
157162
monthlySettlementDate: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
158163
}),
159-
errors: null,
160164
errorFields: {
161165
monthlySettlementDate: null,
162166
},
@@ -172,7 +176,6 @@ describe('TravelInvoicing', () => {
172176
pendingFields: expect.objectContaining({
173177
monthlySettlementDate: null,
174178
}),
175-
errors: null,
176179
errorFields: {
177180
monthlySettlementDate: null,
178181
},
@@ -188,7 +191,6 @@ describe('TravelInvoicing', () => {
188191
pendingFields: expect.objectContaining({
189192
monthlySettlementDate: null,
190193
}),
191-
errors: null,
192194
errorFields: {
193195
monthlySettlementDate: expect.anything() as unknown,
194196
},

0 commit comments

Comments
 (0)