diff --git a/src/libs/Violations/ViolationsUtils.ts b/src/libs/Violations/ViolationsUtils.ts index 5768291ec7c3..b6a7572c0f2b 100644 --- a/src/libs/Violations/ViolationsUtils.ts +++ b/src/libs/Violations/ViolationsUtils.ts @@ -232,13 +232,19 @@ const ViolationsUtils = { const hasOverLimitViolation = transactionViolations.some((violation) => violation.name === 'overLimit'); // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing const amount = updatedTransaction.modifiedAmount || updatedTransaction.amount; + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing + const currency = updatedTransaction.modifiedCurrency || updatedTransaction.currency; + const canCalculateAmountViolations = policy.outputCurrency === currency; + const shouldShowReceiptRequiredViolation = + canCalculateAmountViolations && !isInvoiceTransaction && policy.maxExpenseAmountNoReceipt && Math.abs(amount) > policy.maxExpenseAmountNoReceipt && !TransactionUtils.hasReceipt(updatedTransaction) && isControlPolicy; - const shouldShowOverLimitViolation = !isInvoiceTransaction && policy.maxExpenseAmount && Math.abs(amount) > policy.maxExpenseAmount && isControlPolicy; + const shouldShowOverLimitViolation = + canCalculateAmountViolations && !isInvoiceTransaction && policy.maxExpenseAmount && Math.abs(amount) > policy.maxExpenseAmount && isControlPolicy; const hasFutureDateViolation = transactionViolations.some((violation) => violation.name === 'futureDate'); // Add 'futureDate' violation if transaction date is in the future and policy type is corporate if (!hasFutureDateViolation && shouldDisplayFutureDateViolation) { @@ -250,7 +256,7 @@ const ViolationsUtils = { newTransactionViolations = reject(newTransactionViolations, {name: CONST.VIOLATIONS.FUTURE_DATE}); } - if (!hasReceiptRequiredViolation && shouldShowReceiptRequiredViolation) { + if (canCalculateAmountViolations && !hasReceiptRequiredViolation && shouldShowReceiptRequiredViolation) { newTransactionViolations.push({ name: CONST.VIOLATIONS.RECEIPT_REQUIRED, data: { @@ -261,11 +267,11 @@ const ViolationsUtils = { }); } - if (hasReceiptRequiredViolation && !shouldShowReceiptRequiredViolation) { + if (canCalculateAmountViolations && hasReceiptRequiredViolation && !shouldShowReceiptRequiredViolation) { newTransactionViolations = reject(newTransactionViolations, {name: CONST.VIOLATIONS.RECEIPT_REQUIRED}); } - if (!hasOverLimitViolation && shouldShowOverLimitViolation) { + if (canCalculateAmountViolations && !hasOverLimitViolation && shouldShowOverLimitViolation) { newTransactionViolations.push({ name: CONST.VIOLATIONS.OVER_LIMIT, data: { @@ -276,7 +282,7 @@ const ViolationsUtils = { }); } - if (hasOverLimitViolation && !shouldShowOverLimitViolation) { + if (canCalculateAmountViolations && hasOverLimitViolation && !shouldShowOverLimitViolation) { newTransactionViolations = reject(newTransactionViolations, {name: CONST.VIOLATIONS.OVER_LIMIT}); } diff --git a/tests/unit/ViolationUtilsTest.ts b/tests/unit/ViolationUtilsTest.ts index b58ddfc6d443..607499d2a398 100644 --- a/tests/unit/ViolationUtilsTest.ts +++ b/tests/unit/ViolationUtilsTest.ts @@ -83,7 +83,7 @@ describe('getViolationsOnyxData', () => { comment: {attendees: [{email: 'text@expensify.com', displayName: 'Test User', avatarUrl: ''}]}, created: '2023-07-24 13:46:20', merchant: 'United Airlines', - currency: 'USD', + currency: CONST.CURRENCY.USD, }; transactionViolations = []; policy = {requiresTag: false, requiresCategory: false} as Policy; @@ -155,6 +155,7 @@ describe('getViolationsOnyxData', () => { describe('controlPolicyViolations', () => { beforeEach(() => { policy.type = 'corporate'; + policy.outputCurrency = CONST.CURRENCY.USD; }); it('should not add futureDate violation if the policy is not corporate', () => { @@ -185,12 +186,28 @@ describe('getViolationsOnyxData', () => { expect(result.value).toEqual(expect.arrayContaining([receiptRequiredViolation, ...transactionViolations])); }); + it('should not add receiptRequired violation if the transaction has different currency than the workspace currency', () => { + transaction.amount = 1000000; + transaction.modifiedCurrency = CONST.CURRENCY.CAD; + policy.maxExpenseAmountNoReceipt = 2500; + const result = ViolationsUtils.getViolationsOnyxData(transaction, transactionViolations, policy, policyTags, policyCategories, false, false); + expect(result.value).toEqual([]); + }); + it('should add overLimit violation if the transaction amount is over the policy limit', () => { transaction.amount = 1000000; policy.maxExpenseAmount = 200000; const result = ViolationsUtils.getViolationsOnyxData(transaction, transactionViolations, policy, policyTags, policyCategories, false, false); expect(result.value).toEqual(expect.arrayContaining([overLimitViolation, ...transactionViolations])); }); + + it('should not add overLimit violation if the transaction currency is different from the workspace currency', () => { + transaction.amount = 1000000; + transaction.modifiedCurrency = CONST.CURRENCY.NZD; + policy.maxExpenseAmount = 200000; + const result = ViolationsUtils.getViolationsOnyxData(transaction, transactionViolations, policy, policyTags, policyCategories, false, false); + expect(result.value).toEqual([]); + }); }); describe('policyRequiresCategories', () => {