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 IOU.requestMoney function #52231

Merged
merged 12 commits into from
Nov 22, 2024
89 changes: 62 additions & 27 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,46 @@ type GPSPoint = {
long: number;
};

type RequestMoneyTransactionParams = {
attendees: Attendee[] | undefined;
amount: number;
currency: string;
comment?: string;
receipt?: Receipt;
category?: string;
tag?: string;
taxCode?: string;
taxAmount?: number;
billable?: boolean;
merchant: string;
created: string;
actionableWhisperReportActionID?: string;
linkedTrackedExpenseReportAction?: OnyxTypes.ReportAction;
linkedTrackedExpenseReportID?: string;
};

type RequestMoneyPolicyParams = {
policy?: OnyxEntry<OnyxTypes.Policy>;
policyTagList?: OnyxEntry<OnyxTypes.PolicyTagLists>;
policyCategories?: OnyxEntry<OnyxTypes.PolicyCategories>;
};

type RequestMoneyParticipantParams = {
payeeEmail: string | undefined;
payeeAccountID: number;
participant: Participant;
};

type RequestMoneyInformation = {
report: OnyxEntry<OnyxTypes.Report>;
participantParams: RequestMoneyParticipantParams;
policyParams?: RequestMoneyPolicyParams;
gpsPoints?: GPSPoint;
action?: IOUAction;
reimbursible?: boolean;
transactionParams: RequestMoneyTransactionParams;
};

let allPersonalDetails: OnyxTypes.PersonalDetailsList = {};
Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
Expand Down Expand Up @@ -3524,33 +3564,28 @@ function shareTrackedExpense(
/**
* Submit expense to another user
*/
function requestMoney(
report: OnyxEntry<OnyxTypes.Report>,
amount: number,
attendees: Attendee[] | undefined,
currency: string,
created: string,
merchant: string,
payeeEmail: string | undefined,
payeeAccountID: number,
participant: Participant,
comment: string,
receipt: Receipt | undefined,
category?: string,
tag?: string,
taxCode = '',
taxAmount = 0,
billable?: boolean,
policy?: OnyxEntry<OnyxTypes.Policy>,
policyTagList?: OnyxEntry<OnyxTypes.PolicyTagLists>,
policyCategories?: OnyxEntry<OnyxTypes.PolicyCategories>,
gpsPoints?: GPSPoint,
action?: IOUAction,
actionableWhisperReportActionID?: string,
linkedTrackedExpenseReportAction?: OnyxTypes.ReportAction,
linkedTrackedExpenseReportID?: string,
reimbursible?: boolean,
) {
function requestMoney(requestMoneyInformation: RequestMoneyInformation) {
const {report, participantParams, policyParams = {}, transactionParams, gpsPoints, action, reimbursible} = requestMoneyInformation;
const {participant, payeeAccountID, payeeEmail} = participantParams;
const {policy, policyCategories, policyTagList} = policyParams;
const {
amount,
currency,
merchant,
comment = '',
receipt,
category,
tag,
taxCode = '',
taxAmount = 0,
billable,
created,
attendees,
actionableWhisperReportActionID,
linkedTrackedExpenseReportAction,
linkedTrackedExpenseReportID,
} = transactionParams;

// If the report is iou or expense report, we should get the linked chat report to be passed to the getMoneyRequestInformation function
const isMoneyRequestReport = ReportUtils.isMoneyRequestReport(report);
const currentChatReport = isMoneyRequestReport ? ReportUtils.getReportOrDraftReport(report?.chatReportID) : report;
Expand Down
26 changes: 14 additions & 12 deletions src/pages/iou/request/step/IOURequestStepAmount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,21 @@ function IOURequestStepAmount({
}
if (iouType === CONST.IOU.TYPE.SUBMIT || iouType === CONST.IOU.TYPE.REQUEST) {
playSound(SOUNDS.DONE);
IOU.requestMoney(
IOU.requestMoney({
report,
backendAmount,
transaction?.attendees,
currency,
transaction?.created ?? '',
CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT,
currentUserPersonalDetails.login,
currentUserPersonalDetails.accountID,
participants.at(0) ?? {},
'',
{},
);
participantParams: {
participant: participants.at(0) ?? {},
payeeEmail: currentUserPersonalDetails.login,
payeeAccountID: currentUserPersonalDetails.accountID,
},
transactionParams: {
amount: backendAmount,
currency,
created: transaction?.created ?? '',
merchant: CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT,
attendees: transaction?.attendees,
},
});
return;
}
if (iouType === CONST.IOU.TYPE.TRACK) {
Expand Down
53 changes: 29 additions & 24 deletions src/pages/iou/request/step/IOURequestStepConfirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,33 +238,38 @@ function IOURequestStepConfirmation({
if (!participant) {
return;
}

IOU.requestMoney(
IOU.requestMoney({
report,
transaction.amount,
transaction.attendees,
transaction.currency,
transaction.created,
transaction.merchant,
currentUserPersonalDetails.login,
currentUserPersonalDetails.accountID,
participant,
trimmedComment,
receiptObj,
transaction.category,
transaction.tag,
transactionTaxCode,
transactionTaxAmount,
transaction.billable,
policy,
policyTags,
policyCategories,
participantParams: {
payeeEmail: currentUserPersonalDetails.login,
payeeAccountID: currentUserPersonalDetails.accountID,
participant,
},
policyParams: {
policy,
policyTagList: policyTags,
policyCategories,
},
gpsPoints,
action,
transaction.actionableWhisperReportActionID,
transaction.linkedTrackedExpenseReportAction,
transaction.linkedTrackedExpenseReportID,
);
transactionParams: {
amount: transaction.amount,
attendees: transaction.attendees,
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,
actionableWhisperReportActionID: transaction.actionableWhisperReportActionID,
linkedTrackedExpenseReportAction: transaction.linkedTrackedExpenseReportAction,
linkedTrackedExpenseReportID: transaction.linkedTrackedExpenseReportID,
},
});
},
[report, transaction, transactionTaxCode, transactionTaxAmount, currentUserPersonalDetails.login, currentUserPersonalDetails.accountID, policy, policyTags, policyCategories, action],
);
Expand Down
68 changes: 35 additions & 33 deletions src/pages/iou/request/step/IOURequestStepScan/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,19 +255,22 @@ function IOURequestStepScan({
receipt,
);
} else {
IOU.requestMoney(
IOU.requestMoney({
report,
0,
transaction?.attendees,
transaction?.currency ?? 'USD',
transaction?.created ?? '',
'',
currentUserPersonalDetails.login,
currentUserPersonalDetails.accountID,
participant,
'',
receipt,
);
participantParams: {
payeeEmail: currentUserPersonalDetails.login,
payeeAccountID: currentUserPersonalDetails.accountID,
participant,
},
transactionParams: {
amount: 0,
attendees: transaction?.attendees,
currency: transaction?.currency ?? 'USD',
created: transaction?.created ?? '',
merchant: '',
receipt,
},
});
}
},
[currentUserPersonalDetails.accountID, currentUserPersonalDetails.login, iouType, report, transaction?.attendees, transaction?.created, transaction?.currency],
Expand Down Expand Up @@ -352,31 +355,30 @@ function IOURequestStepScan({
},
);
} else {
IOU.requestMoney(
IOU.requestMoney({
report,
0,
transaction?.attendees,
transaction?.currency ?? 'USD',
transaction?.created ?? '',
'',
currentUserPersonalDetails.login,
currentUserPersonalDetails.accountID,
participant,
'',
receipt,
'',
'',
'',
0,
false,
policy,
{},
{},
{
participantParams: {
payeeEmail: currentUserPersonalDetails.login,
payeeAccountID: currentUserPersonalDetails.accountID,
participant,
},
policyParams: {
policy,
},
gpsPoints: {
lat: successData.coords.latitude,
long: successData.coords.longitude,
},
);
transactionParams: {
amount: 0,
attendees: transaction?.attendees,
currency: transaction?.currency ?? 'USD',
created: transaction?.created ?? '',
merchant: '',
receipt,
billable: false,
},
});
}
},
(errorData) => {
Expand Down
68 changes: 35 additions & 33 deletions src/pages/iou/request/step/IOURequestStepScan/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -284,19 +284,22 @@ function IOURequestStepScan({
receipt,
);
} else {
IOU.requestMoney(
IOU.requestMoney({
report,
0,
transaction?.attendees,
transaction?.currency ?? 'USD',
transaction?.created ?? '',
'',
currentUserPersonalDetails.login,
currentUserPersonalDetails.accountID,
participant,
'',
receipt,
);
participantParams: {
payeeEmail: currentUserPersonalDetails.login,
payeeAccountID: currentUserPersonalDetails.accountID,
participant,
},
transactionParams: {
amount: 0,
attendees: transaction?.attendees,
currency: transaction?.currency ?? 'USD',
created: transaction?.created ?? '',
merchant: '',
receipt,
},
});
}
},
[currentUserPersonalDetails.accountID, currentUserPersonalDetails.login, iouType, report, transaction?.attendees, transaction?.created, transaction?.currency],
Expand Down Expand Up @@ -382,31 +385,30 @@ function IOURequestStepScan({
},
);
} else {
IOU.requestMoney(
IOU.requestMoney({
report,
0,
transaction?.attendees,
transaction?.currency ?? 'USD',
transaction?.created ?? '',
'',
currentUserPersonalDetails.login,
currentUserPersonalDetails.accountID,
participant,
'',
receipt,
'',
'',
'',
0,
false,
policy,
{},
{},
{
participantParams: {
payeeEmail: currentUserPersonalDetails.login,
payeeAccountID: currentUserPersonalDetails.accountID,
participant,
},
policyParams: {
policy,
},
gpsPoints: {
lat: successData.coords.latitude,
long: successData.coords.longitude,
},
);
transactionParams: {
amount: 0,
attendees: transaction?.attendees,
currency: transaction?.currency ?? 'USD',
created: transaction?.created ?? '',
merchant: '',
receipt,
billable: false,
},
});
}
},
(errorData) => {
Expand Down
Loading
Loading