Skip to content
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

feat: Edit Distance Rate flow #40021

Merged
merged 26 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a3ea866
Init: Edit Rate flow
paultsimura Apr 10, 2024
d514b30
Merge branch 'fork/feat/36985-create-new-rate-field' into feat/36987-…
paultsimura Apr 11, 2024
967921b
Rate edit: initial commit
paultsimura Apr 12, 2024
2c07b55
Merge branch 'main' into feat/36987-rate-edit
paultsimura Apr 13, 2024
ed1f907
Merge branch 'fork/feat/36985-create-new-rate-field' into feat/36987-…
paultsimura Apr 13, 2024
180d885
Merge branch 'main' into feat/36987-rate-edit
paultsimura Apr 22, 2024
1158ffd
Merge branch 'main' into feat/36987-rate-edit
paultsimura Jun 30, 2024
21054fe
Some progress with edit rate
paultsimura Jul 2, 2024
2223b04
Merge branch 'refs/heads/main' into feat/36987-rate-edit
paultsimura Jul 16, 2024
8167592
Almost final changes for Rate Edit flow
paultsimura Jul 19, 2024
5cd792a
Use pending field indicator
paultsimura Jul 19, 2024
de51398
Merge branch 'refs/heads/main' into feat/36987-rate-edit
paultsimura Jul 19, 2024
05d522e
Move getDistanceInMeters to DistanceRequestUtils to avoid circular deps
paultsimura Jul 20, 2024
03bd854
Fix type
paultsimura Jul 20, 2024
b7fa71e
Lint
paultsimura Jul 20, 2024
42e2db9
Merge branch 'Expensify:main' into feat/36987-rate-edit
paultsimura Jul 30, 2024
a2ad2db
Move getDistanceInMeters into TransactionUtils
paultsimura Jul 30, 2024
b5544b1
Lint
paultsimura Jul 30, 2024
db7639a
Fix file name
paultsimura Jul 30, 2024
fe0e98e
Lint
paultsimura Jul 30, 2024
867e85e
Merge branch 'Expensify:main' into feat/36987-rate-edit
paultsimura Jul 31, 2024
6ccacb8
Always show wrapper for the rate page
paultsimura Jul 31, 2024
ebdbe0e
Merge branch 'refs/heads/main' into feat/36987-rate-edit
paultsimura Aug 2, 2024
65a07ff
Edit Rate: code cleanup
paultsimura Aug 3, 2024
2029ec5
Merge branch 'refs/heads/main' into feat/36987-rate-edit
paultsimura Aug 3, 2024
63865a3
Edit Rate: code cleanup
paultsimura Aug 3, 2024
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
1 change: 1 addition & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2507,6 +2507,7 @@ const CONST = {
CATEGORY: 'category',
RECEIPT: 'receipt',
DISTANCE: 'distance',
DISTANCE_RATE: 'distanceRate',
TAG: 'tag',
TAX_RATE: 'taxRate',
TAX_AMOUNT: 'taxAmount',
Expand Down
16 changes: 8 additions & 8 deletions src/components/ReportActionItem/MoneyRequestView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ function MoneyRequestView({
const isReceiptBeingScanned = hasReceipt && TransactionUtils.isReceiptBeingScanned(transaction);
const didReceiptScanSucceed = hasReceipt && TransactionUtils.didReceiptScanSucceed(transaction);
const canEditDistance = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.DISTANCE);
const canEditDistanceRate = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.DISTANCE_RATE);

const isAdmin = policy?.role === 'admin';
const isApprover = ReportUtils.isMoneyRequestReport(moneyRequestReport) && moneyRequestReport?.managerID !== null && session?.accountID === moneyRequestReport?.managerID;
Expand Down Expand Up @@ -214,7 +215,7 @@ function MoneyRequestView({
let amountDescription = `${translate('iou.amount')}`;

const hasRoute = TransactionUtils.hasRoute(transaction, isDistanceRequest);
const rateID = transaction?.comment.customUnit?.customUnitRateID ?? '-1';
const rateID = TransactionUtils.getRateID(transaction) ?? '-1';

const currency = policy ? policy.outputCurrency : PolicyUtils.getPersonalPolicy()?.outputCurrency ?? CONST.CURRENCY.USD;

Expand Down Expand Up @@ -333,17 +334,16 @@ function MoneyRequestView({
}
/>
</OfflineWithFeedback>
{/* TODO: correct the pending field action https://github.com/Expensify/App/issues/36987 */}
<OfflineWithFeedback pendingAction={getPendingFieldAction('waypoints')}>
<OfflineWithFeedback pendingAction={getPendingFieldAction('customUnitRateID')}>
<MenuItemWithTopDescription
description={translate('common.rate')}
title={rateToDisplay}
// TODO: https://github.com/Expensify/App/issues/36987 make it interactive and show right icon when EditRatePage is ready
interactive={false}
shouldShowRightIcon={false}
interactive={canEditDistanceRate}
shouldShowRightIcon={canEditDistanceRate}
titleStyle={styles.flex1}
// TODO: https://github.com/Expensify/App/issues/36987 Add route for editing rate
onPress={() => {}}
onPress={() =>
Navigation.navigate(ROUTES.MONEY_REQUEST_STEP_DISTANCE_RATE.getRoute(CONST.IOU.ACTION.EDIT, iouType, transaction?.transactionID ?? '-1', report?.reportID ?? '-1'))
}
/>
</OfflineWithFeedback>
</>
Expand Down
2 changes: 2 additions & 0 deletions src/libs/API/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ const WRITE_COMMANDS = {
UPDATE_MONEY_REQUEST_TAX_AMOUNT: 'UpdateMoneyRequestTaxAmount',
UPDATE_MONEY_REQUEST_TAX_RATE: 'UpdateMoneyRequestTaxRate',
UPDATE_MONEY_REQUEST_DISTANCE: 'UpdateMoneyRequestDistance',
UPDATE_MONEY_REQUEST_DISTANCE_RATE: 'UpdateMoneyRequestDistanceRate',
UPDATE_MONEY_REQUEST_CATEGORY: 'UpdateMoneyRequestCategory',
UPDATE_MONEY_REQUEST_DESCRIPTION: 'UpdateMoneyRequestDescription',
UPDATE_MONEY_REQUEST_AMOUNT_AND_CURRENCY: 'UpdateMoneyRequestAmountAndCurrency',
Expand Down Expand Up @@ -474,6 +475,7 @@ type WriteCommandParameters = {
[WRITE_COMMANDS.UPDATE_MONEY_REQUEST_TAX_AMOUNT]: Parameters.UpdateMoneyRequestParams;
[WRITE_COMMANDS.UPDATE_MONEY_REQUEST_TAX_RATE]: Parameters.UpdateMoneyRequestParams;
[WRITE_COMMANDS.UPDATE_MONEY_REQUEST_DISTANCE]: Parameters.UpdateMoneyRequestParams;
[WRITE_COMMANDS.UPDATE_MONEY_REQUEST_DISTANCE_RATE]: Parameters.UpdateMoneyRequestParams;
[WRITE_COMMANDS.UPDATE_MONEY_REQUEST_CATEGORY]: Parameters.UpdateMoneyRequestParams;
[WRITE_COMMANDS.UPDATE_MONEY_REQUEST_DESCRIPTION]: Parameters.UpdateMoneyRequestParams;
[WRITE_COMMANDS.HOLD_MONEY_REQUEST]: Parameters.HoldMoneyRequestParams;
Expand Down
1 change: 1 addition & 0 deletions src/libs/Navigation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,7 @@ type MoneyRequestNavigatorParamList = {
currency?: string;
};
[SCREENS.MONEY_REQUEST.STEP_DISTANCE_RATE]: {
action: IOUAction;
iouType: ValueOf<typeof CONST.IOU.TYPE>;
transactionID: string;
backTo: Routes;
Expand Down
22 changes: 21 additions & 1 deletion src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ type TransactionDetails = {
currency: string;
merchant: string;
waypoints?: WaypointCollection | string;
customUnitRateID?: string;
comment: string;
category: string;
billable: boolean;
Expand Down Expand Up @@ -2702,6 +2703,7 @@ function getTransactionDetails(transaction: OnyxInputOrEntry<Transaction>, creat
comment: TransactionUtils.getDescription(transaction),
merchant: TransactionUtils.getMerchant(transaction),
waypoints: TransactionUtils.getWaypoints(transaction),
customUnitRateID: TransactionUtils.getRateID(transaction),
category: TransactionUtils.getCategory(transaction),
billable: TransactionUtils.getBillable(transaction),
tag: TransactionUtils.getTag(transaction),
Expand Down Expand Up @@ -2789,6 +2791,7 @@ function canEditFieldOfMoneyRequest(reportAction: OnyxInputOrEntry<ReportAction>
CONST.EDIT_REQUEST_FIELD.DATE,
CONST.EDIT_REQUEST_FIELD.RECEIPT,
CONST.EDIT_REQUEST_FIELD.DISTANCE,
CONST.EDIT_REQUEST_FIELD.DISTANCE_RATE,
];

if (!ReportActionsUtils.isMoneyRequestAction(reportAction) || !canEditMoneyRequest(reportAction)) {
Expand Down Expand Up @@ -2828,6 +2831,11 @@ function canEditFieldOfMoneyRequest(reportAction: OnyxInputOrEntry<ReportAction>
return !isInvoiceReport(moneyRequestReport) && !TransactionUtils.isReceiptBeingScanned(transaction) && !TransactionUtils.isDistanceRequest(transaction) && isRequestor;
}

if (fieldToEdit === CONST.EDIT_REQUEST_FIELD.DISTANCE_RATE) {
// The distance rate can be modified only on the distance expense reports
return isExpenseReport(moneyRequestReport) && TransactionUtils.isDistanceRequest(transaction);
}

return true;
}

Expand Down Expand Up @@ -3203,6 +3211,7 @@ function getReportPreviewMessage(
*/
function getModifiedExpenseOriginalMessage(
oldTransaction: OnyxInputOrEntry<Transaction>,
updatedTransaction: OnyxInputOrEntry<Transaction>,
transactionChanges: TransactionChanges,
isFromExpenseReport: boolean,
policy: OnyxInputOrEntry<Policy>,
Expand Down Expand Up @@ -3265,6 +3274,16 @@ function getModifiedExpenseOriginalMessage(
originalMessage.billable = transactionChanges?.billable ? Localize.translateLocal('common.billable').toLowerCase() : Localize.translateLocal('common.nonBillable').toLowerCase();
}

if ('customUnitRateID' in transactionChanges) {
originalMessage.oldAmount = TransactionUtils.getAmount(oldTransaction, isFromExpenseReport);
originalMessage.oldCurrency = TransactionUtils.getCurrency(oldTransaction);
originalMessage.oldMerchant = TransactionUtils.getMerchant(oldTransaction);

originalMessage.amount = TransactionUtils.getAmount(updatedTransaction, isFromExpenseReport);
originalMessage.currency = TransactionUtils.getCurrency(updatedTransaction);
originalMessage.merchant = TransactionUtils.getMerchant(updatedTransaction);
}
paultsimura marked this conversation as resolved.
Show resolved Hide resolved

return originalMessage;
}

Expand Down Expand Up @@ -4521,11 +4540,12 @@ function buildOptimisticActionableTrackExpenseWhisper(iouAction: OptimisticIOURe
function buildOptimisticModifiedExpenseReportAction(
transactionThread: OnyxInputOrEntry<Report>,
oldTransaction: OnyxInputOrEntry<Transaction>,
updatedTransaction: OnyxInputOrEntry<Transaction>,
transactionChanges: TransactionChanges,
isFromExpenseReport: boolean,
policy: OnyxInputOrEntry<Policy>,
): OptimisticModifiedExpenseReportAction {
const originalMessage = getModifiedExpenseOriginalMessage(oldTransaction, transactionChanges, isFromExpenseReport, policy);
const originalMessage = getModifiedExpenseOriginalMessage(oldTransaction, updatedTransaction, transactionChanges, isFromExpenseReport, policy);
return {
actionName: CONST.REPORT.ACTIONS.TYPE.MODIFIED_EXPENSE,
actorAccountID: currentUserAccountID,
Expand Down
21 changes: 21 additions & 0 deletions src/libs/TransactionUtils/getDistanceInMeters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import DistanceRequestUtils from '@libs/DistanceRequestUtils';
import type {OnyxInputOrEntry, Transaction} from '@src/types/onyx';
import type {Unit} from '@src/types/onyx/Policy';

// Get the distance in meters from the transaction.
// This function is placed in a separate file to avoid circular dependencies.
function getDistanceInMeters(transaction: OnyxInputOrEntry<Transaction>, unit: Unit | undefined) {
// If we are creating a new distance request, the distance is available in routes.route0.distance and it's already in meters.
if (transaction?.routes?.route0?.distance) {
return transaction.routes.route0.distance;
}

// If the request is completed, transaction.routes is cleared and comment.customUnit.quantity holds the new distance in the selected unit.
// We need to convert it from the selected distance unit to meters.
if (transaction?.comment?.customUnit?.quantity && unit) {
return DistanceRequestUtils.convertToDistanceInMeters(transaction.comment.customUnit.quantity, unit);
}
return 0;
}

export default getDistanceInMeters;
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ import lodashIsEqual from 'lodash/isEqual';
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import type {TransactionMergeParams} from '@libs/API/parameters';
import {isCorporateCard, isExpensifyCard} from '@libs/CardUtils';
import {getCurrencyDecimals} from '@libs/CurrencyUtils';
import DateUtils from '@libs/DateUtils';
import * as Localize from '@libs/Localize';
import * as NumberUtils from '@libs/NumberUtils';
import Permissions from '@libs/Permissions';
import {getCleanedTagName, getCustomUnitRate} from '@libs/PolicyUtils';
// eslint-disable-next-line import/no-cycle
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
import * as ReportConnection from '@libs/ReportConnection';
import type {IOURequestType} from '@userActions/IOU';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Beta, OnyxInputOrEntry, Policy, RecentWaypoint, ReviewDuplicates, TaxRate, TaxRates, Transaction, TransactionViolation, TransactionViolations} from '@src/types/onyx';
import type {Comment, Receipt, TransactionChanges, TransactionPendingFieldsKey, Waypoint, WaypointCollection} from '@src/types/onyx/Transaction';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import type {IOURequestType} from './actions/IOU';
import type {TransactionMergeParams} from './API/parameters';
import {isCorporateCard, isExpensifyCard} from './CardUtils';
import {getCurrencyDecimals} from './CurrencyUtils';
import DateUtils from './DateUtils';
import * as Localize from './Localize';
import * as NumberUtils from './NumberUtils';
import Permissions from './Permissions';
import {getCleanedTagName, getCustomUnitRate} from './PolicyUtils';
// eslint-disable-next-line import/no-cycle
import * as ReportActionsUtils from './ReportActionsUtils';
import * as ReportConnection from './ReportConnection';
import getDistanceInMeters from './getDistanceInMeters';

let allTransactions: OnyxCollection<Transaction> = {};
Onyx.connect({
Expand Down Expand Up @@ -239,6 +240,11 @@ function getUpdatedTransaction(transaction: Transaction, transactionChanges: Tra
shouldStopSmartscan = true;
}

if (Object.hasOwn(transactionChanges, 'customUnitRateID')) {
updatedTransaction.modifiedCustomUnitRateID = transactionChanges.customUnitRateID;
neil-marcellini marked this conversation as resolved.
Show resolved Hide resolved
shouldStopSmartscan = true;
}

if (Object.hasOwn(transactionChanges, 'taxAmount') && typeof transactionChanges.taxAmount === 'number') {
updatedTransaction.taxAmount = isFromExpenseReport ? -transactionChanges.taxAmount : transactionChanges.taxAmount;
}
Expand Down Expand Up @@ -746,10 +752,10 @@ function hasReservationList(transaction: Transaction | undefined | null): boolea
}

/**
* Get rate ID from the transaction object
* Get custom unit rate (distance rate) ID from the transaction object
*/
function getRateID(transaction: OnyxInputOrEntry<Transaction>): string | undefined {
return transaction?.comment?.customUnit?.customUnitRateID?.toString();
return transaction?.modifiedCustomUnitRateID ?? transaction?.comment?.customUnit?.customUnitRateID?.toString();
}

/**
Expand Down Expand Up @@ -999,6 +1005,7 @@ export {
getTaxCode,
getCurrency,
getDistance,
getDistanceInMeters,
getCardID,
getOriginalCurrency,
getOriginalAmount,
Expand Down
Loading
Loading