diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index d0b324a78851..6389c84019d9 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -167,6 +167,7 @@ import { isPolicyAdmin as isPolicyAdminPolicyUtils, isPolicyAuditor, isPolicyMember, + isPolicyMemberWithoutPendingDelete, isPolicyOwner, isSubmitAndClose, shouldShowPolicy, @@ -2734,7 +2735,23 @@ function isPayer( reportPolicy?: OnyxInputOrEntry, onlyShowPayElsewhere = false, ) { - const policy = reportPolicy ?? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${iouReport?.policyID}`] ?? null; + const policy = reportPolicy ?? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${iouReport?.policyID}`]; + + // If the report belongs to a workspace, verify the user is still a member + // When a user leaves a workspace, they may no longer have access to the policy data, + // or the policy.role/employeeList may be stale + if (iouReport?.policyID && iouReport.policyID !== CONST.POLICY.ID_FAKE) { + // No policy data means user likely left the workspace + if (!policy) { + return false; + } + // For paid group policies, verify membership via employeeList (only when loaded) + // employeeList may be lazily loaded/partial, so only check if it's present + if (isPaidGroupPolicy(iouReport) && !isEmptyObject(policy?.employeeList) && !isPolicyMemberWithoutPendingDelete(currentUserEmailParam, policy)) { + return false; + } + } + const policyType = policy?.type; const isAdmin = policyType !== CONST.POLICY.TYPE.PERSONAL && policy?.role === CONST.POLICY.ROLE.ADMIN; const isManager = iouReport?.managerID === currentAccountID; diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index 5950a415ff4a..aa06457cc93a 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -8447,196 +8447,248 @@ describe('actions/IOU', () => { let chatReport: OnyxEntry; let policy: OnyxEntry; - return waitForBatchedUpdates() - .then(() => { - createWorkspace({ - policyOwnerEmail: CARLOS_EMAIL, - makeMeAdmin: true, - policyName: "Carlos's Workspace", - policyID: undefined, - engagementChoice: CONST.ONBOARDING_CHOICES.CHAT_SPLIT, - introSelected: {choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM}, - currentUserAccountIDParam: CARLOS_ACCOUNT_ID, - currentUserEmailParam: CARLOS_EMAIL, - }); - return waitForBatchedUpdates(); - }) - .then( - () => - new Promise((resolve) => { - const connection = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (allReports) => { - Onyx.disconnect(connection); - chatReport = Object.values(allReports ?? {}).find((report) => report?.chatType === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT); - resolve(); - }, - }); - }), - ) - .then(() => { - if (chatReport) { - requestMoney({ - report: chatReport, - participantParams: { - payeeEmail: RORY_EMAIL, - payeeAccountID: RORY_ACCOUNT_ID, - participant: {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID, isPolicyExpenseChat: true, reportID: chatReport.reportID}, - }, - transactionParams: { - amount, - attendees: [], - currency: CONST.CURRENCY.USD, - created: '', - merchant, - comment, - reimbursable: true, - }, - shouldGenerateTransactionThreadReport: true, - isASAPSubmitBetaEnabled: false, - currentUserAccountIDParam: 123, - currentUserEmailParam: 'existing@example.com', - transactionViolations: {}, - policyRecentlyUsedCurrencies: [], - isSelfTourViewed: false, - quickAction: undefined, - betas: [], - personalDetails: {}, + return ( + waitForBatchedUpdates() + .then(() => { + createWorkspace({ + policyOwnerEmail: CARLOS_EMAIL, + makeMeAdmin: true, + policyName: "Carlos's Workspace", + policyID: undefined, + engagementChoice: CONST.ONBOARDING_CHOICES.CHAT_SPLIT, + introSelected: {choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM}, + currentUserAccountIDParam: CARLOS_ACCOUNT_ID, + currentUserEmailParam: CARLOS_EMAIL, }); - } - return waitForBatchedUpdates(); - }) - .then( - () => - new Promise((resolve) => { - const connection = Onyx.connect({ - key: ONYXKEYS.COLLECTION.POLICY, - waitForCollectionCallback: true, - callback: (allPolicies) => { - Onyx.disconnect(connection); - policy = Object.values(allPolicies ?? {}).find((p): p is OnyxEntry => p?.name === "Carlos's Workspace"); - expect(policy).toBeTruthy(); - resolve(); + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connection = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connection); + chatReport = Object.values(allReports ?? {}).find((report) => report?.chatType === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT); + resolve(); + }, + }); + }), + ) + .then(() => { + if (chatReport) { + requestMoney({ + report: chatReport, + participantParams: { + payeeEmail: RORY_EMAIL, + payeeAccountID: RORY_ACCOUNT_ID, + participant: {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID, isPolicyExpenseChat: true, reportID: chatReport.reportID}, }, - }); - }), - ) - .then( - () => - new Promise((resolve) => { - const connection = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (allReports) => { - Onyx.disconnect(connection); - expenseReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.EXPENSE); - - Onyx.merge(`report_${expenseReport?.reportID}`, { - statusNum: 0, - stateNum: 0, - }); - resolve(); - - expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], true)).toBe(true); - expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], false)).toBe(false); + transactionParams: { + amount, + attendees: [], + currency: CONST.CURRENCY.USD, + created: '', + merchant, + comment, + reimbursable: true, }, + shouldGenerateTransactionThreadReport: true, + isASAPSubmitBetaEnabled: false, + currentUserAccountIDParam: 123, + currentUserEmailParam: 'existing@example.com', + transactionViolations: {}, + policyRecentlyUsedCurrencies: [], + isSelfTourViewed: false, + quickAction: undefined, + betas: [], + personalDetails: {}, }); - }), - ) - .then( - () => - new Promise((resolve) => { - const connection = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (allReports) => { - Onyx.disconnect(connection); - expenseReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.EXPENSE); - - resolve(); - // Verify report is a draft - expect(expenseReport?.stateNum).toBe(0); - expect(expenseReport?.statusNum).toBe(0); - - expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], true)).toBe(false); - expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], false)).toBe(false); + } + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connection = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connection); + chatReport = Object.values(allReports ?? {}).find((report) => report?.chatType === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT); + resolve(); + }, + }); + }), + ) + .then(() => { + if (chatReport) { + requestMoney({ + report: chatReport, + participantParams: { + payeeEmail: RORY_EMAIL, + payeeAccountID: RORY_ACCOUNT_ID, + participant: {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID, isPolicyExpenseChat: true, reportID: chatReport.reportID}, + }, + transactionParams: { + amount, + attendees: [], + currency: CONST.CURRENCY.USD, + created: '', + merchant, + comment, + reimbursable: true, }, + shouldGenerateTransactionThreadReport: true, + isASAPSubmitBetaEnabled: false, + currentUserAccountIDParam: 123, + currentUserEmailParam: 'existing@example.com', + transactionViolations: {}, + policyRecentlyUsedCurrencies: [], + isSelfTourViewed: false, + quickAction: undefined, + betas: [], + personalDetails: {}, }); - }), - ) - .then(async () => { - if (expenseReport) { - const nextStep = await getOnyxValue(`${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`); - submitReport(expenseReport, policy, CARLOS_ACCOUNT_ID, CARLOS_EMAIL, true, true, nextStep); - } - return waitForBatchedUpdates(); - }) - .then( - () => - new Promise((resolve) => { - const connection = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (allReports) => { - Onyx.disconnect(connection); - expenseReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.EXPENSE); + } + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connection = Onyx.connect({ + key: ONYXKEYS.COLLECTION.POLICY, + waitForCollectionCallback: true, + callback: (allPolicies) => { + Onyx.disconnect(connection); + policy = Object.values(allPolicies ?? {}).find((p): p is OnyxEntry => p?.name === "Carlos's Workspace"); + expect(policy).toBeTruthy(); + resolve(); + }, + }); + }), + ) + .then( + () => + new Promise((resolve) => { + const connection = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connection); + expenseReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.EXPENSE); - resolve(); - // Report is closed since the default policy settings is Submit and Close - expect(expenseReport?.stateNum).toBe(2); - expect(expenseReport?.statusNum).toBe(2); + Onyx.merge(`report_${expenseReport?.reportID}`, { + statusNum: 0, + stateNum: 0, + }); + resolve(); + }, + }); + }), + ) + // Switch session to Carlos (admin) to test canIOUBePaid from the payer's perspective + .then(() => Onyx.merge(ONYXKEYS.SESSION, {accountID: CARLOS_ACCOUNT_ID, email: CARLOS_EMAIL})) + .then(() => waitForBatchedUpdates()) + .then(() => { + expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], true)).toBe(true); + expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], false)).toBe(false); + }) + .then( + () => + new Promise((resolve) => { + const connection = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connection); + expenseReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.EXPENSE); - expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], true)).toBe(true); - expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], false)).toBe(false); - }, - }); - }), - ) - .then(() => { - if (policy) { - const reportToArchive = []; + resolve(); + // Verify report is a draft + expect(expenseReport?.stateNum).toBe(0); + expect(expenseReport?.statusNum).toBe(0); + + expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], true)).toBe(false); + expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], false)).toBe(false); + }, + }); + }), + ) + .then(async () => { if (expenseReport) { - reportToArchive.push(expenseReport); - } - if (chatReport) { - reportToArchive.push(chatReport); + const nextStep = await getOnyxValue(`${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`); + submitReport(expenseReport, policy, CARLOS_ACCOUNT_ID, CARLOS_EMAIL, true, true, nextStep); } + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connection = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connection); + expenseReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.EXPENSE); - deleteWorkspace({ - policyID: policy.id, - personalPolicyID: undefined, - activePolicyID: undefined, - policyName: policy.name, - lastAccessedWorkspacePolicyID: undefined, - policyCardFeeds: undefined, - reportsToArchive: reportToArchive, - transactionViolations: undefined, - reimbursementAccountError: undefined, - bankAccountList: {}, - lastUsedPaymentMethods: undefined, - localeCompare, - }); - } - return waitForBatchedUpdates(); - }) - .then( - () => - new Promise((resolve) => { - const connection = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (allReports) => { - Onyx.disconnect(connection); - chatReport = Object.values(allReports ?? {}).find((report) => report?.chatType === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT); + resolve(); + // Report is closed since the default policy settings is Submit and Close + expect(expenseReport?.stateNum).toBe(2); + expect(expenseReport?.statusNum).toBe(2); - expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], true)).toBe(false); - expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], false)).toBe(false); - resolve(); - }, + expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], true)).toBe(true); + expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], false)).toBe(false); + }, + }); + }), + ) + .then(() => { + if (policy) { + const reportToArchive = []; + if (expenseReport) { + reportToArchive.push(expenseReport); + } + if (chatReport) { + reportToArchive.push(chatReport); + } + + deleteWorkspace({ + policyID: policy.id, + personalPolicyID: undefined, + activePolicyID: undefined, + policyName: policy.name, + lastAccessedWorkspacePolicyID: undefined, + policyCardFeeds: undefined, + reportsToArchive: reportToArchive, + transactionViolations: undefined, + reimbursementAccountError: undefined, + bankAccountList: {}, + lastUsedPaymentMethods: undefined, + localeCompare, }); - }), - ); + } + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connection = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connection); + chatReport = Object.values(allReports ?? {}).find((report) => report?.chatType === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT); + + expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], true)).toBe(false); + expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], false)).toBe(false); + resolve(); + }, + }); + }), + ) + ); }); it('correctly implements error handling', () => { const amount = 10000; @@ -8646,149 +8698,201 @@ describe('actions/IOU', () => { let chatReport: OnyxEntry; let policy: OnyxEntry; - return waitForBatchedUpdates() - .then(() => { - createWorkspace({ - policyOwnerEmail: CARLOS_EMAIL, - makeMeAdmin: true, - policyName: "Carlos's Workspace", - policyID: undefined, - engagementChoice: CONST.ONBOARDING_CHOICES.CHAT_SPLIT, - introSelected: {choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM}, - currentUserAccountIDParam: CARLOS_ACCOUNT_ID, - currentUserEmailParam: CARLOS_EMAIL, - }); - return waitForBatchedUpdates(); - }) - .then( - () => - new Promise((resolve) => { - const connection = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (allReports) => { - Onyx.disconnect(connection); - chatReport = Object.values(allReports ?? {}).find((report) => report?.chatType === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT); - resolve(); - }, - }); - }), - ) - .then(() => { - if (chatReport) { - requestMoney({ - report: chatReport, - participantParams: { - payeeEmail: RORY_EMAIL, - payeeAccountID: RORY_ACCOUNT_ID, - participant: {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID, isPolicyExpenseChat: true, reportID: chatReport.reportID}, - }, - transactionParams: { - amount, - attendees: [], - currency: CONST.CURRENCY.USD, - created: '', - merchant, - comment, - reimbursable: true, - }, - shouldGenerateTransactionThreadReport: true, - isASAPSubmitBetaEnabled: false, - currentUserAccountIDParam: 123, - currentUserEmailParam: 'existing@example.com', - transactionViolations: {}, - policyRecentlyUsedCurrencies: [], - isSelfTourViewed: false, - quickAction: undefined, - betas: [], - personalDetails: {}, + return ( + waitForBatchedUpdates() + .then(() => { + createWorkspace({ + policyOwnerEmail: CARLOS_EMAIL, + makeMeAdmin: true, + policyName: "Carlos's Workspace", + policyID: undefined, + engagementChoice: CONST.ONBOARDING_CHOICES.CHAT_SPLIT, + introSelected: {choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM}, + currentUserAccountIDParam: CARLOS_ACCOUNT_ID, + currentUserEmailParam: CARLOS_EMAIL, }); - } - return waitForBatchedUpdates(); - }) - .then( - () => - new Promise((resolve) => { - const connection = Onyx.connect({ - key: ONYXKEYS.COLLECTION.POLICY, - waitForCollectionCallback: true, - callback: (allPolicies) => { - Onyx.disconnect(connection); - policy = Object.values(allPolicies ?? {}).find((p): p is OnyxEntry => p?.name === "Carlos's Workspace"); - expect(policy).toBeTruthy(); - resolve(); + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connection = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connection); + chatReport = Object.values(allReports ?? {}).find((report) => report?.chatType === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT); + resolve(); + }, + }); + }), + ) + .then(() => { + if (chatReport) { + requestMoney({ + report: chatReport, + participantParams: { + payeeEmail: RORY_EMAIL, + payeeAccountID: RORY_ACCOUNT_ID, + participant: {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID, isPolicyExpenseChat: true, reportID: chatReport.reportID}, }, - }); - }), - ) - .then( - () => - new Promise((resolve) => { - const connection = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (allReports) => { - Onyx.disconnect(connection); - expenseReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.EXPENSE); - - Onyx.merge(`report_${expenseReport?.reportID}`, { - statusNum: 0, - stateNum: 0, - }); - - expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], true)).toBe(true); - expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], false)).toBe(false); - resolve(); + transactionParams: { + amount, + attendees: [], + currency: CONST.CURRENCY.USD, + created: '', + merchant, + comment, + reimbursable: true, }, + shouldGenerateTransactionThreadReport: true, + isASAPSubmitBetaEnabled: false, + currentUserAccountIDParam: 123, + currentUserEmailParam: 'existing@example.com', + transactionViolations: {}, + policyRecentlyUsedCurrencies: [], + isSelfTourViewed: false, + quickAction: undefined, + betas: [], + personalDetails: {}, }); - }), - ) - .then( - () => - new Promise((resolve) => { - const connection = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (allReports) => { - Onyx.disconnect(connection); - expenseReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.EXPENSE); - - // Verify report is a draft - expect(expenseReport?.stateNum).toBe(0); - expect(expenseReport?.statusNum).toBe(0); - resolve(); + } + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connection = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connection); + chatReport = Object.values(allReports ?? {}).find((report) => report?.chatType === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT); + resolve(); + }, + }); + }), + ) + .then(() => { + if (chatReport) { + requestMoney({ + report: chatReport, + participantParams: { + payeeEmail: RORY_EMAIL, + payeeAccountID: RORY_ACCOUNT_ID, + participant: {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID, isPolicyExpenseChat: true, reportID: chatReport.reportID}, }, - }); - }), - ) - .then(async () => { - mockFetch?.fail?.(); - if (expenseReport) { - const nextStep = await getOnyxValue(`${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`); - submitReport(expenseReport, {} as Policy, CARLOS_ACCOUNT_ID, CARLOS_EMAIL, true, true, nextStep); - } - return waitForBatchedUpdates(); - }) - .then( - () => - new Promise((resolve) => { - const connection = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (allReports) => { - Onyx.disconnect(connection); - expenseReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.EXPENSE); - - // Report was submitted with some fail - expect(expenseReport?.stateNum).toBe(0); - expect(expenseReport?.statusNum).toBe(0); - expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], true)).toBe(false); - expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], false)).toBe(false); - resolve(); + transactionParams: { + amount, + attendees: [], + currency: CONST.CURRENCY.USD, + created: '', + merchant, + comment, + reimbursable: true, }, + shouldGenerateTransactionThreadReport: true, + isASAPSubmitBetaEnabled: false, + currentUserAccountIDParam: 123, + currentUserEmailParam: 'existing@example.com', + transactionViolations: {}, + policyRecentlyUsedCurrencies: [], + isSelfTourViewed: false, + quickAction: undefined, + betas: [], + personalDetails: {}, }); - }), - ); + } + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connection = Onyx.connect({ + key: ONYXKEYS.COLLECTION.POLICY, + waitForCollectionCallback: true, + callback: (allPolicies) => { + Onyx.disconnect(connection); + policy = Object.values(allPolicies ?? {}).find((p): p is OnyxEntry => p?.name === "Carlos's Workspace"); + expect(policy).toBeTruthy(); + resolve(); + }, + }); + }), + ) + .then( + () => + new Promise((resolve) => { + const connection = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connection); + expenseReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.EXPENSE); + + Onyx.merge(`report_${expenseReport?.reportID}`, { + statusNum: 0, + stateNum: 0, + }); + resolve(); + }, + }); + }), + ) + // Switch session to Carlos (admin) to test canIOUBePaid from the payer's perspective + .then(() => Onyx.merge(ONYXKEYS.SESSION, {accountID: CARLOS_ACCOUNT_ID, email: CARLOS_EMAIL})) + .then(() => waitForBatchedUpdates()) + .then(() => { + expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], true)).toBe(true); + expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], false)).toBe(false); + }) + .then( + () => + new Promise((resolve) => { + const connection = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connection); + expenseReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.EXPENSE); + + // Verify report is a draft + expect(expenseReport?.stateNum).toBe(0); + expect(expenseReport?.statusNum).toBe(0); + resolve(); + }, + }); + }), + ) + .then(async () => { + mockFetch?.fail?.(); + if (expenseReport) { + const nextStep = await getOnyxValue(`${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`); + submitReport(expenseReport, {} as Policy, CARLOS_ACCOUNT_ID, CARLOS_EMAIL, true, true, nextStep); + } + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connection = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connection); + expenseReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.EXPENSE); + + // Report was submitted with some fail + expect(expenseReport?.stateNum).toBe(0); + expect(expenseReport?.statusNum).toBe(0); + expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], true)).toBe(false); + expect(canIOUBePaid(expenseReport, chatReport, policy, {}, [], false)).toBe(false); + resolve(); + }, + }); + }), + ) + ); }); it('should not set stateNum, statusNum, or nextStep optimistically when submitting with Dynamic External Workflow policy', () => { @@ -9873,10 +9977,11 @@ describe('actions/IOU', () => { describe('canCancelPayment', () => { it('should return true if the report is waiting for a bank account', () => { + // Using ID_FAKE to test the isWaitingOnBankAccount logic without workspace membership concerns const fakeReport: Report = { ...createRandomReport(1, undefined), type: CONST.REPORT.TYPE.EXPENSE, - policyID: 'A', + policyID: CONST.POLICY.ID_FAKE, stateNum: CONST.REPORT.STATE_NUM.APPROVED, statusNum: CONST.REPORT.STATUS_NUM.APPROVED, isWaitingOnBankAccount: true, diff --git a/tests/unit/ReportUtilsTest.ts b/tests/unit/ReportUtilsTest.ts index 4e7ec819bd8d..a8e96551cf61 100644 --- a/tests/unit/ReportUtilsTest.ts +++ b/tests/unit/ReportUtilsTest.ts @@ -5859,6 +5859,11 @@ describe('ReportUtils', () => { ...policyTest, role: CONST.POLICY.ROLE.ADMIN, reimbursementChoice: CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_YES, + employeeList: { + [adminEmail]: { + role: CONST.POLICY.ROLE.ADMIN, + }, + }, achAccount: { reimburser: reimburserEmail, bankAccountID, @@ -5899,6 +5904,11 @@ describe('ReportUtils', () => { ...policyTest, role: CONST.POLICY.ROLE.ADMIN, reimbursementChoice: CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_YES, + employeeList: { + [adminEmail]: { + role: CONST.POLICY.ROLE.ADMIN, + }, + }, achAccount: { reimburser: reimburserEmail, bankAccountID, @@ -5927,6 +5937,52 @@ describe('ReportUtils', () => { expect(isPayer(adminAccountID, adminEmail, report, bankAccountList, policyWithAch, false)).toBe(false); }); + + it('should return false when user left workspace and policy is not available', async () => { + // Clear the policy to simulate user leaving workspace + await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}left-workspace`, null); + + const reportWithMissingPolicy: Report = { + ...createRandomReport(6, undefined), + type: CONST.REPORT.TYPE.EXPENSE, + stateNum: CONST.REPORT.STATE_NUM.APPROVED, + statusNum: CONST.REPORT.STATUS_NUM.APPROVED, + policyID: 'left-workspace', + managerID: currentUserAccountID, + }; + + // User should not be a payer when policy is not available (they likely left the workspace) + expect(isPayer(currentUserAccountID, currentUserEmail, reportWithMissingPolicy, undefined, undefined, false)).toBe(false); + }); + + it('should return false when user is not in employeeList of a paid group policy', async () => { + const otherUserEmail = 'other@test.com'; + const policyWithoutCurrentUser: Policy = { + ...createRandomPolicy(2), + id: 'policy-without-user', + type: CONST.POLICY.TYPE.CORPORATE, + role: CONST.POLICY.ROLE.ADMIN, // Role might be stale + employeeList: { + [otherUserEmail]: { + role: CONST.POLICY.ROLE.ADMIN, + }, + }, + }; + + await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}policy-without-user`, policyWithoutCurrentUser); + + const reportForNonMember: Report = { + ...createRandomReport(7, undefined), + type: CONST.REPORT.TYPE.EXPENSE, + stateNum: CONST.REPORT.STATE_NUM.APPROVED, + statusNum: CONST.REPORT.STATUS_NUM.APPROVED, + policyID: 'policy-without-user', + managerID: currentUserAccountID, + }; + + // User should not be a payer even if role says admin, because they're not in employeeList + expect(isPayer(currentUserAccountID, currentUserEmail, reportForNonMember, undefined, policyWithoutCurrentUser, false)).toBe(false); + }); }); describe('buildReportNameFromParticipantNames', () => { beforeAll(async () => {