Skip to content

Commit 545b50d

Browse files
authored
Merge pull request Expensify#88608 from dukenv0307/fix/66407-part-9
refactor updateMoneyRequestDate and updateMoneyRequestBillable to use isOffline from useOnyx
2 parents 3e5ef59 + 7f8db51 commit 545b50d

4 files changed

Lines changed: 165 additions & 0 deletions

File tree

src/components/ReportActionItem/MoneyRequestView.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ function MoneyRequestView({
514514
currentUserEmailParam,
515515
isASAPSubmitBetaEnabled,
516516
parentReportNextStep,
517+
isOffline,
517518
});
518519
};
519520

src/libs/actions/IOU/UpdateMoneyRequest.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ type UpdateMoneyRequestDateParams = {
7171
currentUserEmailParam: string;
7272
isASAPSubmitBetaEnabled: boolean;
7373
parentReportNextStep: OnyxEntry<OnyxTypes.ReportNextStepDeprecated>;
74+
isOffline: boolean;
7475
};
7576

7677
/** Updates the created date of an expense */
@@ -88,6 +89,7 @@ function updateMoneyRequestDate({
8889
currentUserEmailParam,
8990
isASAPSubmitBetaEnabled,
9091
parentReportNextStep,
92+
isOffline,
9193
}: UpdateMoneyRequestDateParams) {
9294
const transactionChanges: TransactionChanges = {
9395
created: value,
@@ -112,6 +114,7 @@ function updateMoneyRequestDate({
112114
currentUserEmailParam,
113115
isASAPSubmitBetaEnabled,
114116
iouReportNextStep: parentReportNextStep,
117+
isOffline,
115118
});
116119
removeTransactionFromDuplicateTransactionViolation(data.onyxData, transactionID, transactions, transactionViolations);
117120
}
@@ -132,6 +135,7 @@ function updateMoneyRequestBillable({
132135
currentUserEmailParam,
133136
isASAPSubmitBetaEnabled,
134137
parentReportNextStep,
138+
isOffline,
135139
}: {
136140
transactionID: string | undefined;
137141
transactionThreadReport: OnyxEntry<OnyxTypes.Report>;
@@ -144,6 +148,7 @@ function updateMoneyRequestBillable({
144148
currentUserEmailParam: string;
145149
isASAPSubmitBetaEnabled: boolean;
146150
parentReportNextStep: OnyxEntry<OnyxTypes.ReportNextStepDeprecated>;
151+
isOffline: boolean;
147152
}) {
148153
if (!transactionID || !transactionThreadReport?.reportID) {
149154
return;
@@ -166,6 +171,7 @@ function updateMoneyRequestBillable({
166171
currentUserEmailParam,
167172
isASAPSubmitBetaEnabled,
168173
iouReportNextStep: parentReportNextStep,
174+
isOffline,
169175
});
170176
API.write(WRITE_COMMANDS.UPDATE_MONEY_REQUEST_BILLABLE, params, onyxData);
171177
}

src/pages/iou/request/step/IOURequestStepDate.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {FormInputErrors, FormOnyxValues} from '@components/Form/types';
88
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
99
import useDuplicateTransactionsAndViolations from '@hooks/useDuplicateTransactionsAndViolations';
1010
import useLocalize from '@hooks/useLocalize';
11+
import useNetwork from '@hooks/useNetwork';
1112
import useOnyx from '@hooks/useOnyx';
1213
import usePermissions from '@hooks/usePermissions';
1314
import usePolicy from '@hooks/usePolicy';
@@ -59,6 +60,7 @@ function IOURequestStepDate({
5960
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
6061
const {isBetaEnabled} = usePermissions();
6162
const isASAPSubmitBetaEnabled = isBetaEnabled(CONST.BETAS.ASAP_SUBMIT);
63+
const {isOffline} = useNetwork();
6264
const isEditing = action === CONST.IOU.ACTION.EDIT;
6365
const isSplitBill = iouType === CONST.IOU.TYPE.SPLIT;
6466
const isSplitExpense = iouType === CONST.IOU.TYPE.SPLIT_EXPENSE;
@@ -108,6 +110,7 @@ function IOURequestStepDate({
108110
currentUserEmailParam: currentUserPersonalDetails.login ?? '',
109111
isASAPSubmitBetaEnabled,
110112
parentReportNextStep,
113+
isOffline,
111114
});
112115
}
113116

tests/actions/IOUTest/UpdateMoneyRequestTest.ts

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import type {OnyxEntry} from 'react-native-onyx';
55
import {
66
updateMoneyRequestAmountAndCurrency,
77
updateMoneyRequestAttendees,
8+
updateMoneyRequestBillable,
89
updateMoneyRequestCategory,
10+
updateMoneyRequestDate,
911
updateMoneyRequestDistance,
1012
updateMoneyRequestTag,
1113
} from '@libs/actions/IOU/UpdateMoneyRequest';
@@ -728,6 +730,159 @@ describe('actions/IOU/UpdateMoneyRequest', () => {
728730
});
729731
});
730732

733+
describe('updateMoneyRequestDate', () => {
734+
it('should update the transaction created date', async () => {
735+
// Given an expense transaction with an expense report
736+
const transactionID = 'txnDate1';
737+
const transactionThreadReportID = 'threadDate1';
738+
const parentReportID = 'parentDate1';
739+
const policyID = '10';
740+
const originalDate = '2026-01-01';
741+
const newDate = '2026-01-15';
742+
743+
const parentReport: Report = {
744+
...createRandomReport(1, undefined),
745+
reportID: parentReportID,
746+
type: CONST.REPORT.TYPE.EXPENSE,
747+
policyID,
748+
ownerAccountID: RORY_ACCOUNT_ID,
749+
};
750+
const transactionThreadReport: Report = {
751+
...createRandomReport(2, undefined),
752+
reportID: transactionThreadReportID,
753+
parentReportID,
754+
type: CONST.REPORT.TYPE.CHAT,
755+
};
756+
const fakeTransaction: Transaction = {
757+
...createRandomTransaction(3),
758+
transactionID,
759+
reportID: parentReportID,
760+
created: originalDate,
761+
};
762+
const fakePolicy: Policy = createRandomPolicy(Number(policyID));
763+
764+
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${parentReportID}`, parentReport);
765+
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`, transactionThreadReport);
766+
await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, fakeTransaction);
767+
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, fakePolicy);
768+
769+
// When updating the date
770+
updateMoneyRequestDate({
771+
transactionID,
772+
transactionThreadReport,
773+
parentReport,
774+
transactions: {[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]: fakeTransaction},
775+
transactionViolations: {},
776+
value: newDate,
777+
policy: fakePolicy,
778+
policyTags: {},
779+
policyCategories: {},
780+
currentUserAccountIDParam: RORY_ACCOUNT_ID,
781+
currentUserEmailParam: RORY_EMAIL,
782+
isASAPSubmitBetaEnabled: false,
783+
parentReportNextStep: undefined,
784+
isOffline: false,
785+
});
786+
787+
await waitForBatchedUpdates();
788+
789+
// Then the modified date on the transaction should be updated
790+
const transactionAfter = await getOnyxValue(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`);
791+
expect(transactionAfter?.modifiedCreated).toBe(newDate);
792+
});
793+
});
794+
795+
describe('updateMoneyRequestBillable', () => {
796+
it('should update the transaction billable field', async () => {
797+
// Given an expense transaction with billable = false
798+
const transactionID = 'txnBillable1';
799+
const transactionThreadReportID = 'threadBillable1';
800+
const parentReportID = 'parentBillable1';
801+
const policyID = '20';
802+
803+
const parentReport: Report = {
804+
...createRandomReport(1, undefined),
805+
reportID: parentReportID,
806+
type: CONST.REPORT.TYPE.EXPENSE,
807+
policyID,
808+
ownerAccountID: RORY_ACCOUNT_ID,
809+
};
810+
const transactionThreadReport: Report = {
811+
...createRandomReport(2, undefined),
812+
reportID: transactionThreadReportID,
813+
parentReportID,
814+
type: CONST.REPORT.TYPE.CHAT,
815+
};
816+
const fakeTransaction: Transaction = {
817+
...createRandomTransaction(3),
818+
transactionID,
819+
reportID: parentReportID,
820+
billable: false,
821+
};
822+
const fakePolicy: Policy = createRandomPolicy(Number(policyID));
823+
824+
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${parentReportID}`, parentReport);
825+
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`, transactionThreadReport);
826+
await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, fakeTransaction);
827+
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, fakePolicy);
828+
829+
// When updating billable to true
830+
updateMoneyRequestBillable({
831+
transactionID,
832+
transactionThreadReport,
833+
parentReport,
834+
value: true,
835+
policy: fakePolicy,
836+
policyTagList: {},
837+
policyCategories: {},
838+
currentUserAccountIDParam: RORY_ACCOUNT_ID,
839+
currentUserEmailParam: RORY_EMAIL,
840+
isASAPSubmitBetaEnabled: false,
841+
parentReportNextStep: undefined,
842+
isOffline: false,
843+
});
844+
845+
await waitForBatchedUpdates();
846+
847+
// Then the transaction billable field should be updated
848+
const transactionAfter = await getOnyxValue(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`);
849+
expect(transactionAfter?.billable).toBe(true);
850+
});
851+
852+
it('should not update anything if transactionID is missing', async () => {
853+
// Given a transaction
854+
const transactionID = 'txnBillableNoOp';
855+
const fakeTransaction: Transaction = {
856+
...createRandomTransaction(1),
857+
transactionID,
858+
billable: false,
859+
};
860+
await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, fakeTransaction);
861+
862+
// When updateMoneyRequestBillable is called with an undefined transactionID
863+
updateMoneyRequestBillable({
864+
transactionID: undefined,
865+
transactionThreadReport: {reportID: '1'},
866+
parentReport: undefined,
867+
value: true,
868+
policy: undefined,
869+
policyTagList: {},
870+
policyCategories: {},
871+
currentUserAccountIDParam: RORY_ACCOUNT_ID,
872+
currentUserEmailParam: RORY_EMAIL,
873+
isASAPSubmitBetaEnabled: false,
874+
parentReportNextStep: undefined,
875+
isOffline: false,
876+
});
877+
878+
await waitForBatchedUpdates();
879+
880+
// Then the original transaction billable value should be unchanged
881+
const transactionAfter = await getOnyxValue(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`);
882+
expect(transactionAfter?.billable).toBe(false);
883+
});
884+
});
885+
731886
describe('updateMoneyRequestDistance', () => {
732887
it('should update transaction with distance and waypoints', async () => {
733888
// Given a distance request transaction with valid waypoints and existing data

0 commit comments

Comments
 (0)