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
16 changes: 6 additions & 10 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2431,28 +2431,24 @@ function isOneOnOneChat(report: OnyxEntry<Report>): boolean {
*/

function isPayer(session: OnyxEntry<Session>, iouReport: OnyxEntry<Report>, onlyShowPayElsewhere = false, reportPolicy?: OnyxInputOrEntry<Policy> | SearchPolicy) {
const isApproved = isReportApproved({report: iouReport});
const policy = reportPolicy ?? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${iouReport?.policyID}`] ?? null;
const policyType = policy?.type;
const isAdmin = policyType !== CONST.POLICY.TYPE.PERSONAL && policy?.role === CONST.POLICY.ROLE.ADMIN;
const isManager = iouReport?.managerID === session?.accountID;
const reimbursementChoice = policy?.reimbursementChoice;

if (isPaidGroupPolicy(iouReport)) {
if (policy?.reimbursementChoice === CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_YES) {
if (reimbursementChoice === CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_YES) {
if (!policy?.achAccount?.reimburser) {
// If there is a manager assigned, only allow payment if the user is both an admin and the manager.
// Otherwise, if there is no reimburser and no manager, only allow payment if the user is an admin.
if (iouReport?.managerID) {
return isAdmin && isManager;
}
return isAdmin;
}

// If we are the reimburser and the report is approved or we are the manager then we can pay it.
const isReimburser = session?.email === policy?.achAccount?.reimburser;
return isReimburser && (isApproved || isManager);
return isReimburser;
}
if (policy?.reimbursementChoice === CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_MANUAL || onlyShowPayElsewhere) {
return isAdmin && (isApproved || isManager);
if (reimbursementChoice === CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_MANUAL || onlyShowPayElsewhere) {
return isAdmin;
}
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/NextStepUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,6 @@ describe('libs/NextStepUtils', () => {

describe('it generates an optimistic nextStep once a report has been approved', () => {
test('non-payer', () => {
report.managerID = strangeAccountID;
optimisticNextStep.icon = CONST.NEXT_STEP.ICONS.CHECKMARK;
optimisticNextStep.message = [
{
Expand All @@ -686,6 +685,7 @@ describe('libs/NextStepUtils', () => {

return Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {
reimbursementChoice: CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_MANUAL,
role: 'user',
}).then(() => {
const result = buildNextStep(report, CONST.REPORT.STATUS_NUM.APPROVED);

Expand Down
28 changes: 20 additions & 8 deletions tests/unit/ReportUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3793,21 +3793,18 @@ describe('ReportUtils', () => {

const policyTest: Policy = {
...createRandomPolicy(1),
type: CONST.POLICY.TYPE.CORPORATE,
employeeList: {
[currentUserEmail]: {
role: CONST.POLICY.ROLE.AUDITOR,
},
},
role: CONST.POLICY.ROLE.AUDITOR,
};

beforeAll(() => {
Onyx.multiSet({
[ONYXKEYS.SESSION]: {email: currentUserEmail, accountID: currentUserAccountID},
[ONYXKEYS.COLLECTION.POLICY]: {
[`${ONYXKEYS.COLLECTION.POLICY}1`]: policyTest,
},
});
return waitForBatchedUpdates();
beforeAll(async () => {
await Onyx.set(ONYXKEYS.SESSION, {email: currentUserEmail, accountID: currentUserAccountID});
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}1`, policyTest);
});

afterAll(() => Onyx.clear());
Expand All @@ -3819,6 +3816,21 @@ describe('ReportUtils', () => {
it('should return false for non-admin of a group policy', () => {
expect(isPayer({email: currentUserEmail, accountID: currentUserAccountID}, approvedReport, false)).toBe(false);
});

it('should return true for a reimburser of a group policy on a closed report', async () => {
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}1`, {reimbursementChoice: CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_YES, achAccount: {reimburser: currentUserEmail}});

const closedReport: Report = {
...createRandomReport(2),
type: CONST.REPORT.TYPE.EXPENSE,
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
statusNum: CONST.REPORT.STATUS_NUM.CLOSED,
managerID: currentUserAccountID + 1,
policyID: policyTest.id,
};

expect(isPayer({email: currentUserEmail, accountID: currentUserAccountID}, closedReport, false)).toBe(true);
});
});
describe('buildReportNameFromParticipantNames', () => {
/**
Expand Down