Skip to content

Commit f2851aa

Browse files
authored
Merge pull request Expensify#67456 from daledah/fix/67079
fix: incorrect violation when moving smart scan failed expense
2 parents 4e7705c + 19d16fc commit f2851aa

3 files changed

Lines changed: 47 additions & 5 deletions

File tree

src/libs/Violations/ViolationsUtils.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,10 @@ const ViolationsUtils = {
226226
hasDependentTags: boolean,
227227
isInvoiceTransaction: boolean,
228228
): OnyxUpdate {
229-
if (TransactionUtils.isPartial(updatedTransaction)) {
229+
const isScanning = TransactionUtils.isScanning(updatedTransaction);
230+
const isScanRequest = TransactionUtils.isScanRequest(updatedTransaction);
231+
const isPartialTransaction = TransactionUtils.isPartial(updatedTransaction);
232+
if (isPartialTransaction && isScanning) {
230233
return {
231234
onyxMethod: Onyx.METHOD.SET,
232235
key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${updatedTransaction.transactionID}`,
@@ -236,6 +239,19 @@ const ViolationsUtils = {
236239

237240
let newTransactionViolations = [...transactionViolations];
238241

242+
const shouldShowSmartScanFailedError = isScanRequest && updatedTransaction.receipt?.state === CONST.IOU.RECEIPT_STATE.SCAN_FAILED;
243+
const hasSmartScanFailedError = transactionViolations.some((violation) => violation.name === CONST.VIOLATIONS.SMARTSCAN_FAILED);
244+
if (shouldShowSmartScanFailedError && !hasSmartScanFailedError) {
245+
return {
246+
onyxMethod: Onyx.METHOD.SET,
247+
key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${updatedTransaction.transactionID}`,
248+
value: [{name: CONST.VIOLATIONS.SMARTSCAN_FAILED, type: CONST.VIOLATION_TYPES.WARNING, showInReview: true}],
249+
};
250+
}
251+
if (!shouldShowSmartScanFailedError && hasSmartScanFailedError) {
252+
newTransactionViolations = reject(newTransactionViolations, {name: CONST.VIOLATIONS.SMARTSCAN_FAILED});
253+
}
254+
239255
// Calculate client-side category violations
240256
const policyRequiresCategories = !!policy.requiresCategory;
241257
if (policyRequiresCategories) {

src/pages/workspace/WorkspaceOverviewPage.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ function WorkspaceOverviewPage({policyDraft, policy: policyProp, route}: Workspa
7070

7171
// When we create a new workspace, the policy prop will be empty on the first render. Therefore, we have to use policyDraft until policy has been set in Onyx.
7272
const policy = policyDraft?.id ? policyDraft : policyProp;
73+
7374
const isPolicyAdmin = isPolicyAdminPolicyUtils(policy);
7475
const outputCurrency = policy?.outputCurrency ?? '';
7576
const currencySymbol = currencyList?.[outputCurrency]?.symbol ?? '';

tests/unit/ViolationUtilsTest.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,14 @@ describe('getViolationsOnyxData', () => {
282282
expect(result.value).not.toContainEqual(categoryOutOfPolicyViolation);
283283
});
284284

285-
it('should not add a category violation when the transaction is partial', () => {
286-
const partialTransaction = {...transaction, amount: 0, merchant: CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT, category: undefined};
285+
it('should not add a category violation when the transaction is scanning', () => {
286+
const partialTransaction = {
287+
...transaction,
288+
amount: 0,
289+
merchant: CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT,
290+
category: undefined,
291+
receipt: {state: CONST.IOU.RECEIPT_STATE.SCANNING},
292+
};
287293
const result = ViolationsUtils.getViolationsOnyxData(partialTransaction, transactionViolations, policy, policyTags, policyCategories, false, false);
288294
expect(result.value).not.toContainEqual(missingCategoryViolation);
289295
});
@@ -307,6 +313,19 @@ describe('getViolationsOnyxData', () => {
307313

308314
expect(result.value).toEqual(expect.arrayContaining([missingCategoryViolation, ...transactionViolations]));
309315
});
316+
317+
it('should only return smartscanFailed violation for smart scan failed transactions', () => {
318+
const partialTransaction = {
319+
...transaction,
320+
amount: 0,
321+
merchant: CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT,
322+
category: undefined,
323+
iouRequestType: CONST.IOU.REQUEST_TYPE.SCAN,
324+
receipt: {state: CONST.IOU.RECEIPT_STATE.SCAN_FAILED},
325+
};
326+
const result = ViolationsUtils.getViolationsOnyxData(partialTransaction, transactionViolations, policy, policyTags, policyCategories, false, false);
327+
expect(result.value).toEqual([{name: CONST.VIOLATIONS.SMARTSCAN_FAILED, type: CONST.VIOLATION_TYPES.WARNING, showInReview: true}]);
328+
});
310329
});
311330

312331
describe('policy does not require Categories', () => {
@@ -361,8 +380,14 @@ describe('getViolationsOnyxData', () => {
361380
expect(result.value).toEqual([]);
362381
});
363382

364-
it('should not add a tag violation when the transaction is partial', () => {
365-
const partialTransaction = {...transaction, amount: 0, merchant: CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT, tag: undefined};
383+
it('should not add a tag violation when the transaction is scanning', () => {
384+
const partialTransaction = {
385+
...transaction,
386+
amount: 0,
387+
merchant: CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT,
388+
tag: undefined,
389+
receipt: {state: CONST.IOU.RECEIPT_STATE.SCANNING},
390+
};
366391
const result = ViolationsUtils.getViolationsOnyxData(partialTransaction, transactionViolations, policy, policyTags, policyCategories, false, false);
367392
expect(result.value).not.toContainEqual(missingTagViolation);
368393
});

0 commit comments

Comments
 (0)