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
9 changes: 6 additions & 3 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14470,13 +14470,16 @@ function canBeExported(report: OnyxEntry<Report>) {
}

/**
* Whether the current user can email the report to someone. The submitter, the approver and workspace admins can once the report is submitted.
* Whether the current user can email the report to someone. The submitter and workspace admins can, and so can the approver once the report is submitted.
*/
function canShareReport(report: OnyxEntry<Report>, policy: OnyxEntry<Policy>, currentUserAccountID: number): boolean {
if (!isExpenseReport(report) || isOpenReport(report)) {
if (!isExpenseReport(report)) {
return false;
}
return isCurrentUserSubmitter(report, currentUserAccountID) || report?.managerID === currentUserAccountID || isPolicyAdmin(policy);
if (isCurrentUserSubmitter(report, currentUserAccountID) || isPolicyAdmin(policy)) {
return true;
}
return !isOpenReport(report) && report?.managerID === currentUserAccountID;
}

function getIntegrationNameFromExportMessage(reportActions: OnyxEntry<ReportActions> | ReportAction[]) {
Expand Down
37 changes: 34 additions & 3 deletions tests/unit/hooks/useShareActionsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,46 @@ describe('useShareActions', () => {
expect(options.map((option) => option.text)).toEqual(['common.shareReport', 'common.shareCode', 'referralProgram.referralFriend.header', 'qrCodes.copy']);
});

it('hides share report on an open report', () => {
it('lists share report on an open report', () => {
// Given the same report before it is submitted
mockReport = {...submittedReport, stateNum: CONST.REPORT.STATE_NUM.OPEN, statusNum: CONST.REPORT.STATUS_NUM.OPEN};

// When the share options are built
const options = getShareOptions();

// Then a report that was never submitted cannot be emailed
expect(options.map((option) => option.text)).not.toContain('common.shareReport');
// Then the submitter can email the draft
expect(options.at(0)?.text).toBe('common.shareReport');
});

it('lists share report on an open report for a workspace admin', () => {
// Given an open report that someone else owns and approves
mockReport = {...submittedReport, ownerAccountID: OTHER_ACCOUNT_ID, stateNum: CONST.REPORT.STATE_NUM.OPEN, statusNum: CONST.REPORT.STATUS_NUM.OPEN};

// When the share options are built for a workspace admin
const options = getShareOptions(createMock<Policy>({id: 'policy1', role: CONST.POLICY.ROLE.ADMIN}));

// Then the admin can email the draft, since admins can share every report in the workspace
expect(options.at(0)?.text).toBe('common.shareReport');
});

it('lists share report for the approver only once the report is submitted', () => {
// Given an open report that the current user approves but does not own
mockReport = {
...submittedReport,
ownerAccountID: OTHER_ACCOUNT_ID,
managerID: CURRENT_USER_ACCOUNT_ID,
stateNum: CONST.REPORT.STATE_NUM.OPEN,
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
};
const memberPolicy = createMock<Policy>({id: 'policy1', role: CONST.POLICY.ROLE.USER});

// When the share options are built for the approver
const draftOptions = getShareOptions(memberPolicy);

// Then they cannot share the draft, because the approver only gets the share permission on submit
expect(draftOptions.map((option) => option.text)).not.toContain('common.shareReport');
mockReport = {...mockReport, stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED};
expect(getShareOptions(memberPolicy).at(0)?.text).toBe('common.shareReport');
});

it('hides share report from a member who is not the submitter, the approver or an admin', () => {
Expand Down
Loading