From 2cd62cef121210147090aa49d4c63863ed26531b Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Wed, 18 Dec 2024 12:58:35 +0100 Subject: [PATCH 01/24] 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/24] 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/24] 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/24] 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/24] 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/24] 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/24] 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/24] 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/24] 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/24] 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/24] 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/24] 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/24] 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/24] 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/24] 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/24] 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/24] 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/24] 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/24] 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/24] 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/24] 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/24] 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/24] 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/24] 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;