From 7e4fcfef5ec03fb331c9e6b0ab74919f5f8c3ef5 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Wed, 14 Jan 2026 16:06:53 +0700 Subject: [PATCH 1/5] fix: Unexpected error occurs when a category/Tag approver submits an expense --- src/libs/PolicyUtils.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index 203735d086d5..7cad593525f7 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -856,7 +856,9 @@ function getRuleApprovers(policy: OnyxEntry, expenseReport: OnyxEntry index === 0 || approver !== allRuleApprovers.at(index - 1)); } function getManagerAccountID(policy: OnyxEntry, expenseReport: OnyxEntry | {ownerAccountID: number}) { @@ -884,7 +886,7 @@ function getSubmitToAccountID(policy: OnyxEntry, expenseReport: OnyxEntr const ruleApprovers = getRuleApprovers(policy, expenseReport); const employeeAccountID = expenseReport?.ownerAccountID ?? CONST.DEFAULT_NUMBER_ID; const employeeLogin = getLoginsByAccountIDs([employeeAccountID]).at(0) ?? ''; - if (ruleApprovers.length > 0 && ruleApprovers.at(0) === employeeLogin && policy?.preventSelfApproval) { + if (ruleApprovers.length > 0 && ruleApprovers.at(0) === employeeLogin) { ruleApprovers.shift(); } if (ruleApprovers.length > 0 && !isSubmitAndClose(policy)) { From 4c48fbd87c8cfb07e017c549adb0ef3a6ddc2099 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Fri, 16 Jan 2026 14:10:21 +0700 Subject: [PATCH 2/5] remove duplicate approver --- src/libs/PolicyUtils.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index 7cad593525f7..ee079d65c120 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -856,9 +856,7 @@ function getRuleApprovers(policy: OnyxEntry, expenseReport: OnyxEntry index === 0 || approver !== allRuleApprovers.at(index - 1)); + return [...new Set([...categoryApprovers, ...tagApprovers])]; } function getManagerAccountID(policy: OnyxEntry, expenseReport: OnyxEntry | {ownerAccountID: number}) { From 64e6b93fc33c1fa188650c4ba64a91a041269a98 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Fri, 16 Jan 2026 14:11:57 +0700 Subject: [PATCH 3/5] add comment --- src/libs/PolicyUtils.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index ee079d65c120..09edb12c8591 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -856,6 +856,7 @@ function getRuleApprovers(policy: OnyxEntry, expenseReport: OnyxEntry Date: Mon, 19 Jan 2026 21:44:22 +0700 Subject: [PATCH 4/5] add unit test --- tests/unit/PolicyUtilsTest.ts | 81 +++++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 9 deletions(-) diff --git a/tests/unit/PolicyUtilsTest.ts b/tests/unit/PolicyUtilsTest.ts index 4fabf81d7891..7b39fead2990 100644 --- a/tests/unit/PolicyUtilsTest.ts +++ b/tests/unit/PolicyUtilsTest.ts @@ -37,6 +37,7 @@ import createCollection from '../utils/collections/createCollection'; import createRandomPolicy from '../utils/collections/policies'; import {createRandomReport} from '../utils/collections/reports'; import createRandomTransaction from '../utils/collections/transaction'; +import getOnyxValue from '../utils/getOnyxValue'; import * as TestHelper from '../utils/TestHelper'; import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct'; @@ -187,6 +188,17 @@ const rules = { approver: 'tagapprover2@test.com', id: '4', }, + { + applyWhen: [ + { + condition: 'matches', + field: 'category', + value: 'cat3', + }, + ], + approver: 'categoryapprover1@test.com', + id: '5', + }, ], }; const policyTags = { @@ -531,9 +543,11 @@ describe('PolicyUtils', () => { category: '', reportID: expenseReport.reportID, }; - await Onyx.set(ONYXKEYS.COLLECTION.TRANSACTION, { - [transaction1.transactionID]: transaction1, - [transaction2.transactionID]: transaction2, + await Onyx.multiSet({ + [ONYXKEYS.COLLECTION.TRANSACTION]: { + [transaction1.transactionID]: transaction1, + [transaction2.transactionID]: transaction2, + }, }); expect(getSubmitToAccountID(policy, expenseReport)).toBe(categoryApprover1AccountID); }); @@ -560,9 +574,7 @@ describe('PolicyUtils', () => { tag: '', }; - await Onyx.set(ONYXKEYS.COLLECTION.TRANSACTION, { - [transaction.transactionID]: transaction, - }); + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, transaction); expect(getSubmitToAccountID(policy, expenseReport)).toBe(adminAccountID); }); it('should return the category approver of the first transaction sorted by created if we have many transaction categories match with the category approver rule', async () => { @@ -592,12 +604,63 @@ describe('PolicyUtils', () => { created: DateUtils.subtractMillisecondsFromDateTime(testDate, 1), reportID: expenseReport.reportID, }; - await Onyx.set(ONYXKEYS.COLLECTION.TRANSACTION, { - [transaction1.transactionID]: transaction1, - [transaction2.transactionID]: transaction2, + await Onyx.multiSet({ + [ONYXKEYS.COLLECTION.TRANSACTION]: { + [transaction1.transactionID]: transaction1, + [transaction2.transactionID]: transaction2, + }, }); expect(getSubmitToAccountID(policy, expenseReport)).toBe(categoryApprover2AccountID); }); + it('should return the first rule approver who is not the current submitter', async () => { + const policy: Policy = { + ...createRandomPolicy(0), + approver: 'owner@test.com', + owner: 'owner@test.com', + type: CONST.POLICY.TYPE.CORPORATE, + employeeList, + rules, + approvalMode: CONST.POLICY.APPROVAL_MODE.ADVANCED, + }; + const expenseReport: Report = { + ...createRandomReport(0, undefined), + ownerAccountID: categoryApprover1AccountID, + type: CONST.REPORT.TYPE.EXPENSE, + }; + const transaction1: Transaction = { + ...createRandomTransaction(0), + category: 'cat1', + reportID: expenseReport.reportID, + tag: '', + created: DateUtils.subtractMillisecondsFromDateTime(testDate, 2), + }; + + const transaction2: Transaction = { + ...createRandomTransaction(1), + category: 'cat3', + reportID: expenseReport.reportID, + tag: '', + created: DateUtils.subtractMillisecondsFromDateTime(testDate, 1), + }; + + const transaction3: Transaction = { + ...createRandomTransaction(2), + category: '', + reportID: expenseReport.reportID, + tag: 'tag1', + created: testDate, + }; + + await Onyx.multiSet({ + [ONYXKEYS.COLLECTION.TRANSACTION]: { + [transaction1.transactionID]: transaction1, + [transaction2.transactionID]: transaction2, + [transaction3.transactionID]: transaction3, + }, + }); + + expect(getSubmitToAccountID(policy, expenseReport)).toBe(tagApprover1AccountID); + }); describe('Has no transaction match with the category approver rule', () => { it('should return the first tag approver if has any transaction tag match with with the tag approver rule ', async () => { const policy: Policy = { From f48b1a3ac0c2b949ce4be1b5fd7739b77918d56d Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Mon, 19 Jan 2026 21:47:14 +0700 Subject: [PATCH 5/5] remove test function --- tests/unit/PolicyUtilsTest.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/PolicyUtilsTest.ts b/tests/unit/PolicyUtilsTest.ts index 7b39fead2990..6a004f726ee5 100644 --- a/tests/unit/PolicyUtilsTest.ts +++ b/tests/unit/PolicyUtilsTest.ts @@ -37,7 +37,6 @@ import createCollection from '../utils/collections/createCollection'; import createRandomPolicy from '../utils/collections/policies'; import {createRandomReport} from '../utils/collections/reports'; import createRandomTransaction from '../utils/collections/transaction'; -import getOnyxValue from '../utils/getOnyxValue'; import * as TestHelper from '../utils/TestHelper'; import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct';