Skip to content

Commit

Permalink
Merge pull request #53994 from mkzie2/mkzie2-issue/53789
Browse files Browse the repository at this point in the history
Refactor createDistanceRequest function
  • Loading branch information
neil-marcellini authored Dec 16, 2024
2 parents 8d9094a + 61c5813 commit 7e49080
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 67 deletions.
64 changes: 40 additions & 24 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,32 @@ type BuildOnyxDataForMoneyRequestParams = {
optimisticParams: MoneyRequestOptimisticParams;
};

type DistanceRequestTransactionParams = {
comment: string;
created: string;
category?: string;
tag?: string;
taxCode?: string;
taxAmount?: number;
amount: number;
currency: string;
merchant: string;
billable?: boolean;
validWaypoints: WaypointCollection;
customUnitRateID?: string;
splitShares?: SplitShares;
};
type CreateDistanceRequestInformation = {
report: OnyxEntry<OnyxTypes.Report>;
participants: Participant[];
currentUserLogin?: string;
currentUserAccountID?: number;
iouType?: ValueOf<typeof CONST.IOU.TYPE>;
existingTransaction?: OnyxEntry<OnyxTypes.Transaction>;
transactionParams: DistanceRequestTransactionParams;
policyParams?: RequestMoneyPolicyParams;
};

let allPersonalDetails: OnyxTypes.PersonalDetailsList = {};
Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
Expand Down Expand Up @@ -5347,30 +5373,20 @@ function setDraftSplitTransaction(transactionID: string, transactionChanges: Tra
}

/** Requests money based on a distance (e.g. mileage from a map) */
function createDistanceRequest(
report: OnyxEntry<OnyxTypes.Report>,
participants: Participant[],
comment: string,
created: string,
category: string | undefined,
tag: string | undefined,
taxCode: string | undefined,
taxAmount: number | undefined,
amount: number,
currency: string,
merchant: string,
billable: boolean | undefined,
validWaypoints: WaypointCollection,
policy?: OnyxEntry<OnyxTypes.Policy>,
policyTagList?: OnyxEntry<OnyxTypes.PolicyTagLists>,
policyCategories?: OnyxEntry<OnyxTypes.PolicyCategories>,
customUnitRateID = '',
currentUserLogin = '',
currentUserAccountID = -1,
splitShares: SplitShares = {},
iouType: ValueOf<typeof CONST.IOU.TYPE> = CONST.IOU.TYPE.SUBMIT,
existingTransaction: OnyxEntry<OnyxTypes.Transaction> | undefined = undefined,
) {
function createDistanceRequest(distanceRequestInformation: CreateDistanceRequestInformation) {
const {
report,
participants,
currentUserLogin = '',
currentUserAccountID = -1,
iouType = CONST.IOU.TYPE.SUBMIT,
existingTransaction,
transactionParams,
policyParams = {},
} = distanceRequestInformation;
const {policy, policyCategories, policyTagList} = policyParams;
const {amount, comment, currency, created, category, tag, taxAmount, taxCode, merchant, billable, validWaypoints, customUnitRateID = '', splitShares = {}} = transactionParams;

// If the report is an 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
48 changes: 26 additions & 22 deletions src/pages/iou/request/step/IOURequestStepConfirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -320,30 +320,34 @@ function IOURequestStepConfirmation({
if (!transaction) {
return;
}
IOU.createDistanceRequest(
IOU.createDistanceRequest({
report,
selectedParticipants,
trimmedComment,
transaction.created,
transaction.category,
transaction.tag,
transactionTaxCode,
transactionTaxAmount,
transaction.amount,
transaction.currency,
transaction.merchant,
transaction.billable,
TransactionUtils.getValidWaypoints(transaction.comment?.waypoints, true),
policy,
policyTags,
policyCategories,
customUnitRateID,
currentUserPersonalDetails.login,
currentUserPersonalDetails.accountID,
transaction.splitShares,
participants: selectedParticipants,
currentUserLogin: currentUserPersonalDetails.login,
currentUserAccountID: currentUserPersonalDetails.accountID,
iouType,
transaction,
);
existingTransaction: transaction,
policyParams: {
policy,
policyCategories,
policyTagList: policyTags,
},
transactionParams: {
amount: transaction.amount,
comment: trimmedComment,
created: transaction.created,
currency: transaction.currency,
merchant: transaction.merchant,
category: transaction.category,
tag: transaction.tag,
taxCode: transactionTaxCode,
taxAmount: transactionTaxAmount,
customUnitRateID,
splitShares: transaction.splitShares,
validWaypoints: TransactionUtils.getValidWaypoints(transaction.comment?.waypoints, true),
billable: transaction.billable,
},
});
},
[policy, policyCategories, policyTags, report, transaction, transactionTaxCode, transactionTaxAmount, customUnitRateID, currentUserPersonalDetails, iouType],
);
Expand Down
37 changes: 16 additions & 21 deletions src/pages/iou/request/step/IOURequestStepDistance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -341,30 +341,25 @@ function IOURequestStepDistance({
}

playSound(SOUNDS.DONE);
IOU.createDistanceRequest(
IOU.createDistanceRequest({
report,
participants,
'',
transaction?.created ?? '',
'',
'',
'',
0,
0,
transaction?.currency ?? 'USD',
translate('iou.fieldPending'),
!!policy?.defaultBillable,
TransactionUtils.getValidWaypoints(waypoints, true),
undefined,
undefined,
undefined,
DistanceRequestUtils.getCustomUnitRateID(report.reportID),
currentUserPersonalDetails.login ?? '',
currentUserPersonalDetails.accountID,
transaction?.splitShares,
currentUserLogin: currentUserPersonalDetails.login,
currentUserAccountID: currentUserPersonalDetails.accountID,
iouType,
transaction,
);
existingTransaction: transaction,
transactionParams: {
amount: 0,
comment: '',
created: transaction?.created ?? '',
currency: transaction?.currency ?? 'USD',
merchant: translate('iou.fieldPending'),
billable: !!policy?.defaultBillable,
validWaypoints: TransactionUtils.getValidWaypoints(waypoints, true),
customUnitRateID: DistanceRequestUtils.getCustomUnitRateID(report.reportID),
splitShares: transaction?.splitShares,
},
});
return;
}
IOU.setMoneyRequestParticipantsFromReport(transactionID, report);
Expand Down

0 comments on commit 7e49080

Please sign in to comment.