Skip to content
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
5 changes: 3 additions & 2 deletions src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,8 @@ function getRuleApprovers(policy: OnyxEntry<Policy>, expenseReport: OnyxEntry<Re
}
}

return [...categoryApprovers, ...tagApprovers];
// Return the unique approvers list
return [...new Set([...categoryApprovers, ...tagApprovers])];
}

function getManagerAccountID(policy: OnyxEntry<Policy>, expenseReport: OnyxEntry<Report> | {ownerAccountID: number}) {
Expand Down Expand Up @@ -884,7 +885,7 @@ function getSubmitToAccountID(policy: OnyxEntry<Policy>, 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();
Comment thread
neil-marcellini marked this conversation as resolved.
Resolved
}
if (ruleApprovers.length > 0 && !isSubmitAndClose(policy)) {
Expand Down
80 changes: 71 additions & 9 deletions tests/unit/PolicyUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,17 @@ const rules = {
approver: 'tagapprover2@test.com',
id: '4',
},
{
applyWhen: [
{
condition: 'matches',
field: 'category',
value: 'cat3',
},
],
approver: 'categoryapprover1@test.com',
id: '5',
},
],
};
const policyTags = {
Expand Down Expand Up @@ -531,9 +542,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);
});
Expand All @@ -560,9 +573,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 () => {
Expand Down Expand Up @@ -592,12 +603,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 = {
Expand Down