Skip to content

Commit

Permalink
Merge pull request #51122 from Nodebrute/negativeamount
Browse files Browse the repository at this point in the history
  • Loading branch information
blimpich authored Oct 30, 2024
2 parents 30b0b8d + d734c9c commit 91214d3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6558,15 +6558,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);
});
});

0 comments on commit 91214d3

Please sign in to comment.