Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/components/TestDrive/Modal/EmployeeTestDriveModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function EmployeeTestDriveModal() {
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
const personalPolicy = usePersonalPolicy();
const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: true});
const [draftTransactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_DRAFT, {canBeMissing: true});
const hasOnlyPersonalPolicies = useMemo(() => hasOnlyPersonalPoliciesUtil(allPolicies), [allPolicies]);

const onBossEmailChange = useCallback((value: string) => {
Expand Down Expand Up @@ -85,6 +86,7 @@ function EmployeeTestDriveModal() {
currentDate,
currentUserPersonalDetails,
hasOnlyPersonalPolicies,
draftTransactions,
});

setMoneyRequestReceipt(transactionID, source, filename, true, CONST.TEST_RECEIPT.FILE_TYPE, false, true);
Expand Down
4 changes: 3 additions & 1 deletion src/libs/actions/IOU/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@
lastSelectedDistanceRates?: OnyxEntry<OnyxTypes.LastSelectedDistanceRates>;
currentUserPersonalDetails: CurrentUserPersonalDetails;
hasOnlyPersonalPolicies: boolean;
draftTransactions: OnyxCollection<OnyxTypes.Transaction>;
};

type MoneyRequestInformation = {
Expand Down Expand Up @@ -760,7 +761,7 @@
};

let allPersonalDetails: OnyxTypes.PersonalDetailsList = {};
Onyx.connect({

Check warning on line 764 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (value) => {
allPersonalDetails = value ?? {};
Expand Down Expand Up @@ -838,7 +839,7 @@
};

let allTransactions: NonNullable<OnyxCollection<OnyxTypes.Transaction>> = {};
Onyx.connect({

Check warning on line 842 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.TRANSACTION,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -852,7 +853,7 @@
});

let allTransactionDrafts: NonNullable<OnyxCollection<OnyxTypes.Transaction>> = {};
Onyx.connect({

Check warning on line 856 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.TRANSACTION_DRAFT,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -861,7 +862,7 @@
});

let allTransactionViolations: NonNullable<OnyxCollection<OnyxTypes.TransactionViolations>> = {};
Onyx.connect({

Check warning on line 865 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -875,7 +876,7 @@
});

let allPolicyTags: OnyxCollection<OnyxTypes.PolicyTagLists> = {};
Onyx.connect({

Check warning on line 879 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.POLICY_TAGS,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -888,7 +889,7 @@
});

let allReports: OnyxCollection<OnyxTypes.Report>;
Onyx.connect({

Check warning on line 892 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -897,7 +898,7 @@
});

let allReportNameValuePairs: OnyxCollection<OnyxTypes.ReportNameValuePairs>;
Onyx.connect({

Check warning on line 901 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -907,7 +908,7 @@

let userAccountID = -1;
let currentUserEmail = '';
Onyx.connect({

Check warning on line 911 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.SESSION,
callback: (value) => {
currentUserEmail = value?.email ?? '';
Expand All @@ -916,7 +917,7 @@
});

let deprecatedCurrentUserPersonalDetails: OnyxEntry<OnyxTypes.PersonalDetails>;
Onyx.connect({

Check warning on line 920 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (value) => {
deprecatedCurrentUserPersonalDetails = value?.[userAccountID] ?? undefined;
Expand All @@ -924,7 +925,7 @@
});

let allReportActions: OnyxCollection<OnyxTypes.ReportActions>;
Onyx.connect({

Check warning on line 928 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
waitForCollectionCallback: true,
callback: (actions) => {
Expand Down Expand Up @@ -1095,6 +1096,7 @@
lastSelectedDistanceRates,
currentUserPersonalDetails,
hasOnlyPersonalPolicies,
draftTransactions,
}: InitMoneyRequestParams) {
// Generate a brand new transactionID
const newTransactionID = CONST.IOU.OPTIMISTIC_TRANSACTION_ID;
Expand All @@ -1105,7 +1107,7 @@
const created = currentDate || format(new Date(), 'yyyy-MM-dd');

// We remove draft transactions created during multi scanning if there are some
removeDraftTransactions(true);
removeDraftTransactions(true, draftTransactions);

// in case we have to re-init money request, but the IOU request type is the same with the old draft transaction,
// we should keep most of the existing data by using the ONYX MERGE operation
Expand Down
2 changes: 2 additions & 0 deletions src/pages/Search/SearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ function SearchPage({route}: SearchPageProps) {
const [activePolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`, {canBeMissing: true});
const [personalPolicyID] = useOnyx(ONYXKEYS.PERSONAL_POLICY_ID, {canBeMissing: true});
const [personalPolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${personalPolicyID}`, {canBeMissing: true});
const [draftTransactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_DRAFT, {canBeMissing: true});
const [bankAccountList] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST, {canBeMissing: true});
const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: true});
const [integrationsExportTemplates] = useOnyx(ONYXKEYS.NVP_INTEGRATION_SERVER_EXPORT_TEMPLATES, {canBeMissing: true});
Expand Down Expand Up @@ -1038,6 +1039,7 @@ function SearchPage({route}: SearchPageProps) {
currentDate,
currentUserPersonalDetails,
hasOnlyPersonalPolicies,
draftTransactions,
});

const newReceiptFiles: ReceiptFile[] = [];
Expand Down
4 changes: 4 additions & 0 deletions src/pages/Share/SubmitDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function SubmitDetailsPage({
const fileName = shouldUsePreValidatedFile ? getFileName(validFilesToUpload?.uri ?? CONST.ATTACHMENT_IMAGE_DEFAULT_NAME) : getFileName(currentAttachment?.content ?? '');
const fileType = shouldUsePreValidatedFile ? (validFilesToUpload?.type ?? CONST.RECEIPT_ALLOWED_FILE_TYPES.JPEG) : (currentAttachment?.mimeType ?? '');
const [hasOnlyPersonalPolicies = false] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: true, selector: hasOnlyPersonalPoliciesUtil});
const [draftTransactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_DRAFT, {canBeMissing: true});

useEffect(() => {
if (!errorTitle || !errorMessage) {
Expand All @@ -107,7 +108,10 @@ function SubmitDetailsPage({
currentDate,
currentUserPersonalDetails,
hasOnlyPersonalPolicies,
draftTransactions,
});
// The draftTransactions can be changed if users update the expense, so we don't want to re-init the money request
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [reportOrAccountID, policy, personalPolicy, report, parentReport, currentDate, currentUserPersonalDetails, hasOnlyPersonalPolicies]);

const selectedParticipants = unknownUserDetails ? [unknownUserDetails] : getMoneyRequestParticipantsFromReport(report, currentUserPersonalDetails.accountID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function useAttachmentUploadValidation({
const [policyCategories] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policy?.id}`, {canBeMissing: true});
const personalPolicy = usePersonalPolicy();
const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: true});
const [draftTransactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_DRAFT, {canBeMissing: true});
const hasOnlyPersonalPolicies = useMemo(() => hasOnlyPersonalPoliciesUtil(allPolicies), [allPolicies]);

const reportAttachmentsContext = useContext(AttachmentModalContext);
Expand Down Expand Up @@ -101,6 +102,7 @@ function useAttachmentUploadValidation({
currentDate,
currentUserPersonalDetails,
hasOnlyPersonalPolicies,
draftTransactions,
});

for (const [index, file] of files.entries()) {
Expand Down
3 changes: 3 additions & 0 deletions src/pages/iou/request/DistanceRequestStartPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function DistanceRequestStartPage({
const [lastDistanceExpenseType] = useOnyx(ONYXKEYS.NVP_LAST_DISTANCE_EXPENSE_TYPE, {canBeMissing: true});
const isLoadingSelectedTab = isLoadingOnyxValue(selectedTabResult);
const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${getNonEmptyStringOnyxID(route?.params.transactionID)}`, {canBeMissing: true});
const [draftTransactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_DRAFT, {canBeMissing: true});
const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: false});
const [lastSelectedDistanceRates] = useOnyx(ONYXKEYS.NVP_LAST_SELECTED_DISTANCE_RATES, {canBeMissing: true});
const [currentDate] = useOnyx(ONYXKEYS.CURRENT_DATE, {canBeMissing: true});
Expand Down Expand Up @@ -120,6 +121,7 @@ function DistanceRequestStartPage({
lastSelectedDistanceRates,
currentUserPersonalDetails,
hasOnlyPersonalPolicies,
draftTransactions,
});
},
[
Expand All @@ -134,6 +136,7 @@ function DistanceRequestStartPage({
lastSelectedDistanceRates,
currentUserPersonalDetails,
hasOnlyPersonalPolicies,
draftTransactions,
],
);

Expand Down
3 changes: 3 additions & 0 deletions src/pages/iou/request/IOURequestStartPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ function IOURequestStartPage({
selector: transactionDraftValuesSelector,
canBeMissing: true,
});
const [draftTransactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_DRAFT, {canBeMissing: true});
const [isMultiScanEnabled, setIsMultiScanEnabled] = useState((optimisticTransactions ?? []).length > 1);
const [currentDate] = useOnyx(ONYXKEYS.CURRENT_DATE, {canBeMissing: true});
const {isOffline} = useNetwork();
Expand Down Expand Up @@ -196,6 +197,7 @@ function IOURequestStartPage({
lastSelectedDistanceRates,
currentUserPersonalDetails,
hasOnlyPersonalPolicies,
draftTransactions,
});
},
[
Expand All @@ -211,6 +213,7 @@ function IOURequestStartPage({
lastSelectedDistanceRates,
currentUserPersonalDetails,
hasOnlyPersonalPolicies,
draftTransactions,
],
);

Expand Down
131 changes: 131 additions & 0 deletions tests/actions/IOUTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8091,6 +8091,7 @@ describe('actions/IOU', () => {
currentDate,
currentUserPersonalDetails,
hasOnlyPersonalPolicies: false,
draftTransactions: undefined,
});
})
.then(async () => {
Expand All @@ -8113,6 +8114,7 @@ describe('actions/IOU', () => {
currentDate,
currentUserPersonalDetails,
hasOnlyPersonalPolicies: false,
draftTransactions: undefined,
});
})
.then(async () => {
Expand All @@ -8136,6 +8138,7 @@ describe('actions/IOU', () => {
currentDate,
currentUserPersonalDetails,
hasOnlyPersonalPolicies: false,
draftTransactions: undefined,
});
})
.then(async () => {
Expand All @@ -8145,6 +8148,134 @@ describe('actions/IOU', () => {
});
});
});

it('should remove non-optimistic draft transactions when draftTransactions is provided', async () => {
const otherDraftTransactionID = '123456';
const otherDraftTransaction: Transaction = {
...createRandomTransaction(1),
transactionID: otherDraftTransactionID,
};

// Set up an additional draft transaction
await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${otherDraftTransactionID}`, otherDraftTransaction);

const draftTransactions = {
[`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${otherDraftTransactionID}`]: otherDraftTransaction,
};

await waitForBatchedUpdates()
.then(() => {
initMoneyRequest({
reportID: fakeReport.reportID,
policy: fakePolicy,
personalPolicy: fakePersonalPolicy,
isFromGlobalCreate: true,
newIouRequestType: CONST.IOU.REQUEST_TYPE.MANUAL,
report: fakeReport,
parentReport: fakeParentReport,
currentDate,
currentUserPersonalDetails,
hasOnlyPersonalPolicies: false,
draftTransactions,
});
})
.then(async () => {
// The other draft transaction should be removed (Onyx returns undefined for removed keys)
expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${otherDraftTransactionID}`)).toBeUndefined();
// The optimistic transaction should be created
expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${CONST.IOU.OPTIMISTIC_TRANSACTION_ID}`)).toStrictEqual(transactionResult);
});
});

it('should preserve optimistic transaction in draftTransactions while removing others', async () => {
const otherDraftTransactionID = '789012';
const otherDraftTransaction: Transaction = {
...createRandomTransaction(2),
transactionID: otherDraftTransactionID,
};
const existingOptimisticTransaction: Transaction = {
...createRandomTransaction(3),
transactionID: CONST.IOU.OPTIMISTIC_TRANSACTION_ID,
};

// Set up both draft transactions
await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${otherDraftTransactionID}`, otherDraftTransaction);
await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${CONST.IOU.OPTIMISTIC_TRANSACTION_ID}`, existingOptimisticTransaction);

const draftTransactions = {
[`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${otherDraftTransactionID}`]: otherDraftTransaction,
[`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${CONST.IOU.OPTIMISTIC_TRANSACTION_ID}`]: existingOptimisticTransaction,
};

await waitForBatchedUpdates()
.then(() => {
initMoneyRequest({
reportID: fakeReport.reportID,
policy: fakePolicy,
personalPolicy: fakePersonalPolicy,
isFromGlobalCreate: true,
newIouRequestType: CONST.IOU.REQUEST_TYPE.MANUAL,
report: fakeReport,
parentReport: fakeParentReport,
currentDate,
currentUserPersonalDetails,
hasOnlyPersonalPolicies: false,
draftTransactions,
});
})
.then(async () => {
// The other draft transaction should be removed (Onyx returns undefined for removed keys)
expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${otherDraftTransactionID}`)).toBeUndefined();
// The optimistic transaction should be updated with the new transaction result (not removed)
expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${CONST.IOU.OPTIMISTIC_TRANSACTION_ID}`)).toStrictEqual(transactionResult);
});
});

it('should remove multiple draft transactions when draftTransactions contains several entries', async () => {
const draftTransactionID1 = '111111';
const draftTransactionID2 = '222222';
const draftTransaction1: Transaction = {
...createRandomTransaction(4),
transactionID: draftTransactionID1,
};
const draftTransaction2: Transaction = {
...createRandomTransaction(5),
transactionID: draftTransactionID2,
};

// Set up multiple draft transactions
await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${draftTransactionID1}`, draftTransaction1);
await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${draftTransactionID2}`, draftTransaction2);

const draftTransactions = {
[`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${draftTransactionID1}`]: draftTransaction1,
[`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${draftTransactionID2}`]: draftTransaction2,
};

await waitForBatchedUpdates()
.then(() => {
initMoneyRequest({
reportID: fakeReport.reportID,
policy: fakePolicy,
personalPolicy: fakePersonalPolicy,
isFromGlobalCreate: true,
newIouRequestType: CONST.IOU.REQUEST_TYPE.MANUAL,
report: fakeReport,
parentReport: fakeParentReport,
currentDate,
currentUserPersonalDetails,
hasOnlyPersonalPolicies: false,
draftTransactions,
});
})
.then(async () => {
// Both draft transactions should be removed (Onyx returns undefined for removed keys)
expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${draftTransactionID1}`)).toBeUndefined();
expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${draftTransactionID2}`)).toBeUndefined();
// The optimistic transaction should be created
expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${CONST.IOU.OPTIMISTIC_TRANSACTION_ID}`)).toStrictEqual(transactionResult);
});
});
});

describe('updateMoneyRequestAmountAndCurrency', () => {
Expand Down
Loading