Skip to content
11 changes: 10 additions & 1 deletion src/libs/actions/IOU/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@
};

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

Check warning on line 760 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 @@ -859,7 +859,7 @@
};

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

Check warning on line 862 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 @@ -873,7 +873,7 @@
});

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

Check warning on line 876 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 @@ -882,7 +882,7 @@
});

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

Check warning on line 885 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 @@ -896,7 +896,7 @@
});

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

Check warning on line 899 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 @@ -905,7 +905,7 @@
});

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

Check warning on line 908 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 @@ -915,7 +915,7 @@

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

Check warning on line 918 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 @@ -924,7 +924,7 @@
});

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

Check warning on line 927 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 @@ -932,7 +932,7 @@
});

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

Check warning on line 935 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 All @@ -944,7 +944,7 @@
});

let personalDetailsList: OnyxEntry<OnyxTypes.PersonalDetailsList>;
Onyx.connect({

Check warning on line 947 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) => (personalDetailsList = value),
});
Expand Down Expand Up @@ -3241,8 +3241,17 @@
}

if (isSplitExpense && existingTransaction) {
const {convertedAmount: omittedConvertedAmount, ...existingTransactionWithoutConvertedAmount} = existingTransaction;
const {convertedAmount: originalConvertedAmount, ...existingTransactionWithoutConvertedAmount} = existingTransaction;
optimisticTransaction = fastMerge(existingTransactionWithoutConvertedAmount, optimisticTransaction, false);

// Calculate proportional convertedAmount for the split based on the original conversion rate
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- modifiedAmount can be empty string
const originalAmount = Number(existingTransaction.modifiedAmount) || existingTransaction.amount;
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- modifiedAmount can be empty string
const splitAmount = Number(optimisticTransaction.modifiedAmount) || optimisticTransaction.amount;
if (originalConvertedAmount && originalAmount && splitAmount) {
optimisticTransaction.convertedAmount = Math.round((originalConvertedAmount * splitAmount) / originalAmount);
Comment thread
TaduJR marked this conversation as resolved.
Resolved
}
}

// STEP 4: Build optimistic reportActions. We need:
Expand Down
151 changes: 151 additions & 0 deletions tests/actions/IOUTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3620,6 +3620,157 @@ describe('actions/IOU', () => {
// Then the description should be the same since it was not changed
expect(splitTransaction?.comment?.comment).toBe('<h1>test</h1>');
});

it('should calculate proportional convertedAmount for split transactions with foreign currency', async () => {
jest.setTimeout(10 * 1000);

// Given: An expense report with AED currency and a USD transaction with convertedAmount
const originalAmount = -1000;
const originalConvertedAmount = -3673;
const reportID = rand64();
const originalTransactionID = rand64();

const expenseReport: Report = {
reportID,
type: CONST.REPORT.TYPE.EXPENSE,
currency: 'AED',
ownerAccountID: RORY_ACCOUNT_ID,
total: originalAmount,
};

const originalTransaction = {
transactionID: originalTransactionID,
amount: originalAmount,
modifiedAmount: '', // Empty string - the edge case that was causing the bug
currency: 'USD',
modifiedCurrency: '',
convertedAmount: originalConvertedAmount,
created: DateUtils.getDBTime(),
merchant: 'Test Merchant',
reportID,
comment: {},
} as unknown as Transaction;

const transactionThread: Report = {
reportID: rand64(),
type: CONST.REPORT.TYPE.CHAT,
parentReportID: reportID,
parentReportActionID: rand64(),
};

const iouAction: ReportAction = {
...buildOptimisticIOUReportAction({
type: CONST.IOU.REPORT_ACTION_TYPE.CREATE,
amount: Math.abs(originalAmount),
currency: 'USD',
comment: '',
participants: [],
transactionID: originalTransactionID,
iouReportID: reportID,
}),
childReportID: transactionThread.reportID,
};

await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${expenseReport.reportID}`, expenseReport);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${transactionThread.reportID}`, transactionThread);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReport.reportID}`, {
[iouAction.reportActionID]: iouAction,
});
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${originalTransactionID}`, originalTransaction);

const splitExpenses: SplitExpense[] = [
{
transactionID: rand64(),
amount: -500,
created: DateUtils.getDBTime(),
merchant: 'Test Merchant',
},
{
transactionID: rand64(),
amount: -500,
created: DateUtils.getDBTime(),
merchant: 'Test Merchant',
},
];

let allTransactions: OnyxCollection<Transaction>;
let allReports: OnyxCollection<Report>;
let allReportNameValuePairs: OnyxCollection<ReportNameValuePairs>;
await getOnyxData({
key: ONYXKEYS.COLLECTION.TRANSACTION,
waitForCollectionCallback: true,
callback: (value) => {
allTransactions = value;
},
});
await getOnyxData({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
allReports = value;
},
});
await getOnyxData({
key: ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS,
waitForCollectionCallback: true,
callback: (value) => {
allReportNameValuePairs = value;
},
});

// When splitting the expense
updateSplitTransactionsFromSplitExpensesFlow({
allTransactionsList: allTransactions,
allReportsList: allReports,
allReportNameValuePairsList: allReportNameValuePairs,
transactionData: {
reportID,
originalTransactionID,
splitExpenses,
splitExpensesTotal: -1000,
},
searchContext: {
currentSearchHash: -1,
},
policyCategories: undefined,
policy: undefined,
policyRecentlyUsedCategories: [],
iouReport: expenseReport,
firstIOU: iouAction,
isASAPSubmitBetaEnabled: false,
currentUserPersonalDetails,
transactionViolations: {},
policyRecentlyUsedCurrencies: [],
quickAction: undefined,
iouReportNextStep: undefined,
});

await waitForBatchedUpdates();

// Then each split transaction should have proportional convertedAmount
// Formula: Math.round((originalConvertedAmount * splitAmount) / originalAmount)
const expectedProportionalConvertedAmount = -1836;

const splitTransactions = await new Promise<Transaction[]>((resolve) => {
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.TRANSACTION,
waitForCollectionCallback: true,
callback: (transactions) => {
Onyx.disconnect(connection);
const splits = Object.values(transactions ?? {}).filter(
(t) => t?.transactionID !== originalTransactionID && t?.comment?.originalTransactionID === originalTransactionID,
);
resolve(splits as Transaction[]);
},
});
});

expect(splitTransactions.length).toBe(2);

for (const splitTransaction of splitTransactions) {
expect(splitTransaction.convertedAmount).toBe(expectedProportionalConvertedAmount);
}
});
});

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