Skip to content

Commit

Permalink
Merge pull request Expensify#57316 from mkzie2/mkzie2-issue/57190
Browse files Browse the repository at this point in the history
Refactor getTrackExpenseInformation function
  • Loading branch information
neil-marcellini authored Feb 26, 2025
2 parents 429e55c + 00b184f commit 7215156
Showing 1 changed file with 75 additions and 55 deletions.
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

0 comments on commit 7215156

Please sign in to comment.