Skip to content

Refactor getTrackExpenseInformation function #57316

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 7 commits into from
Feb 26, 2025
Merged
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
130 changes: 75 additions & 55 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ type PerDiemExpenseTransactionParams = {
billable?: boolean;
};

type RequestMoneyPolicyParams = {
type BasePolicyParams = {
policy?: OnyxEntry<OnyxTypes.Policy>;
policyTagList?: OnyxEntry<OnyxTypes.PolicyTagLists>;
policyCategories?: OnyxEntry<OnyxTypes.PolicyCategories>;
Expand All @@ -359,22 +359,22 @@ type RequestMoneyParticipantParams = {
type PerDiemExpenseInformation = {
report: OnyxEntry<OnyxTypes.Report>;
participantParams: RequestMoneyParticipantParams;
policyParams?: RequestMoneyPolicyParams;
policyParams?: BasePolicyParams;
transactionParams: PerDiemExpenseTransactionParams;
};

type PerDiemExpenseInformationParams = {
parentChatReport: OnyxEntry<OnyxTypes.Report>;
transactionParams: PerDiemExpenseTransactionParams;
participantParams: RequestMoneyParticipantParams;
policyParams?: RequestMoneyPolicyParams;
policyParams?: BasePolicyParams;
moneyRequestReportID?: string;
};

type RequestMoneyInformation = {
report: OnyxEntry<OnyxTypes.Report>;
participantParams: RequestMoneyParticipantParams;
policyParams?: RequestMoneyPolicyParams;
policyParams?: BasePolicyParams;
gpsPoints?: GPSPoint;
action?: IOUAction;
reimbursible?: boolean;
Expand All @@ -385,7 +385,7 @@ type MoneyRequestInformationParams = {
parentChatReport: OnyxEntry<OnyxTypes.Report>;
transactionParams: RequestMoneyTransactionParams;
participantParams: RequestMoneyParticipantParams;
policyParams?: RequestMoneyPolicyParams;
policyParams?: BasePolicyParams;
moneyRequestReportID?: string;
existingTransactionID?: string;
existingTransaction?: OnyxEntry<OnyxTypes.Transaction>;
Expand Down Expand Up @@ -422,7 +422,7 @@ type BuildOnyxDataForMoneyRequestParams = {
shouldCreateNewMoneyRequestReport: boolean;
isOneOnOneSplit?: boolean;
existingTransactionThreadReportID?: string;
policyParams?: RequestMoneyPolicyParams;
policyParams?: BasePolicyParams;
optimisticParams: MoneyRequestOptimisticParams;
};

Expand All @@ -449,7 +449,7 @@ type CreateDistanceRequestInformation = {
iouType?: ValueOf<typeof CONST.IOU.TYPE>;
existingTransaction?: OnyxEntry<OnyxTypes.Transaction>;
transactionParams: DistanceRequestTransactionParams;
policyParams?: RequestMoneyPolicyParams;
policyParams?: BasePolicyParams;
};

type TrackExpenseTransactionParams = {
Expand Down Expand Up @@ -477,10 +477,40 @@ type CreateTrackExpenseParams = {
isDraftPolicy: boolean;
action?: IOUAction;
participantParams: RequestMoneyParticipantParams;
policyParams?: RequestMoneyPolicyParams;
policyParams?: BasePolicyParams;
transactionParams: TrackExpenseTransactionParams;
};

type GetTrackExpenseInformationTransactionParams = {
comment: string;
amount: number;
currency: string;
created: string;
merchant: string;
receipt: OnyxEntry<Receipt>;
category?: string;
tag?: string;
taxCode?: string;
taxAmount?: number;
billable?: boolean;
linkedTrackedExpenseReportAction?: OnyxTypes.ReportAction;
};

type GetTrackExpenseInformationParticipantParams = {
payeeEmail?: string;
payeeAccountID?: number;
participant: Participant;
};

type GetTrackExpenseInformationParams = {
parentChatReport: OnyxEntry<OnyxTypes.Report>;
moneyRequestReportID?: string;
existingTransactionID?: string;
participantParams: GetTrackExpenseInformationParticipantParams;
policyParams: BasePolicyParams;
transactionParams: GetTrackExpenseInformationTransactionParams;
};

let allPersonalDetails: OnyxTypes.PersonalDetailsList = {};
Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
Expand Down Expand Up @@ -3078,29 +3108,12 @@ function getPerDiemExpenseInformation(perDiemExpenseInformation: PerDiemExpenseI
* Gathers all the data needed to make an expense. It attempts to find existing reports, iouReports, and receipts. If it doesn't find them, then
* it creates optimistic versions of them and uses those instead
*/
function getTrackExpenseInformation(
parentChatReport: OnyxEntry<OnyxTypes.Report>,
participant: Participant,
comment: string,
amount: number,
currency: string,
created: string,
merchant: string,
receipt: OnyxEntry<Receipt>,
category: string | undefined,
tag: string | undefined,
taxCode: string | undefined,
taxAmount: number | undefined,
billable: boolean | undefined,
policy: OnyxEntry<OnyxTypes.Policy> | undefined,
policyTagList: OnyxEntry<OnyxTypes.PolicyTagLists> | undefined,
policyCategories: OnyxEntry<OnyxTypes.PolicyCategories> | undefined,
payeeEmail = currentUserEmail,
payeeAccountID = userAccountID,
moneyRequestReportID = '',
linkedTrackedExpenseReportAction?: OnyxTypes.ReportAction,
existingTransactionID?: string,
): TrackExpenseInformation | null {
function getTrackExpenseInformation(params: GetTrackExpenseInformationParams): TrackExpenseInformation | null {
const {parentChatReport, moneyRequestReportID = '', existingTransactionID, participantParams, policyParams, transactionParams} = params;
const {payeeAccountID = userAccountID, payeeEmail = currentUserEmail, participant} = participantParams;
const {policy, policyCategories, policyTagList} = policyParams;
const {comment, amount, currency, created, merchant, receipt, category, tag, taxCode, taxAmount, billable, linkedTrackedExpenseReportAction} = transactionParams;

const optimisticData: OnyxUpdate[] = [];
const successData: OnyxUpdate[] = [];
const failureData: OnyxUpdate[] = [];
Expand Down Expand Up @@ -4824,31 +4837,38 @@ function trackExpense(params: CreateTrackExpenseParams) {
actionableWhisperReportActionIDParam,
onyxData,
} =
getTrackExpenseInformation(
currentChatReport,
participant,
comment,
amount,
currency,
created,
merchant,
trackedReceipt,
category,
tag,
taxCode,
taxAmount,
billable,
policy,
policyTagList,
policyCategories,
payeeEmail,
payeeAccountID,
getTrackExpenseInformation({
parentChatReport: currentChatReport,
moneyRequestReportID,
linkedTrackedExpenseReportAction,
isMovingTransactionFromTrackExpense && linkedTrackedExpenseReportAction && isMoneyRequestAction(linkedTrackedExpenseReportAction)
? getOriginalMessage(linkedTrackedExpenseReportAction)?.IOUTransactionID
: undefined,
) ?? {};
existingTransactionID:
isMovingTransactionFromTrackExpense && linkedTrackedExpenseReportAction && isMoneyRequestAction(linkedTrackedExpenseReportAction)
? getOriginalMessage(linkedTrackedExpenseReportAction)?.IOUTransactionID
: undefined,
participantParams: {
participant,
payeeAccountID,
payeeEmail,
},
transactionParams: {
comment,
amount,
currency,
created,
merchant,
receipt: trackedReceipt,
category,
tag,
taxCode,
taxAmount,
billable,
linkedTrackedExpenseReportAction,
},
policyParams: {
policy,
policyCategories,
policyTagList,
},
}) ?? {};
const activeReportID = isMoneyRequestReport ? report.reportID : chatReport?.reportID;

const recentServerValidatedWaypoints = getRecentWaypoints().filter((item) => !item.pendingAction);
Expand Down
Loading