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

Refactor TrackExpense function #55261

Merged
merged 11 commits into from
Feb 5, 2025
81 changes: 53 additions & 28 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,35 @@ type CreateDistanceRequestInformation = {
policyParams?: RequestMoneyPolicyParams;
};

type TrackExpenseTransactionParams = {
amount: number;
currency: string;
created: string | undefined;
merchant: string;
comment: string;
receipt?: Receipt;
category?: string;
tag?: string;
taxCode?: string;
taxAmount?: number;
billable?: boolean;
validWaypoints?: WaypointCollection;
gpsPoints?: GPSPoint;
actionableWhisperReportActionID?: string;
linkedTrackedExpenseReportAction?: OnyxTypes.ReportAction;
linkedTrackedExpenseReportID?: string;
customUnitRateID?: string;
};

type CreateTrackExpenseParams = {
report: OnyxTypes.Report;
isDraftPolicy: boolean;
action?: IOUAction;
participantParams: RequestMoneyParticipantParams;
policyParams?: RequestMoneyPolicyParams;
transactionParams: TrackExpenseTransactionParams;
};

let allPersonalDetails: OnyxTypes.PersonalDetailsList = {};
Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
Expand Down Expand Up @@ -4649,34 +4678,30 @@ function sendInvoice(
/**
* Track an expense
*/
function trackExpense(
report: OnyxTypes.Report,
amount: number,
currency: string,
created: string,
merchant: string,
payeeEmail: string | undefined,
payeeAccountID: number,
participant: Participant,
comment: string,
isDraftPolicy: boolean,
receipt?: Receipt,
category?: string,
tag?: string,
taxCode = '',
taxAmount = 0,
billable?: boolean,
policy?: OnyxEntry<OnyxTypes.Policy>,
policyTagList?: OnyxEntry<OnyxTypes.PolicyTagLists>,
policyCategories?: OnyxEntry<OnyxTypes.PolicyCategories>,
gpsPoints?: GPSPoint,
validWaypoints?: WaypointCollection,
action?: IOUAction,
actionableWhisperReportActionID?: string,
linkedTrackedExpenseReportAction?: OnyxTypes.ReportAction,
linkedTrackedExpenseReportID?: string,
customUnitRateID?: string,
) {
function trackExpense(params: CreateTrackExpenseParams) {
const {report, action, isDraftPolicy, participantParams, policyParams: policyData = {}, transactionParams: transactionData} = params;
const {participant, payeeAccountID, payeeEmail} = participantParams;
const {policy, policyCategories, policyTagList} = policyData;
const {
amount,
currency,
created = '',
Copy link
Contributor

Choose a reason for hiding this comment

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

why is created having default value? While the previous didn't have default one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason is other places we're passing the created like this then that will cause the lint to fail and the code isn't cleaner. So I updated the created with the default value '' then we can remove ?? ''.

transaction?.created ?? '',

merchant,
comment,
receipt,
category,
tag,
taxCode = '',
taxAmount = 0,
billable,
gpsPoints,
validWaypoints,
actionableWhisperReportActionID,
linkedTrackedExpenseReportAction,
linkedTrackedExpenseReportID,
customUnitRateID,
} = transactionData;

const isMoneyRequestReport = isMoneyRequestReportReportUtils(report);
const currentChatReport = isMoneyRequestReport ? getReportOrDraftReport(report.chatReportID) : report;
const moneyRequestReportID = isMoneyRequestReport ? report.reportID : '';
Expand Down
30 changes: 17 additions & 13 deletions src/pages/iou/request/step/IOURequestStepAmount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type * as OnyxTypes from '@src/types/onyx';
import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage';
import type Transaction from '@src/types/onyx/Transaction';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import StepScreenWrapper from './StepScreenWrapper';
import withFullTransactionOrNotFound from './withFullTransactionOrNotFound';
Expand All @@ -47,7 +47,7 @@ type AmountParams = {
type IOURequestStepAmountProps = WithCurrentUserPersonalDetailsProps &
WithWritableReportOrNotFoundProps<typeof SCREENS.MONEY_REQUEST.STEP_AMOUNT | typeof SCREENS.MONEY_REQUEST.CREATE> & {
/** The transaction object being modified in Onyx */
transaction: OnyxEntry<OnyxTypes.Transaction>;
transaction: OnyxEntry<Transaction>;

/** Whether the user input should be kept or not */
shouldKeepUserInput?: boolean;
Expand Down Expand Up @@ -219,18 +219,22 @@ function IOURequestStepAmount({
}
if (iouType === CONST.IOU.TYPE.TRACK) {
playSound(SOUNDS.DONE);
trackExpense(
trackExpense({
report,
backendAmount,
currency ?? 'USD',
transaction?.created ?? '',
CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT,
currentUserPersonalDetails.login,
currentUserPersonalDetails.accountID,
participants.at(0) ?? {},
'',
false,
);
isDraftPolicy: false,
participantParams: {
payeeEmail: currentUserPersonalDetails.login,
payeeAccountID: currentUserPersonalDetails.accountID,
participant: participants.at(0) ?? {},
},
transactionParams: {
amount: backendAmount,
currency: currency ?? 'USD',
created: transaction?.created,
merchant: CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT,
comment: '',
},
});
return;
}
}
Expand Down
56 changes: 31 additions & 25 deletions src/pages/iou/request/step/IOURequestStepConfirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -341,34 +341,40 @@ function IOURequestStepConfirmation({
if (!participant) {
return;
}
trackExpenseIOUActions(
trackExpenseIOUActions({
report,
transaction.amount,
transaction.currency,
transaction.created,
transaction.merchant,
currentUserPersonalDetails.login,
currentUserPersonalDetails.accountID,
participant,
trimmedComment,
isDraftPolicy,
receiptObj,
transaction.category,
transaction.tag,
transactionTaxCode,
transactionTaxAmount,
transaction.billable,
policy,
policyTags,
policyCategories,
gpsPoints,
Object.keys(transaction?.comment?.waypoints ?? {}).length ? getValidWaypoints(transaction.comment?.waypoints, true) : undefined,
action,
transaction.actionableWhisperReportActionID,
transaction.linkedTrackedExpenseReportAction,
transaction.linkedTrackedExpenseReportID,
customUnitRateID,
);
participantParams: {
payeeEmail: currentUserPersonalDetails.login,
payeeAccountID: currentUserPersonalDetails.accountID,
participant,
},
policyParams: {
policy,
policyCategories,
policyTagList: policyTags,
},
transactionParams: {
amount: transaction.amount,
currency: transaction.currency,
created: transaction.created,
merchant: transaction.merchant,
comment: trimmedComment,
receipt: receiptObj,
category: transaction.category,
tag: transaction.tag,
taxCode: transactionTaxCode,
taxAmount: transactionTaxAmount,
billable: transaction.billable,
gpsPoints,
validWaypoints: Object.keys(transaction?.comment?.waypoints ?? {}).length ? getValidWaypoints(transaction.comment?.waypoints, true) : undefined,
actionableWhisperReportActionID: transaction.actionableWhisperReportActionID,
linkedTrackedExpenseReportAction: transaction.linkedTrackedExpenseReportAction,
linkedTrackedExpenseReportID: transaction.linkedTrackedExpenseReportID,
customUnitRateID,
},
});
},
[
report,
Expand Down
49 changes: 22 additions & 27 deletions src/pages/iou/request/step/IOURequestStepDistance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -320,34 +320,29 @@ function IOURequestStepDistance({
const participant = participants.at(0);
if (iouType === CONST.IOU.TYPE.TRACK && participant) {
playSound(SOUNDS.DONE);
trackExpense(
trackExpense({
report,
0,
transaction?.currency ?? 'USD',
transaction?.created ?? '',
translate('iou.fieldPending'),
currentUserPersonalDetails.login,
currentUserPersonalDetails.accountID,
participant,
'',
false,
{},
'',
'',
'',
0,
false,
policy,
undefined,
undefined,
undefined,
getValidWaypoints(waypoints, true),
undefined,
undefined,
undefined,
undefined,
customUnitRateID,
);
isDraftPolicy: false,
participantParams: {
payeeEmail: currentUserPersonalDetails.login,
payeeAccountID: currentUserPersonalDetails.accountID,
participant,
},
policyParams: {
policy,
},
transactionParams: {
amount: 0,
currency: transaction?.currency ?? 'USD',
created: transaction?.created ?? '',
merchant: translate('iou.fieldPending'),
comment: '',
receipt: {},
billable: false,
validWaypoints: getValidWaypoints(waypoints, true),
customUnitRateID,
},
});
return;
}

Expand Down
74 changes: 39 additions & 35 deletions src/pages/iou/request/step/IOURequestStepScan/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -251,19 +251,23 @@ function IOURequestStepScan({
const createTransaction = useCallback(
(receipt: Receipt, participant: Participant) => {
if (iouType === CONST.IOU.TYPE.TRACK && report) {
trackExpense(
trackExpense({
report,
0,
transaction?.currency ?? 'USD',
transaction?.created ?? '',
'',
currentUserPersonalDetails.login,
currentUserPersonalDetails.accountID,
participant,
'',
false,
receipt,
);
isDraftPolicy: false,
participantParams: {
payeeEmail: currentUserPersonalDetails.login,
payeeAccountID: currentUserPersonalDetails.accountID,
participant,
},
transactionParams: {
amount: 0,
currency: transaction?.currency ?? 'USD',
created: transaction?.created,
merchant: '',
comment: '',
receipt,
},
});
} else {
requestMoney({
report,
Expand Down Expand Up @@ -339,31 +343,31 @@ function IOURequestStepScan({
(successData) => {
playSound(SOUNDS.DONE);
if (iouType === CONST.IOU.TYPE.TRACK && report) {
trackExpense(
trackExpense({
report,
0,
transaction?.currency ?? 'USD',
transaction?.created ?? '',
'',
currentUserPersonalDetails.login,
currentUserPersonalDetails.accountID,
participant,
'',
false,
receipt,
'',
'',
'',
0,
false,
policy,
{},
{},
{
lat: successData.coords.latitude,
long: successData.coords.longitude,
isDraftPolicy: false,
participantParams: {
payeeEmail: currentUserPersonalDetails.login,
payeeAccountID: currentUserPersonalDetails.accountID,
participant,
},
);
policyParams: {
policy,
},
transactionParams: {
amount: 0,
currency: transaction?.currency ?? 'USD',
created: transaction?.created,
merchant: '',
comment: '',
receipt,
billable: false,
gpsPoints: {
lat: successData.coords.latitude,
long: successData.coords.longitude,
},
},
});
} else {
requestMoney({
report,
Expand Down
Loading