Skip to content

Borys3kk 59999 follow ups #60891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
72 changes: 43 additions & 29 deletions src/components/MoneyReportHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
isReceiptBeingScanned,
shouldShowBrokenConnectionViolationForMultipleTransactions,
} from '@libs/TransactionUtils';
import type {ExportType} from '@pages/home/report/ReportDetailsExportPage';
import variables from '@styles/variables';
import {
approveMoneyRequest,
Expand Down Expand Up @@ -146,13 +147,14 @@ function MoneyReportHeader({policy, report: moneyRequestReport, transactionThrea
const isLoadingHoldUseExplained = isLoadingOnyxValue(dismissedHoldUseExplanationResult);

const isExported = isExportedUtils(reportActions);
const [markAsExportedModalVisible, setMarkAsExportedModalVisible] = useState(false);

const [downloadErrorModalVisible, setDownloadErrorModalVisible] = useState(false);
const [isCancelPaymentModalVisible, setIsCancelPaymentModalVisible] = useState(false);
const [isDeleteModalVisible, setIsDeleteModalVisible] = useState(false);
const [isUnapproveModalVisible, setIsUnapproveModalVisible] = useState(false);

const [exportModalStatus, setExportModalStatus] = useState<ExportType | null>(null);

const {isPaidAnimationRunning, isApprovedAnimationRunning, startAnimation, stopAnimation, startApprovedAnimation} = usePaymentAnimations();
const styles = useThemeStyles();
const theme = useTheme();
Expand Down Expand Up @@ -286,14 +288,6 @@ function MoneyReportHeader({policy, report: moneyRequestReport, transactionThrea
markAsCashAction(iouTransactionID, reportID);
}, [requestParentReportAction, transactionThreadReport?.reportID]);

const confirmManualExport = useCallback(() => {
if (!connectedIntegration || !moneyRequestReport) {
throw new Error('Missing data');
}

markAsManuallyExported(moneyRequestReport.reportID, connectedIntegration);
}, [connectedIntegration, moneyRequestReport]);

const getStatusIcon: (src: IconAsset) => React.ReactNode = (src) => (
<Icon
src={src}
Expand Down Expand Up @@ -363,7 +357,19 @@ function MoneyReportHeader({policy, report: moneyRequestReport, transactionThrea
return '';
}
return getReportPrimaryAction(moneyRequestReport, transactions, violations, policy, reportNameValuePairs);
}, [isPaidAnimationRunning, moneyRequestReport, policy, reportNameValuePairs, transactions, violations]);
}, [isPaidAnimationRunning, moneyRequestReport, reportNameValuePairs, policy, transactions, violations]);

const confirmExport = useCallback(() => {
setExportModalStatus(null);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In ExportWithDropdownMenu we also check if isExported, do we need to do it here as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isExported is used in another flow in ExportWithDropdownMenu
the confirm export function is only used in warning modal for reexport
modal -> function

if (!moneyRequestReport?.reportID || !connectedIntegration) {
return;
}
if (exportModalStatus === CONST.REPORT.EXPORT_OPTIONS.EXPORT_TO_INTEGRATION) {
exportToIntegration(moneyRequestReport?.reportID, connectedIntegration);
} else if (exportModalStatus === CONST.REPORT.EXPORT_OPTIONS.MARK_AS_EXPORTED) {
markAsManuallyExported(moneyRequestReport?.reportID, connectedIntegration);
}
}, [connectedIntegration, exportModalStatus, moneyRequestReport?.reportID]);

const primaryActionsImplementation = {
[CONST.REPORT.PRIMARY_ACTIONS.SUBMIT]: (
Expand Down Expand Up @@ -417,7 +423,11 @@ function MoneyReportHeader({policy, report: moneyRequestReport, transactionThrea
if (!connectedIntegration || !moneyRequestReport) {
return;
}
exportToIntegration(moneyRequestReport.reportID, connectedIntegration);
if (isExported) {
setExportModalStatus(CONST.REPORT.EXPORT_OPTIONS.EXPORT_TO_INTEGRATION);
return;
}
exportToIntegration(moneyRequestReport?.reportID, connectedIntegration);
}}
/>
),
Expand Down Expand Up @@ -554,22 +564,28 @@ function MoneyReportHeader({policy, report: moneyRequestReport, transactionThrea
value: CONST.REPORT.SECONDARY_ACTIONS.EXPORT_TO_ACCOUNTING,
onSelected: () => {
if (!connectedIntegration || !moneyRequestReport) {
throw new Error('Missing data');
return;
}

exportToIntegration(moneyRequestReport.reportID, connectedIntegration);
if (isExported) {
setExportModalStatus(CONST.REPORT.EXPORT_OPTIONS.EXPORT_TO_INTEGRATION);
return;
}
exportToIntegration(moneyRequestReport?.reportID, connectedIntegration);
},
},
[CONST.REPORT.SECONDARY_ACTIONS.MARK_AS_EXPORTED]: {
text: translate('workspace.common.markAsExported'),
icon: Expensicons.CheckCircle,
value: CONST.REPORT.SECONDARY_ACTIONS.MARK_AS_EXPORTED,
onSelected: () => {
if (!connectedIntegration || !moneyRequestReport) {
return;
}
if (isExported) {
setMarkAsExportedModalVisible(true);
setExportModalStatus(CONST.REPORT.EXPORT_OPTIONS.MARK_AS_EXPORTED);
return;
}
confirmManualExport();
markAsManuallyExported(moneyRequestReport?.reportID, connectedIntegration);
},
},
[CONST.REPORT.SECONDARY_ACTIONS.HOLD]: {
Expand Down Expand Up @@ -799,19 +815,17 @@ function MoneyReportHeader({policy, report: moneyRequestReport, transactionThrea
danger
shouldEnableNewFocusManagement
/>
<ConfirmModal
title={translate('workspace.exportAgainModal.title')}
onConfirm={() => {
confirmManualExport();
setMarkAsExportedModalVisible(false);
}}
onCancel={() => setMarkAsExportedModalVisible(false)}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
prompt={translate('workspace.exportAgainModal.description', {connectionName: connectedIntegration!, reportName: moneyRequestReport?.reportName ?? ''})}
confirmText={translate('workspace.exportAgainModal.confirmText')}
cancelText={translate('workspace.exportAgainModal.cancelText')}
isVisible={markAsExportedModalVisible}
/>
{!!connectedIntegration && (
<ConfirmModal
title={translate('workspace.exportAgainModal.title')}
onConfirm={confirmExport}
onCancel={() => setExportModalStatus(null)}
prompt={translate('workspace.exportAgainModal.description', {connectionName: connectedIntegration, reportName: moneyRequestReport?.reportName ?? ''})}
confirmText={translate('workspace.exportAgainModal.confirmText')}
cancelText={translate('workspace.exportAgainModal.cancelText')}
isVisible={!!exportModalStatus}
/>
)}
<ConfirmModal
title={translate('iou.unapproveReport')}
isVisible={isUnapproveModalVisible}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {FlatList, View} from 'react-native';
import type {LayoutChangeEvent, ListRenderItemInfo, ViewToken} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import Animated, {useAnimatedStyle, useSharedValue, withDelay, withSpring, withTiming} from 'react-native-reanimated';
import type {LayoutRectangle} from 'react-native/Libraries/Types/CoreEventTypes';
import Button from '@components/Button';
Expand Down Expand Up @@ -62,6 +63,7 @@ import {approveMoneyRequest, canApproveIOU, canIOUBePaid as canIOUBePaidIOUActio
import Timing from '@userActions/Timing';
import CONST from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {Transaction} from '@src/types/onyx';
import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage';
Expand Down Expand Up @@ -188,9 +190,10 @@ function MoneyRequestReportPreviewContent({

// The submit button should be success green color only if the user is submitter and the policy does not have Scheduled Submit turned on
const isWaitingForSubmissionFromCurrentUser = useMemo(() => isWaitingForSubmissionFromCurrentUserReportUtils(chatReport, policy), [chatReport, policy]);

const [isNoDelegateAccessMenuVisible, setIsNoDelegateAccessMenuVisible] = useState(false);

const [reportNameValuePairs] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${iouReportID}`, {canBeMissing: true});

const confirmPayment = useCallback(
(type: PaymentMethodType | undefined, payAsBusiness?: boolean) => {
if (!type) {
Expand Down Expand Up @@ -452,8 +455,8 @@ function MoneyRequestReportPreviewContent({
if (isPaidAnimationRunning) {
return CONST.REPORT.REPORT_PREVIEW_ACTIONS.PAY;
}
return getReportPreviewAction(violations, iouReport, policy, transactions);
}, [isPaidAnimationRunning, violations, iouReport, policy, transactions]);
return getReportPreviewAction(violations, iouReport, policy, transactions, reportNameValuePairs);
}, [isPaidAnimationRunning, violations, iouReport, policy, transactions, reportNameValuePairs]);

const reportPreviewActions = {
[CONST.REPORT.REPORT_PREVIEW_ACTIONS.SUBMIT]: (
Expand Down
9 changes: 6 additions & 3 deletions src/components/ReportActionItem/ReportPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import truncate from 'lodash/truncate';
import React, {useCallback, useEffect, useMemo, useState} from 'react';
import type {StyleProp, ViewStyle} from 'react-native';
import {View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import Animated, {useAnimatedStyle, useSharedValue, withDelay, withSpring, withTiming} from 'react-native-reanimated';
import Button from '@components/Button';
import {getButtonRole} from '@components/Button/utils';
Expand All @@ -20,7 +21,6 @@ import Text from '@components/Text';
import useDelegateUserDetails from '@hooks/useDelegateUserDetails';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useOnyx from '@hooks/useOnyx';
import usePaymentAnimations from '@hooks/usePaymentAnimations';
import usePolicy from '@hooks/usePolicy';
import useReportWithTransactionsAndViolations from '@hooks/useReportWithTransactionsAndViolations';
Expand Down Expand Up @@ -203,6 +203,7 @@ function ReportPreview({
const managerID = iouReport?.managerID ?? action.childManagerAccountID ?? CONST.DEFAULT_NUMBER_ID;
const {totalDisplaySpend, reimbursableSpend} = getMoneyRequestSpendBreakdown(iouReport);
const [reports] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}`, {canBeMissing: false});

const iouSettled = isSettled(iouReportID, isOnSearch ? reports : undefined) || action?.childStatusNum === CONST.REPORT.STATUS_NUM.REIMBURSED;
const previewMessageOpacity = useSharedValue(1);
const previewMessageStyle = useAnimatedStyle(() => ({
Expand Down Expand Up @@ -258,6 +259,8 @@ function ReportPreview({
const {isDelegateAccessRestricted} = useDelegateUserDetails();
const [isNoDelegateAccessMenuVisible, setIsNoDelegateAccessMenuVisible] = useState(false);

const [reportNameValuePairs] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${iouReport?.reportID}`, {canBeMissing: true});

const confirmPayment = useCallback(
(type: PaymentMethodType | undefined, payAsBusiness?: boolean, methodID?: number, paymentMethod?: PaymentMethod) => {
if (!type) {
Expand Down Expand Up @@ -512,8 +515,8 @@ function ReportPreview({
if (isPaidAnimationRunning) {
return CONST.REPORT.REPORT_PREVIEW_ACTIONS.PAY;
}
return getReportPreviewAction(violations, iouReport, policy, transactions);
}, [isPaidAnimationRunning, violations, iouReport, policy, transactions]);
return getReportPreviewAction(violations, iouReport, policy, transactions, reportNameValuePairs);
}, [isPaidAnimationRunning, violations, iouReport, policy, transactions, reportNameValuePairs]);

const reportPreviewActions = {
[CONST.REPORT.REPORT_PREVIEW_ACTIONS.SUBMIT]: (
Expand Down
2 changes: 1 addition & 1 deletion src/libs/ReportActionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ function getOriginalMessage<T extends ReportActionName>(reportAction: OnyxInputO
}

function isExportIntegrationAction(reportAction: OnyxInputOrEntry<ReportAction>): boolean {
return reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.INTEGRATIONS_MESSAGE && !!getOriginalMessage(reportAction as ReportAction<'INTEGRATIONSMESSAGE'>)?.result?.success;
return reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.EXPORTED_TO_INTEGRATION;
}

/**
Expand Down
18 changes: 10 additions & 8 deletions src/libs/ReportPreviewActionUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {OnyxCollection} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import CONST from '@src/CONST';
import type {Policy, Report, Transaction, TransactionViolation} from '@src/types/onyx';
import type {Policy, Report, ReportNameValuePairs, Transaction, TransactionViolation} from '@src/types/onyx';
import {isApprover as isApproverMember} from './actions/Policy/Member';
import {getCurrentUserAccountID} from './actions/Report';
import {
Expand All @@ -16,7 +16,6 @@ import {
import {
getMoneyRequestSpendBreakdown,
getParentReport,
getReportNameValuePairs,
getReportTransactions,
hasMissingSmartscanFields,
hasNoticeTypeViolations,
Expand Down Expand Up @@ -59,10 +58,13 @@ function canSubmit(report: Report, violations: OnyxCollection<TransactionViolati
}

function canApprove(report: Report, violations: OnyxCollection<TransactionViolation[]>, policy?: Policy, transactions?: Transaction[]) {
const currentUserID = getCurrentUserAccountID();
const isExpense = isExpenseReport(report);
const isApprover = isApproverMember(policy, getCurrentUserAccountID());
const isApprover = isApproverMember(policy, currentUserID);
const isProcessing = isProcessingReport(report);
const isApprovalEnabled = policy ? policy.approvalMode && policy.approvalMode !== CONST.POLICY.APPROVAL_MODE.OPTIONAL : false;
const managerID = report?.managerID ?? CONST.DEFAULT_NUMBER_ID;
const isCurrentUserManager = managerID === currentUserID;
const hasAnyViolations =
hasMissingSmartscanFields(report.reportID, transactions) ||
hasViolations(report.reportID, violations) ||
Expand All @@ -77,11 +79,10 @@ function canApprove(report: Report, violations: OnyxCollection<TransactionViolat
return false;
}

return isExpense && isApprover && isProcessing && isApprovalEnabled && !hasAnyViolations && reportTransactions.length > 0;
return isExpense && isApprover && isProcessing && isApprovalEnabled && !hasAnyViolations && reportTransactions.length > 0 && isCurrentUserManager;
}

function canPay(report: Report, violations: OnyxCollection<TransactionViolation[]>, policy?: Policy) {
const reportNameValuePairs = getReportNameValuePairs(report.chatReportID);
function canPay(report: Report, violations: OnyxCollection<TransactionViolation[]>, policy?: Policy, reportNameValuePairs?: ReportNameValuePairs) {
const isChatReportArchived = isArchivedReport(reportNameValuePairs);

if (isChatReportArchived) {
Expand Down Expand Up @@ -113,7 +114,7 @@ function canPay(report: Report, violations: OnyxCollection<TransactionViolation[

const isIOU = isIOUReport(report);

if (isIOU && isReportPayer && !isReimbursed) {
if (isIOU && isReportPayer && !isReimbursed && reimbursableSpend > 0) {
return true;
}

Expand Down Expand Up @@ -180,6 +181,7 @@ function getReportPreviewAction(
report?: Report,
policy?: Policy,
transactions?: Transaction[],
reportNameValuePairs?: ReportNameValuePairs,
): ValueOf<typeof CONST.REPORT.REPORT_PREVIEW_ACTIONS> {
if (!report) {
return CONST.REPORT.REPORT_PREVIEW_ACTIONS.VIEW;
Expand All @@ -190,7 +192,7 @@ function getReportPreviewAction(
if (canApprove(report, violations, policy, transactions)) {
return CONST.REPORT.REPORT_PREVIEW_ACTIONS.APPROVE;
}
if (canPay(report, violations, policy)) {
if (canPay(report, violations, policy, reportNameValuePairs)) {
return CONST.REPORT.REPORT_PREVIEW_ACTIONS.PAY;
}
if (canExport(report, violations, policy)) {
Expand Down
10 changes: 8 additions & 2 deletions src/libs/ReportPrimaryActionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,14 @@ function isSubmitAction(report: Report, reportTransactions: Transaction[], polic
}

function isApproveAction(report: Report, reportTransactions: Transaction[], policy?: Policy) {
const currentUserAccountID = getCurrentUserAccountID();
const managerID = report?.managerID ?? CONST.DEFAULT_NUMBER_ID;
const isCurrentUserManager = managerID === currentUserAccountID;
if (!isCurrentUserManager) {
return false;
}
const isExpenseReport = isExpenseReportUtils(report);
const isReportApprover = isApproverUtils(policy, getCurrentUserAccountID());
const isReportApprover = isApproverUtils(policy, currentUserAccountID);
const isApprovalEnabled = policy?.approvalMode && policy.approvalMode !== CONST.POLICY.APPROVAL_MODE.OPTIONAL;

if (!isExpenseReport || !isReportApprover || !isApprovalEnabled || reportTransactions.length === 0) {
Expand Down Expand Up @@ -130,7 +136,7 @@ function isPayAction(report: Report, policy?: Policy, reportNameValuePairs?: Rep

const isIOUReport = isIOUReportUtils(report);

if (isIOUReport && isReportPayer) {
if (isIOUReport && isReportPayer && reimbursableSpend > 0) {
return true;
}

Expand Down
12 changes: 9 additions & 3 deletions src/libs/ReportSecondaryActionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,14 @@ function isSubmitAction(report: Report, reportTransactions: Transaction[], polic
}

function isApproveAction(report: Report, reportTransactions: Transaction[], violations: OnyxCollection<TransactionViolation[]>, policy?: Policy): boolean {
const currentUserAccountID = getCurrentUserAccountID();
const managerID = report?.managerID ?? CONST.DEFAULT_NUMBER_ID;
const isCurrentUserManager = managerID === currentUserAccountID;
if (!isCurrentUserManager) {
return false;
}
const isExpenseReport = isExpenseReportUtils(report);
const isReportApprover = isApproverUtils(policy, getCurrentUserAccountID());
const isReportApprover = isApproverUtils(policy, currentUserAccountID);
const isProcessingReport = isProcessingReportUtils(report);
const reportHasDuplicatedTransactions = reportTransactions.some((transaction) => isDuplicate(transaction.transactionID));

Expand Down Expand Up @@ -254,15 +260,15 @@ function isMarkAsExportedAction(report: Report, policy?: Policy): boolean {
const syncEnabled = hasIntegrationAutoSync(policy, connectedIntegration);
const isReportFinished = isReportClosedOrApproved || isReportReimbursed;

if (!isReportFinished || !syncEnabled) {
if (!isReportFinished) {
return false;
}

const isAdmin = policy?.role === CONST.POLICY.ROLE.ADMIN;

const isExporter = isPrefferedExporter(policy);

return isAdmin || isExporter;
return (isAdmin && syncEnabled) || (isExporter && !syncEnabled);
}

function isHoldAction(report: Report, reportTransactions: Transaction[]): boolean {
Expand Down
1 change: 1 addition & 0 deletions tests/actions/ReportPreviewActionUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ describe('getReportPreviewAction', () => {
ownerAccountID: CURRENT_USER_ACCOUNT_ID,
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
managerID: CURRENT_USER_ACCOUNT_ID,
};

const policy = createRandomPolicy(0);
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/ReportPrimaryActionUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ describe('getPrimaryAction', () => {
ownerAccountID: CURRENT_USER_ACCOUNT_ID,
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
managerID: CURRENT_USER_ACCOUNT_ID,
} as unknown as Report;
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, report);
const policy = {
Expand Down Expand Up @@ -326,7 +327,6 @@ describe('getTransactionThreadPrimaryAction', () => {
},
} as unknown as TransactionViolation;

getTransactionThreadPrimaryAction({} as Report, report, transaction, [violation], policy as Policy);
expect(getTransactionThreadPrimaryAction({} as Report, report, transaction, [violation], policy as Policy)).toBe(CONST.REPORT.TRANSACTION_PRIMARY_ACTIONS.MARK_AS_CASH);
});

Expand Down
Loading