Skip to content

Commit

Permalink
Merge pull request Expensify#53424 from mkzie2/mkzie2-issue/53416
Browse files Browse the repository at this point in the history
Refactor getMoneyRequestInformation function
  • Loading branch information
neil-marcellini authored Dec 4, 2024
2 parents d7859ea + 1f14d11 commit a7adbb1
Showing 1 changed file with 56 additions and 77 deletions.
133 changes: 56 additions & 77 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ type GPSPoint = {
};

type RequestMoneyTransactionParams = {
attendees: Attendee[] | undefined;
attendees?: Attendee[];
amount: number;
currency: string;
comment?: string;
Expand Down Expand Up @@ -206,6 +206,16 @@ type RequestMoneyInformation = {
transactionParams: RequestMoneyTransactionParams;
};

type MoneyRequestInformationParams = {
parentChatReport: OnyxEntry<OnyxTypes.Report>;
transactionParams: RequestMoneyTransactionParams;
participantParams: RequestMoneyParticipantParams;
policyParams?: RequestMoneyPolicyParams;
moneyRequestReportID?: string;
existingTransactionID?: string;
existingTransaction?: OnyxEntry<OnyxTypes.Transaction>;
};

let allPersonalDetails: OnyxTypes.PersonalDetailsList = {};
Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
Expand Down Expand Up @@ -2044,31 +2054,12 @@ function getSendInvoiceInformation(
* Gathers all the data needed to submit 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 getMoneyRequestInformation(
parentChatReport: OnyxEntry<OnyxTypes.Report>,
participant: Participant,
comment: string,
amount: number,
currency: string,
created: string,
merchant: string,
receipt: Receipt | undefined,
existingTransactionID: string | undefined,
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,
payeeAccountID = userAccountID,
payeeEmail = currentUserEmail,
moneyRequestReportID = '',
linkedTrackedExpenseReportAction?: OnyxTypes.ReportAction,
attendees?: Attendee[],
existingTransaction: OnyxEntry<OnyxTypes.Transaction> | undefined = undefined,
): MoneyRequestInformation {
function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInformationParams): MoneyRequestInformation {
const {parentChatReport, transactionParams, participantParams, policyParams = {}, existingTransaction, existingTransactionID, moneyRequestReportID = ''} = moneyRequestInformation;
const {payeeAccountID = userAccountID, payeeEmail = currentUserEmail, participant} = participantParams;
const {policy, policyCategories, policyTagList} = policyParams;
const {attendees, amount, comment = '', currency, created, merchant, receipt, category, tag, taxCode, taxAmount, billable, linkedTrackedExpenseReportAction} = transactionParams;

const payerEmail = PhoneNumber.addSMSDomainIfPhoneNumber(participant.login ?? '');
const payerAccountID = Number(participant.accountID);
const isPolicyExpenseChat = participant.isPolicyExpenseChat;
Expand Down Expand Up @@ -3598,8 +3589,7 @@ function shareTrackedExpense(
*/
function requestMoney(requestMoneyInformation: RequestMoneyInformation) {
const {report, participantParams, policyParams = {}, transactionParams, gpsPoints, action, reimbursible} = requestMoneyInformation;
const {participant, payeeAccountID, payeeEmail} = participantParams;
const {policy, policyCategories, policyTagList} = policyParams;
const {payeeAccountID} = participantParams;
const {
amount,
currency,
Expand Down Expand Up @@ -3637,32 +3627,17 @@ function requestMoney(requestMoneyInformation: RequestMoneyInformation) {
transactionThreadReportID,
createdReportActionIDForThread,
onyxData,
} = getMoneyRequestInformation(
isMovingTransactionFromTrackExpense ? undefined : currentChatReport,
participant,
comment,
amount,
currency,
created,
merchant,
receipt,
isMovingTransactionFromTrackExpense && linkedTrackedExpenseReportAction && ReportActionsUtils.isMoneyRequestAction(linkedTrackedExpenseReportAction)
? ReportActionsUtils.getOriginalMessage(linkedTrackedExpenseReportAction)?.IOUTransactionID
: undefined,
category,
tag,
taxCode,
taxAmount,
billable,
policy,
policyTagList,
policyCategories,
payeeAccountID,
payeeEmail,
} = getMoneyRequestInformation({
parentChatReport: isMovingTransactionFromTrackExpense ? undefined : currentChatReport,
participantParams,
policyParams,
transactionParams,
moneyRequestReportID,
linkedTrackedExpenseReportAction,
attendees,
);
existingTransactionID:
isMovingTransactionFromTrackExpense && linkedTrackedExpenseReportAction && ReportActionsUtils.isMoneyRequestAction(linkedTrackedExpenseReportAction)
? ReportActionsUtils.getOriginalMessage(linkedTrackedExpenseReportAction)?.IOUTransactionID
: undefined,
});
const activeReportID = isMoneyRequestReport ? report?.reportID : chatReport.reportID;

switch (action) {
Expand Down Expand Up @@ -5319,31 +5294,35 @@ function createDistanceRequest(
createdReportActionIDForThread,
payerEmail,
onyxData: moneyRequestOnyxData,
} = getMoneyRequestInformation(
currentChatReport,
participant,
comment,
amount,
currency,
created,
merchant,
optimisticReceipt,
undefined,
category,
tag,
taxCode,
taxAmount,
billable,
policy,
policyTagList,
policyCategories,
userAccountID,
currentUserEmail,
moneyRequestReportID,
undefined,
undefined,
} = getMoneyRequestInformation({
parentChatReport: currentChatReport,
existingTransaction,
);
moneyRequestReportID,
participantParams: {
participant,
payeeAccountID: userAccountID,
payeeEmail: currentUserEmail,
},
policyParams: {
policy,
policyCategories,
policyTagList,
},
transactionParams: {
amount,
currency,
comment,
created,
merchant,
receipt: optimisticReceipt,
category,
tag,
taxCode,
taxAmount,
billable,
},
});

onyxData = moneyRequestOnyxData;

parameters = {
Expand Down

0 comments on commit a7adbb1

Please sign in to comment.