Skip to content
Draft
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
30 changes: 29 additions & 1 deletion src/libs/actions/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
import {getCurrentUserAccountID} from './Report';

const allTransactions: Record<string, Transaction> = {};
Onyx.connect({

Check warning on line 56 in src/libs/actions/Transaction.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,
callback: (transaction, key) => {
if (!key || !transaction) {
Expand All @@ -65,7 +65,7 @@
});

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

Check warning on line 68 in src/libs/actions/Transaction.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 @@ -74,7 +74,7 @@
});

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

Check warning on line 77 in src/libs/actions/Transaction.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 @@ -86,7 +86,7 @@
});

const allTransactionViolation: OnyxCollection<TransactionViolation[]> = {};
Onyx.connect({

Check warning on line 89 in src/libs/actions/Transaction.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,
callback: (transactionViolation, key) => {
if (!key || !transactionViolation) {
Expand All @@ -98,7 +98,7 @@
});

let allTransactionViolations: TransactionViolations = [];
Onyx.connect({

Check warning on line 101 in src/libs/actions/Transaction.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,
callback: (val) => (allTransactionViolations = val ?? []),
});
Expand Down Expand Up @@ -709,6 +709,7 @@
const updatedReportTotals: Record<string, number> = {};
const updatedReportNonReimbursableTotals: Record<string, number> = {};
const updatedReportUnheldNonReimbursableTotals: Record<string, number> = {};
const updatedReportCurrencies: Record<string, string> = {};

// Store current violations for each transaction to restore on failure
const currentTransactionViolations: Record<string, TransactionViolation[]> = {};
Expand Down Expand Up @@ -829,6 +830,7 @@
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`,
value: {
reportID,
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
comment: {
hold: null,
},
Expand All @@ -840,6 +842,7 @@
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`,
value: {
reportID,
pendingAction: null,
},
});

Expand All @@ -848,6 +851,7 @@
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`,
value: {
reportID: transaction.reportID,
pendingAction: transaction.pendingAction ?? null,
comment: {
hold: transaction.comment?.hold,
},
Expand Down Expand Up @@ -961,7 +965,17 @@
const targetReport =
allReports?.[targetReportKey] ?? (targetReportID === newReport?.reportID ? newReport : undefined) ?? (targetReportID === selfDMReport?.reportID ? selfDMReport : undefined);

if (transactionCurrency === targetReport?.currency) {
// For newly created reports (with pendingFields.createReport), set the currency to match the first transaction's currency
// This mirrors the behavior of buildOptimisticExpenseReport which sets the report currency to the transaction's currency
const isNewlyCreatedReport = targetReportID === newReport?.reportID && newReport?.pendingFields?.createReport === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD;
if (isNewlyCreatedReport && transactionCurrency && !updatedReportCurrencies[targetReportID]) {
updatedReportCurrencies[targetReportID] = transactionCurrency;
}

// Use the updated currency if available (for new reports), otherwise use the report's existing currency
const currencyForMatching = updatedReportCurrencies[targetReportID] ?? targetReport?.currency;

if (transactionCurrency === currencyForMatching) {
const currentTotal = updatedReportTotals[targetReportID] ?? targetReport?.total ?? 0;
updatedReportTotals[targetReportID] = currentTotal - transactionAmount;

Expand Down Expand Up @@ -1267,6 +1281,20 @@
});
}

for (const [reportIDToUpdate, currency] of Object.entries(updatedReportCurrencies)) {
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`,
value: {currency},
});

failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`,
value: {currency: allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`]?.currency ?? newReport?.currency},
});
}

const reportTransactions = getReportTransactions(reportID);
for (const transaction of reportTransactions) {
if (!isPaidGroupPolicy(policy) || !policy?.id) {
Expand Down
Loading