Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix negative amount #51122

Merged
merged 4 commits into from
Oct 30, 2024
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
23 changes: 14 additions & 9 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6510,15 +6510,20 @@ function getReportFromHoldRequestsOnyxData(
const firstHoldTransaction = holdTransactions.at(0);
const newParentReportActionID = rand64();

const optimisticExpenseReport = ReportUtils.buildOptimisticExpenseReport(
chatReport.reportID,
chatReport.policyID ?? iouReport?.policyID ?? '',
recipient.accountID ?? 1,
holdTransactions.reduce((acc, transaction) => acc + TransactionUtils.getAmount(transaction), 0),
getCurrency(firstHoldTransaction),
false,
newParentReportActionID,
);
const isPolicyExpenseChat = ReportUtils.isPolicyExpenseChat(chatReport);
const holdTransactionAmount = holdTransactions.reduce((acc, transaction) => acc + TransactionUtils.getAmount(transaction), 0);
const optimisticExpenseReport = isPolicyExpenseChat
? ReportUtils.buildOptimisticExpenseReport(
chatReport.reportID,
chatReport.policyID ?? iouReport?.policyID ?? '',
recipient.accountID ?? 1,
holdTransactionAmount,
getCurrency(firstHoldTransaction),
false,
newParentReportActionID,
)
: ReportUtils.buildOptimisticIOUReport(recipient.accountID ?? 1, iouReport?.managerID ?? 1, holdTransactionAmount, chatReport.reportID, getCurrency(firstHoldTransaction), false);

const optimisticExpenseReportPreview = ReportUtils.buildOptimisticReportPreview(
chatReport,
optimisticExpenseReport,
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/IOUUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,19 @@ describe('isValidMoneyRequestType', () => {
expect(IOUUtils.isValidMoneyRequestType('money')).toBe(false);
});
});

describe('Check valid amount for IOU/Expense request', () => {
test('IOU amount should be positive', () => {
const iouReport = ReportUtils.buildOptimisticIOUReport(1, 2, 100, '1', 'USD');
const iouTransaction = TransactionUtils.buildOptimisticTransaction(100, 'USD', iouReport.reportID);
const iouAmount = TransactionUtils.getAmount(iouTransaction, false, false);
expect(iouAmount).toBeGreaterThan(0);
});

test('Expense amount should be negative', () => {
const expenseReport = ReportUtils.buildOptimisticExpenseReport('212', '123', 100, 122, 'USD');
const expenseTransaction = TransactionUtils.buildOptimisticTransaction(100, 'USD', expenseReport.reportID);
const expenseAmount = TransactionUtils.getAmount(expenseTransaction, true, false);
expect(expenseAmount).toBeLessThan(0);
});
});
Loading