Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/components/MoneyRequestHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function MoneyRequestHeader({reportID: reportIDProp, onBackButtonPress}: MoneyRe
const isParentReportSettled = isSettledReportUtils(parentReport);
const isDuplicate = !isParentReportSettled && isDuplicateTransactionUtils(transaction, email ?? '', accountID, report, ownerLogin, policy, transactionViolations);
const hasPendingRTERViolation = hasPendingRTERViolationTransactionUtils(transactionViolations);
const shouldShowBrokenConnectionViolation = shouldShowBrokenConnectionViolationTransactionUtils(parentReport, policy, transactionViolations);
const shouldShowBrokenConnectionViolation = shouldShowBrokenConnectionViolationTransactionUtils(parentReport, policy, transactionViolations, accountID);

const reportID = report?.reportID;
const isReportInRHP = route.name === SCREENS.RIGHT_MODAL.SEARCH_REPORT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ function MoneyRequestReceiptView({
const doesTransactionHaveReceipt = !!transactionToCheck?.receipt && !isEmptyObject(transactionToCheck?.receipt);
// Empty state for invoices should be displayed only in WideRHP
const shouldShowReceiptEmptyState = (isDisplayedInWideRHP || !isInvoice) && !hasReceipt && !!transactionToCheck && !doesTransactionHaveReceipt;
const isMarkAsCash = parentReport && currentUserLogin ? isMarkAsCashActionForTransaction(currentUserLogin, parentReport, transactionViolations, policy) : false;
const isMarkAsCash = parentReport && currentUserLogin ? isMarkAsCashActionForTransaction(currentUserLogin, currentUserAccountID, parentReport, transactionViolations, policy) : false;

const routeDistanceMeters = transaction?.comment?.customUnit?.routeDistanceMeters;
const distanceUnit = transaction?.comment?.customUnit?.distanceUnit;
Expand Down
3 changes: 2 additions & 1 deletion src/components/ReportActionItem/MoneyRequestView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,8 @@ function MoneyRequestView({
const [isTrackIntentUser] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {selector: isTrackIntentUserSelector});

const {getViolationsForField} = useViolations(transactionViolations ?? [], isTransactionScanning || !isGroupPolicyByType(reportPolicyType));
const isMarkAsCash = parentReport && currentUserEmailParam ? isMarkAsCashActionForTransaction(currentUserEmailParam, parentReport, transactionViolations, policy) : false;
const isMarkAsCash =
parentReport && currentUserEmailParam ? isMarkAsCashActionForTransaction(currentUserEmailParam, currentUserAccountIDParam, parentReport, transactionViolations, policy) : false;
// Need to return undefined when we have pendingAction to avoid the duplicate pending action
const getPendingFieldAction = (fieldPath: TransactionPendingFieldsKey) => (pendingAction ? undefined : transaction?.pendingFields?.[fieldPath]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function TransactionPreviewContent({
const canEdit = isIOUActionType && canEditMoneyRequest(action, transaction, rules, isChatReportArchived, report, policy, reportActions);
const companyCardPageURL = `${environmentURL}/${ROUTES.WORKSPACE_COMPANY_CARDS.getRoute(report?.policyID)}`;
const connectionLink = getBrokenConnectionUrlToFixPersonalCard(personalCardsWithBrokenConnection, environmentURL, buildDynamicRoute);
const isMarkAsCash = parentReport && currentUserLogin ? isMarkAsCashActionForTransaction(currentUserLogin, parentReport, violations, policy) : false;
const isMarkAsCash = parentReport && currentUserLogin ? isMarkAsCashActionForTransaction(currentUserLogin, currentUserAccountID, parentReport, violations, policy) : false;

const violationMessage = firstViolation
? ViolationsUtils.getViolationTranslation({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function TransactionItemRowRBRInner({transaction, violations, report, containerS
const [transactionThreadActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadId}`);
const {login: currentUserLogin} = useCurrentUserPersonalDetails();
const [currentUserAccountID = CONST.DEFAULT_NUMBER_ID] = useOnyx(ONYXKEYS.SESSION, {selector: accountIDSelector});
const isMarkAsCash = parentReport && currentUserLogin && violations ? isMarkAsCashActionForTransaction(currentUserLogin, parentReport, violations, policy) : false;
const isMarkAsCash = parentReport && currentUserLogin && violations ? isMarkAsCashActionForTransaction(currentUserLogin, currentUserAccountID, parentReport, violations, policy) : false;

const canEdit = wasActionTakenByCurrentUser(iouAction, currentUserAccountID);
const RBRMessages = ViolationsUtils.getRBRMessages({
Expand Down
8 changes: 4 additions & 4 deletions src/libs/ReportPrimaryActionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,14 +613,14 @@ function isSubmitViaPDFAction(report: Report, currentUserAccountID: number, poli
return isSubmitPolicy(policy) && isCurrentUserSubmitter(report, currentUserAccountID);
}

function isMarkAsCashActionForTransaction(currentUserLogin: string, parentReport: Report, violations: TransactionViolation[], policy?: Policy): boolean {
function isMarkAsCashActionForTransaction(currentUserLogin: string, currentUserAccountID: number, parentReport: Report, violations: TransactionViolation[], policy?: Policy): boolean {
const hasPendingRTERViolation = hasPendingRTERViolationTransactionUtils(violations);

if (hasPendingRTERViolation) {
return true;
}

const shouldShowBrokenConnectionViolation = shouldShowBrokenConnectionViolationTransactionUtils(parentReport, policy, violations);
const shouldShowBrokenConnectionViolation = shouldShowBrokenConnectionViolationTransactionUtils(parentReport, policy, violations, currentUserAccountID);

if (!shouldShowBrokenConnectionViolation) {
return false;
Expand All @@ -630,7 +630,7 @@ function isMarkAsCashActionForTransaction(currentUserLogin: string, parentReport
return false;
}

const isReportSubmitter = isCurrentUserSubmitter(parentReport);
const isReportSubmitter = isCurrentUserSubmitter(parentReport, currentUserAccountID);
const isReportApprover = isPolicyApprover(policy, currentUserLogin);
const isAdmin = policy?.role === CONST.POLICY.ROLE.ADMIN;

Expand Down Expand Up @@ -664,7 +664,7 @@ function getTransactionThreadPrimaryAction(
return isFromReviewDuplicates ? CONST.REPORT.TRANSACTION_PRIMARY_ACTIONS.KEEP_THIS_ONE : CONST.REPORT.TRANSACTION_PRIMARY_ACTIONS.REVIEW_DUPLICATES;
}

if (isMarkAsCashActionForTransaction(currentUserLogin, parentReport, violations, policy)) {
if (isMarkAsCashActionForTransaction(currentUserLogin, currentUserAccountID, parentReport, violations, policy)) {
return CONST.REPORT.TRANSACTION_PRIMARY_ACTIONS.MARK_AS_CASH;
}

Expand Down
24 changes: 24 additions & 0 deletions src/libs/ReportRoles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Checks whether a given account is the submitter or the manager of a report. This file never imports ReportUtils, so modules
* whose graph reaches ReportUtils (like TransactionUtils/violations) can use them without an import cycle. ReportUtils keeps
* wrapper functions with the same names that fall back to the session account.
*/
import type {Report} from '@src/types/onyx';

import type {OnyxEntry} from 'react-native-onyx';

/**
* Checks if the given user is the submitter of the report.
*/
function isCurrentUserSubmitter(report: OnyxEntry<Report>, currentUserAccountID: number | undefined): boolean {
return !!report && report.ownerAccountID === currentUserAccountID;
}

/**
* Checks if the given user is the manager of the supplied report.
*/
function isReportManager(report: OnyxEntry<Report>, currentUserAccountID: number | undefined): boolean {
return !!(report && report.managerID === currentUserAccountID);
}

export {isCurrentUserSubmitter, isReportManager};
54 changes: 54 additions & 0 deletions src/libs/ReportTypeAndStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Checks on a report's type, state and status. This file never imports ReportUtils, so modules whose graph reaches ReportUtils
* (like TransactionUtils/violations) can use them without an import cycle. ReportUtils keeps wrapper functions with the same names.
*/
import CONST from '@src/CONST';
import type {OnyxInputOrEntry, Report} from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';

import type {OnyxEntry} from 'react-native-onyx';

/**
* Returns true if report is still being processed
*/
function isProcessingReport(report: OnyxEntry<Report>): boolean {
return report?.stateNum === CONST.REPORT.STATE_NUM.SUBMITTED && report?.statusNum === CONST.REPORT.STATUS_NUM.SUBMITTED;
}

function isIOUReport(report: OnyxInputOrEntry<Report> | null): boolean {
return report?.type === CONST.REPORT.TYPE.IOU;
}

function isInvoiceReport(report: OnyxInputOrEntry<Report>): boolean {
return report?.type === CONST.REPORT.TYPE.INVOICE;
}

/**
* Checks if the supplied report is an expense report in Open state and status.
*/
function isOpenExpenseReport(report: OnyxInputOrEntry<Report>): boolean {
return report?.type === CONST.REPORT.TYPE.EXPENSE && report?.stateNum === CONST.REPORT.STATE_NUM.OPEN && report?.statusNum === CONST.REPORT.STATUS_NUM.OPEN;
}

/**
* Whether the Money Request report is settled.
*/
function isSettled(report: OnyxInputOrEntry<Report> | undefined): boolean {
if (!report) {
return false;
}

if (isEmptyObject(report)) {
return false;
}

// In case the payment is scheduled and we are waiting for the payee to set up their wallet,
// consider the report as paid as well.
if (report.isWaitingOnBankAccount && report.statusNum === CONST.REPORT.STATUS_NUM.APPROVED) {
return false;
}

return report?.statusNum === CONST.REPORT.STATUS_NUM.REIMBURSED;
}

export {isProcessingReport, isIOUReport, isInvoiceReport, isOpenExpenseReport, isSettled};
36 changes: 15 additions & 21 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,14 @@ import {
// ReportNameUtils imports helper functions from ReportUtils, and ReportUtils imports name generation functions from ReportNameUtils.
// eslint-disable-next-line import/no-cycle
import {getGroupChatName, getInvoicePayerName, getInvoiceReportName, getReportName} from './ReportNameUtils';
import {isCurrentUserSubmitter as isCurrentUserSubmitterState, isReportManager as isReportManagerState} from './ReportRoles';
import {
isInvoiceReport as isInvoiceReportState,
isIOUReport as isIOUReportState,
isOpenExpenseReport as isOpenExpenseReportState,
isProcessingReport as isProcessingReportState,
isSettled as isSettledState,
} from './ReportTypeAndStatus';
import {isTaskCompleted} from './TaskUtils';
import {
allHavePendingRTERViolation,
Expand Down Expand Up @@ -1501,7 +1509,7 @@ function isChatReport(report: OnyxEntry<Report>): boolean {
}

function isInvoiceReport(report: OnyxInputOrEntry<Report>): boolean {
return report?.type === CONST.REPORT.TYPE.INVOICE;
return isInvoiceReportState(report);
}

function isFinancialReportsForBusinesses(report: OnyxEntry<Report>): boolean {
Expand Down Expand Up @@ -1529,7 +1537,7 @@ function isExpenseReport(reportOrID: OnyxInputOrEntry<Report> | string): boolean
*/
function isIOUReport(reportOrID: OnyxInputOrEntry<Report> | string): boolean {
const report = typeof reportOrID === 'string' ? (getReport(reportOrID, deprecatedAllReports) ?? null) : reportOrID;
return report?.type === CONST.REPORT.TYPE.IOU;
return isIOUReportState(report);
}

/**
Expand Down Expand Up @@ -1604,7 +1612,7 @@ function isCompletedTaskReport(report: OnyxEntry<Report>): boolean {
*/
// TODO: currentUserAccountID will be required eventually so this becomes a pure function. Subscribe the data via useOnyx and pass it from the component. Refactor issue: https://github.com/Expensify/App/issues/66412
function isReportManager(report: OnyxEntry<Report>, currentUserAccountID?: number): boolean {
return !!(report && report.managerID === (currentUserAccountID ?? deprecatedCurrentUserAccountID));
return isReportManagerState(report, currentUserAccountID ?? deprecatedCurrentUserAccountID);
}

/**
Expand All @@ -1628,7 +1636,7 @@ function isReportManuallyReimbursed(report: OnyxEntry<Report>): boolean {
* Checks if the supplied report is an expense report in Open state and status.
*/
function isOpenExpenseReport(report: OnyxInputOrEntry<Report>): boolean {
return isExpenseReport(report) && report?.stateNum === CONST.REPORT.STATE_NUM.OPEN && report?.statusNum === CONST.REPORT.STATUS_NUM.OPEN;
return isOpenExpenseReportState(report);
}

/**
Expand Down Expand Up @@ -1666,29 +1674,15 @@ function isSettled(reportOrID: OnyxInputOrEntry<Report> | string | undefined, re
return false;
}
const report = typeof reportOrID === 'string' ? (getReport(reportOrID, reports ?? deprecatedAllReports) ?? null) : reportOrID;
if (!report) {
return false;
}

if (isEmptyObject(report)) {
return false;
}

// In case the payment is scheduled and we are waiting for the payee to set up their wallet,
// consider the report as paid as well.
if (report.isWaitingOnBankAccount && report.statusNum === CONST.REPORT.STATUS_NUM.APPROVED) {
return false;
}

return report?.statusNum === CONST.REPORT.STATUS_NUM.REIMBURSED;
return isSettledState(report);
}

/**
* Whether the current user is the submitter of the report
*/
// TODO: currentUserAccountID will be required eventually so this becomes a pure function. Subscribe the data via useOnyx and pass it from the component. Refactor issue: https://github.com/Expensify/App/issues/66412
function isCurrentUserSubmitter(report: OnyxEntry<Report>, currentUserAccountID?: number): boolean {
return !!report && report.ownerAccountID === (currentUserAccountID ?? deprecatedCurrentUserAccountID);
return isCurrentUserSubmitterState(report, currentUserAccountID ?? deprecatedCurrentUserAccountID);
}

/**
Expand Down Expand Up @@ -2117,7 +2111,7 @@ function shouldEnableNegative(report: OnyxEntry<Report>, policy?: OnyxEntry<Poli
* Returns true if report is still being processed
*/
function isProcessingReport(report: OnyxEntry<Report>): boolean {
return report?.stateNum === CONST.REPORT.STATE_NUM.SUBMITTED && report?.statusNum === CONST.REPORT.STATUS_NUM.SUBMITTED;
return isProcessingReportState(report);
}

function isOpenReport(report: OnyxEntry<Report>): boolean {
Expand Down
9 changes: 9 additions & 0 deletions src/libs/TransactionUtils/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* that TransactionUtils/index.ts itself imports, like DistanceRequestUtils, can read a transaction without an import cycle.
*/
import DateUtils from '@libs/DateUtils';
import {isInvalidMerchantValue} from '@libs/ValidationUtils';

import type {IOURequestType} from '@src/CONST';
import CONST from '@src/CONST';
Expand Down Expand Up @@ -150,6 +151,13 @@ function getMerchant(transaction: OnyxInputOrEntry<Transaction>): string {
return transaction?.modifiedMerchant ? transaction.modifiedMerchant : (transaction?.merchant ?? '');
}

function isMerchantMissing(transaction: OnyxEntry<Transaction>) {
if (transaction?.modifiedMerchant && transaction.modifiedMerchant !== '') {
return isInvalidMerchantValue(transaction.modifiedMerchant);
}
return isInvalidMerchantValue(transaction?.merchant);
}

/**
* Determine whether a transaction is made with a centrally managed card (Expensify or Company Card).
*/
Expand Down Expand Up @@ -266,6 +274,7 @@ export {
getTaxCode,
getDescription,
getMerchant,
isMerchantMissing,
isManagedCardTransaction,
isExpensifyCardTransaction,
isCardTransactionReimbursableEditable,
Expand Down
Loading
Loading