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
15 changes: 9 additions & 6 deletions src/libs/ModifiedExpenseMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ function getMovedFromOrToReportMessage(translate: LocalizedTranslate, movedFromR
}
}

function getPolicyRulesModifiedMessage(translate: LocalizedTranslate, fields: PolicyRulesModifiedFields, policyID: string) {
const route = `${environmentURL}/${ROUTES.WORKSPACE_RULES.getRoute(policyID)}`;
function getPolicyRulesModifiedMessage(translate: LocalizedTranslate, fields: PolicyRulesModifiedFields, policyID: string, hasPolicyRuleAccess: boolean) {
const route = hasPolicyRuleAccess ? `${environmentURL}/${ROUTES.WORKSPACE_RULES.getRoute(policyID)}` : CONST.CONFIGURE_EXPENSE_REPORT_RULES_HELP_URL;
const entries = ObjectUtils.typedEntries(fields);

const fragments = entries.map(([key, value], i) => {
Expand Down Expand Up @@ -517,7 +517,10 @@ function getForReportAction({

if (policyRulesModifiedFields && rulePolicyID) {
// eslint-disable-next-line @typescript-eslint/no-deprecated
return getPolicyRulesModifiedMessage(translateLocal, policyRulesModifiedFields, rulePolicyID);
const rulePolicy = getPolicy(rulePolicyID);
const hasPolicyRuleAccess = !!rulePolicy?.areRulesEnabled && isPolicyAdmin(rulePolicy, storedCurrentUserLogin);
// eslint-disable-next-line @typescript-eslint/no-deprecated
Comment thread
luacmartins marked this conversation as resolved.
Resolved
return getPolicyRulesModifiedMessage(translateLocal, policyRulesModifiedFields, rulePolicyID, hasPolicyRuleAccess);
}
}

Expand Down Expand Up @@ -756,11 +759,11 @@ function getForReportActionTemp({

const hasPolicyRulesModifiedFields = isReportActionOriginalMessageAnObject && 'policyRulesModifiedFields' in reportActionOriginalMessage && 'policyID' in reportActionOriginalMessage;
if (hasPolicyRulesModifiedFields) {
const rulePolicyID = reportActionOriginalMessage.policyID;
const policyRulesModifiedFields = reportActionOriginalMessage.policyRulesModifiedFields;

if (policyRulesModifiedFields && rulePolicyID) {
return getPolicyRulesModifiedMessage(translate, policyRulesModifiedFields, rulePolicyID);
if (policyRulesModifiedFields && policy?.id) {
Comment thread
luacmartins marked this conversation as resolved.
Resolved
const hasPolicyRuleAccess = !!policy?.areRulesEnabled && isPolicyAdmin(policy, currentUserLogin);
return getPolicyRulesModifiedMessage(translate, policyRulesModifiedFields, policy?.id, hasPolicyRuleAccess);
}
}

Expand Down
42 changes: 42 additions & 0 deletions tests/unit/ModifiedExpenseMessageTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ import {createRandomReport} from '../utils/collections/reports';
import {translateLocal} from '../utils/TestHelper';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';

// Mock PolicyUtils so getPolicy and isPolicyAdmin are controllable in tests. ModifiedExpenseMessage
// uses named imports from this module; spies alone do not affect those references, so we need a module mock.
jest.mock('@libs/PolicyUtils', () => ({
...jest.requireActual<typeof PolicyUtils>('@libs/PolicyUtils'),
getPolicy: jest.fn(),
isPolicyAdmin: jest.fn(),
}));

const MOVED_TO_REPORT_ID = '1';
const MOVED_FROM_REPORT_ID = '2';

Expand Down Expand Up @@ -814,10 +822,22 @@ describe('ModifiedExpenseMessage', () => {

describe('when policy rules modify an expense', () => {
let environmentURL: string;
const policyRulesPolicyId = '1234';

beforeAll(async () => {
environmentURL = await getEnvironmentURL();
});

beforeEach(() => {
// Default: current user has policy rule access (admin + rules enabled), so link points to workspace rules
// eslint-disable-next-line @typescript-eslint/no-deprecated
(PolicyUtils.getPolicy as jest.Mock).mockReturnValue({
id: policyRulesPolicyId,
areRulesEnabled: true,
} as Policy);
(PolicyUtils.isPolicyAdmin as jest.Mock).mockReturnValue(true);
});

it('returns the correct text message with multiple overrides', () => {
const reportAction = {
...createRandomReportAction(1),
Expand Down Expand Up @@ -924,6 +944,28 @@ describe('ModifiedExpenseMessage', () => {

expect(result).toEqual(expectedResult);
});

it('returns the correct text message with help link for non-admin', () => {
(PolicyUtils.isPolicyAdmin as jest.Mock).mockReturnValue(false);

const reportAction = {
...createRandomReportAction(1),
actionName: CONST.REPORT.ACTIONS.TYPE.MODIFIED_EXPENSE,
originalMessage: {
policyID: policyRulesPolicyId,
policyRulesModifiedFields: {
category: 'Travel',
merchant: "McDonald's",
},
} as OriginalMessageModifiedExpense,
};

const result = getForReportAction({reportAction, policyID: report.policyID});

expect(result).toContain(CONST.CONFIGURE_EXPENSE_REPORT_RULES_HELP_URL);
expect(result).toContain('workspace rules</a>');
expect(result).toContain('set the category to "Travel" and merchant to "McDonald\'s"');
});
});

describe('when the category is changed without source (backward compatibility)', () => {
Expand Down
Loading