Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
4 changes: 2 additions & 2 deletions src/components/MoneyReportHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -969,8 +969,8 @@ function MoneyReportHeader({
if (!moneyRequestReport) {
return [];
}
return getSecondaryExportReportActions(moneyRequestReport, policy, exportTemplates);
}, [moneyRequestReport, policy, exportTemplates]);
return getSecondaryExportReportActions(accountID, email ?? '', moneyRequestReport, policy, exportTemplates);
}, [moneyRequestReport, accountID, email, policy, exportTemplates]);

const connectedIntegrationName = connectedIntegration ? translate('workspace.accounting.connectionName', {connectionName: connectedIntegration}) : '';
const unapproveWarningText = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ function MoneyRequestReportPreviewContent({
return getReportPreviewAction(
isIouReportArchived || isChatReportArchived,
currentUserDetails.accountID,
currentUserDetails.email ?? '',
iouReport,
policy,
transactions,
Expand All @@ -501,16 +502,17 @@ function MoneyRequestReportPreviewContent({
isSubmittingAnimationRunning,
);
}, [
isPaidAnimationRunning,
isApprovedAnimationRunning,
isSubmittingAnimationRunning,
isIouReportArchived,
isChatReportArchived,
currentUserDetails.accountID,
currentUserDetails.email,
iouReport,
policy,
transactions,
isIouReportArchived,
invoiceReceiverPolicy,
isChatReportArchived,
currentUserDetails.accountID,
isPaidAnimationRunning,
isApprovedAnimationRunning,
isSubmittingAnimationRunning,
]);

const addExpenseDropdownOptions = useMemo(
Expand Down
13 changes: 10 additions & 3 deletions src/libs/Firebase/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// We have opted for `Onyx.connectWithoutView` here as this logic is strictly non-UI in nature.
import Onyx from 'react-native-onyx';
import * as SessionUtils from '@libs/SessionUtils';
import type {OnyxEntry} from 'react-native-onyx';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Session} from '@src/types/onyx';
import type {PerfAttributes} from './types';

let reportsCount = 0;
Expand Down Expand Up @@ -57,9 +58,15 @@ Onyx.connectWithoutView({
},
});

function getAttributes<T extends keyof PerfAttributes>(attributes?: T[]): Pick<PerfAttributes, T> {
const session = SessionUtils.getSession();
let session: OnyxEntry<Session>;
Onyx.connectWithoutView({
key: ONYXKEYS.SESSION,
callback: (value) => {
session = value;
},
});

function getAttributes<T extends keyof PerfAttributes>(attributes?: T[]): Pick<PerfAttributes, T> {
const allAttributes: PerfAttributes = {
accountId: session?.accountID?.toString() ?? 'N/A',
reportsLength: reportsCount.toString(),
Expand Down
43 changes: 5 additions & 38 deletions src/libs/NextStepUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ function buildOptimisticNextStep(params: BuildNextStepNewParams): ReportNextStep
nextStep = {
messageKey: CONST.NEXT_STEP.MESSAGE_KEY.WAITING_TO_PAY,
icon: CONST.NEXT_STEP.ICONS.HOURGLASS,
actorAccountID: isPayer({accountID: currentUserAccountIDParam, email: currentUserEmailParam}, report) ? currentUserAccountIDParam : -1,
actorAccountID: isPayer(currentUserAccountIDParam, currentUserEmailParam, report) ? currentUserAccountIDParam : -1,
};
}
break;
Expand All @@ -214,16 +214,7 @@ function buildOptimisticNextStep(params: BuildNextStepNewParams): ReportNextStep

// Generates an optimistic nextStep once a report has been approved
case CONST.REPORT.STATUS_NUM.APPROVED:
if (
isInvoiceReport(report) ||
!isPayer(
{
accountID: currentUserAccountIDParam,
email: currentUserEmailParam,
},
report,
)
) {
if (isInvoiceReport(report) || !isPayer(currentUserAccountIDParam, currentUserEmailParam, report)) {
nextStep = nextStepNoActionRequired;
break;
}
Expand Down Expand Up @@ -595,13 +586,7 @@ function buildNextStepNew(params: BuildNextStepNewParams): ReportNextStepDepreca
{
text: 'Waiting for ',
},
isPayer(
{
accountID: currentUserAccountIDParam,
email: currentUserEmailParam,
},
report,
)
isPayer(currentUserAccountIDParam, currentUserEmailParam, report)
? {
text: `you`,
type: 'strong',
Expand Down Expand Up @@ -638,32 +623,14 @@ function buildNextStepNew(params: BuildNextStepNewParams): ReportNextStepDepreca

// Generates an optimistic nextStep once a report has been approved
case CONST.REPORT.STATUS_NUM.APPROVED: {
if (
isInvoiceReport(report) ||
!isPayer(
{
accountID: currentUserAccountIDParam,
email: currentUserEmailParam,
},
report,
) ||
reimbursableSpend === 0
) {
if (isInvoiceReport(report) || !isPayer(currentUserAccountIDParam, currentUserEmailParam, report) || reimbursableSpend === 0) {
optimisticNextStep = noActionRequired;

break;
}
// Self review
let payerMessage: Message;
if (
isPayer(
{
accountID: currentUserAccountIDParam,
email: currentUserEmailParam,
},
report,
)
) {
if (isPayer(currentUserAccountIDParam, currentUserEmailParam, report)) {
payerMessage = {text: 'you', type: 'strong'};
} else if (reimburserAccountID === -1) {
payerMessage = {text: 'an admin'};
Expand Down
8 changes: 4 additions & 4 deletions src/libs/ReportPreviewActionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
isReportApproved,
isSettled,
} from './ReportUtils';
import {getSession} from './SessionUtils';
import {isPending, isScanning} from './TransactionUtils';

function canSubmit(report: Report, isReportArchived: boolean, currentUserAccountID: number, policy?: Policy, transactions?: Transaction[]) {
Expand Down Expand Up @@ -74,12 +73,12 @@ function canApprove(report: Report, currentUserAccountID: number, policy?: Polic
return isExpense && isProcessing && !!isApprovalEnabled && reportTransactions.length > 0 && isCurrentUserManager;
}

function canPay(report: Report, isReportArchived: boolean, currentUserAccountID: number, policy?: Policy, invoiceReceiverPolicy?: Policy) {
function canPay(report: Report, isReportArchived: boolean, currentUserAccountID: number, currentUserEmail: string, policy?: Policy, invoiceReceiverPolicy?: Policy) {
if (isReportArchived) {
return false;
}

const isReportPayer = isPayer(getSession(), report, false, policy);
const isReportPayer = isPayer(currentUserAccountID, currentUserEmail, report, false, policy);
const isExpense = isExpenseReport(report);
const isPaymentsEnabled = arePaymentsEnabled(policy);
const isProcessing = isProcessingReport(report);
Expand Down Expand Up @@ -152,6 +151,7 @@ function canExport(report: Report, policy?: Policy) {
function getReportPreviewAction(
isReportArchived: boolean,
currentUserAccountID: number,
currentUserEmail: string,
report: Report | undefined,
policy: Policy | undefined,
transactions: Transaction[],
Expand Down Expand Up @@ -183,7 +183,7 @@ function getReportPreviewAction(
if (canApprove(report, currentUserAccountID, policy, transactions)) {
return CONST.REPORT.REPORT_PREVIEW_ACTIONS.APPROVE;
}
if (canPay(report, isReportArchived, currentUserAccountID, policy, invoiceReceiverPolicy)) {
if (canPay(report, isReportArchived, currentUserAccountID, currentUserEmail, policy, invoiceReceiverPolicy)) {
return CONST.REPORT.REPORT_PREVIEW_ACTIONS.PAY;
}
if (canExport(report, policy)) {
Expand Down
18 changes: 13 additions & 5 deletions src/libs/ReportPrimaryActionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import {
isReportManager,
isSettled,
} from './ReportUtils';
import {getSession} from './SessionUtils';
import {
allHavePendingRTERViolation,
getTransactionViolations,
Expand Down Expand Up @@ -139,12 +138,20 @@ function isApproveAction(report: Report, reportTransactions: Transaction[], poli
return isProcessingReportUtils(report);
}

function isPrimaryPayAction(report: Report, policy?: Policy, reportNameValuePairs?: ReportNameValuePairs, isChatReportArchived?: boolean, invoiceReceiverPolicy?: Policy) {
function isPrimaryPayAction(
report: Report,
currentUserAccountID: number,
currentUserEmail: string,
policy?: Policy,
reportNameValuePairs?: ReportNameValuePairs,
isChatReportArchived?: boolean,
invoiceReceiverPolicy?: Policy,
) {
if (isArchivedReport(reportNameValuePairs) || isChatReportArchived) {
return false;
}
const isExpenseReport = isExpenseReportUtils(report);
const isReportPayer = isPayer(getSession(), report, false, policy);
const isReportPayer = isPayer(currentUserAccountID, currentUserEmail, report, false, policy);
const arePaymentsEnabled = arePaymentsEnabledUtils(policy);
const isReportApproved = isReportApprovedUtils({report});
const isReportClosed = isClosedReportUtils(report);
Expand Down Expand Up @@ -389,7 +396,8 @@ function getReportPrimaryAction(params: GetReportPrimaryActionParams): ValueOf<t
return '';
}

const isPayActionWithAllExpensesHeld = isPrimaryPayAction(report, policy, reportNameValuePairs, isChatReportArchived) && hasOnlyHeldExpenses(report?.reportID);
const isPayActionWithAllExpensesHeld =
isPrimaryPayAction(report, currentUserAccountID, currentUserEmail, policy, reportNameValuePairs, isChatReportArchived) && hasOnlyHeldExpenses(report?.reportID);
const expensesToHold = getAllExpensesToHoldIfApplicable(report, reportActions, reportTransactions, policy);

if (isMarkAsCashAction(currentUserEmail, currentUserAccountID, report, reportTransactions, violations, policy)) {
Expand All @@ -415,7 +423,7 @@ function getReportPrimaryAction(params: GetReportPrimaryActionParams): ValueOf<t
return CONST.REPORT.PRIMARY_ACTIONS.SUBMIT;
}

if (isPrimaryPayAction(report, policy, reportNameValuePairs, isChatReportArchived, invoiceReceiverPolicy)) {
if (isPrimaryPayAction(report, currentUserAccountID, currentUserEmail, policy, reportNameValuePairs, isChatReportArchived, invoiceReceiverPolicy)) {
return CONST.REPORT.PRIMARY_ACTIONS.PAY;
}

Expand Down
29 changes: 17 additions & 12 deletions src/libs/ReportSecondaryActionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import {
isSettled,
isWorkspaceEligibleForReportChange,
} from './ReportUtils';
import {getSession} from './SessionUtils';
import {
allHavePendingRTERViolation,
getOriginalTransactionWithSplitInfo,
Expand Down Expand Up @@ -276,15 +275,15 @@ function isUnapproveAction(currentUserLogin: string, report: Report, policy?: Po
return isReportApprover;
}

function isCancelPaymentAction(report: Report, reportTransactions: Transaction[], policy?: Policy): boolean {
function isCancelPaymentAction(currentAccountID: number, currentUserEmail: string, report: Report, reportTransactions: Transaction[], policy?: Policy): boolean {
const isExpenseReport = isExpenseReportUtils(report);

if (!isExpenseReport) {
return false;
}

const isAdmin = policy?.role === CONST.POLICY.ROLE.ADMIN;
const isPayer = isPayerUtils(getSession(), report, false, policy);
const isPayer = isPayerUtils(currentAccountID, currentUserEmail, report, false, policy);

if (!isAdmin || !isPayer) {
return false;
Expand Down Expand Up @@ -317,7 +316,7 @@ function isCancelPaymentAction(report: Report, reportTransactions: Transaction[]
return isPaymentProcessing && !hasDailyNachaCutoffPassed;
}

function isExportAction(report: Report, policy?: Policy): boolean {
function isExportAction(currentAccountID: number, currentUserEmail: string, report: Report, policy?: Policy): boolean {
if (!policy) {
return false;
}
Expand All @@ -341,7 +340,7 @@ function isExportAction(report: Report, policy?: Policy): boolean {
}

const isReportApproved = isReportApprovedUtils({report});
const isReportPayer = isPayerUtils(getSession(), report, false, policy);
const isReportPayer = isPayerUtils(currentAccountID, currentUserEmail, report, false, policy);
const arePaymentsEnabled = arePaymentsEnabledUtils(policy);
const isReportClosed = isClosedReportUtils(report);

Expand All @@ -359,7 +358,7 @@ function isExportAction(report: Report, policy?: Policy): boolean {
return isAdmin && isReportFinished && syncEnabled;
}

function isMarkAsExportedAction(report: Report, policy?: Policy): boolean {
function isMarkAsExportedAction(currentAccountID: number, currentUserEmail: string, report: Report, policy?: Policy): boolean {
if (!policy) {
return false;
}
Expand All @@ -382,7 +381,7 @@ function isMarkAsExportedAction(report: Report, policy?: Policy): boolean {
return false;
}

const isReportPayer = isPayerUtils(getSession(), report, false, policy);
const isReportPayer = isPayerUtils(currentAccountID, currentUserEmail, report, false, policy);
const arePaymentsEnabled = arePaymentsEnabledUtils(policy);
const isReportApproved = isReportApprovedUtils({report});
const isReportClosed = isClosedReportUtils(report);
Expand Down Expand Up @@ -654,7 +653,7 @@ function getSecondaryReportActions({
}): Array<ValueOf<typeof CONST.REPORT.SECONDARY_ACTIONS>> {
const options: Array<ValueOf<typeof CONST.REPORT.SECONDARY_ACTIONS>> = [];

if (isPrimaryPayAction(report, policy, reportNameValuePairs) && hasOnlyHeldExpenses(report?.reportID)) {
if (isPrimaryPayAction(report, currentUserAccountID, currentUserEmail, policy, reportNameValuePairs) && hasOnlyHeldExpenses(report?.reportID)) {
options.push(CONST.REPORT.SECONDARY_ACTIONS.PAY);
}

Expand Down Expand Up @@ -687,7 +686,7 @@ function getSecondaryReportActions({
options.push(CONST.REPORT.SECONDARY_ACTIONS.UNAPPROVE);
}

if (isCancelPaymentAction(report, reportTransactions, policy)) {
if (isCancelPaymentAction(currentUserAccountID, currentUserEmail, report, reportTransactions, policy)) {
options.push(CONST.REPORT.SECONDARY_ACTIONS.CANCEL_PAYMENT);
}

Expand Down Expand Up @@ -743,13 +742,19 @@ function getSecondaryReportActions({
return options;
}

function getSecondaryExportReportActions(report: Report, policy?: Policy, exportTemplates: ExportTemplate[] = []): Array<ValueOf<string>> {
function getSecondaryExportReportActions(
currentUserAccountID: number,
currentUserEmail: string,
report: Report,
policy?: Policy,
exportTemplates: ExportTemplate[] = [],
): Array<ValueOf<string>> {
const options: Array<ValueOf<string>> = [];
if (isExportAction(report, policy)) {
if (isExportAction(currentUserAccountID, currentUserEmail, report, policy)) {
options.push(CONST.REPORT.EXPORT_OPTIONS.EXPORT_TO_INTEGRATION);
}

if (isMarkAsExportedAction(report, policy)) {
if (isMarkAsExportedAction(currentUserAccountID, currentUserEmail, report, policy)) {
options.push(CONST.REPORT.EXPORT_OPTIONS.MARK_AS_EXPORTED);
}

Expand Down
13 changes: 9 additions & 4 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ import type {
ReportNameValuePairs,
ReportViolationName,
ReportViolations,
Session,
Task,
Transaction,
TransactionViolation,
Expand Down Expand Up @@ -2639,11 +2638,17 @@ function isOneOnOneChat(report: OnyxEntry<Report>): boolean {
* Checks if the current user is a payer of the expense
*/

function isPayer(session: OnyxEntry<Session>, iouReport: OnyxEntry<Report>, onlyShowPayElsewhere = false, reportPolicy?: OnyxInputOrEntry<Policy>) {
function isPayer(
currentAccountID: number | undefined,
currentUserEmailParm: string | undefined,
iouReport: OnyxEntry<Report>,
onlyShowPayElsewhere = false,
reportPolicy?: OnyxInputOrEntry<Policy>,
) {
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 isManager = iouReport?.managerID === currentAccountID;
const reimbursementChoice = policy?.reimbursementChoice;

if (isPaidGroupPolicy(iouReport)) {
Expand All @@ -2653,7 +2658,7 @@ function isPayer(session: OnyxEntry<Session>, iouReport: OnyxEntry<Report>, only
}

// 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;
const isReimburser = currentUserEmailParm === policy?.achAccount?.reimburser;
return isReimburser;
}
if (reimbursementChoice === CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_MANUAL || onlyShowPayElsewhere) {
Expand Down
Loading
Loading