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
46 changes: 35 additions & 11 deletions src/libs/ReportSecondaryActionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,31 @@ function everyPayActionHasPaymentType(payActions: ReportAction[], matchesPayment
);
}

// Cancelling appends a new action instead of removing the old pay action, so a report can hold stale pay actions
// (paid elsewhere, cancelled, re-paid via bank). Use the latest pay action so a superseded one doesn't keep Cancel around.
function getLatestPayAction(payActions: ReportAction[]): ReportAction | undefined {
return payActions.reduce<ReportAction | undefined>((latest, action) => (!latest || action.created > latest.created ? action : latest), undefined);
}

function getPayActionPaymentType(action: ReportAction | undefined): string | undefined {
if (!action) {
return undefined;
}
const originalMessage = getOriginalMessage(action);
return originalMessage && 'paymentType' in originalMessage ? originalMessage.paymentType : undefined;
}

function hasPayActionPassedNachaCutoff(action: ReportAction | undefined): boolean {
if (!action) {
return false;
}
const now = new Date();
const paymentDatetime = new Date(action.created);
const nowUTC = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds()));
const cutoffTimeUTC = new Date(Date.UTC(paymentDatetime.getUTCFullYear(), paymentDatetime.getUTCMonth(), paymentDatetime.getUTCDate(), 23, 45, 0));
return nowUTC.getTime() > cutoffTimeUTC.getTime();
}

function isCancelPaymentAction(
currentAccountID: number,
currentUserEmail: string,
Expand Down Expand Up @@ -443,16 +468,21 @@ function isCancelPaymentAction(
return everyPayActionHasPaymentType(payActions, (paymentType) => paymentType === CONST.IOU.PAYMENT_TYPE.EXPENSIFY);
}

const isAdmin = policy?.role === CONST.POLICY.ROLE.ADMIN;
// Mirror the pay gate (canIOUBePaid.canPay): whoever could mark the report paid can cancel it, no admin requirement.
const canCancelPayment =
Comment thread
David-2002065 marked this conversation as resolved.
Resolved
isPayer ||
(policy?.reimbursementChoice === CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_MANUAL && canMemberWrite(policy, currentUserEmail, CONST.POLICY.POLICY_FEATURE.WORKFLOWS_PAYMENTS));

if (!isAdmin || !isPayer) {
if (!canCancelPayment) {
return false;
}

const payActions = getReportPayActions(report.reportID);
const latestPayAction = getLatestPayAction(payActions);
const latestPaymentType = getPayActionPaymentType(latestPayAction);

// Check if payment was made via bank account (not elsewhere)
const isPaidViaBankAccount = everyPayActionHasPaymentType(payActions, (paymentType) => paymentType !== CONST.IOU.PAYMENT_TYPE.ELSEWHERE);
// An undetermined payment type (no pay action) is treated as paid elsewhere below so we still surface Cancel.
const isPaidViaBankAccount = !!latestPaymentType && latestPaymentType !== CONST.IOU.PAYMENT_TYPE.ELSEWHERE;

// For reports marked as paid elsewhere or when we can't determine payment type, show cancel button
if (report.stateNum === CONST.REPORT.STATE_NUM.APPROVED && report.statusNum === CONST.REPORT.STATUS_NUM.REIMBURSED && !isPaidViaBankAccount) {
Expand All @@ -469,13 +499,7 @@ function isCancelPaymentAction(
const isBankProcessing = isPaidViaBankAccount && (isInBillingState || isApprovedAndReimbursed || isAutoReimbursed);
const isPaymentProcessing = (!!report.isWaitingOnBankAccount && report.statusNum === CONST.REPORT.STATUS_NUM.APPROVED) || isBankProcessing;

const hasDailyNachaCutoffPassed = payActions.some((action) => {
const now = new Date();
const paymentDatetime = new Date(action.created);
const nowUTC = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds()));
const cutoffTimeUTC = new Date(Date.UTC(paymentDatetime.getUTCFullYear(), paymentDatetime.getUTCMonth(), paymentDatetime.getUTCDate(), 23, 45, 0));
return nowUTC.getTime() > cutoffTimeUTC.getTime();
});
const hasDailyNachaCutoffPassed = hasPayActionPassedNachaCutoff(latestPayAction);

return isPaymentProcessing && !hasDailyNachaCutoffPassed;
}
Expand Down
287 changes: 287 additions & 0 deletions tests/unit/ReportSecondaryActionUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import createRandomPolicy from '../utils/collections/policies';
import {createExpenseReport} from '../utils/collections/reports';
import createRandomTransaction from '../utils/collections/transaction';
import createMock from '../utils/createMock';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';

const EMPLOYEE_ACCOUNT_ID = 1;
const EMPLOYEE_EMAIL = 'employee@mail.com';
Expand Down Expand Up @@ -278,6 +279,292 @@ describe('getSecondaryAction', () => {
expect(result.includes(CONST.REPORT.SECONDARY_ACTIONS.PAY)).toBe(true);
});

it('includes CANCEL_PAYMENT for a non-reimburser admin who could pay on a manual-reimbursement workspace', async () => {
// beforeEach's Onyx.clear isn't awaited; explicit clear guarantees a clean policy collection for this seed.
await Onyx.clear();

// Seed the policy into Onyx so isPayer narrows to the reimburser; only the WORKFLOWS_PAYMENTS branch admits this admin.
const designatedPayerEmail = 'owner@manual-test.com';
const report = createMock<Report>({
reportID: REPORT_ID,
type: CONST.REPORT.TYPE.EXPENSE,
policyID: POLICY_ID,
ownerAccountID: EMPLOYEE_ACCOUNT_ID,
// Marked as paid elsewhere: APPROVED/REIMBURSED with no bank pay action.
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
statusNum: CONST.REPORT.STATUS_NUM.REIMBURSED,
total: -10000,
isWaitingOnBankAccount: false,
});
const policy = createMock<Policy>({
id: POLICY_ID,
type: CONST.POLICY.TYPE.CORPORATE,
role: CONST.POLICY.ROLE.ADMIN,
owner: designatedPayerEmail,
approvalMode: CONST.POLICY.APPROVAL_MODE.OPTIONAL,
reimbursementChoice: CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_MANUAL,
reimburser: designatedPayerEmail,
outputCurrency: CONST.CURRENCY.USD,
isPolicyExpenseChatEnabled: true,
employeeList: {
[ADMIN_EMAIL]: {role: CONST.POLICY.ROLE.ADMIN},
[designatedPayerEmail]: {role: CONST.POLICY.ROLE.ADMIN},
},
});
const transaction = createMock<Transaction>({
reportID: REPORT_ID,
amount: 10000,
});

await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy);
Comment thread
David-2002065 marked this conversation as resolved.
Resolved
await waitForBatchedUpdates();

const result = getSecondaryReportActions({
currentUserLogin: ADMIN_EMAIL,
currentUserAccountID: ADMIN_ACCOUNT_ID,
submitterLogin: EMPLOYEE_EMAIL,
report,
chatReport,
reportTransactions: [transaction],
originalTransaction: createMock<Transaction>({}),
violations: {},
bankAccountList: {},
policy,
reportActions: [],
});

expect(result.includes(CONST.REPORT.SECONDARY_ACTIONS.CANCEL_PAYMENT)).toBe(true);
});

it('includes CANCEL_PAYMENT for a non-admin payments admin who could pay on a manual-reimbursement workspace', async () => {
// beforeEach's Onyx.clear isn't awaited; explicit clear + seeding the policy keeps this on the same path as the test above.
await Onyx.clear();

// A non-admin with WORKFLOWS_PAYMENTS access can pay on a manual workspace, so they can cancel too.
const paymentsAdminEmail = 'payments-admin@manual-test.com';
const paymentsAdminAccountID = 55;
const ownerEmail = 'owner@manual-test.com';
const report = createMock<Report>({
reportID: REPORT_ID,
type: CONST.REPORT.TYPE.EXPENSE,
policyID: POLICY_ID,
ownerAccountID: EMPLOYEE_ACCOUNT_ID,
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
statusNum: CONST.REPORT.STATUS_NUM.REIMBURSED,
total: -10000,
isWaitingOnBankAccount: false,
});
const policy = createMock<Policy>({
id: POLICY_ID,
type: CONST.POLICY.TYPE.CORPORATE,
// The current user is a payments admin, NOT a workspace admin.
role: CONST.POLICY.ROLE.PAYMENTS_ADMIN,
owner: ownerEmail,
approvalMode: CONST.POLICY.APPROVAL_MODE.OPTIONAL,
reimbursementChoice: CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_MANUAL,
// Designated reimburser is someone else, so isPayer is false and only the WORKFLOWS_PAYMENTS branch can pass.
reimburser: ownerEmail,
outputCurrency: CONST.CURRENCY.USD,
isPolicyExpenseChatEnabled: true,
employeeList: {
[paymentsAdminEmail]: {role: CONST.POLICY.ROLE.PAYMENTS_ADMIN},
[ownerEmail]: {role: CONST.POLICY.ROLE.ADMIN},
},
});
const transaction = createMock<Transaction>({
reportID: REPORT_ID,
amount: 10000,
});

await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy);
await waitForBatchedUpdates();

const result = getSecondaryReportActions({
currentUserLogin: paymentsAdminEmail,
currentUserAccountID: paymentsAdminAccountID,
submitterLogin: EMPLOYEE_EMAIL,
report,
chatReport,
reportTransactions: [transaction],
originalTransaction: createMock<Transaction>({}),
violations: {},
bankAccountList: {},
policy,
reportActions: [],
});

expect(result.includes(CONST.REPORT.SECONDARY_ACTIONS.CANCEL_PAYMENT)).toBe(true);
Comment thread
David-2002065 marked this conversation as resolved.
Resolved
});

it('does not include CANCEL_PAYMENT for a plain workspace member on a manual-reimbursement workspace', async () => {
// beforeEach's Onyx.clear isn't awaited; explicit clear + reset guarantees state.
await Onyx.clear();

const memberEmail = 'member@manual-test.com';
const memberAccountID = 77;
const reimburserEmail = 'owner@manual-test.com';
const report = createMock<Report>({
reportID: REPORT_ID,
type: CONST.REPORT.TYPE.EXPENSE,
policyID: POLICY_ID,
ownerAccountID: EMPLOYEE_ACCOUNT_ID,
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
statusNum: CONST.REPORT.STATUS_NUM.REIMBURSED,
total: -10000,
isWaitingOnBankAccount: false,
});
const policy = createMock<Policy>({
id: POLICY_ID,
type: CONST.POLICY.TYPE.CORPORATE,
// A regular member: not an admin, not the reimburser, no payments access.
role: CONST.POLICY.ROLE.USER,
owner: reimburserEmail,
reimbursementChoice: CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_MANUAL,
reimburser: reimburserEmail,
outputCurrency: CONST.CURRENCY.USD,
isPolicyExpenseChatEnabled: true,
employeeList: {
[memberEmail]: {role: CONST.POLICY.ROLE.USER},
[reimburserEmail]: {role: CONST.POLICY.ROLE.ADMIN},
},
});

await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy);
await waitForBatchedUpdates();

const result = getSecondaryReportActions({
currentUserLogin: memberEmail,
currentUserAccountID: memberAccountID,
submitterLogin: EMPLOYEE_EMAIL,
report,
chatReport,
reportTransactions: [createMock<Transaction>({reportID: REPORT_ID, amount: 10000})],
originalTransaction: createMock<Transaction>({}),
violations: {},
bankAccountList: {},
policy,
reportActions: [],
});

expect(result.includes(CONST.REPORT.SECONDARY_ACTIONS.CANCEL_PAYMENT)).toBe(false);
});

it('does not include CANCEL_PAYMENT for a report manager who is not the payer on a manual-reimbursement workspace', async () => {
// beforeEach's Onyx.clear isn't awaited; explicit clear + reset guarantees state.
await Onyx.clear();

const managerEmail = 'manager@manual-test.com';
const managerAccountID = 88;
const reimburserEmail = 'owner@manual-test.com';
const report = createMock<Report>({
reportID: REPORT_ID,
type: CONST.REPORT.TYPE.EXPENSE,
policyID: POLICY_ID,
ownerAccountID: EMPLOYEE_ACCOUNT_ID,
// Current user is the report manager but not the payer.
managerID: managerAccountID,
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
statusNum: CONST.REPORT.STATUS_NUM.REIMBURSED,
total: -10000,
isWaitingOnBankAccount: false,
});
const policy = createMock<Policy>({
id: POLICY_ID,
type: CONST.POLICY.TYPE.CORPORATE,
role: CONST.POLICY.ROLE.USER,
owner: reimburserEmail,
reimbursementChoice: CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_MANUAL,
reimburser: reimburserEmail,
outputCurrency: CONST.CURRENCY.USD,
isPolicyExpenseChatEnabled: true,
employeeList: {
[managerEmail]: {role: CONST.POLICY.ROLE.USER},
[reimburserEmail]: {role: CONST.POLICY.ROLE.ADMIN},
},
});

await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy);
await waitForBatchedUpdates();

const result = getSecondaryReportActions({
currentUserLogin: managerEmail,
currentUserAccountID: managerAccountID,
submitterLogin: EMPLOYEE_EMAIL,
report,
chatReport,
reportTransactions: [createMock<Transaction>({reportID: REPORT_ID, amount: 10000})],
originalTransaction: createMock<Transaction>({}),
violations: {},
bankAccountList: {},
policy,
reportActions: [],
});

expect(result.includes(CONST.REPORT.SECONDARY_ACTIONS.CANCEL_PAYMENT)).toBe(false);
});

it('does not include CANCEL_PAYMENT past the NACHA cutoff after a paid-elsewhere payment was cancelled and re-paid via bank', async () => {
// Cancelling doesn't remove the original pay action, so the report keeps a stale ELSEWHERE action next to the
// newer bank one. Cancel eligibility must follow the latest (bank) payment and respect its cutoff, not fall back
// to the paid-elsewhere branch because of the leftover action.
await Onyx.clear();

const report = createMock<Report>({
reportID: REPORT_ID,
type: CONST.REPORT.TYPE.EXPENSE,
ownerAccountID: EMPLOYEE_ACCOUNT_ID,
managerID: EMPLOYEE_ACCOUNT_ID,
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
statusNum: CONST.REPORT.STATUS_NUM.REIMBURSED,
});
const policy = createMock<Policy>({
role: CONST.POLICY.ROLE.ADMIN,
type: CONST.POLICY.TYPE.TEAM,
reimbursementChoice: CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_MANUAL,
});
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, report);

const elsewherePayAction = createMock<ReportAction>({
reportActionID: 'elsewhere_pay_action',
actionName: CONST.REPORT.ACTIONS.TYPE.IOU,
message: {
type: CONST.IOU.REPORT_ACTION_TYPE.PAY,
paymentType: CONST.IOU.PAYMENT_TYPE.ELSEWHERE,
},
created: '2020-01-01 00:00:00.000',
});
const bankPayAction = createMock<ReportAction>({
reportActionID: 'bank_pay_action',
actionName: CONST.REPORT.ACTIONS.TYPE.IOU,
message: {
type: CONST.IOU.REPORT_ACTION_TYPE.PAY,
paymentType: CONST.IOU.PAYMENT_TYPE.VBBA,
},
// Newer than the elsewhere action, and well past the daily NACHA cutoff.
created: '2020-06-01 12:00:00.000',
});
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, {
[elsewherePayAction.reportActionID]: elsewherePayAction,
[bankPayAction.reportActionID]: bankPayAction,
});
await waitForBatchedUpdates();

const result = getSecondaryReportActions({
currentUserLogin: EMPLOYEE_EMAIL,
currentUserAccountID: EMPLOYEE_ACCOUNT_ID,
submitterLogin: '',
report,
chatReport,
reportTransactions: [],
originalTransaction: createMock<Transaction>({}),
violations: {},
bankAccountList: {},
policy,
});

expect(result.includes(CONST.REPORT.SECONDARY_ACTIONS.CANCEL_PAYMENT)).toBe(false);
});

it('does not include PRINT option when the report is in OPEN state', () => {
const report = createMock<Report>({
reportID: REPORT_ID,
Expand Down
Loading