From 2cd62cef121210147090aa49d4c63863ed26531b Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Wed, 18 Dec 2024 12:58:35 +0100 Subject: [PATCH 01/42] chore: test commit for the linter --- src/libs/actions/IOU.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 5f8b83d4b5c2..0bc83f801885 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -78,6 +78,8 @@ import * as Report from './Report'; import {getRecentWaypoints, sanitizeRecentWaypoints} from './Transaction'; import * as TransactionEdit from './TransactionEdit'; +console.log('test'); + type IOURequestType = ValueOf; type OneOnOneIOUReport = OnyxTypes.Report | undefined | null; From 2f53a1ae917d7b8b421525d9444b93c0df57a4b9 Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Wed, 18 Dec 2024 13:36:36 +0100 Subject: [PATCH 02/42] chore: fix lint errors on missing CONST.DEFAULT_NUMBER_ID usages for numeric ids --- src/libs/actions/IOU.ts | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 0bc83f801885..e8dca8db1067 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -390,7 +390,7 @@ Onyx.connect({ key: ONYXKEYS.SESSION, callback: (value) => { currentUserEmail = value?.email ?? ''; - userAccountID = value?.accountID ?? -1; + userAccountID = value?.accountID ?? CONST.DEFAULT_NUMBER_ID; }, }); @@ -2080,7 +2080,7 @@ function getSendInvoiceInformation( const trimmedComment = (comment?.comment ?? '').trim(); const senderWorkspaceID = participants?.find((participant) => participant?.isSender)?.policyID ?? '-1'; const receiverParticipant = participants?.find((participant) => participant?.accountID) ?? invoiceChatReport?.invoiceReceiver; - const receiverAccountID = receiverParticipant && 'accountID' in receiverParticipant && receiverParticipant.accountID ? receiverParticipant.accountID : -1; + const receiverAccountID = receiverParticipant && 'accountID' in receiverParticipant && receiverParticipant.accountID ? receiverParticipant.accountID : CONST.DEFAULT_NUMBER_ID; let receiver = ReportUtils.getPersonalDetailsForAccountID(receiverAccountID); let optimisticPersonalDetailListAction = {}; @@ -2845,7 +2845,7 @@ function getUpdateMoneyRequestParams( } else { updatedMoneyRequestReport = IOUUtils.updateIOUOwnerAndTotal( iouReport, - updatedReportAction.actorAccountID ?? -1, + updatedReportAction.actorAccountID ?? CONST.DEFAULT_NUMBER_ID, diff, TransactionUtils.getCurrency(transaction), false, @@ -4414,7 +4414,7 @@ function createSplitsAndOnyxData( participants.forEach((participant) => { // In a case when a participant is a workspace, even when a current user is not an owner of the workspace const isPolicyExpenseChat = ReportUtils.isPolicyExpenseChat(participant); - const splitAmount = splitShares?.[participant.accountID ?? -1]?.amount ?? IOUUtils.calculateAmount(participants.length, amount, currency, false); + const splitAmount = splitShares?.[participant.accountID ?? CONST.DEFAULT_NUMBER_ID]?.amount ?? IOUUtils.calculateAmount(participants.length, amount, currency, false); const splitTaxAmount = IOUUtils.calculateAmount(participants.length, taxAmount, currency, false); // To exclude someone from a split, the amount can be 0. The scenario for this is when creating a split from a group chat, we have remove the option to deselect users to exclude them. @@ -5015,7 +5015,7 @@ function startSplitBill({ return; } - const participantPersonalDetails = allPersonalDetails[participant?.accountID ?? -1]; + const participantPersonalDetails = allPersonalDetails[participant?.accountID ?? CONST.DEFAULT_NUMBER_ID]; if (!participantPersonalDetails) { optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, @@ -5206,7 +5206,7 @@ function completeSplitBill(chatReportID: string, reportAction: OnyxTypes.ReportA // In case this is still the optimistic accountID saved in the splits array, return early as we cannot know // if there is an existing chat between the split creator and this participant // Instead, we will rely on Auth generating the report IDs and the user won't see any optimistic chats or reports created - const participantPersonalDetails: OnyxTypes.PersonalDetails | null = allPersonalDetails[participant?.accountID ?? -1]; + const participantPersonalDetails: OnyxTypes.PersonalDetails | null = allPersonalDetails[participant?.accountID ?? CONST.DEFAULT_NUMBER_ID]; if (!participantPersonalDetails || participantPersonalDetails.isOptimisticPersonalDetail) { splits.push({ email: participant.email, @@ -5233,7 +5233,7 @@ function completeSplitBill(chatReportID: string, reportAction: OnyxTypes.ReportA if (!oneOnOneIOUReport || shouldCreateNewOneOnOneIOUReport) { oneOnOneIOUReport = isPolicyExpenseChat ? ReportUtils.buildOptimisticExpenseReport(oneOnOneChatReport?.reportID ?? '-1', participant.policyID ?? '-1', sessionAccountID, splitAmount, currency ?? '') - : ReportUtils.buildOptimisticIOUReport(sessionAccountID, participant.accountID ?? -1, splitAmount, oneOnOneChatReport?.reportID ?? '-1', currency ?? ''); + : ReportUtils.buildOptimisticIOUReport(sessionAccountID, participant.accountID ?? CONST.DEFAULT_NUMBER_ID, splitAmount, oneOnOneChatReport?.reportID ?? '-1', currency ?? ''); } else if (isPolicyExpenseChat) { if (typeof oneOnOneIOUReport?.total === 'number') { // Because of the Expense reports are stored as negative values, we subtract the total from the amount @@ -5664,7 +5664,7 @@ function prepareToCleanUpMoneyRequest(transactionID: string, reportAction: OnyxT } else { updatedIOUReport = IOUUtils.updateIOUOwnerAndTotal( iouReport, - reportAction.actorAccountID ?? -1, + reportAction.actorAccountID ?? CONST.DEFAULT_NUMBER_ID, TransactionUtils.getAmount(transaction, false), currency, true, @@ -5680,7 +5680,7 @@ function prepareToCleanUpMoneyRequest(transactionID: string, reportAction: OnyxT const hasNonReimbursableTransactions = ReportUtils.hasNonReimbursableTransactions(iouReport?.reportID); const messageText = Localize.translateLocal(hasNonReimbursableTransactions ? 'iou.payerSpentAmount' : 'iou.payerOwesAmount', { - payer: ReportUtils.getPersonalDetailsForAccountID(updatedIOUReport?.managerID ?? -1).login ?? '', + payer: ReportUtils.getPersonalDetailsForAccountID(updatedIOUReport?.managerID ?? CONST.DEFAULT_NUMBER_ID).login ?? '', amount: CurrencyUtils.convertToDisplayString(updatedIOUReport?.total, updatedIOUReport?.currency), }); @@ -6611,8 +6611,8 @@ function getReportFromHoldRequestsOnyxData( newParentReportActionID, ) : ReportUtils.buildOptimisticIOUReport( - iouReport?.ownerAccountID ?? -1, - iouReport?.managerID ?? -1, + iouReport?.ownerAccountID ?? CONST.DEFAULT_NUMBER_ID, + iouReport?.managerID ?? CONST.DEFAULT_NUMBER_ID, holdAmount, chatReport.reportID, iouReport?.currency ?? '', @@ -7128,7 +7128,7 @@ function canApproveIOU( return false; } - const managerID = iouReport?.managerID ?? -1; + const managerID = iouReport?.managerID ?? CONST.DEFAULT_NUMBER_ID; const isCurrentUserManager = managerID === userAccountID; const isOpenExpenseReport = ReportUtils.isOpenExpenseReport(iouReport); const isApproved = ReportUtils.isReportApproved(iouReport); @@ -7814,7 +7814,7 @@ function cancelPayment(expenseReport: OnyxEntry, chatReport: O { iouReportID: expenseReport.reportID, chatReportID: chatReport.reportID, - managerAccountID: expenseReport.managerID ?? -1, + managerAccountID: expenseReport.managerID ?? CONST.DEFAULT_NUMBER_ID, reportActionID: optimisticReportAction.reportActionID, }, {optimisticData, successData, failureData}, @@ -7869,7 +7869,7 @@ function payMoneyRequest(paymentType: PaymentMethodType, chatReport: OnyxTypes.R const paymentSelected = paymentType === CONST.IOU.PAYMENT_TYPE.VBBA ? CONST.IOU.PAYMENT_SELECTED.BBA : CONST.IOU.PAYMENT_SELECTED.PBA; completePaymentOnboarding(paymentSelected); - const recipient = {accountID: iouReport?.ownerAccountID ?? -1}; + const recipient = {accountID: iouReport?.ownerAccountID ?? CONST.DEFAULT_NUMBER_ID}; const {params, optimisticData, successData, failureData} = getPayMoneyRequestParams(chatReport, iouReport, recipient, paymentType, full); // For now, we need to call the PayMoneyRequestWithWallet API since PayMoneyRequest was not updated to work with @@ -7881,7 +7881,7 @@ function payMoneyRequest(paymentType: PaymentMethodType, chatReport: OnyxTypes.R } function payInvoice(paymentMethodType: PaymentMethodType, chatReport: OnyxTypes.Report, invoiceReport: OnyxEntry, payAsBusiness = false) { - const recipient = {accountID: invoiceReport?.ownerAccountID ?? -1}; + const recipient = {accountID: invoiceReport?.ownerAccountID ?? CONST.DEFAULT_NUMBER_ID}; const { optimisticData, successData, From 9f0352f6fec18f3f52645b4cfb0e9d5e4b0d1201 Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Wed, 18 Dec 2024 13:37:11 +0100 Subject: [PATCH 03/42] chore: rm a dummy console log --- src/libs/actions/IOU.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index e8dca8db1067..39c39e852dfb 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -78,8 +78,6 @@ import * as Report from './Report'; import {getRecentWaypoints, sanitizeRecentWaypoints} from './Transaction'; import * as TransactionEdit from './TransactionEdit'; -console.log('test'); - type IOURequestType = ValueOf; type OneOnOneIOUReport = OnyxTypes.Report | undefined | null; From dd0de4690314c649db9750808268066d060369d2 Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Wed, 18 Dec 2024 13:47:16 +0100 Subject: [PATCH 04/42] chore: remove an eslint override for the IOU.ts file exclusion --- .eslintrc.changed.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.eslintrc.changed.js b/.eslintrc.changed.js index a72dd6a9250a..3284b3cafafd 100644 --- a/.eslintrc.changed.js +++ b/.eslintrc.changed.js @@ -10,7 +10,7 @@ module.exports = { }, overrides: [ { - files: ['src/libs/ReportUtils.ts', 'src/libs/actions/IOU.ts', 'src/libs/actions/Report.ts', 'src/libs/actions/Task.ts'], + files: ['src/libs/ReportUtils.ts', 'src/libs/actions/Report.ts', 'src/libs/actions/Task.ts'], rules: { 'rulesdir/no-default-id-values': 'off', }, From 661790f2d4dd6cad84a7e296421be6e465e0b57a Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Wed, 18 Dec 2024 17:13:27 +0100 Subject: [PATCH 05/42] chore: add an early return for transactionThreadReportIDs of undefined --- src/libs/actions/IOU.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 39c39e852dfb..471ccb0b6e5c 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -8704,9 +8704,13 @@ function resolveDuplicates(params: TransactionMergeParams) { const optimisticHoldTransactionActions: OnyxUpdate[] = []; const failureHoldTransactionActions: OnyxUpdate[] = []; transactionThreadReportIDList.forEach((transactionThreadReportID) => { + if (!transactionThreadReportID) { + return; + } + const createdReportAction = ReportUtils.buildOptimisticHoldReportAction(); reportActionIDList.push(createdReportAction.reportActionID); - const transactionID = TransactionUtils.getTransactionID(transactionThreadReportID ?? '-1'); + const transactionID = TransactionUtils.getTransactionID(transactionThreadReportID); optimisticHoldTransactionActions.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, From 1ddfa9093607423689efdd3386f0fe0010d80fa9 Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Wed, 18 Dec 2024 17:24:13 +0100 Subject: [PATCH 06/42] chore: add an early return on invalid input to the payInvoice function --- src/libs/actions/IOU.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 471ccb0b6e5c..fe7a318994d9 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -7901,8 +7901,12 @@ function payInvoice(paymentMethodType: PaymentMethodType, chatReport: OnyxTypes. const paymentSelected = paymentMethodType === CONST.IOU.PAYMENT_TYPE.VBBA ? CONST.IOU.PAYMENT_SELECTED.BBA : CONST.IOU.PAYMENT_SELECTED.PBA; completePaymentOnboarding(paymentSelected); + if (!invoiceReport?.reportID) { + return; + } + let params: PayInvoiceParams = { - reportID: invoiceReport?.reportID ?? '', + reportID: invoiceReport.reportID, reportActionID, paymentMethodType, payAsBusiness, From 529b1f6004d5ec9c073123e028ed3236cef82df7 Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Thu, 19 Dec 2024 11:04:42 +0100 Subject: [PATCH 07/42] chore: filter out undefiend values --- src/libs/actions/IOU.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index fe7a318994d9..f1b5805cad83 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -8697,10 +8697,12 @@ function resolveDuplicates(params: TransactionMergeParams) { const iouActionList = getIOUActionForTransactions(params.transactionIDList, params.reportID); const transactionThreadReportIDList = iouActionList.map((action) => action?.childReportID); - const orderedTransactionIDList = iouActionList.map((action) => { - const message = ReportActionsUtils.getOriginalMessage(action); - return message?.IOUTransactionID ?? ''; - }); + const orderedTransactionIDList = iouActionList + .map((action) => { + const message = ReportActionsUtils.getOriginalMessage(action); + return message?.IOUTransactionID; + }) + .filter(Boolean) as string[]; const optimisticHoldActions: OnyxUpdate[] = []; const failureHoldActions: OnyxUpdate[] = []; From 17829b76748a256ff66b3b6f8e7efc398e0d3b7b Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Thu, 19 Dec 2024 11:10:26 +0100 Subject: [PATCH 08/42] chore: allow undefined output from getIOURequestPolicyID --- src/libs/actions/IOU.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index f1b5805cad83..b2e4dcf4994c 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -8476,10 +8476,10 @@ function savePreferredPaymentMethod(policyID: string, paymentMethod: PaymentMeth } /** Get report policy id of IOU request */ -function getIOURequestPolicyID(transaction: OnyxEntry, report: OnyxEntry): string { +function getIOURequestPolicyID(transaction: OnyxEntry, report: OnyxEntry): string | undefined { // Workspace sender will exist for invoices const workspaceSender = transaction?.participants?.find((participant) => participant.isSender); - return workspaceSender?.policyID ?? report?.policyID ?? '-1'; + return workspaceSender?.policyID ?? report?.policyID; } function getIOUActionForTransactions(transactionIDList: string[], iouReportID: string): Array> { From cf617bd1c124c79f7c0414605b39107ef2bd90ae Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Thu, 19 Dec 2024 11:12:50 +0100 Subject: [PATCH 09/42] chore: remove a fallback index access on the failure data --- src/libs/actions/IOU.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index b2e4dcf4994c..d58e63bb6280 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -7745,7 +7745,7 @@ function cancelPayment(expenseReport: OnyxEntry, chatReport: O onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReport.reportID}`, value: { - [optimisticReportAction.reportActionID ?? '-1']: { + [optimisticReportAction.reportActionID]: { errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('iou.error.other'), }, }, From 1b42796864004fc46a38df909a189d81dc885775 Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Thu, 19 Dec 2024 11:25:25 +0100 Subject: [PATCH 10/42] chore: early return on missing input to the cancelPayment func --- src/libs/actions/IOU.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index d58e63bb6280..80ba667c14e4 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -7680,14 +7680,18 @@ function cancelPayment(expenseReport: OnyxEntry, chatReport: O return; } + if (!chatReport.iouReportID) { + return; + } + const optimisticReportAction = ReportUtils.buildOptimisticCancelPaymentReportAction(expenseReport.reportID, -(expenseReport.total ?? 0), expenseReport.currency ?? ''); const policy = PolicyUtils.getPolicy(chatReport.policyID); const approvalMode = policy?.approvalMode ?? CONST.POLICY.APPROVAL_MODE.BASIC; const stateNum: ValueOf = approvalMode === CONST.POLICY.APPROVAL_MODE.OPTIONAL ? CONST.REPORT.STATE_NUM.SUBMITTED : CONST.REPORT.STATE_NUM.APPROVED; const statusNum: ValueOf = approvalMode === CONST.POLICY.APPROVAL_MODE.OPTIONAL ? CONST.REPORT.STATUS_NUM.CLOSED : CONST.REPORT.STATUS_NUM.APPROVED; const optimisticNextStep = NextStepUtils.buildNextStep(expenseReport, statusNum); - const iouReportActions = ReportActionsUtils.getAllReportActions(chatReport.iouReportID ?? '-1'); - const expenseReportActions = ReportActionsUtils.getAllReportActions(expenseReport.reportID ?? '-1'); + const iouReportActions = ReportActionsUtils.getAllReportActions(chatReport.iouReportID); + const expenseReportActions = ReportActionsUtils.getAllReportActions(expenseReport.reportID); const iouCreatedAction = Object.values(iouReportActions).find((action) => ReportActionsUtils.isCreatedAction(action)); const expenseCreatedAction = Object.values(expenseReportActions).find((action) => ReportActionsUtils.isCreatedAction(action)); const optimisticData: OnyxUpdate[] = [ From 2042569ecd4baf4f1a3ccefe82a364f35109702a Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Thu, 19 Dec 2024 11:29:42 +0100 Subject: [PATCH 11/42] chore: remove more onyx key fallbacks --- src/libs/actions/IOU.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 80ba667c14e4..7ab880aed8ac 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -6910,7 +6910,7 @@ function getPayMoneyRequestParams( }, { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport?.reportID ?? ''}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport?.reportID}`, value: { [optimisticIOUReportAction.reportActionID]: { ...(optimisticIOUReportAction as OnyxTypes.ReportAction), @@ -6920,7 +6920,7 @@ function getPayMoneyRequestParams( }, { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID ?? ''}`, + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, value: { ...iouReport, lastMessageText: ReportActionsUtils.getReportActionText(optimisticIOUReportAction), @@ -6941,14 +6941,14 @@ function getPayMoneyRequestParams( }, { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport?.reportID ?? ''}`, + key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport?.reportID}`, value: optimisticNextStep, }, ); successData.push({ onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID ?? ''}`, + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, value: { pendingFields: { preview: null, @@ -6961,7 +6961,7 @@ function getPayMoneyRequestParams( failureData.push( { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport?.reportID ?? ''}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport?.reportID}`, value: { [optimisticIOUReportAction.reportActionID]: { errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('iou.error.other'), @@ -6970,7 +6970,7 @@ function getPayMoneyRequestParams( }, { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID ?? ''}`, + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, value: { ...iouReport, }, @@ -6982,7 +6982,7 @@ function getPayMoneyRequestParams( }, { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport?.reportID ?? ''}`, + key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport?.reportID}`, value: currentNextStep, }, ); From d26800270cacfc8c39198f588d9be1305a0a6695 Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Thu, 19 Dec 2024 14:55:00 +0100 Subject: [PATCH 12/42] chore: optionally push to the optimistic data --- src/libs/actions/IOU.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 7ab880aed8ac..ee3ca8d71564 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -1156,14 +1156,18 @@ function buildOnyxDataForInvoice( key: `${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReport.reportID}`, value: transactionThreadReport, }, - { + ]; + + if (transactionThreadCreatedReportAction?.reportActionID) { + optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReport.reportID}`, value: { - [transactionThreadCreatedReportAction?.reportActionID ?? '-1']: transactionThreadCreatedReportAction, + [transactionThreadCreatedReportAction.reportActionID]: transactionThreadCreatedReportAction, }, - }, - ]; + }); + } + const successData: OnyxUpdate[] = []; if (chatReport) { From 93d2cbd535351580f9e809d6abb9dc0d9455366c Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Thu, 19 Dec 2024 17:09:53 +0100 Subject: [PATCH 13/42] chore: wrap a success data push in an if statement for undefined input --- src/libs/actions/IOU.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index ee3ca8d71564..0e6e8a8c5997 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -1304,17 +1304,20 @@ function buildOnyxDataForInvoice( }, }, }, - { + ); + + if (transactionThreadCreatedReportAction?.reportActionID) { + successData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReport.reportID}`, value: { - [transactionThreadCreatedReportAction?.reportActionID ?? '-1']: { + [transactionThreadCreatedReportAction.reportActionID]: { pendingAction: null, errors: null, }, }, - }, - ); + }); + } if (isNewChatReport) { successData.push( From 2c83daa839285b6c947191da22b3c114c3c3d152 Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Thu, 19 Dec 2024 17:13:49 +0100 Subject: [PATCH 14/42] chore: wrap a failure data push in an if statement for undefined input --- src/libs/actions/IOU.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 0e6e8a8c5997..b06d551a7258 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -1401,16 +1401,19 @@ function buildOnyxDataForInvoice( }, }, }, - { + ]; + + if (transactionThreadCreatedReportAction?.reportActionID) { + failureData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReport.reportID}`, value: { - [transactionThreadCreatedReportAction?.reportActionID ?? '-1']: { + [transactionThreadCreatedReportAction.reportActionID]: { errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('iou.error.genericCreateInvoiceFailureMessage', errorKey), }, }, - }, - ]; + }); + } if (companyName && companyWebsite) { optimisticData.push({ From 135baa70c6595dbeb609d539eb72d34395737ea2 Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Thu, 19 Dec 2024 17:30:22 +0100 Subject: [PATCH 15/42] chore: wrap L1831 failure data push --- src/libs/actions/IOU.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index b06d551a7258..deaea827af85 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -1824,16 +1824,19 @@ function buildOnyxDataForTrackExpense( pendingFields: clearedPendingFields, }, }, - { + ); + + if (transactionThreadCreatedReportAction?.reportActionID) { + failureData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReport?.reportID}`, value: { - [transactionThreadCreatedReportAction?.reportActionID ?? '-1']: { + [transactionThreadCreatedReportAction?.reportActionID]: { errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('iou.error.genericCreateFailureMessage'), }, }, - }, - ); + }); + } // We don't need to compute violations unless we're on a paid policy if (!policy || !PolicyUtils.isPaidGroupPolicy(policy)) { From 0ab1de15c16d90ae53d64d9fb9b552de65874c5b Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Thu, 19 Dec 2024 17:42:55 +0100 Subject: [PATCH 16/42] chore: early return on missing splitData.createdReportActionID for the distance request --- src/libs/actions/IOU.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index deaea827af85..5f429760e3d2 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -5444,12 +5444,16 @@ function createDistanceRequest(distanceRequestInformation: CreateDistanceRequest ); onyxData = splitOnyxData; + if (!splitData.createdReportActionID) { + return; + } + // Splits don't use the IOU report param. The split transaction isn't linked to a report shown in the UI, it's linked to a special default reportID of -2. // Therefore, any params related to the IOU report are irrelevant and omitted below. parameters = { transactionID: splitData.transactionID, chatReportID: splitData.chatReportID, - createdChatReportActionID: splitData.createdReportActionID ?? '', + createdChatReportActionID: splitData.createdReportActionID, reportActionID: splitData.reportActionID, waypoints: JSON.stringify(sanitizedWaypoints), customUnitRateID, From 46e8c8e01ae22398635e0928cc7de41fdcad3a0d Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Thu, 19 Dec 2024 17:44:41 +0100 Subject: [PATCH 17/42] chore: pass in undefined with a missing childReportID --- src/libs/actions/IOU.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 5f429760e3d2..ab6aa9803476 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -7248,7 +7248,7 @@ function getIOUReportActionToApproveOrPay(chatReport: OnyxEntry { - const iouReport = ReportUtils.getReportOrDraftReport(action.childReportID ?? '-1'); + const iouReport = ReportUtils.getReportOrDraftReport(action.childReportID); const policy = PolicyUtils.getPolicy(iouReport?.policyID); const shouldShowSettlementButton = canIOUBePaid(iouReport, chatReport, policy) || canApproveIOU(iouReport, policy); return action.childReportID?.toString() !== excludedIOUReportID && action.actionName === CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW && shouldShowSettlementButton; From 390df26e054d7eb658e8b2e82b63445452670e7e Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Thu, 19 Dec 2024 17:50:29 +0100 Subject: [PATCH 18/42] chore: extract an optimisticData push to ensure a param --- src/libs/actions/IOU.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index ab6aa9803476..97f7f3c39ad3 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -6951,11 +6951,6 @@ function getPayMoneyRequestParams( }, }, }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.NVP_LAST_PAYMENT_METHOD, - value: {[iouReport?.policyID ?? '-1']: paymentMethodType}, - }, { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport?.reportID}`, @@ -6963,6 +6958,16 @@ function getPayMoneyRequestParams( }, ); + if (iouReport?.policyID) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.NVP_LAST_PAYMENT_METHOD, + value: { + [iouReport.policyID]: paymentMethodType, + }, + }); + } + successData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, From cdccb13627d465c44cad9422fab06af16ec9899d Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Thu, 19 Dec 2024 17:56:39 +0100 Subject: [PATCH 19/42] chore: use string or undefined as a value --- src/libs/actions/IOU.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 97f7f3c39ad3..3f3045275e97 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -2657,7 +2657,7 @@ function getTrackExpenseInformation( reportPreviewAction: reportPreviewAction ?? undefined, transactionThreadReportID: optimisticTransactionThread.reportID, createdReportActionIDForThread: optimisticCreatedActionForTransactionThread?.reportActionID ?? '-1', - actionableWhisperReportActionIDParam: actionableTrackExpenseWhisper?.reportActionID ?? '', + actionableWhisperReportActionIDParam: actionableTrackExpenseWhisper?.reportActionID, onyxData: { optimisticData: optimisticData.concat(trackExpenseOnyxData[0]), successData: successData.concat(trackExpenseOnyxData[1]), From de2c69bca3fb198384af6db75e0c0d5b6430336f Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Thu, 19 Dec 2024 18:13:20 +0100 Subject: [PATCH 20/42] chore: call 2 new optimistic updates only on conditions met --- src/libs/actions/IOU.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 3f3045275e97..4d22288d27b6 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -2903,30 +2903,33 @@ function getUpdateMoneyRequestParams( }); if (isScanning && ('amount' in transactionChanges || 'currency' in transactionChanges)) { - optimisticData.push( - { + if (transactionThread?.parentReportActionID) { + optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport?.reportID}`, value: { - [transactionThread?.parentReportActionID ?? '-1']: { + [transactionThread?.parentReportActionID]: { originalMessage: { whisperedTo: [], }, }, }, - }, - { + }); + } + + if (iouReport?.parentReportActionID) { + optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport?.parentReportID}`, value: { - [iouReport?.parentReportActionID ?? '-1']: { + [iouReport.parentReportActionID]: { originalMessage: { whisperedTo: [], }, }, }, - }, - ); + }); + } } // Update recently used categories if the category is changed From 2332bebfb120e38d4c1021c30c0c2df256ffea5a Mon Sep 17 00:00:00 2001 From: VickyStash Date: Fri, 20 Dec 2024 12:25:21 +0100 Subject: [PATCH 21/42] Update getLastVisibleAction and getLastVisibleMessage functions. Remove unnecessary default ids --- src/libs/ReportActionsUtils.ts | 8 +++- src/libs/actions/IOU.ts | 86 ++++++++++++++++++---------------- 2 files changed, 51 insertions(+), 43 deletions(-) diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index dd17adbda338..1c9195e741e7 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -772,7 +772,11 @@ function replaceBaseURLInPolicyChangeLogAction(reportAction: ReportAction): Repo return updatedReportAction; } -function getLastVisibleAction(reportID: string, canUserPerformWriteAction?: boolean, actionsToMerge: Record | null> = {}): OnyxEntry { +function getLastVisibleAction( + reportID: string | undefined, + canUserPerformWriteAction?: boolean, + actionsToMerge: Record | null> = {}, +): OnyxEntry { let reportActions: Array = []; if (!isEmpty(actionsToMerge)) { reportActions = Object.values(fastMerge(allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`] ?? {}, actionsToMerge ?? {}, true)) as Array< @@ -802,7 +806,7 @@ function formatLastMessageText(lastMessageText: string) { } function getLastVisibleMessage( - reportID: string, + reportID: string | undefined, canUserPerformWriteAction?: boolean, actionsToMerge: Record | null> = {}, reportAction: OnyxInputOrEntry | undefined = undefined, diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 6a6a1c3c36f8..e9822936538e 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -449,7 +449,7 @@ Onyx.connect({ /** * Find the report preview action from given chat report and iou report */ -function getReportPreviewAction(chatReportID: string, iouReportID: string): OnyxInputValue> { +function getReportPreviewAction(chatReportID: string | undefined, iouReportID: string | undefined): OnyxInputValue> { const reportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReportID}`] ?? {}; // Find the report preview action from the chat report @@ -1933,8 +1933,8 @@ function getDeleteTrackExpenseInformation( if (chatReport) { canUserPerformWriteAction = !!ReportUtils.canUserPerformWriteAction(chatReport); } - const lastVisibleAction = ReportActionsUtils.getLastVisibleAction(chatReport?.reportID ?? '-1', canUserPerformWriteAction, updatedReportAction); - const {lastMessageText = '', lastMessageHtml = ''} = ReportActionsUtils.getLastVisibleMessage(chatReport?.reportID ?? '-1', canUserPerformWriteAction, updatedReportAction); + const lastVisibleAction = ReportActionsUtils.getLastVisibleAction(chatReportID, canUserPerformWriteAction, updatedReportAction); + const {lastMessageText = '', lastMessageHtml = ''} = ReportActionsUtils.getLastVisibleMessage(chatReportID, canUserPerformWriteAction, updatedReportAction); // STEP 4: Build Onyx data const optimisticData: OnyxUpdate[] = []; @@ -4185,7 +4185,7 @@ function trackExpense( function getOrCreateOptimisticSplitChatReport(existingSplitChatReportID: string, participants: Participant[], participantAccountIDs: number[], currentUserAccountID: number) { // The existing chat report could be passed as reportID or exist on the sole "participant" (in this case a report option) - const existingChatReportID = existingSplitChatReportID || (participants.at(0)?.reportID ?? '-1'); + const existingChatReportID = existingSplitChatReportID || participants.at(0)?.reportID; // Check if the report is available locally if we do have one let existingSplitChatReport = existingChatReportID ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${existingChatReportID}`] : null; @@ -5309,7 +5309,7 @@ function completeSplitBill(chatReportID: string, reportAction: OnyxTypes.ReportA undefined, ); - let oneOnOneReportPreviewAction = getReportPreviewAction(oneOnOneChatReport?.reportID ?? '-1', oneOnOneIOUReport?.reportID ?? '-1'); + let oneOnOneReportPreviewAction = getReportPreviewAction(oneOnOneChatReport?.reportID, oneOnOneIOUReport?.reportID); if (oneOnOneReportPreviewAction) { oneOnOneReportPreviewAction = ReportUtils.updateReportPreview(oneOnOneIOUReport, oneOnOneReportPreviewAction); } else { @@ -5575,7 +5575,7 @@ function createDistanceRequest(distanceRequestInformation: CreateDistanceRequest API.write(WRITE_COMMANDS.CREATE_DISTANCE_REQUEST, parameters, onyxData); InteractionManager.runAfterInteractions(() => TransactionEdit.removeDraftTransaction(CONST.IOU.OPTIMISTIC_TRANSACTION_ID)); - const activeReportID = isMoneyRequestReport ? report?.reportID ?? '-1' : parameters.chatReportID; + const activeReportID = isMoneyRequestReport && report?.reportID ? report.reportID : parameters.chatReportID; Navigation.dismissModal(isSearchTopmostCentralPane() ? undefined : activeReportID); Report.notifyNewAction(activeReportID, userAccountID); } @@ -5634,7 +5634,7 @@ function prepareToCleanUpMoneyRequest(transactionID: string, reportAction: OnyxT const iouReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`] ?? null; const chatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${iouReport?.chatReportID}`]; // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const reportPreviewAction = getReportPreviewAction(iouReport?.chatReportID ?? '-1', iouReport?.reportID ?? '-1')!; + const reportPreviewAction = getReportPreviewAction(iouReport?.chatReportID, iouReport?.reportID)!; const transaction = allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]; const isTransactionOnHold = TransactionUtils.isOnHold(transaction); const transactionViolations = allTransactionViolations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`]; @@ -5675,8 +5675,8 @@ function prepareToCleanUpMoneyRequest(transactionID: string, reportAction: OnyxT if (chatReport) { canUserPerformWriteAction = !!ReportUtils.canUserPerformWriteAction(chatReport); } - const lastVisibleAction = ReportActionsUtils.getLastVisibleAction(iouReport?.reportID ?? '-1', canUserPerformWriteAction, updatedReportAction); - const iouReportLastMessageText = ReportActionsUtils.getLastVisibleMessage(iouReport?.reportID ?? '-1', canUserPerformWriteAction, updatedReportAction).lastMessageText; + const lastVisibleAction = ReportActionsUtils.getLastVisibleAction(iouReport?.reportID, canUserPerformWriteAction, updatedReportAction); + const iouReportLastMessageText = ReportActionsUtils.getLastVisibleMessage(iouReport?.reportID, canUserPerformWriteAction, updatedReportAction).lastMessageText; const shouldDeleteIOUReport = iouReportLastMessageText.length === 0 && !ReportActionsUtils.isDeletedParentAction(lastVisibleAction) && (!transactionThreadID || shouldDeleteTransactionThread); @@ -5935,11 +5935,11 @@ function cleanUpMoneyRequest(transactionID: string, reportAction: OnyxTypes.Repo value: { hasOutstandingChildRequest: false, iouReportID: null, - lastMessageText: ReportActionsUtils.getLastVisibleMessage(iouReport?.chatReportID ?? '-1', canUserPerformWriteAction, { - [reportPreviewAction?.reportActionID ?? '-1']: null, + lastMessageText: ReportActionsUtils.getLastVisibleMessage(iouReport?.chatReportID, canUserPerformWriteAction, { + [reportPreviewAction.reportActionID]: null, })?.lastMessageText, - lastVisibleActionCreated: ReportActionsUtils.getLastVisibleAction(iouReport?.chatReportID ?? '-1', canUserPerformWriteAction, { - [reportPreviewAction?.reportActionID ?? '-1']: null, + lastVisibleActionCreated: ReportActionsUtils.getLastVisibleAction(iouReport?.chatReportID, canUserPerformWriteAction, { + [reportPreviewAction.reportActionID]: null, })?.created, }, }, @@ -6075,10 +6075,10 @@ function deleteMoneyRequest(transactionID: string, reportAction: OnyxTypes.Repor value: { hasOutstandingChildRequest: false, iouReportID: null, - lastMessageText: ReportActionsUtils.getLastVisibleMessage(iouReport?.chatReportID ?? '-1', canUserPerformWriteAction, {[reportPreviewAction?.reportActionID ?? '-1']: null}) + lastMessageText: ReportActionsUtils.getLastVisibleMessage(iouReport?.chatReportID, canUserPerformWriteAction, {[reportPreviewAction?.reportActionID ?? '-1']: null}) ?.lastMessageText, - lastVisibleActionCreated: ReportActionsUtils.getLastVisibleAction(iouReport?.chatReportID ?? '-1', canUserPerformWriteAction, { - [reportPreviewAction?.reportActionID ?? '-1']: null, + lastVisibleActionCreated: ReportActionsUtils.getLastVisibleAction(iouReport?.chatReportID, canUserPerformWriteAction, { + [reportPreviewAction.reportActionID]: null, })?.created, }, }); @@ -6923,7 +6923,7 @@ function getPayMoneyRequestParams( // In some instances, the report preview action might not be available to the payer (only whispered to the requestor) // hence we need to make the updates to the action safely. let optimisticReportPreviewAction = null; - const reportPreviewAction = getReportPreviewAction(chatReport.reportID, iouReport?.reportID ?? ''); + const reportPreviewAction = getReportPreviewAction(chatReport.reportID, iouReport?.reportID); if (reportPreviewAction) { optimisticReportPreviewAction = ReportUtils.updateReportPreview(iouReport, reportPreviewAction, true); } @@ -7280,7 +7280,7 @@ function canSubmitReport(report: OnyxEntry | SearchReport, pol return isOpenExpenseReport && reimbursableSpend !== 0 && (report?.ownerAccountID === currentUserAccountID || isAdmin || report?.managerID === currentUserAccountID); } -function getIOUReportActionToApproveOrPay(chatReport: OnyxEntry, excludedIOUReportID: string): OnyxEntry { +function getIOUReportActionToApproveOrPay(chatReport: OnyxEntry, excludedIOUReportID: string | undefined): OnyxEntry { const chatReportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport?.reportID}`] ?? {}; return Object.values(chatReportActions).find((action) => { @@ -7291,7 +7291,7 @@ function getIOUReportActionToApproveOrPay(chatReport: OnyxEntry, excludedIOUReportID: string): boolean { +function hasIOUToApproveOrPay(chatReport: OnyxEntry, excludedIOUReportID: string | undefined): boolean { return !!getIOUReportActionToApproveOrPay(chatReport, excludedIOUReportID); } @@ -7320,31 +7320,35 @@ function getNextApproverAccountID(report: OnyxEntry) { } function approveMoneyRequest(expenseReport: OnyxEntry, full?: boolean) { - if (expenseReport?.policyID && SubscriptionUtils.shouldRestrictUserBillableActions(expenseReport.policyID)) { + if (!expenseReport) { + return; + } + + if (expenseReport.policyID && SubscriptionUtils.shouldRestrictUserBillableActions(expenseReport.policyID)) { Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(expenseReport.policyID)); return; } - const currentNextStep = allNextSteps[`${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport?.reportID}`] ?? null; - let total = expenseReport?.total ?? 0; - const hasHeldExpenses = ReportUtils.hasHeldExpenses(expenseReport?.reportID); - if (hasHeldExpenses && !full && !!expenseReport?.unheldTotal) { - total = expenseReport?.unheldTotal; + const currentNextStep = allNextSteps[`${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`] ?? null; + let total = expenseReport.total ?? 0; + const hasHeldExpenses = ReportUtils.hasHeldExpenses(expenseReport.reportID); + if (hasHeldExpenses && !full && !!expenseReport.unheldTotal) { + total = expenseReport.unheldTotal; } - const optimisticApprovedReportAction = ReportUtils.buildOptimisticApprovedReportAction(total, expenseReport?.currency ?? '', expenseReport?.reportID ?? '-1'); + const optimisticApprovedReportAction = ReportUtils.buildOptimisticApprovedReportAction(total, expenseReport.currency ?? '', expenseReport.reportID); - const approvalChain = ReportUtils.getApprovalChain(PolicyUtils.getPolicy(expenseReport?.policyID), expenseReport); + const approvalChain = ReportUtils.getApprovalChain(PolicyUtils.getPolicy(expenseReport.policyID), expenseReport); const predictedNextStatus = isLastApprover(approvalChain) ? CONST.REPORT.STATUS_NUM.APPROVED : CONST.REPORT.STATUS_NUM.SUBMITTED; const predictedNextState = isLastApprover(approvalChain) ? CONST.REPORT.STATE_NUM.APPROVED : CONST.REPORT.STATE_NUM.SUBMITTED; - const managerID = isLastApprover(approvalChain) ? expenseReport?.managerID : getNextApproverAccountID(expenseReport); + const managerID = isLastApprover(approvalChain) ? expenseReport.managerID : getNextApproverAccountID(expenseReport); const optimisticNextStep = NextStepUtils.buildNextStep(expenseReport, predictedNextStatus); - const chatReport = ReportUtils.getReportOrDraftReport(expenseReport?.chatReportID); + const chatReport = ReportUtils.getReportOrDraftReport(expenseReport.chatReportID); const optimisticReportActionsData: OnyxUpdate = { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReport?.reportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReport.reportID}`, value: { [optimisticApprovedReportAction.reportActionID]: { ...(optimisticApprovedReportAction as OnyxTypes.ReportAction), @@ -7354,7 +7358,7 @@ function approveMoneyRequest(expenseReport: OnyxEntry, full?: }; const optimisticIOUReportData: OnyxUpdate = { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${expenseReport?.reportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT}${expenseReport.reportID}`, value: { ...expenseReport, lastMessageText: ReportActionsUtils.getReportActionText(optimisticApprovedReportAction), @@ -7370,15 +7374,15 @@ function approveMoneyRequest(expenseReport: OnyxEntry, full?: const optimisticChatReportData: OnyxUpdate = { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${expenseReport?.chatReportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT}${expenseReport.chatReportID}`, value: { - hasOutstandingChildRequest: hasIOUToApproveOrPay(chatReport, expenseReport?.reportID ?? '-1'), + hasOutstandingChildRequest: hasIOUToApproveOrPay(chatReport, expenseReport.reportID), }, }; const optimisticNextStepData: OnyxUpdate = { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport?.reportID}`, + key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`, value: optimisticNextStep, }; const optimisticData: OnyxUpdate[] = [optimisticIOUReportData, optimisticReportActionsData, optimisticNextStepData, optimisticChatReportData]; @@ -7386,7 +7390,7 @@ function approveMoneyRequest(expenseReport: OnyxEntry, full?: const successData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReport?.reportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReport.reportID}`, value: { [optimisticApprovedReportAction.reportActionID]: { pendingAction: null, @@ -7395,7 +7399,7 @@ function approveMoneyRequest(expenseReport: OnyxEntry, full?: }, { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${expenseReport?.reportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT}${expenseReport.reportID}`, value: { pendingFields: { partial: null, @@ -7407,7 +7411,7 @@ function approveMoneyRequest(expenseReport: OnyxEntry, full?: const failureData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReport?.reportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReport.reportID}`, value: { [optimisticApprovedReportAction.reportActionID]: { errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('iou.error.other'), @@ -7416,7 +7420,7 @@ function approveMoneyRequest(expenseReport: OnyxEntry, full?: }, { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${expenseReport?.chatReportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT}${expenseReport.chatReportID}`, value: { hasOutstandingChildRequest: chatReport?.hasOutstandingChildRequest, pendingFields: { @@ -7426,14 +7430,14 @@ function approveMoneyRequest(expenseReport: OnyxEntry, full?: }, { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport?.reportID}`, + key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`, value: currentNextStep, }, ]; // Clear hold reason of all transactions if we approve all requests if (full && hasHeldExpenses) { - const heldTransactions = ReportUtils.getAllHeldTransactions(expenseReport?.reportID); + const heldTransactions = ReportUtils.getAllHeldTransactions(expenseReport.reportID); heldTransactions.forEach((heldTransaction) => { optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, @@ -7471,7 +7475,7 @@ function approveMoneyRequest(expenseReport: OnyxEntry, full?: } const parameters: ApproveMoneyRequestParams = { - reportID: expenseReport?.reportID ?? '-1', + reportID: expenseReport.reportID, approvedReportActionID: optimisticApprovedReportAction.reportActionID, full, optimisticHoldReportID, From 871a25717b30cfc4f7134986657b3e8af00e3111 Mon Sep 17 00:00:00 2001 From: VickyStash Date: Fri, 20 Dec 2024 12:46:21 +0100 Subject: [PATCH 22/42] Remove '-1' id from object keys --- src/libs/actions/IOU.ts | 41 +++++++++++++---------------------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index e9822936538e..5bb01a280dea 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -3191,13 +3191,7 @@ function getUpdateTrackExpenseParams( optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport?.reportID}`, - value: { - [transactionThread?.parentReportActionID ?? '-1']: { - originalMessage: { - whisperedTo: [], - }, - }, - }, + value: transactionThread?.parentReportActionID ? {[transactionThread.parentReportActionID]: {originalMessage: {whisperedTo: []}}} : null, }); } @@ -6044,7 +6038,7 @@ function deleteMoneyRequest(transactionID: string, reportAction: OnyxTypes.Repor onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport?.reportID}`, value: { - [reportPreviewAction?.reportActionID ?? '-1']: updatedReportPreviewAction, + [reportPreviewAction.reportActionID]: updatedReportPreviewAction, }, }, { @@ -6075,8 +6069,7 @@ function deleteMoneyRequest(transactionID: string, reportAction: OnyxTypes.Repor value: { hasOutstandingChildRequest: false, iouReportID: null, - lastMessageText: ReportActionsUtils.getLastVisibleMessage(iouReport?.chatReportID, canUserPerformWriteAction, {[reportPreviewAction?.reportActionID ?? '-1']: null}) - ?.lastMessageText, + lastMessageText: ReportActionsUtils.getLastVisibleMessage(iouReport?.chatReportID, canUserPerformWriteAction, {[reportPreviewAction.reportActionID]: null})?.lastMessageText, lastVisibleActionCreated: ReportActionsUtils.getLastVisibleAction(iouReport?.chatReportID, canUserPerformWriteAction, { [reportPreviewAction.reportActionID]: null, })?.created, @@ -6109,7 +6102,7 @@ function deleteMoneyRequest(transactionID: string, reportAction: OnyxTypes.Repor onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport?.reportID}`, value: { - [reportPreviewAction?.reportActionID ?? '-1']: { + [reportPreviewAction.reportActionID]: { pendingAction: null, errors: null, }, @@ -6188,7 +6181,7 @@ function deleteMoneyRequest(transactionID: string, reportAction: OnyxTypes.Repor onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport?.reportID}`, value: { - [reportPreviewAction?.reportActionID ?? '-1']: { + [reportPreviewAction.reportActionID]: { ...reportPreviewAction, pendingAction: null, errors: { @@ -6382,9 +6375,7 @@ function getSendMoneyParams( const optimisticTransactionThreadReportActionsData: OnyxUpdate = { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticTransactionThread.reportID}`, - value: { - [optimisticCreatedActionForTransactionThread?.reportActionID ?? '-1']: optimisticCreatedActionForTransactionThread, - }, + value: optimisticCreatedActionForTransactionThread ? {[optimisticCreatedActionForTransactionThread?.reportActionID]: optimisticCreatedActionForTransactionThread} : null, }; const successData: OnyxUpdate[] = []; @@ -6481,11 +6472,7 @@ function getSendMoneyParams( { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticTransactionThread.reportID}`, - value: { - [optimisticCreatedActionForTransactionThread?.reportActionID ?? '-1']: { - pendingAction: null, - }, - }, + value: optimisticCreatedActionForTransactionThread ? {[optimisticCreatedActionForTransactionThread?.reportActionID]: {pendingAction: null}} : null, }, ); @@ -6509,11 +6496,9 @@ function getSendMoneyParams( { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticTransactionThread.reportID}`, - value: { - [optimisticCreatedActionForTransactionThread?.reportActionID ?? '-1']: { - errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('iou.error.genericCreateFailureMessage'), - }, - }, + value: optimisticCreatedActionForTransactionThread?.reportActionID + ? {[optimisticCreatedActionForTransactionThread?.reportActionID]: {errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('iou.error.genericCreateFailureMessage')}} + : null, }, { onyxMethod: Onyx.METHOD.SET, @@ -6756,7 +6741,7 @@ function getReportFromHoldRequestsOnyxData( // remove hold report actions from old iou report { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport?.reportID ?? ''}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport?.reportID}`, value: deleteHoldReportActions, }, // add hold report actions to new iou report @@ -6904,7 +6889,7 @@ function getPayMoneyRequestParams( } let total = (iouReport?.total ?? 0) - (iouReport?.nonReimbursableTotal ?? 0); - if (ReportUtils.hasHeldExpenses(iouReport?.reportID ?? '') && !full && !!iouReport?.unheldTotal) { + if (ReportUtils.hasHeldExpenses(iouReport?.reportID) && !full && !!iouReport?.unheldTotal) { total = iouReport.unheldTotal - (iouReport?.unheldNonReimbursableTotal ?? 0); } @@ -6930,7 +6915,7 @@ function getPayMoneyRequestParams( let currentNextStep = null; let optimisticNextStep = null; if (!isInvoiceReport) { - currentNextStep = allNextSteps[`${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport?.reportID ?? ''}`] ?? null; + currentNextStep = allNextSteps[`${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport?.reportID}`] ?? null; optimisticNextStep = NextStepUtils.buildNextStep(iouReport, CONST.REPORT.STATUS_NUM.REIMBURSED); } From 80901b5f301103286c94539e035aa4c7a484488b Mon Sep 17 00:00:00 2001 From: VickyStash Date: Fri, 20 Dec 2024 13:49:36 +0100 Subject: [PATCH 23/42] Fix test --- src/libs/actions/IOU.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 5bb01a280dea..8fbb27d0767d 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -7722,17 +7722,13 @@ function cancelPayment(expenseReport: OnyxEntry, chatReport: O return; } - if (!chatReport.iouReportID) { - return; - } - const optimisticReportAction = ReportUtils.buildOptimisticCancelPaymentReportAction(expenseReport.reportID, -(expenseReport.total ?? 0), expenseReport.currency ?? ''); const policy = PolicyUtils.getPolicy(chatReport.policyID); const approvalMode = policy?.approvalMode ?? CONST.POLICY.APPROVAL_MODE.BASIC; const stateNum: ValueOf = approvalMode === CONST.POLICY.APPROVAL_MODE.OPTIONAL ? CONST.REPORT.STATE_NUM.SUBMITTED : CONST.REPORT.STATE_NUM.APPROVED; const statusNum: ValueOf = approvalMode === CONST.POLICY.APPROVAL_MODE.OPTIONAL ? CONST.REPORT.STATUS_NUM.CLOSED : CONST.REPORT.STATUS_NUM.APPROVED; const optimisticNextStep = NextStepUtils.buildNextStep(expenseReport, statusNum); - const iouReportActions = ReportActionsUtils.getAllReportActions(chatReport.iouReportID); + const iouReportActions = chatReport.iouReportID ? ReportActionsUtils.getAllReportActions(chatReport.iouReportID) : {}; const expenseReportActions = ReportActionsUtils.getAllReportActions(expenseReport.reportID); const iouCreatedAction = Object.values(iouReportActions).find((action) => ReportActionsUtils.isCreatedAction(action)); const expenseCreatedAction = Object.values(expenseReportActions).find((action) => ReportActionsUtils.isCreatedAction(action)); From 0caab12608aa1f49764194b7a6b433d863c60ead Mon Sep 17 00:00:00 2001 From: VickyStash Date: Tue, 24 Dec 2024 17:19:15 +0100 Subject: [PATCH 24/42] Fix params related lint errors --- .../CategorizeTrackedExpenseParams.ts | 12 +- .../API/parameters/CompleteSplitBillParams.ts | 2 +- .../ConvertTrackedExpenseToRequestParams.ts | 4 +- .../parameters/CreateDistanceRequestParams.ts | 2 +- .../parameters/DeleteMoneyRequestParams.ts | 2 +- .../API/parameters/PayMoneyRequestParams.ts | 2 +- src/libs/API/parameters/RequestMoneyParams.ts | 6 +- src/libs/API/parameters/SendInvoiceParams.ts | 4 +- src/libs/API/parameters/SendMoneyParams.ts | 4 +- .../parameters/ShareTrackedExpenseParams.ts | 12 +- src/libs/API/parameters/TrackExpenseParams.ts | 12 +- src/libs/ReportActionsUtils.ts | 2 +- src/libs/ReportUtils.ts | 25 ++- src/libs/actions/IOU.ts | 174 +++++++++--------- src/libs/actions/Report.ts | 2 +- 15 files changed, 137 insertions(+), 128 deletions(-) diff --git a/src/libs/API/parameters/CategorizeTrackedExpenseParams.ts b/src/libs/API/parameters/CategorizeTrackedExpenseParams.ts index 78eb0adecc5e..7186ae131b83 100644 --- a/src/libs/API/parameters/CategorizeTrackedExpenseParams.ts +++ b/src/libs/API/parameters/CategorizeTrackedExpenseParams.ts @@ -6,14 +6,14 @@ type CategorizeTrackedExpenseParams = { comment: string; created: string; merchant: string; - policyID: string; - transactionID: string; - moneyRequestPreviewReportActionID: string; - moneyRequestReportID: string; - moneyRequestCreatedReportActionID: string; + policyID: string | undefined; + transactionID: string | undefined; + moneyRequestPreviewReportActionID: string | undefined; + moneyRequestReportID: string | undefined; + moneyRequestCreatedReportActionID: string | undefined; actionableWhisperReportActionID: string; modifiedExpenseReportActionID: string; - reportPreviewReportActionID: string; + reportPreviewReportActionID: string | undefined; category?: string; tag?: string; receipt?: Receipt; diff --git a/src/libs/API/parameters/CompleteSplitBillParams.ts b/src/libs/API/parameters/CompleteSplitBillParams.ts index 67ca011b70d9..123ee7b05257 100644 --- a/src/libs/API/parameters/CompleteSplitBillParams.ts +++ b/src/libs/API/parameters/CompleteSplitBillParams.ts @@ -1,5 +1,5 @@ type CompleteSplitBillParams = { - transactionID: string; + transactionID: string | undefined; amount?: number; currency?: string; created?: string; diff --git a/src/libs/API/parameters/ConvertTrackedExpenseToRequestParams.ts b/src/libs/API/parameters/ConvertTrackedExpenseToRequestParams.ts index c51161b043a8..dcf6301069a9 100644 --- a/src/libs/API/parameters/ConvertTrackedExpenseToRequestParams.ts +++ b/src/libs/API/parameters/ConvertTrackedExpenseToRequestParams.ts @@ -10,10 +10,10 @@ type ConvertTrackedExpenseToRequestParams = { chatReportID: string; transactionID: string; actionableWhisperReportActionID: string; - createdChatReportActionID: string; + createdChatReportActionID: string | undefined; receipt?: Receipt; moneyRequestReportID: string; - moneyRequestCreatedReportActionID: string; + moneyRequestCreatedReportActionID: string | undefined; moneyRequestPreviewReportActionID: string; reportPreviewReportActionID: string; }; diff --git a/src/libs/API/parameters/CreateDistanceRequestParams.ts b/src/libs/API/parameters/CreateDistanceRequestParams.ts index 07dd594d7356..108707b71dff 100644 --- a/src/libs/API/parameters/CreateDistanceRequestParams.ts +++ b/src/libs/API/parameters/CreateDistanceRequestParams.ts @@ -1,7 +1,7 @@ type CreateDistanceRequestParams = { transactionID: string; chatReportID: string; - createdChatReportActionID: string; + createdChatReportActionID: string | undefined; reportActionID: string; waypoints: string; customUnitRateID: string; diff --git a/src/libs/API/parameters/DeleteMoneyRequestParams.ts b/src/libs/API/parameters/DeleteMoneyRequestParams.ts index 6e7fe30811c4..b44223905e8c 100644 --- a/src/libs/API/parameters/DeleteMoneyRequestParams.ts +++ b/src/libs/API/parameters/DeleteMoneyRequestParams.ts @@ -1,5 +1,5 @@ type DeleteMoneyRequestParams = { - transactionID: string; + transactionID: string | undefined; reportActionID: string; }; diff --git a/src/libs/API/parameters/PayMoneyRequestParams.ts b/src/libs/API/parameters/PayMoneyRequestParams.ts index 3ad98429b75c..337c38d7164e 100644 --- a/src/libs/API/parameters/PayMoneyRequestParams.ts +++ b/src/libs/API/parameters/PayMoneyRequestParams.ts @@ -1,7 +1,7 @@ import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage'; type PayMoneyRequestParams = { - iouReportID: string; + iouReportID: string | undefined; chatReportID: string; reportActionID: string; paymentMethodType: PaymentMethodType; diff --git a/src/libs/API/parameters/RequestMoneyParams.ts b/src/libs/API/parameters/RequestMoneyParams.ts index e3e600a4e367..8ce1aaa97cc0 100644 --- a/src/libs/API/parameters/RequestMoneyParams.ts +++ b/src/libs/API/parameters/RequestMoneyParams.ts @@ -14,8 +14,8 @@ type RequestMoneyParams = { chatReportID: string; transactionID: string; reportActionID: string; - createdChatReportActionID: string; - createdIOUReportActionID: string; + createdChatReportActionID: string | undefined; + createdIOUReportActionID: string | undefined; reportPreviewReportActionID: string; receipt?: Receipt; receiptState?: ValueOf; @@ -26,7 +26,7 @@ type RequestMoneyParams = { billable?: boolean; receiptGpsPoints?: string; transactionThreadReportID: string; - createdReportActionIDForThread: string; + createdReportActionIDForThread: string | undefined; reimbursible?: boolean; }; diff --git a/src/libs/API/parameters/SendInvoiceParams.ts b/src/libs/API/parameters/SendInvoiceParams.ts index e2cac84e0d12..2b172ee6ce6d 100644 --- a/src/libs/API/parameters/SendInvoiceParams.ts +++ b/src/libs/API/parameters/SendInvoiceParams.ts @@ -2,7 +2,7 @@ import type {RequireAtLeastOne} from 'type-fest'; type SendInvoiceParams = RequireAtLeastOne< { - senderWorkspaceID: string; + senderWorkspaceID: string | undefined; accountID: number; receiverEmail?: string; receiverInvoiceRoomID?: string; @@ -21,7 +21,7 @@ type SendInvoiceParams = RequireAtLeastOne< companyName?: string; companyWebsite?: string; createdIOUReportActionID: string; - createdReportActionIDForThread: string; + createdReportActionIDForThread: string | undefined; reportActionID: string; }, 'receiverEmail' | 'receiverInvoiceRoomID' diff --git a/src/libs/API/parameters/SendMoneyParams.ts b/src/libs/API/parameters/SendMoneyParams.ts index 449a87fb5313..1e9a58ab74a5 100644 --- a/src/libs/API/parameters/SendMoneyParams.ts +++ b/src/libs/API/parameters/SendMoneyParams.ts @@ -7,11 +7,11 @@ type SendMoneyParams = { paymentMethodType: PaymentMethodType; transactionID: string; newIOUReportDetails: string; - createdReportActionID: string; + createdReportActionID: string | undefined; reportPreviewReportActionID: string; createdIOUReportActionID: string; transactionThreadReportID: string; - createdReportActionIDForThread: string; + createdReportActionIDForThread: string | undefined; }; export default SendMoneyParams; diff --git a/src/libs/API/parameters/ShareTrackedExpenseParams.ts b/src/libs/API/parameters/ShareTrackedExpenseParams.ts index cee4bc40d9ac..ba69b5410968 100644 --- a/src/libs/API/parameters/ShareTrackedExpenseParams.ts +++ b/src/libs/API/parameters/ShareTrackedExpenseParams.ts @@ -6,14 +6,14 @@ type ShareTrackedExpenseParams = { comment: string; created: string; merchant: string; - policyID: string; - transactionID: string; - moneyRequestPreviewReportActionID: string; - moneyRequestReportID: string; - moneyRequestCreatedReportActionID: string; + policyID: string | undefined; + transactionID: string | undefined; + moneyRequestPreviewReportActionID: string | undefined; + moneyRequestReportID: string | undefined; + moneyRequestCreatedReportActionID: string | undefined; actionableWhisperReportActionID: string; modifiedExpenseReportActionID: string; - reportPreviewReportActionID: string; + reportPreviewReportActionID: string | undefined; category?: string; tag?: string; receipt?: Receipt; diff --git a/src/libs/API/parameters/TrackExpenseParams.ts b/src/libs/API/parameters/TrackExpenseParams.ts index 3a7d0df6736c..f3f2cc539967 100644 --- a/src/libs/API/parameters/TrackExpenseParams.ts +++ b/src/libs/API/parameters/TrackExpenseParams.ts @@ -9,10 +9,10 @@ type TrackExpenseParams = { created: string; merchant: string; iouReportID?: string; - chatReportID: string; - transactionID: string; - reportActionID: string; - createdChatReportActionID: string; + chatReportID: string | undefined; + transactionID: string | undefined; + reportActionID: string | undefined; + createdChatReportActionID: string | undefined; createdIOUReportActionID?: string; reportPreviewReportActionID?: string; receipt?: Receipt; @@ -23,8 +23,8 @@ type TrackExpenseParams = { taxAmount: number; billable?: boolean; receiptGpsPoints?: string; - transactionThreadReportID: string; - createdReportActionIDForThread: string; + transactionThreadReportID: string | undefined; + createdReportActionIDForThread: string | undefined; waypoints?: string; actionableWhisperReportActionID?: string; customUnitRateID?: string; diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index 6b4d95c9f62e..9059703c8d16 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -1126,7 +1126,7 @@ function doesReportHaveVisibleActions(reportID: string, canUserPerformWriteActio return visibleReportActionsWithoutTaskSystemMessage.length > 0; } -function getAllReportActions(reportID: string): ReportActions { +function getAllReportActions(reportID: string | undefined): ReportActions { return allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`] ?? {}; } diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index e9c017db43ce..aea77ac24c02 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -787,7 +787,7 @@ function isDraftReport(reportID: string | undefined): boolean { /** * Returns the report */ -function getReport(reportID: string): OnyxEntry { +function getReport(reportID: string | undefined): OnyxEntry { return allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; } @@ -3030,11 +3030,11 @@ function getReportFieldKey(reportFieldId: string) { /** * Get the report fields attached to the policy given policyID */ -function getReportFieldsByPolicyID(policyID: string): Record { +function getReportFieldsByPolicyID(policyID: string | undefined): Record { const policyReportFields = Object.entries(allPolicies ?? {}).find(([key]) => key.replace(ONYXKEYS.COLLECTION.POLICY, '') === policyID); const fieldList = policyReportFields?.[1]?.fieldList; - if (!policyReportFields || !fieldList) { + if (!policyReportFields || !fieldList || !policyID) { return {}; } @@ -4587,7 +4587,7 @@ function buildOptimisticIOUReport( payeeAccountID: number, payerAccountID: number, total: number, - chatReportID: string, + chatReportID: string | undefined, currency: string, isSendingMoney = false, parentReportActionID?: string, @@ -4659,7 +4659,14 @@ function populateOptimisticReportFormula(formula: string, report: OptimisticExpe } /** Builds an optimistic invoice report with a randomly generated reportID */ -function buildOptimisticInvoiceReport(chatReportID: string, policyID: string, receiverAccountID: number, receiverName: string, total: number, currency: string): OptimisticExpenseReport { +function buildOptimisticInvoiceReport( + chatReportID: string, + policyID: string | undefined, + receiverAccountID: number, + receiverName: string, + total: number, + currency: string, +): OptimisticExpenseReport { const formattedTotal = CurrencyUtils.convertToDisplayString(total, currency); return { @@ -4729,8 +4736,8 @@ function getExpenseReportStateAndStatus(policy: OnyxEntry) { * @param parentReportActionID – The parent ReportActionID of the PolicyExpenseChat */ function buildOptimisticExpenseReport( - chatReportID: string, - policyID: string, + chatReportID: string | undefined, + policyID: string | undefined, payeeAccountID: number, total: number, currency: string, @@ -5323,7 +5330,7 @@ function buildOptimisticModifiedExpenseReportAction( * @param transactionThreadID - The reportID of the transaction thread * @param movedToReportID - The reportID of the report the transaction is moved to */ -function buildOptimisticMovedTrackedExpenseModifiedReportAction(transactionThreadID: string, movedToReportID: string): OptimisticModifiedExpenseReportAction { +function buildOptimisticMovedTrackedExpenseModifiedReportAction(transactionThreadID: string | undefined, movedToReportID: string | undefined): OptimisticModifiedExpenseReportAction { const delegateAccountDetails = PersonalDetailsUtils.getPersonalDetailByEmail(delegateEmail); return { @@ -6751,7 +6758,7 @@ function getChatByParticipants(newParticipantList: number[], reports: OnyxCollec /** * Attempts to find an invoice chat report in onyx with the provided policyID and receiverID. */ -function getInvoiceChatByParticipants(policyID: string, receiverID: string | number, reports: OnyxCollection = allReports): OnyxEntry { +function getInvoiceChatByParticipants(policyID: string | undefined, receiverID: string | number, reports: OnyxCollection = allReports): OnyxEntry { return Object.values(reports ?? {}).find((report) => { if (!report || !isInvoiceRoom(report) || isArchivedRoom(report)) { return false; diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 2834363a4598..39458af04c63 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -89,11 +89,11 @@ type MoneyRequestInformation = { chatReport: OnyxTypes.Report; transaction: OnyxTypes.Transaction; iouAction: OptimisticIOUReportAction; - createdChatReportActionID: string; - createdIOUReportActionID: string; + createdChatReportActionID: string | undefined; + createdIOUReportActionID: string | undefined; reportPreviewAction: OnyxTypes.ReportAction; transactionThreadReportID: string; - createdReportActionIDForThread: string; + createdReportActionIDForThread: string | undefined; onyxData: OnyxData; }; @@ -103,16 +103,16 @@ type TrackExpenseInformation = { chatReport: OnyxTypes.Report; transaction: OnyxTypes.Transaction; iouAction: OptimisticIOUReportAction; - createdChatReportActionID: string; + createdChatReportActionID?: string; createdIOUReportActionID?: string; reportPreviewAction?: OnyxTypes.ReportAction; transactionThreadReportID: string; - createdReportActionIDForThread: string; + createdReportActionIDForThread: string | undefined; actionableWhisperReportActionIDParam?: string; onyxData: OnyxData; }; type CategorizeTrackedExpenseTransactionParams = { - transactionID: string; + transactionID: string | undefined; amount: number; currency: string; comment: string; @@ -126,18 +126,18 @@ type CategorizeTrackedExpenseTransactionParams = { receipt?: Receipt; }; type CategorizeTrackedExpensePolicyParams = { - policyID: string; + policyID: string | undefined; isDraftPolicy: boolean; }; type CategorizeTrackedExpenseReportInformation = { - moneyRequestPreviewReportActionID: string; - moneyRequestReportID: string; - moneyRequestCreatedReportActionID: string; + moneyRequestPreviewReportActionID: string | undefined; + moneyRequestReportID: string | undefined; + moneyRequestCreatedReportActionID: string | undefined; actionableWhisperReportActionID: string; linkedTrackedExpenseReportAction: OnyxTypes.ReportAction; linkedTrackedExpenseReportID: string; - transactionThreadReportID: string; - reportPreviewReportActionID: string; + transactionThreadReportID: string | undefined; + reportPreviewReportActionID: string | undefined; }; type CategorizeTrackedExpenseParams = { onyxData: OnyxData | undefined; @@ -147,7 +147,7 @@ type CategorizeTrackedExpenseParams = { createdWorkspaceParams?: CreateWorkspaceParams; }; type SendInvoiceInformation = { - senderWorkspaceID: string; + senderWorkspaceID: string | undefined; receiver: Partial; invoiceRoom: OnyxTypes.Report; createdChatReportActionID: string; @@ -156,7 +156,7 @@ type SendInvoiceInformation = { transactionID: string; transactionThreadReportID: string; createdIOUReportActionID: string; - createdReportActionIDForThread: string; + createdReportActionIDForThread: string | undefined; reportActionID: string; onyxData: OnyxData; }; @@ -1882,7 +1882,7 @@ function buildOnyxDataForTrackExpense( function getDeleteTrackExpenseInformation( chatReportID: string, - transactionID: string, + transactionID: string | undefined, reportAction: OnyxTypes.ReportAction, shouldDeleteTransactionFromOnyx = true, isMovingTransactionFromTrackExpense = false, @@ -2098,7 +2098,7 @@ function getSendInvoiceInformation( ): SendInvoiceInformation { const {amount = 0, currency = '', created = '', merchant = '', category = '', tag = '', taxCode = '', taxAmount = 0, billable, comment, participants} = transaction ?? {}; const trimmedComment = (comment?.comment ?? '').trim(); - const senderWorkspaceID = participants?.find((participant) => participant?.isSender)?.policyID ?? '-1'; + const senderWorkspaceID = participants?.find((participant) => participant?.isSender)?.policyID; const receiverParticipant = participants?.find((participant) => participant?.accountID) ?? invoiceChatReport?.invoiceReceiver; const receiverAccountID = receiverParticipant && 'accountID' in receiverParticipant && receiverParticipant.accountID ? receiverParticipant.accountID : CONST.DEFAULT_NUMBER_ID; let receiver = ReportUtils.getPersonalDetailsForAccountID(receiverAccountID); @@ -2218,7 +2218,7 @@ function getSendInvoiceInformation( return { createdIOUReportActionID: optimisticCreatedActionForIOUReport.reportActionID, - createdReportActionIDForThread: optimisticCreatedActionForTransactionThread?.reportActionID ?? '-1', + createdReportActionIDForThread: optimisticCreatedActionForTransactionThread?.reportActionID, reportActionID: iouAction.reportActionID, senderWorkspaceID, receiver, @@ -2283,7 +2283,7 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma if (!iouReport || shouldCreateNewMoneyRequestReport) { iouReport = isPolicyExpenseChat - ? ReportUtils.buildOptimisticExpenseReport(chatReport.reportID, chatReport.policyID ?? '-1', payeeAccountID, amount, currency) + ? ReportUtils.buildOptimisticExpenseReport(chatReport.reportID, chatReport.policyID, payeeAccountID, amount, currency) : ReportUtils.buildOptimisticIOUReport(payeeAccountID, payerAccountID, amount, chatReport.reportID, currency); } else if (isPolicyExpenseChat) { iouReport = {...iouReport}; @@ -2439,11 +2439,11 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma chatReport, transaction: optimisticTransaction, iouAction, - createdChatReportActionID: isNewChatReport ? optimisticCreatedActionForChat.reportActionID : '-1', - createdIOUReportActionID: shouldCreateNewMoneyRequestReport ? optimisticCreatedActionForIOUReport.reportActionID : '-1', + createdChatReportActionID: isNewChatReport ? optimisticCreatedActionForChat.reportActionID : undefined, + createdIOUReportActionID: shouldCreateNewMoneyRequestReport ? optimisticCreatedActionForIOUReport.reportActionID : undefined, reportPreviewAction, - transactionThreadReportID: optimisticTransactionThread?.reportID ?? '-1', - createdReportActionIDForThread: optimisticCreatedActionForTransactionThread?.reportActionID ?? '-1', + transactionThreadReportID: optimisticTransactionThread?.reportID, + createdReportActionIDForThread: optimisticCreatedActionForTransactionThread?.reportActionID, onyxData: { optimisticData, successData, @@ -2528,7 +2528,7 @@ function getTrackExpenseInformation( shouldCreateNewMoneyRequestReport = ReportUtils.shouldCreateNewMoneyRequestReport(iouReport, chatReport); if (!iouReport || shouldCreateNewMoneyRequestReport) { - iouReport = ReportUtils.buildOptimisticExpenseReport(chatReport.reportID, chatReport.policyID ?? '-1', payeeAccountID, amount, currency, amount); + iouReport = ReportUtils.buildOptimisticExpenseReport(chatReport.reportID, chatReport.policyID, payeeAccountID, amount, currency, amount); } else { iouReport = {...iouReport}; // Because of the Expense reports are stored as negative values, we subtract the total from the amount @@ -2565,7 +2565,8 @@ function getTrackExpenseInformation( let optimisticTransaction = TransactionUtils.buildOptimisticTransaction( ReportUtils.isExpenseReport(iouReport) ? -amount : amount, currency, - shouldUseMoneyReport && iouReport ? iouReport.reportID : '-1', + // @ts-expect-error TODO: consider logic update + shouldUseMoneyReport && iouReport ? iouReport.reportID : undefined, comment, [], created, @@ -2661,11 +2662,10 @@ function getTrackExpenseInformation( iouReport: iouReport ?? undefined, transaction: optimisticTransaction, iouAction, - createdChatReportActionID: '-1', - createdIOUReportActionID: shouldCreateNewMoneyRequestReport ? optimisticCreatedActionForIOUReport.reportActionID : '-1', + createdIOUReportActionID: shouldCreateNewMoneyRequestReport ? optimisticCreatedActionForIOUReport.reportActionID : undefined, reportPreviewAction: reportPreviewAction ?? undefined, transactionThreadReportID: optimisticTransactionThread.reportID, - createdReportActionIDForThread: optimisticCreatedActionForTransactionThread?.reportActionID ?? '-1', + createdReportActionIDForThread: optimisticCreatedActionForTransactionThread?.reportActionID, actionableWhisperReportActionIDParam: actionableTrackExpenseWhisper?.reportActionID, onyxData: { optimisticData: optimisticData.concat(trackExpenseOnyxData[0]), @@ -3533,12 +3533,12 @@ function updateMoneyRequestDistanceRate( } const getConvertTrackedExpenseInformation = ( - transactionID: string, + transactionID: string | undefined, actionableWhisperReportActionID: string, - moneyRequestReportID: string, + moneyRequestReportID: string | undefined, linkedTrackedExpenseReportAction: OnyxTypes.ReportAction, linkedTrackedExpenseReportID: string, - transactionThreadReportID: string, + transactionThreadReportID: string | undefined, resolution: IOUAction, ) => { const optimisticData: OnyxUpdate[] = []; @@ -3593,9 +3593,9 @@ function convertTrackedExpenseToRequest( chatReportID: string, transactionID: string, actionableWhisperReportActionID: string, - createdChatReportActionID: string, + createdChatReportActionID: string | undefined, moneyRequestReportID: string, - moneyRequestCreatedReportActionID: string, + moneyRequestCreatedReportActionID: string | undefined, moneyRequestPreviewReportActionID: string, linkedTrackedExpenseReportAction: OnyxTypes.ReportAction, linkedTrackedExpenseReportID: string, @@ -3701,16 +3701,16 @@ function categorizeTrackedExpense(trackedExpenseParams: CategorizeTrackedExpense } function shareTrackedExpense( - policyID: string, - transactionID: string, - moneyRequestPreviewReportActionID: string, - moneyRequestReportID: string, - moneyRequestCreatedReportActionID: string, + policyID: string | undefined, + transactionID: string | undefined, + moneyRequestPreviewReportActionID: string | undefined, + moneyRequestReportID: string | undefined, + moneyRequestCreatedReportActionID: string | undefined, actionableWhisperReportActionID: string, linkedTrackedExpenseReportAction: OnyxTypes.ReportAction, linkedTrackedExpenseReportID: string, - transactionThreadReportID: string, - reportPreviewReportActionID: string, + transactionThreadReportID: string | undefined, + reportPreviewReportActionID: string | undefined, onyxData: OnyxData | undefined, amount: number, currency: string, @@ -4058,7 +4058,7 @@ function trackExpense( return; } const transactionParams = { - transactionID: transaction?.transactionID ?? '-1', + transactionID: transaction?.transactionID, amount, currency, comment, @@ -4072,18 +4072,18 @@ function trackExpense( receipt: trackedReceipt, }; const policyParams = { - policyID: chatReport?.policyID ?? '-1', + policyID: chatReport?.policyID, isDraftPolicy, }; const reportInformation = { - moneyRequestPreviewReportActionID: iouAction?.reportActionID ?? '-1', - moneyRequestReportID: iouReport?.reportID ?? '-1', - moneyRequestCreatedReportActionID: createdIOUReportActionID ?? '-1', + moneyRequestPreviewReportActionID: iouAction?.reportActionID, + moneyRequestReportID: iouReport?.reportID, + moneyRequestCreatedReportActionID: createdIOUReportActionID, actionableWhisperReportActionID, linkedTrackedExpenseReportAction, linkedTrackedExpenseReportID, - transactionThreadReportID: transactionThreadReportID ?? '-1', - reportPreviewReportActionID: reportPreviewAction?.reportActionID ?? '-1', + transactionThreadReportID, + reportPreviewReportActionID: reportPreviewAction?.reportActionID, }; const trackedExpenseParams = { onyxData, @@ -4101,16 +4101,16 @@ function trackExpense( return; } shareTrackedExpense( - chatReport?.policyID ?? '-1', - transaction?.transactionID ?? '-1', - iouAction?.reportActionID ?? '-1', - iouReport?.reportID ?? '-1', - createdIOUReportActionID ?? '-1', + chatReport?.policyID, + transaction?.transactionID, + iouAction?.reportActionID, + iouReport?.reportID, + createdIOUReportActionID, actionableWhisperReportActionID, linkedTrackedExpenseReportAction, linkedTrackedExpenseReportID, - transactionThreadReportID ?? '-1', - reportPreviewAction?.reportActionID ?? '-1', + transactionThreadReportID, + reportPreviewAction?.reportActionID, onyxData, amount, currency, @@ -4135,10 +4135,10 @@ function trackExpense( created, merchant, iouReportID: iouReport?.reportID, - chatReportID: chatReport?.reportID ?? '-1', - transactionID: transaction?.transactionID ?? '-1', - reportActionID: iouAction?.reportActionID ?? '-1', - createdChatReportActionID: createdChatReportActionID ?? '-1', + chatReportID: chatReport?.reportID, + transactionID: transaction?.transactionID, + reportActionID: iouAction?.reportActionID, + createdChatReportActionID, createdIOUReportActionID, reportPreviewReportActionID: reportPreviewAction?.reportActionID, receipt: trackedReceipt, @@ -4150,8 +4150,8 @@ function trackExpense( billable, // This needs to be a string of JSON because of limitations with the fetch() API and nested objects receiptGpsPoints: gpsPoints ? JSON.stringify(gpsPoints) : undefined, - transactionThreadReportID: transactionThreadReportID ?? '-1', - createdReportActionIDForThread: createdReportActionIDForThread ?? '-1', + transactionThreadReportID, + createdReportActionIDForThread, waypoints: validWaypoints ? JSON.stringify(sanitizeRecentWaypoints(validWaypoints)) : undefined, customUnitRateID, }; @@ -4164,15 +4164,15 @@ function trackExpense( InteractionManager.runAfterInteractions(() => TransactionEdit.removeDraftTransaction(CONST.IOU.OPTIMISTIC_TRANSACTION_ID)); Navigation.dismissModal(isSearchTopmostCentralPane() ? undefined : activeReportID); - if (action === CONST.IOU.ACTION.SHARE) { - if (isSearchTopmostCentralPane() && activeReportID) { + if (action === CONST.IOU.ACTION.SHARE && activeReportID) { + if (isSearchTopmostCentralPane()) { Navigation.goBack(); Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(activeReportID)); } - Navigation.setNavigationActionToMicrotaskQueue(() => Navigation.navigate(ROUTES.ROOM_INVITE.getRoute(activeReportID ?? '-1', CONST.IOU.SHARE.ROLE.ACCOUNTANT))); + Navigation.setNavigationActionToMicrotaskQueue(() => Navigation.navigate(ROUTES.ROOM_INVITE.getRoute(activeReportID, CONST.IOU.SHARE.ROLE.ACCOUNTANT))); } - Report.notifyNewAction(activeReportID ?? '', payeeAccountID); + Report.notifyNewAction(activeReportID, payeeAccountID); } function getOrCreateOptimisticSplitChatReport(existingSplitChatReportID: string, participants: Participant[], participantAccountIDs: number[], currentUserAccountID: number) { @@ -4484,7 +4484,7 @@ function createSplitsAndOnyxData( if (!oneOnOneIOUReport || shouldCreateNewOneOnOneIOUReport) { oneOnOneIOUReport = isOwnPolicyExpenseChat - ? ReportUtils.buildOptimisticExpenseReport(oneOnOneChatReport.reportID, oneOnOneChatReport.policyID ?? '-1', currentUserAccountID, splitAmount, currency) + ? ReportUtils.buildOptimisticExpenseReport(oneOnOneChatReport.reportID, oneOnOneChatReport.policyID, currentUserAccountID, splitAmount, currency) : ReportUtils.buildOptimisticIOUReport(currentUserAccountID, accountID, splitAmount, oneOnOneChatReport.reportID, currency); } else if (isOwnPolicyExpenseChat) { // Because of the Expense reports are stored as negative values, we subtract the total from the amount @@ -5135,7 +5135,7 @@ function startSplitBill({ API.write(WRITE_COMMANDS.START_SPLIT_BILL, parameters, {optimisticData, successData, failureData}); Navigation.dismissModalWithReport(splitChatReport); - Report.notifyNewAction(splitChatReport.reportID ?? '-1', currentUserAccountID); + Report.notifyNewAction(splitChatReport.reportID, currentUserAccountID); } /** Used for editing a split expense while it's still scanning or when SmartScan fails, it completes a split expense started by startSplitBill above. @@ -5148,7 +5148,7 @@ function startSplitBill({ */ function completeSplitBill(chatReportID: string, reportAction: OnyxTypes.ReportAction, updatedTransaction: OnyxEntry, sessionAccountID: number, sessionEmail: string) { const currentUserEmailForIOUSplit = PhoneNumber.addSMSDomainIfPhoneNumber(sessionEmail); - const transactionID = updatedTransaction?.transactionID ?? '-1'; + const transactionID = updatedTransaction?.transactionID; const unmodifiedTransaction = allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]; // Save optimistic updated transaction and action @@ -5256,9 +5256,9 @@ function completeSplitBill(chatReportID: string, reportAction: OnyxTypes.ReportA const shouldCreateNewOneOnOneIOUReport = ReportUtils.shouldCreateNewMoneyRequestReport(oneOnOneIOUReport, oneOnOneChatReport); if (!oneOnOneIOUReport || shouldCreateNewOneOnOneIOUReport) { - oneOnOneIOUReport = isPolicyExpenseChat - ? ReportUtils.buildOptimisticExpenseReport(oneOnOneChatReport?.reportID ?? '-1', participant.policyID ?? '-1', sessionAccountID, splitAmount, currency ?? '') - : ReportUtils.buildOptimisticIOUReport(sessionAccountID, participant.accountID ?? CONST.DEFAULT_NUMBER_ID, splitAmount, oneOnOneChatReport?.reportID ?? '-1', currency ?? ''); + oneOnOneIOUReport = participant.policyID + ? ReportUtils.buildOptimisticExpenseReport(oneOnOneChatReport?.reportID, participant.policyID, sessionAccountID, splitAmount, currency ?? '') + : ReportUtils.buildOptimisticIOUReport(sessionAccountID, participant.accountID ?? CONST.DEFAULT_NUMBER_ID, splitAmount, oneOnOneChatReport?.reportID, currency ?? ''); } else if (isPolicyExpenseChat) { if (typeof oneOnOneIOUReport?.total === 'number') { // Because of the Expense reports are stored as negative values, we subtract the total from the amount @@ -5271,7 +5271,7 @@ function completeSplitBill(chatReportID: string, reportAction: OnyxTypes.ReportA const oneOnOneTransaction = TransactionUtils.buildOptimisticTransaction( isPolicyExpenseChat ? -splitAmount : splitAmount, currency ?? '', - oneOnOneIOUReport?.reportID ?? '-1', + oneOnOneIOUReport?.reportID, updatedTransaction?.comment?.comment, [], updatedTransaction?.modifiedCreated, @@ -5459,7 +5459,7 @@ function createDistanceRequest(distanceRequestInformation: CreateDistanceRequest category ?? '', tag ?? '', splitShares, - report?.reportID ?? '', + report?.reportID, billable, CONST.IOU.REQUEST_TYPE.DISTANCE, taxCode, @@ -5622,7 +5622,7 @@ function updateMoneyRequestAmountAndCurrency({ */ function prepareToCleanUpMoneyRequest(transactionID: string, reportAction: OnyxTypes.ReportAction) { // STEP 1: Get all collections we're updating - const iouReportID = ReportActionsUtils.isMoneyRequestAction(reportAction) ? ReportActionsUtils.getOriginalMessage(reportAction)?.IOUReportID : '-1'; + const iouReportID = ReportActionsUtils.isMoneyRequestAction(reportAction) ? ReportActionsUtils.getOriginalMessage(reportAction)?.IOUReportID : undefined; const iouReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`] ?? null; const chatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${iouReport?.chatReportID}`]; // eslint-disable-next-line @typescript-eslint/no-non-null-assertion @@ -6574,11 +6574,11 @@ function getSendMoneyParams( paymentMethodType, transactionID: optimisticTransaction.transactionID, newIOUReportDetails, - createdReportActionID: isNewChat ? optimisticCreatedActionForChat.reportActionID : '-1', + createdReportActionID: isNewChat ? optimisticCreatedActionForChat.reportActionID : undefined, reportPreviewReportActionID: reportPreviewAction.reportActionID, createdIOUReportActionID: optimisticCreatedActionForIOUReport.reportActionID, transactionThreadReportID: optimisticTransactionThread.reportID, - createdReportActionIDForThread: optimisticCreatedActionForTransactionThread?.reportActionID ?? '-1', + createdReportActionIDForThread: optimisticCreatedActionForTransactionThread?.reportActionID, }, optimisticData, successData, @@ -6591,14 +6591,14 @@ type OptimisticHoldReportExpenseActionID = { oldReportActionID: string; }; -function getHoldReportActionsAndTransactions(reportID: string) { +function getHoldReportActionsAndTransactions(reportID: string | undefined) { const iouReportActions = ReportActionsUtils.getAllReportActions(reportID); const holdReportActions: Array> = []; const holdTransactions: OnyxTypes.Transaction[] = []; Object.values(iouReportActions).forEach((action) => { - const transactionID = ReportActionsUtils.isMoneyRequestAction(action) ? ReportActionsUtils.getOriginalMessage(action)?.IOUTransactionID ?? null : null; - const transaction = getTransaction(transactionID ?? '-1'); + const transactionID = ReportActionsUtils.isMoneyRequestAction(action) ? ReportActionsUtils.getOriginalMessage(action)?.IOUTransactionID : undefined; + const transaction = getTransaction(transactionID); if (transaction?.comment?.hold) { holdReportActions.push(action as OnyxTypes.ReportAction); @@ -6621,7 +6621,7 @@ function getReportFromHoldRequestsOnyxData( successData: OnyxUpdate[]; failureData: OnyxUpdate[]; } { - const {holdReportActions, holdTransactions} = getHoldReportActionsAndTransactions(iouReport?.reportID ?? ''); + const {holdReportActions, holdTransactions} = getHoldReportActionsAndTransactions(iouReport?.reportID); const firstHoldTransaction = holdTransactions.at(0); const newParentReportActionID = rand64(); @@ -6632,7 +6632,7 @@ function getReportFromHoldRequestsOnyxData( const optimisticExpenseReport = isPolicyExpenseChat ? ReportUtils.buildOptimisticExpenseReport( chatReport.reportID, - chatReport.policyID ?? iouReport?.policyID ?? '', + chatReport.policyID ?? iouReport?.policyID, recipient.accountID ?? 1, holdAmount, iouReport?.currency ?? '', @@ -6807,7 +6807,7 @@ function getReportFromHoldRequestsOnyxData( // add hold report actions back to old iou report { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport?.reportID ?? ''}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport?.reportID}`, value: bringReportActionsBack, }, // remove hold report actions from the new iou report @@ -6880,7 +6880,7 @@ function getPayMoneyRequestParams( } if (ReportUtils.isIndividualInvoiceRoom(chatReport) && payAsBusiness && activePolicyID) { - const existingB2BInvoiceRoom = ReportUtils.getInvoiceChatByParticipants(chatReport.policyID ?? '', activePolicyID); + const existingB2BInvoiceRoom = ReportUtils.getInvoiceChatByParticipants(chatReport.policyID, activePolicyID); if (existingB2BInvoiceRoom) { chatReport = existingB2BInvoiceRoom; } @@ -7103,7 +7103,7 @@ function getPayMoneyRequestParams( return { params: { - iouReportID: iouReport?.reportID ?? '', + iouReportID: iouReport?.reportID, chatReportID: chatReport.reportID, reportActionID: optimisticIOUReportAction.reportActionID, paymentMethodType, @@ -8740,10 +8740,12 @@ function resolveDuplicates(params: TransactionMergeParams) { }); const iouActionList = params.reportID ? getIOUActionForTransactions(params.transactionIDList, params.reportID) : []; - const orderedTransactionIDList = iouActionList.map((action) => { - const message = ReportActionsUtils.getOriginalMessage(action); - return message?.IOUTransactionID ?? ''; - }); + const orderedTransactionIDList = iouActionList + .map((action) => { + const message = ReportActionsUtils.getOriginalMessage(action); + return message?.IOUTransactionID; + }) + .filter((id): id is string => !!id); const optimisticHoldActions: OnyxUpdate[] = []; const failureHoldActions: OnyxUpdate[] = []; diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 5d390221822b..43d8f0255edb 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -485,7 +485,7 @@ function subscribeToNewActionEvent(reportID: string, callback: SubscriberCallbac } /** Notify the ReportActionsView that a new comment has arrived */ -function notifyNewAction(reportID: string, accountID?: number, reportActionID?: string) { +function notifyNewAction(reportID: string | undefined, accountID?: number, reportActionID?: string) { const actionSubscriber = newActionSubscribers.find((subscriber) => subscriber.reportID === reportID); if (!actionSubscriber) { return; From bf734303da2a316866f3401c2164b95f67e7218e Mon Sep 17 00:00:00 2001 From: VickyStash Date: Mon, 30 Dec 2024 15:52:25 +0100 Subject: [PATCH 25/42] Lint fix --- src/libs/actions/IOU.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 1f8983e6217f..688d4a55f2f8 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -7947,7 +7947,7 @@ function payMoneyRequest(paymentType: PaymentMethodType, chatReport: OnyxTypes.R playSound(SOUNDS.SUCCESS); API.write(apiCommand, params, {optimisticData, successData, failureData}); - Report.notifyNewAction(iouReport?.reportID ?? '', userAccountID); + Report.notifyNewAction(iouReport?.reportID, userAccountID); } function payInvoice(paymentMethodType: PaymentMethodType, chatReport: OnyxTypes.Report, invoiceReport: OnyxEntry, payAsBusiness = false) { From a0905d606648a206cfda5d7b818147e707d1e3cf Mon Sep 17 00:00:00 2001 From: VickyStash Date: Mon, 30 Dec 2024 18:35:49 +0100 Subject: [PATCH 26/42] Code polish --- src/libs/API/parameters/PayInvoiceParams.ts | 2 +- src/libs/actions/IOU.ts | 12 ++---------- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/src/libs/API/parameters/PayInvoiceParams.ts b/src/libs/API/parameters/PayInvoiceParams.ts index 7f80e5d20c4c..aaf1d906d143 100644 --- a/src/libs/API/parameters/PayInvoiceParams.ts +++ b/src/libs/API/parameters/PayInvoiceParams.ts @@ -2,7 +2,7 @@ import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage'; import type CreateWorkspaceParams from './CreateWorkspaceParams'; type PayInvoiceParams = Partial & { - reportID: string; + reportID: string | undefined; reportActionID: string; paymentMethodType: PaymentMethodType; payAsBusiness: boolean; diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 688d4a55f2f8..06fb731cedcf 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -2587,7 +2587,7 @@ function getTrackExpenseInformation( let optimisticTransaction = TransactionUtils.buildOptimisticTransaction( ReportUtils.isExpenseReport(iouReport) ? -amount : amount, currency, - // @ts-expect-error TODO: consider logic update + // @ts-expect-error TODO: update buildOptimisticTransaction return type to indicate that reportID can be undefined shouldUseMoneyReport && iouReport ? iouReport.reportID : undefined, comment, [], @@ -5474,10 +5474,6 @@ function createDistanceRequest(distanceRequestInformation: CreateDistanceRequest ); onyxData = splitOnyxData; - if (!splitData.createdReportActionID) { - return; - } - // Splits don't use the IOU report param. The split transaction isn't linked to a report shown in the UI, it's linked to a special default reportID of -2. // Therefore, any params related to the IOU report are irrelevant and omitted below. parameters = { @@ -7973,12 +7969,8 @@ function payInvoice(paymentMethodType: PaymentMethodType, chatReport: OnyxTypes. const paymentSelected = paymentMethodType === CONST.IOU.PAYMENT_TYPE.VBBA ? CONST.IOU.PAYMENT_SELECTED.BBA : CONST.IOU.PAYMENT_SELECTED.PBA; completePaymentOnboarding(paymentSelected); - if (!invoiceReport?.reportID) { - return; - } - let params: PayInvoiceParams = { - reportID: invoiceReport.reportID, + reportID: invoiceReport?.reportID, reportActionID, paymentMethodType, payAsBusiness, From 3980ff618cb97c9824aaf1b8f49a6f193086c7a9 Mon Sep 17 00:00:00 2001 From: VickyStash Date: Thu, 2 Jan 2025 17:24:15 +0100 Subject: [PATCH 27/42] Minor updates after merging main --- src/libs/actions/IOU.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index c651614eafec..ed3389f5189c 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -2719,7 +2719,8 @@ function getTrackExpenseInformation( transactionParams: { amount: ReportUtils.isExpenseReport(iouReport) ? -amount : amount, currency, - reportID: shouldUseMoneyReport && iouReport ? iouReport.reportID : '-1', + // @ts-expect-error TODO: update buildOptimisticTransaction return type to indicate that reportID can be undefined + reportID: shouldUseMoneyReport && iouReport ? iouReport.reportID : undefined, comment, created, merchant, @@ -7874,7 +7875,7 @@ function cancelPayment(expenseReport: OnyxEntry, chatReport: O const stateNum: ValueOf = approvalMode === CONST.POLICY.APPROVAL_MODE.OPTIONAL ? CONST.REPORT.STATE_NUM.SUBMITTED : CONST.REPORT.STATE_NUM.APPROVED; const statusNum: ValueOf = approvalMode === CONST.POLICY.APPROVAL_MODE.OPTIONAL ? CONST.REPORT.STATUS_NUM.CLOSED : CONST.REPORT.STATUS_NUM.APPROVED; const optimisticNextStep = NextStepUtils.buildNextStep(expenseReport, statusNum); - const iouReportActions = chatReport.iouReportID ? ReportActionsUtils.getAllReportActions(chatReport.iouReportID) : {}; + const iouReportActions = ReportActionsUtils.getAllReportActions(chatReport.iouReportID); const expenseReportActions = ReportActionsUtils.getAllReportActions(expenseReport.reportID); const iouCreatedAction = Object.values(iouReportActions).find((action) => ReportActionsUtils.isCreatedAction(action)); const expenseCreatedAction = Object.values(expenseReportActions).find((action) => ReportActionsUtils.isCreatedAction(action)); From 6c6026c010e64f7b8001e247dbd840983af5023f Mon Sep 17 00:00:00 2001 From: VickyStash Date: Thu, 2 Jan 2025 17:40:51 +0100 Subject: [PATCH 28/42] Remove null value merging --- src/libs/actions/IOU.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index ed3389f5189c..f31b3b405f81 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -3338,7 +3338,7 @@ function getUpdateTrackExpenseParams( optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport?.reportID}`, - value: transactionThread?.parentReportActionID ? {[transactionThread.parentReportActionID]: {originalMessage: {whisperedTo: []}}} : null, + value: transactionThread?.parentReportActionID ? {[transactionThread.parentReportActionID]: {originalMessage: {whisperedTo: []}}} : {}, }); } @@ -6522,7 +6522,7 @@ function getSendMoneyParams( const optimisticTransactionThreadReportActionsData: OnyxUpdate = { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticTransactionThread.reportID}`, - value: optimisticCreatedActionForTransactionThread ? {[optimisticCreatedActionForTransactionThread?.reportActionID]: optimisticCreatedActionForTransactionThread} : null, + value: optimisticCreatedActionForTransactionThread ? {[optimisticCreatedActionForTransactionThread?.reportActionID]: optimisticCreatedActionForTransactionThread} : {}, }; const successData: OnyxUpdate[] = []; @@ -6619,7 +6619,7 @@ function getSendMoneyParams( { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticTransactionThread.reportID}`, - value: optimisticCreatedActionForTransactionThread ? {[optimisticCreatedActionForTransactionThread?.reportActionID]: {pendingAction: null}} : null, + value: optimisticCreatedActionForTransactionThread ? {[optimisticCreatedActionForTransactionThread?.reportActionID]: {pendingAction: null}} : {}, }, ); @@ -6645,7 +6645,7 @@ function getSendMoneyParams( key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticTransactionThread.reportID}`, value: optimisticCreatedActionForTransactionThread?.reportActionID ? {[optimisticCreatedActionForTransactionThread?.reportActionID]: {errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('iou.error.genericCreateFailureMessage')}} - : null, + : {}, }, { onyxMethod: Onyx.METHOD.SET, From 435df719cb6cad2c3b3d646c58a752879c144c0a Mon Sep 17 00:00:00 2001 From: VickyStash Date: Thu, 2 Jan 2025 17:54:48 +0100 Subject: [PATCH 29/42] Minor improvement --- src/libs/actions/IOU.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index f31b3b405f81..1a61818f8ace 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -5389,7 +5389,7 @@ function completeSplitBill(chatReportID: string, reportAction: OnyxTypes.ReportA const shouldCreateNewOneOnOneIOUReport = ReportUtils.shouldCreateNewMoneyRequestReport(oneOnOneIOUReport, oneOnOneChatReport); if (!oneOnOneIOUReport || shouldCreateNewOneOnOneIOUReport) { - oneOnOneIOUReport = participant.policyID + oneOnOneIOUReport = isPolicyExpenseChat ? ReportUtils.buildOptimisticExpenseReport(oneOnOneChatReport?.reportID, participant.policyID, sessionAccountID, splitAmount, currency ?? '') : ReportUtils.buildOptimisticIOUReport(sessionAccountID, participant.accountID ?? CONST.DEFAULT_NUMBER_ID, splitAmount, oneOnOneChatReport?.reportID, currency ?? ''); } else if (isPolicyExpenseChat) { From 64a3bb9cc21188ac53030b11732788add2bccd89 Mon Sep 17 00:00:00 2001 From: VickyStash Date: Thu, 2 Jan 2025 18:28:41 +0100 Subject: [PATCH 30/42] Fix reportPreviewAction type --- src/libs/ReportUtils.ts | 6 +++- src/libs/actions/IOU.ts | 73 ++++++++++++++++++++++++----------------- 2 files changed, 47 insertions(+), 32 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index c2c9d48a7a18..a1c02dce5c39 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -3054,10 +3054,14 @@ function getReportFieldKey(reportFieldId: string | undefined) { * Get the report fields attached to the policy given policyID */ function getReportFieldsByPolicyID(policyID: string | undefined): Record { + if (!policyID) { + return {}; + } + const policyReportFields = Object.entries(allPolicies ?? {}).find(([key]) => key.replace(ONYXKEYS.COLLECTION.POLICY, '') === policyID); const fieldList = policyReportFields?.[1]?.fieldList; - if (!policyReportFields || !fieldList || !policyID) { + if (!policyReportFields || !fieldList) { return {}; } diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 1a61818f8ace..19e6ed8e98f0 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -5746,8 +5746,7 @@ function prepareToCleanUpMoneyRequest(transactionID: string, reportAction: OnyxT const iouReportID = ReportActionsUtils.isMoneyRequestAction(reportAction) ? ReportActionsUtils.getOriginalMessage(reportAction)?.IOUReportID : undefined; const iouReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`] ?? null; const chatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${iouReport?.chatReportID}`]; - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const reportPreviewAction = getReportPreviewAction(iouReport?.chatReportID, iouReport?.reportID)!; + const reportPreviewAction = getReportPreviewAction(iouReport?.chatReportID, iouReport?.reportID); const transaction = allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]; const isTransactionOnHold = TransactionUtils.isOnHold(transaction); const transactionViolations = allTransactionViolations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`]; @@ -5796,7 +5795,7 @@ function prepareToCleanUpMoneyRequest(transactionID: string, reportAction: OnyxT // STEP 4: Update the iouReport and reportPreview with new totals and messages if it wasn't deleted let updatedIOUReport: OnyxInputValue; const currency = TransactionUtils.getCurrency(transaction); - const updatedReportPreviewAction: OnyxTypes.ReportAction = {...reportPreviewAction}; + const updatedReportPreviewAction: Partial> = {...reportPreviewAction}; updatedReportPreviewAction.pendingAction = shouldDeleteIOUReport ? CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE : CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE; if (iouReport && ReportUtils.isExpenseReport(iouReport)) { updatedIOUReport = {...iouReport}; @@ -6061,12 +6060,16 @@ function cleanUpMoneyRequest(transactionID: string, reportAction: OnyxTypes.Repo value: { hasOutstandingChildRequest: false, iouReportID: null, - lastMessageText: ReportActionsUtils.getLastVisibleMessage(iouReport?.chatReportID, canUserPerformWriteAction, { - [reportPreviewAction.reportActionID]: null, - })?.lastMessageText, - lastVisibleActionCreated: ReportActionsUtils.getLastVisibleAction(iouReport?.chatReportID, canUserPerformWriteAction, { - [reportPreviewAction.reportActionID]: null, - })?.created, + lastMessageText: ReportActionsUtils.getLastVisibleMessage( + iouReport?.chatReportID, + canUserPerformWriteAction, + reportPreviewAction?.reportActionID ? {[reportPreviewAction.reportActionID]: null} : {}, + )?.lastMessageText, + lastVisibleActionCreated: ReportActionsUtils.getLastVisibleAction( + iouReport?.chatReportID, + canUserPerformWriteAction, + reportPreviewAction?.reportActionID ? {[reportPreviewAction.reportActionID]: null} : {}, + )?.created, }, }, { @@ -6173,9 +6176,7 @@ function deleteMoneyRequest(transactionID: string | undefined, reportAction: Ony { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport?.reportID}`, - value: { - [reportPreviewAction.reportActionID]: updatedReportPreviewAction, - }, + value: reportPreviewAction?.reportActionID ? {[reportPreviewAction.reportActionID]: updatedReportPreviewAction} : {}, }, { onyxMethod: Onyx.METHOD.MERGE, @@ -6205,10 +6206,16 @@ function deleteMoneyRequest(transactionID: string | undefined, reportAction: Ony value: { hasOutstandingChildRequest: false, iouReportID: null, - lastMessageText: ReportActionsUtils.getLastVisibleMessage(iouReport?.chatReportID, canUserPerformWriteAction, {[reportPreviewAction.reportActionID]: null})?.lastMessageText, - lastVisibleActionCreated: ReportActionsUtils.getLastVisibleAction(iouReport?.chatReportID, canUserPerformWriteAction, { - [reportPreviewAction.reportActionID]: null, - })?.created, + lastMessageText: ReportActionsUtils.getLastVisibleMessage( + iouReport?.chatReportID, + canUserPerformWriteAction, + reportPreviewAction?.reportActionID ? {[reportPreviewAction.reportActionID]: null} : {}, + )?.lastMessageText, + lastVisibleActionCreated: ReportActionsUtils.getLastVisibleAction( + iouReport?.chatReportID, + canUserPerformWriteAction, + reportPreviewAction?.reportActionID ? {[reportPreviewAction.reportActionID]: null} : {}, + )?.created, }, }); optimisticData.push({ @@ -6237,12 +6244,14 @@ function deleteMoneyRequest(transactionID: string | undefined, reportAction: Ony { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport?.reportID}`, - value: { - [reportPreviewAction.reportActionID]: { - pendingAction: null, - errors: null, - }, - }, + value: reportPreviewAction?.reportActionID + ? { + [reportPreviewAction.reportActionID]: { + pendingAction: null, + errors: null, + }, + } + : {}, }, ]; @@ -6316,15 +6325,17 @@ function deleteMoneyRequest(transactionID: string | undefined, reportAction: Ony { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport?.reportID}`, - value: { - [reportPreviewAction.reportActionID]: { - ...reportPreviewAction, - pendingAction: null, - errors: { - [errorKey]: Localize.translateLocal('iou.error.genericDeleteFailureMessage'), - }, - }, - }, + value: reportPreviewAction?.reportActionID + ? { + [reportPreviewAction.reportActionID]: { + ...reportPreviewAction, + pendingAction: null, + errors: { + [errorKey]: Localize.translateLocal('iou.error.genericDeleteFailureMessage'), + }, + }, + } + : {}, }, ); From f6e9122ac763f622669be205ef6952e6722ec120 Mon Sep 17 00:00:00 2001 From: VickyStash Date: Thu, 2 Jan 2025 20:33:08 +0100 Subject: [PATCH 31/42] Update Transaction type --- src/libs/ReportUtils.ts | 4 ++-- src/libs/TransactionUtils/index.ts | 2 +- src/libs/TripReservationUtils.ts | 2 +- src/libs/actions/IOU.ts | 1 - src/types/onyx/Transaction.ts | 2 +- 5 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 7ccceb5117e5..c216caf225c2 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -677,7 +677,7 @@ Onyx.connect({ reportsTransactions = Object.values(value).reduce>((all, transaction) => { const reportsMap = all; - if (!transaction) { + if (!transaction || !transaction.reportID) { return reportsMap; } @@ -8412,7 +8412,7 @@ function canReportBeMentionedWithinPolicy(report: OnyxEntry, policyID: s } function shouldShowMerchantColumn(transactions: Transaction[]) { - return transactions.some((transaction) => isExpenseReport(allReports?.[transaction.reportID] ?? null)); + return transactions.some((transaction) => isExpenseReport(transaction.reportID ? allReports?.[transaction.reportID] : undefined)); } /** diff --git a/src/libs/TransactionUtils/index.ts b/src/libs/TransactionUtils/index.ts index 528481dae237..e95a0228ebcc 100644 --- a/src/libs/TransactionUtils/index.ts +++ b/src/libs/TransactionUtils/index.ts @@ -36,7 +36,7 @@ import getDistanceInMeters from './getDistanceInMeters'; type TransactionParams = { amount: number; currency: string; - reportID: string; + reportID: string | undefined; comment?: string; attendees?: Attendee[]; created?: string; diff --git a/src/libs/TripReservationUtils.ts b/src/libs/TripReservationUtils.ts index 2c774637b4a0..4db2cbece77c 100644 --- a/src/libs/TripReservationUtils.ts +++ b/src/libs/TripReservationUtils.ts @@ -65,7 +65,7 @@ function getTripReservationIcon(reservationType?: ReservationType): IconAsset { } } -type ReservationData = {reservation: Reservation; transactionID: string; reportID: string; reservationIndex: number}; +type ReservationData = {reservation: Reservation; transactionID: string; reportID: string | undefined; reservationIndex: number}; function getReservationsFromTripTransactions(transactions: Transaction[]): ReservationData[] { return transactions diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 1927cfe00ede..bc0b93c70665 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -2719,7 +2719,6 @@ function getTrackExpenseInformation( transactionParams: { amount: ReportUtils.isExpenseReport(iouReport) ? -amount : amount, currency, - // @ts-expect-error TODO: update buildOptimisticTransaction return type to indicate that reportID can be undefined reportID: shouldUseMoneyReport && iouReport ? iouReport.reportID : undefined, comment, created, diff --git a/src/types/onyx/Transaction.ts b/src/types/onyx/Transaction.ts index b183cbb06831..a4e6d4a4afab 100644 --- a/src/types/onyx/Transaction.ts +++ b/src/types/onyx/Transaction.ts @@ -452,7 +452,7 @@ type Transaction = OnyxCommon.OnyxValueWithOfflineFeedback< receipt?: Receipt; /** The iouReportID associated with the transaction */ - reportID: string; + reportID: string | undefined; /** Existing routes */ routes?: Routes; From eebf2ed7725ed782859bfb01eb0f078072b7aaf0 Mon Sep 17 00:00:00 2001 From: VickyStash Date: Thu, 2 Jan 2025 20:39:48 +0100 Subject: [PATCH 32/42] Lint fix --- src/ROUTES.ts | 2 +- src/libs/TripReservationUtils.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ROUTES.ts b/src/ROUTES.ts index d7774828a1c7..31f9720c28ba 100644 --- a/src/ROUTES.ts +++ b/src/ROUTES.ts @@ -748,7 +748,7 @@ const ROUTES = { }, WORKSPACE_PROFILE_ADDRESS: { route: 'settings/workspaces/:policyID/profile/address', - getRoute: (policyID: string, backTo?: string) => getUrlWithBackToParam(`settings/workspaces/${policyID}/profile/address` as const, backTo), + getRoute: (policyID: string | undefined, backTo?: string) => getUrlWithBackToParam(`settings/workspaces/${policyID}/profile/address` as const, backTo), }, WORKSPACE_PROFILE_PLAN: { route: 'settings/workspaces/:policyID/profile/plan', diff --git a/src/libs/TripReservationUtils.ts b/src/libs/TripReservationUtils.ts index 4db2cbece77c..8604a5c38dff 100644 --- a/src/libs/TripReservationUtils.ts +++ b/src/libs/TripReservationUtils.ts @@ -102,7 +102,7 @@ function bookATrip(translate: LocaleContextProps['translate'], setCtaErrorMessag } const policy = PolicyUtils.getPolicy(activePolicyID); if (isEmptyObject(policy?.address)) { - Navigation.navigate(ROUTES.WORKSPACE_PROFILE_ADDRESS.getRoute(activePolicyID ?? '-1', Navigation.getActiveRoute())); + Navigation.navigate(ROUTES.WORKSPACE_PROFILE_ADDRESS.getRoute(activePolicyID, Navigation.getActiveRoute())); return; } if (!travelSettings?.hasAcceptedTerms) { From de9d29ad36f05d1d85bb567445ec9fae041b1e2e Mon Sep 17 00:00:00 2001 From: VickyStash Date: Fri, 3 Jan 2025 10:37:15 +0100 Subject: [PATCH 33/42] Apply reviewer feedback --- src/libs/ReportUtils.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index c216caf225c2..5787fab22a78 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -677,7 +677,7 @@ Onyx.connect({ reportsTransactions = Object.values(value).reduce>((all, transaction) => { const reportsMap = all; - if (!transaction || !transaction.reportID) { + if (!transaction?.reportID) { return reportsMap; } @@ -8411,10 +8411,6 @@ function canReportBeMentionedWithinPolicy(report: OnyxEntry, policyID: s return isChatRoom(report) && !isInvoiceRoom(report) && !isThread(report); } -function shouldShowMerchantColumn(transactions: Transaction[]) { - return transactions.some((transaction) => isExpenseReport(transaction.reportID ? allReports?.[transaction.reportID] : undefined)); -} - /** * Whether a given report is used for onboarding tasks. In the past, it could be either the Concierge chat or the system * DM, and we saved the report ID in the user's `onboarding` NVP. As a fallback for users who don't have the NVP, we now @@ -8896,7 +8892,6 @@ export { getTripIDFromTransactionParentReportID, buildOptimisticInvoiceReport, getInvoiceChatByParticipants, - shouldShowMerchantColumn, isCurrentUserInvoiceReceiver, isDraftReport, changeMoneyRequestHoldStatus, From 5c94b538d055a2168c404c37341dd6a93f6b9736 Mon Sep 17 00:00:00 2001 From: VickyStash Date: Fri, 3 Jan 2025 10:52:22 +0100 Subject: [PATCH 34/42] Add earlier return --- src/libs/ReportUtils.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 5787fab22a78..3feab85f1102 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -3069,6 +3069,10 @@ function getReportFieldKey(reportFieldId: string | undefined) { * Get the report fields attached to the policy given policyID */ function getReportFieldsByPolicyID(policyID: string | undefined): Record { + if (!policyID) { + return {}; + } + const policyReportFields = Object.entries(allPolicies ?? {}).find(([key]) => key.replace(ONYXKEYS.COLLECTION.POLICY, '') === policyID); const fieldList = policyReportFields?.[1]?.fieldList; From 3928260b7aa08e377ed36a7d2a652cd6a5b2b30d Mon Sep 17 00:00:00 2001 From: VickyStash Date: Wed, 8 Jan 2025 10:25:33 +0100 Subject: [PATCH 35/42] Add warn logs to invalid routes --- src/ROUTES.ts | 13 +++++++++++-- src/libs/actions/IOU.ts | 4 ++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/ROUTES.ts b/src/ROUTES.ts index 614b01f1c2d8..cce6a2d8e7ed 100644 --- a/src/ROUTES.ts +++ b/src/ROUTES.ts @@ -3,6 +3,7 @@ import type {SearchQueryString} from './components/Search/types'; import type CONST from './CONST'; import type {IOUAction, IOUType} from './CONST'; import type {IOURequestType} from './libs/actions/IOU'; +import Log from './libs/Log'; import type {ExitReason} from './types/form/ExitSurveyReasonForm'; import type {ConnectionName, SageIntacctMappingName} from './types/onyx/Policy'; import type AssertTypesNotEqual from './types/utils/AssertTypesNotEqual'; @@ -422,7 +423,10 @@ const ROUTES = { }, ROOM_INVITE: { route: 'r/:reportID/invite/:role?', - getRoute: (reportID: string, role?: string, backTo?: string) => { + getRoute: (reportID: string | undefined, role?: string, backTo?: string) => { + if (!reportID) { + Log.warn('Invalid reportID is used to build the ROOM_INVITE route'); + } const route = role ? (`r/${reportID}/invite/${role}` as const) : (`r/${reportID}/invite` as const); return getUrlWithBackToParam(route, backTo); }, @@ -747,7 +751,12 @@ const ROUTES = { }, WORKSPACE_PROFILE_ADDRESS: { route: 'settings/workspaces/:policyID/profile/address', - getRoute: (policyID: string | undefined, backTo?: string) => getUrlWithBackToParam(`settings/workspaces/${policyID}/profile/address` as const, backTo), + getRoute: (policyID: string | undefined, backTo?: string) => { + if (!policyID) { + Log.warn('Invalid policyID is used to build the WORKSPACE_PROFILE_ADDRESS route'); + } + return getUrlWithBackToParam(`settings/workspaces/${policyID}/profile/address` as const, backTo); + }, }, WORKSPACE_PROFILE_PLAN: { route: 'settings/workspaces/:policyID/profile/plan', diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 0cafbe5ed44f..e1df4c8b2b87 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -4305,8 +4305,8 @@ function trackExpense( InteractionManager.runAfterInteractions(() => TransactionEdit.removeDraftTransaction(CONST.IOU.OPTIMISTIC_TRANSACTION_ID)); Navigation.dismissModal(isSearchTopmostCentralPane() ? undefined : activeReportID); - if (action === CONST.IOU.ACTION.SHARE && activeReportID) { - if (isSearchTopmostCentralPane()) { + if (action === CONST.IOU.ACTION.SHARE) { + if (isSearchTopmostCentralPane() && activeReportID) { Navigation.goBack(); Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(activeReportID)); } From 8b015b92440199edc32c4d0b99f42501efa8941f Mon Sep 17 00:00:00 2001 From: VickyStash Date: Thu, 16 Jan 2025 10:56:44 +0100 Subject: [PATCH 36/42] TS fix --- src/libs/ReportActionsUtils.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index de89fd2cb783..05026bbfd52d 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -1601,7 +1601,10 @@ function wasActionTakenByCurrentUser(reportAction: OnyxInputOrEntry { +function getIOUActionForReportID(reportID: string | undefined, transactionID: string): OnyxEntry { + if (!reportID) { + return; + } const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; const reportActions = getAllReportActions(report?.reportID); const action = Object.values(reportActions ?? {})?.find((reportAction) => { From d23067ed33e6e2bb8cd2f34cf0211cb6801322e4 Mon Sep 17 00:00:00 2001 From: VickyStash Date: Thu, 16 Jan 2025 11:04:30 +0100 Subject: [PATCH 37/42] Lint fixes --- src/libs/TripReservationUtils.ts | 8 ++++---- src/libs/actions/IOU.ts | 30 +++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/libs/TripReservationUtils.ts b/src/libs/TripReservationUtils.ts index 8604a5c38dff..6d376620deef 100644 --- a/src/libs/TripReservationUtils.ts +++ b/src/libs/TripReservationUtils.ts @@ -13,10 +13,10 @@ import type {Reservation, ReservationType} from '@src/types/onyx/Transaction'; import type Transaction from '@src/types/onyx/Transaction'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import type IconAsset from '@src/types/utils/IconAsset'; -import * as Link from './actions/Link'; +import {openTravelDotLink} from './actions/Link'; import Log from './Log'; import Navigation from './Navigation/Navigation'; -import * as PolicyUtils from './PolicyUtils'; +import {getPolicy} from './PolicyUtils'; let travelSettings: OnyxEntry; Onyx.connect({ @@ -100,7 +100,7 @@ function bookATrip(translate: LocaleContextProps['translate'], setCtaErrorMessag setCtaErrorMessage(translate('travel.phoneError')); return; } - const policy = PolicyUtils.getPolicy(activePolicyID); + const policy = getPolicy(activePolicyID); if (isEmptyObject(policy?.address)) { Navigation.navigate(ROUTES.WORKSPACE_PROFILE_ADDRESS.getRoute(activePolicyID, Navigation.getActiveRoute())); return; @@ -112,7 +112,7 @@ function bookATrip(translate: LocaleContextProps['translate'], setCtaErrorMessag if (ctaErrorMessage) { setCtaErrorMessage(''); } - Link.openTravelDotLink(activePolicyID) + openTravelDotLink(activePolicyID) ?.then(() => { if (!NativeModules.HybridAppModule || !isSingleNewDotEntry) { return; diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index c8253048ac73..bc26a1527c75 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -3076,7 +3076,15 @@ function getUpdateMoneyRequestParams( } } } else { - updatedMoneyRequestReport = updateIOUOwnerAndTotal(iouReport, updatedReportAction.actorAccountID ?? CONST.DEFAULT_NUMBER_ID, diff, getCurrency(transaction), false, true, isTransactionOnHold); + updatedMoneyRequestReport = updateIOUOwnerAndTotal( + iouReport, + updatedReportAction.actorAccountID ?? CONST.DEFAULT_NUMBER_ID, + diff, + getCurrency(transaction), + false, + true, + isTransactionOnHold, + ); } optimisticData.push( @@ -5871,7 +5879,15 @@ function prepareToCleanUpMoneyRequest(transactionID: string, reportAction: OnyxT } } } else { - updatedIOUReport = updateIOUOwnerAndTotal(iouReport, reportAction.actorAccountID ?? CONST.DEFAULT_NUMBER_ID, getAmount(transaction, false), currency, true, false, isTransactionOnHold); + updatedIOUReport = updateIOUOwnerAndTotal( + iouReport, + reportAction.actorAccountID ?? CONST.DEFAULT_NUMBER_ID, + getAmount(transaction, false), + currency, + true, + false, + isTransactionOnHold, + ); } if (updatedIOUReport) { @@ -6842,7 +6858,15 @@ function getReportFromHoldRequestsOnyxData( holdNonReimbursableAmount, newParentReportActionID, ) - : buildOptimisticIOUReport(iouReport?.ownerAccountID ?? CONST.DEFAULT_NUMBER_ID, iouReport?.managerID ?? CONST.DEFAULT_NUMBER_ID, holdAmount, chatReport.reportID, iouReport?.currency ?? '', false, newParentReportActionID); + : buildOptimisticIOUReport( + iouReport?.ownerAccountID ?? CONST.DEFAULT_NUMBER_ID, + iouReport?.managerID ?? CONST.DEFAULT_NUMBER_ID, + holdAmount, + chatReport.reportID, + iouReport?.currency ?? '', + false, + newParentReportActionID, + ); const optimisticExpenseReportPreview = buildOptimisticReportPreview( chatReport, From 8ddbf17b002b8741510db2f6b7b9037272fa89c9 Mon Sep 17 00:00:00 2001 From: VickyStash Date: Thu, 16 Jan 2025 11:22:08 +0100 Subject: [PATCH 38/42] Minor lint fix --- .eslintrc.changed.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/.eslintrc.changed.js b/.eslintrc.changed.js index ebfe1979b128..4aeb568f8e2e 100644 --- a/.eslintrc.changed.js +++ b/.eslintrc.changed.js @@ -21,12 +21,7 @@ module.exports = { }, overrides: [ { - files: [ - 'src/libs/actions/Report.ts', - 'src/pages/workspace/WorkspaceInitialPage.tsx', - 'src/pages/home/report/PureReportActionItem.tsx', - 'src/libs/SidebarUtils.ts', - ], + files: ['src/libs/actions/Report.ts', 'src/pages/workspace/WorkspaceInitialPage.tsx', 'src/pages/home/report/PureReportActionItem.tsx', 'src/libs/SidebarUtils.ts'], rules: { 'rulesdir/no-default-id-values': 'off', }, From 7d8cc986db0dea6154ff58ea613a7641b9b6161d Mon Sep 17 00:00:00 2001 From: VickyStash Date: Mon, 20 Jan 2025 09:40:17 +0100 Subject: [PATCH 39/42] Apply reviewer feedback, fix errors after merging main --- .../ConvertTrackedExpenseToRequestParams.ts | 2 +- .../API/parameters/CreateDistanceRequestParams.ts | 2 +- .../API/parameters/CreatePerDiemRequestParams.ts | 6 +++--- src/libs/API/parameters/RequestMoneyParams.ts | 4 ++-- src/libs/API/parameters/TrackExpenseParams.ts | 2 +- src/libs/actions/IOU.ts | 14 +++++++------- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/libs/API/parameters/ConvertTrackedExpenseToRequestParams.ts b/src/libs/API/parameters/ConvertTrackedExpenseToRequestParams.ts index a6e643e851cb..21a59f7e614f 100644 --- a/src/libs/API/parameters/ConvertTrackedExpenseToRequestParams.ts +++ b/src/libs/API/parameters/ConvertTrackedExpenseToRequestParams.ts @@ -8,7 +8,7 @@ type ConvertTrackedExpenseToRequestParams = { chatReportID: string; transactionID: string; actionableWhisperReportActionID: string; - createdChatReportActionID: string | undefined; + createdChatReportActionID?: string; moneyRequestReportID: string; moneyRequestCreatedReportActionID: string | undefined; moneyRequestPreviewReportActionID: string; diff --git a/src/libs/API/parameters/CreateDistanceRequestParams.ts b/src/libs/API/parameters/CreateDistanceRequestParams.ts index 108707b71dff..81385d092530 100644 --- a/src/libs/API/parameters/CreateDistanceRequestParams.ts +++ b/src/libs/API/parameters/CreateDistanceRequestParams.ts @@ -1,7 +1,7 @@ type CreateDistanceRequestParams = { transactionID: string; chatReportID: string; - createdChatReportActionID: string | undefined; + createdChatReportActionID?: string; reportActionID: string; waypoints: string; customUnitRateID: string; diff --git a/src/libs/API/parameters/CreatePerDiemRequestParams.ts b/src/libs/API/parameters/CreatePerDiemRequestParams.ts index 2a64748807d5..61740fbd0a37 100644 --- a/src/libs/API/parameters/CreatePerDiemRequestParams.ts +++ b/src/libs/API/parameters/CreatePerDiemRequestParams.ts @@ -14,11 +14,11 @@ type CreatePerDiemRequestParams = { chatReportID: string; transactionID: string; reportActionID: string; - createdChatReportActionID: string; - createdIOUReportActionID: string; + createdChatReportActionID?: string; + createdIOUReportActionID?: string; reportPreviewReportActionID: string; transactionThreadReportID: string; - createdReportActionIDForThread: string; + createdReportActionIDForThread: string | undefined; }; export default CreatePerDiemRequestParams; diff --git a/src/libs/API/parameters/RequestMoneyParams.ts b/src/libs/API/parameters/RequestMoneyParams.ts index 8ce1aaa97cc0..4fe0a0a29e9f 100644 --- a/src/libs/API/parameters/RequestMoneyParams.ts +++ b/src/libs/API/parameters/RequestMoneyParams.ts @@ -14,8 +14,8 @@ type RequestMoneyParams = { chatReportID: string; transactionID: string; reportActionID: string; - createdChatReportActionID: string | undefined; - createdIOUReportActionID: string | undefined; + createdChatReportActionID?: string; + createdIOUReportActionID?: string; reportPreviewReportActionID: string; receipt?: Receipt; receiptState?: ValueOf; diff --git a/src/libs/API/parameters/TrackExpenseParams.ts b/src/libs/API/parameters/TrackExpenseParams.ts index f3f2cc539967..89b8d79e3b9a 100644 --- a/src/libs/API/parameters/TrackExpenseParams.ts +++ b/src/libs/API/parameters/TrackExpenseParams.ts @@ -12,7 +12,7 @@ type TrackExpenseParams = { chatReportID: string | undefined; transactionID: string | undefined; reportActionID: string | undefined; - createdChatReportActionID: string | undefined; + createdChatReportActionID?: string; createdIOUReportActionID?: string; reportPreviewReportActionID?: string; receipt?: Receipt; diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 718bae536986..03a4977bc3fe 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -186,8 +186,8 @@ type MoneyRequestInformation = { chatReport: OnyxTypes.Report; transaction: OnyxTypes.Transaction; iouAction: OptimisticIOUReportAction; - createdChatReportActionID: string | undefined; - createdIOUReportActionID: string | undefined; + createdChatReportActionID?: string; + createdIOUReportActionID?: string; reportPreviewAction: OnyxTypes.ReportAction; transactionThreadReportID: string; createdReportActionIDForThread: string | undefined; @@ -2791,7 +2791,7 @@ function getPerDiemExpenseInformation(perDiemExpenseInformation: PerDiemExpenseI if (!iouReport || shouldCreateNewMoneyRequestReport) { iouReport = isPolicyExpenseChat - ? buildOptimisticExpenseReport(chatReport.reportID, chatReport.policyID ?? '-1', payeeAccountID, amount, currency) + ? buildOptimisticExpenseReport(chatReport.reportID, chatReport.policyID, payeeAccountID, amount, currency) : buildOptimisticIOUReport(payeeAccountID, payerAccountID, amount, chatReport.reportID, currency); } else if (isPolicyExpenseChat) { iouReport = {...iouReport}; @@ -2931,11 +2931,11 @@ function getPerDiemExpenseInformation(perDiemExpenseInformation: PerDiemExpenseI chatReport, transaction: optimisticTransaction, iouAction, - createdChatReportActionID: isNewChatReport ? optimisticCreatedActionForChat.reportActionID : '-1', - createdIOUReportActionID: shouldCreateNewMoneyRequestReport ? optimisticCreatedActionForIOUReport.reportActionID : '-1', + createdChatReportActionID: isNewChatReport ? optimisticCreatedActionForChat.reportActionID : undefined, + createdIOUReportActionID: shouldCreateNewMoneyRequestReport ? optimisticCreatedActionForIOUReport.reportActionID : undefined, reportPreviewAction, - transactionThreadReportID: optimisticTransactionThread?.reportID ?? '-1', - createdReportActionIDForThread: optimisticCreatedActionForTransactionThread?.reportActionID ?? '-1', + transactionThreadReportID: optimisticTransactionThread?.reportID, + createdReportActionIDForThread: optimisticCreatedActionForTransactionThread?.reportActionID, onyxData: { optimisticData, successData, From 1dbedd9bc0c8970a4887fd923f113a66b6509124 Mon Sep 17 00:00:00 2001 From: VickyStash Date: Mon, 20 Jan 2025 09:49:54 +0100 Subject: [PATCH 40/42] Minor code improvements --- src/libs/actions/IOU.ts | 48 ++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 03a4977bc3fe..cd5574900979 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -6464,6 +6464,18 @@ function cleanUpMoneyRequest(transactionID: string, reportAction: OnyxTypes.Repo if (chatReport) { canUserPerformWriteAction = !!canUserPerformWriteActionReportUtils(chatReport); } + + const lastMessageText = getLastVisibleMessage( + iouReport?.chatReportID, + canUserPerformWriteAction, + reportPreviewAction?.reportActionID ? {[reportPreviewAction.reportActionID]: null} : {}, + )?.lastMessageText; + const lastVisibleActionCreated = getLastVisibleAction( + iouReport?.chatReportID, + canUserPerformWriteAction, + reportPreviewAction?.reportActionID ? {[reportPreviewAction.reportActionID]: null} : {}, + )?.created; + onyxUpdates.push( { onyxMethod: Onyx.METHOD.MERGE, @@ -6471,16 +6483,8 @@ function cleanUpMoneyRequest(transactionID: string, reportAction: OnyxTypes.Repo value: { hasOutstandingChildRequest: false, iouReportID: null, - lastMessageText: getLastVisibleMessage( - iouReport?.chatReportID, - canUserPerformWriteAction, - reportPreviewAction?.reportActionID ? {[reportPreviewAction.reportActionID]: null} : {}, - )?.lastMessageText, - lastVisibleActionCreated: getLastVisibleAction( - iouReport?.chatReportID, - canUserPerformWriteAction, - reportPreviewAction?.reportActionID ? {[reportPreviewAction.reportActionID]: null} : {}, - )?.created, + lastMessageText, + lastVisibleActionCreated, }, }, { @@ -6611,22 +6615,26 @@ function deleteMoneyRequest(transactionID: string | undefined, reportAction: Ony if (chatReport) { canUserPerformWriteAction = !!canUserPerformWriteActionReportUtils(chatReport); } + + const lastMessageText = getLastVisibleMessage( + iouReport?.chatReportID, + canUserPerformWriteAction, + reportPreviewAction?.reportActionID ? {[reportPreviewAction.reportActionID]: null} : {}, + )?.lastMessageText; + const lastVisibleActionCreated = getLastVisibleAction( + iouReport?.chatReportID, + canUserPerformWriteAction, + reportPreviewAction?.reportActionID ? {[reportPreviewAction.reportActionID]: null} : {}, + )?.created; + optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport?.reportID}`, value: { hasOutstandingChildRequest: false, iouReportID: null, - lastMessageText: getLastVisibleMessage( - iouReport?.chatReportID, - canUserPerformWriteAction, - reportPreviewAction?.reportActionID ? {[reportPreviewAction.reportActionID]: null} : {}, - )?.lastMessageText, - lastVisibleActionCreated: getLastVisibleAction( - iouReport?.chatReportID, - canUserPerformWriteAction, - reportPreviewAction?.reportActionID ? {[reportPreviewAction.reportActionID]: null} : {}, - )?.created, + lastMessageText, + lastVisibleActionCreated, }, }); optimisticData.push({ From a91d701f1039e7d45384ce3f4ea29a2dedee8f5d Mon Sep 17 00:00:00 2001 From: VickyStash Date: Mon, 20 Jan 2025 12:24:15 +0100 Subject: [PATCH 41/42] Don't merge empty object to onyx --- src/libs/actions/IOU.ts | 110 ++++++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 50 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index cd5574900979..b376ff376593 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -3674,11 +3674,11 @@ function getUpdateTrackExpenseParams( }, }); - if (isScanning && ('amount' in transactionChanges || 'currency' in transactionChanges)) { + if (isScanning && transactionThread?.parentReportActionID && ('amount' in transactionChanges || 'currency' in transactionChanges)) { optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport?.reportID}`, - value: transactionThread?.parentReportActionID ? {[transactionThread.parentReportActionID]: {originalMessage: {whisperedTo: []}}} : {}, + value: {[transactionThread.parentReportActionID]: {originalMessage: {whisperedTo: []}}}, }); } @@ -6588,11 +6588,6 @@ function deleteMoneyRequest(transactionID: string | undefined, reportAction: Ony key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, value: updatedIOUReport, }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport?.reportID}`, - value: reportPreviewAction?.reportActionID ? {[reportPreviewAction.reportActionID]: updatedReportPreviewAction} : {}, - }, { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport?.reportID}`, @@ -6600,6 +6595,14 @@ function deleteMoneyRequest(transactionID: string | undefined, reportAction: Ony }, ); + if (reportPreviewAction?.reportActionID) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport?.reportID}`, + value: {[reportPreviewAction.reportActionID]: updatedReportPreviewAction}, + }); + } + if (!shouldDeleteIOUReport && updatedReportPreviewAction?.childMoneyRequestCount === 0) { optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, @@ -6660,19 +6663,20 @@ function deleteMoneyRequest(transactionID: string | undefined, reportAction: Ony }, }, }, - { + ]; + + if (reportPreviewAction?.reportActionID) { + successData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport?.reportID}`, - value: reportPreviewAction?.reportActionID - ? { - [reportPreviewAction.reportActionID]: { - pendingAction: null, - errors: null, - }, - } - : {}, - }, - ]; + value: { + [reportPreviewAction.reportActionID]: { + pendingAction: null, + errors: null, + }, + }, + }); + } // Ensure that any remaining data is removed upon successful completion, even if the server sends a report removal response. // This is done to prevent the removal update from lingering in the applyHTTPSOnyxUpdates function. @@ -6741,22 +6745,23 @@ function deleteMoneyRequest(transactionID: string | undefined, reportAction: Ony key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, value: iouReport, }, - { + ); + + if (reportPreviewAction?.reportActionID) { + failureData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport?.reportID}`, - value: reportPreviewAction?.reportActionID - ? { - [reportPreviewAction.reportActionID]: { - ...reportPreviewAction, - pendingAction: null, - errors: { - [errorKey]: Localize.translateLocal('iou.error.genericDeleteFailureMessage'), - }, - }, - } - : {}, - }, - ); + value: { + [reportPreviewAction.reportActionID]: { + ...reportPreviewAction, + pendingAction: null, + errors: { + [errorKey]: Localize.translateLocal('iou.error.genericDeleteFailureMessage'), + }, + }, + }, + }); + } if (chatReport && shouldDeleteIOUReport) { failureData.push({ @@ -6949,11 +6954,13 @@ function getSendMoneyParams( [reportPreviewAction.reportActionID]: reportPreviewAction, }, }; - const optimisticTransactionThreadReportActionsData: OnyxUpdate = { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticTransactionThread.reportID}`, - value: optimisticCreatedActionForTransactionThread ? {[optimisticCreatedActionForTransactionThread?.reportActionID]: optimisticCreatedActionForTransactionThread} : {}, - }; + const optimisticTransactionThreadReportActionsData: OnyxUpdate | undefined = optimisticCreatedActionForTransactionThread + ? { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticTransactionThread.reportID}`, + value: {[optimisticCreatedActionForTransactionThread?.reportActionID]: optimisticCreatedActionForTransactionThread}, + } + : undefined; const successData: OnyxUpdate[] = []; @@ -7046,11 +7053,6 @@ function getSendMoneyParams( }, }, }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticTransactionThread.reportID}`, - value: optimisticCreatedActionForTransactionThread ? {[optimisticCreatedActionForTransactionThread?.reportActionID]: {pendingAction: null}} : {}, - }, ); const failureData: OnyxUpdate[] = [ @@ -7070,13 +7072,6 @@ function getSendMoneyParams( }, }, }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticTransactionThread.reportID}`, - value: optimisticCreatedActionForTransactionThread?.reportActionID - ? {[optimisticCreatedActionForTransactionThread?.reportActionID]: {errors: getMicroSecondOnyxErrorWithTranslationKey('iou.error.genericCreateFailureMessage')}} - : {}, - }, { onyxMethod: Onyx.METHOD.SET, key: ONYXKEYS.NVP_QUICK_ACTION_GLOBAL_CREATE, @@ -7084,6 +7079,19 @@ function getSendMoneyParams( }, ]; + if (optimisticCreatedActionForTransactionThread?.reportActionID) { + successData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticTransactionThread.reportID}`, + value: {[optimisticCreatedActionForTransactionThread?.reportActionID]: {pendingAction: null}}, + }); + failureData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticTransactionThread.reportID}`, + value: {[optimisticCreatedActionForTransactionThread?.reportActionID]: {errors: getMicroSecondOnyxErrorWithTranslationKey('iou.error.genericCreateFailureMessage')}}, + }); + } + // Now, let's add the data we need just when we are creating a new chat report if (isNewChat) { successData.push({ @@ -7138,9 +7146,11 @@ function getSendMoneyParams( optimisticIOUReportActionsData, optimisticTransactionData, optimisticTransactionThreadData, - optimisticTransactionThreadReportActionsData, ]; + if (optimisticTransactionThreadReportActionsData) { + optimisticData.push(optimisticTransactionThreadReportActionsData); + } if (!isEmptyObject(optimisticPersonalDetailListData)) { optimisticData.push(optimisticPersonalDetailListData); } From 4d75438e9b6fcdbdbabdb06aa84223a52dfe892a Mon Sep 17 00:00:00 2001 From: VickyStash Date: Tue, 21 Jan 2025 20:24:32 +0100 Subject: [PATCH 42/42] Fix lint errors --- src/libs/actions/IOU.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 0a15be81fc6c..b2ec4478284c 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -4635,7 +4635,7 @@ function trackExpense( return; } const transactionParams = { - transactionID: transaction?.transactionID ?? '-1', + transactionID: transaction?.transactionID, amount, currency, comment, @@ -4649,17 +4649,17 @@ function trackExpense( receipt: trackedReceipt instanceof Blob ? trackedReceipt : undefined, }; const policyParams = { - policyID: chatReport?.policyID ?? '-1', + policyID: chatReport?.policyID, }; const reportInformation = { - moneyRequestPreviewReportActionID: iouAction?.reportActionID ?? '-1', - moneyRequestReportID: iouReport?.reportID ?? '-1', - moneyRequestCreatedReportActionID: createdIOUReportActionID ?? '-1', + moneyRequestPreviewReportActionID: iouAction?.reportActionID, + moneyRequestReportID: iouReport?.reportID, + moneyRequestCreatedReportActionID: createdIOUReportActionID, actionableWhisperReportActionID, linkedTrackedExpenseReportAction, linkedTrackedExpenseReportID, - transactionThreadReportID: transactionThreadReportID ?? '-1', - reportPreviewReportActionID: reportPreviewAction?.reportActionID ?? '-1', + transactionThreadReportID, + reportPreviewReportActionID: reportPreviewAction?.reportActionID, }; const trackedExpenseParams = { onyxData,