From 10a75ac289e2926ee01da1fdbbb2f3dbe1cc376d Mon Sep 17 00:00:00 2001 From: FitseTLT Date: Fri, 29 Nov 2024 21:06:34 +0300 Subject: [PATCH 1/6] added failure handling of update distance request --- .../ReportActionItem/MoneyRequestView.tsx | 2 +- .../ReportActionItemImage.tsx | 2 +- src/libs/actions/IOU.ts | 31 +++++++++++++++++++ src/libs/actions/Transaction.ts | 2 +- .../request/step/IOURequestStepDistance.tsx | 1 + src/types/onyx/Transaction.ts | 2 +- 6 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/components/ReportActionItem/MoneyRequestView.tsx b/src/components/ReportActionItem/MoneyRequestView.tsx index ca50e93e536f..1c3a9336714f 100644 --- a/src/components/ReportActionItem/MoneyRequestView.tsx +++ b/src/components/ReportActionItem/MoneyRequestView.tsx @@ -390,7 +390,7 @@ function MoneyRequestView({report, shouldShowAnimatedBackground, readonly = fals const shouldShowReceiptAudit = isReceiptAllowed && (shouldShowReceiptEmptyState || hasReceipt); const errors = { - ...(transaction?.errorFields?.route ?? transaction?.errors), + ...(transaction?.errorFields?.route ?? transaction?.errorFields?.waypoints ?? transaction?.errors), ...parentReportAction?.errors, }; diff --git a/src/components/ReportActionItem/ReportActionItemImage.tsx b/src/components/ReportActionItem/ReportActionItemImage.tsx index 668338440f73..ca54d759fb69 100644 --- a/src/components/ReportActionItem/ReportActionItemImage.tsx +++ b/src/components/ReportActionItem/ReportActionItemImage.tsx @@ -84,7 +84,7 @@ function ReportActionItemImage({ const {translate} = useLocalize(); const isDistanceRequest = !!transaction && TransactionUtils.isDistanceRequest(transaction); const hasPendingWaypoints = transaction && TransactionUtils.isFetchingWaypointsFromServer(transaction); - const hasErrors = !isEmptyObject(transaction?.errors) || !isEmptyObject(transaction?.errorFields); + const hasErrors = !isEmptyObject(transaction?.errors) || !isEmptyObject(transaction?.errorFields?.route) || !isEmptyObject(transaction?.errorFields?.waypoints); const showMapAsImage = isDistanceRequest && (hasErrors || hasPendingWaypoints); if (showMapAsImage) { diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 90719ffeed55..662ad4fc0592 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -3161,6 +3161,7 @@ type UpdateMoneyRequestDistanceParams = { policy?: OnyxEntry; policyTagList?: OnyxEntry; policyCategories?: OnyxEntry; + transactionBackup: OnyxEntry; }; /** Updates the waypoints of a distance expense */ @@ -3172,6 +3173,7 @@ function updateMoneyRequestDistance({ policy = {} as OnyxTypes.Policy, policyTagList = {}, policyCategories = {}, + transactionBackup, }: UpdateMoneyRequestDistanceParams) { const transactionChanges: TransactionChanges = { waypoints: sanitizeRecentWaypoints(waypoints), @@ -3195,6 +3197,35 @@ function updateMoneyRequestDistance({ value: recentServerValidatedWaypoints, }); + if (transactionBackup) { + const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]; + // We need to have all the keys of the original waypoint in the failure data for onyx merge to properly reset + // waypoints keys that do not exist in the waypoint of the reverting failure data. + const allWaypointKeys = [...new Set([...Object.keys(transactionBackup.comment?.waypoints ?? {}), ...Object.keys(transaction?.comment?.waypoints ?? {})])]; + const onyxWaypoints = allWaypointKeys.reduce((acc: NullishDeep, key) => { + acc[key] = transactionBackup.comment?.waypoints?.[key] ? {...transactionBackup.comment?.waypoints?.[key]} : null; + return acc; + }, {}); + const allModifiedWaypointsKeys = [...new Set([...Object.keys(waypoints ?? {}), ...Object.keys(transaction?.modifiedWaypoints ?? {})])]; + const onyxModifiedWaypoints = allModifiedWaypointsKeys.reduce((acc: NullishDeep, key) => { + acc[key] = transactionBackup.modifiedWaypoints?.[key] ? {...transactionBackup.modifiedWaypoints?.[key]} : null; + return acc; + }, {}); + onyxData?.failureData?.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, + value: { + comment: { + waypoints: onyxWaypoints, + customUnit: { + quantity: transactionBackup?.comment?.customUnit?.quantity, + }, + }, + modifiedWaypoints: onyxModifiedWaypoints, + }, + }); + } + API.write(WRITE_COMMANDS.UPDATE_MONEY_REQUEST_DISTANCE, params, onyxData); } diff --git a/src/libs/actions/Transaction.ts b/src/libs/actions/Transaction.ts index 3cb6e3dc44ba..c8a007458242 100644 --- a/src/libs/actions/Transaction.ts +++ b/src/libs/actions/Transaction.ts @@ -458,7 +458,7 @@ function abandonReviewDuplicateTransactions() { } function clearError(transactionID: string) { - Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, {errors: null, errorFields: {route: null}}); + Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, {errors: null, errorFields: {route: null, waypoints: null, routes: null}}); } function markAsCash(transactionID: string, transactionThreadReportID: string) { diff --git a/src/pages/iou/request/step/IOURequestStepDistance.tsx b/src/pages/iou/request/step/IOURequestStepDistance.tsx index efe5d293036b..dd02df8f4177 100644 --- a/src/pages/iou/request/step/IOURequestStepDistance.tsx +++ b/src/pages/iou/request/step/IOURequestStepDistance.tsx @@ -465,6 +465,7 @@ function IOURequestStepDistance({ waypoints, ...(hasRouteChanged ? {routes: transaction?.routes} : {}), policy, + transactionBackup, }); navigateBack(); return; diff --git a/src/types/onyx/Transaction.ts b/src/types/onyx/Transaction.ts index 547e41463c70..2aa31904a35a 100644 --- a/src/types/onyx/Transaction.ts +++ b/src/types/onyx/Transaction.ts @@ -338,7 +338,7 @@ type Transaction = OnyxCommon.OnyxValueWithOfflineFeedback< errors?: OnyxCommon.Errors | ReceiptErrors; /** Server side errors keyed by microtime */ - errorFields?: OnyxCommon.ErrorFields<'route'>; + errorFields?: OnyxCommon.ErrorFields<'route'> & OnyxCommon.ErrorFields<'routes'> & OnyxCommon.ErrorFields<'waypoints'>; /** The name of the file used for a receipt (formerly receiptFilename) */ filename?: string; From b818d541098933d910be22a4853d380128b6e057 Mon Sep 17 00:00:00 2001 From: FitseTLT Date: Sat, 30 Nov 2024 02:42:42 +0300 Subject: [PATCH 2/6] fix typescript --- src/types/onyx/Transaction.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/onyx/Transaction.ts b/src/types/onyx/Transaction.ts index 2aa31904a35a..594503af78c3 100644 --- a/src/types/onyx/Transaction.ts +++ b/src/types/onyx/Transaction.ts @@ -338,7 +338,7 @@ type Transaction = OnyxCommon.OnyxValueWithOfflineFeedback< errors?: OnyxCommon.Errors | ReceiptErrors; /** Server side errors keyed by microtime */ - errorFields?: OnyxCommon.ErrorFields<'route'> & OnyxCommon.ErrorFields<'routes'> & OnyxCommon.ErrorFields<'waypoints'>; + errorFields?: OnyxCommon.ErrorFields; /** The name of the file used for a receipt (formerly receiptFilename) */ filename?: string; From 669de444be8018a6b8a52a868585c1e191fa194f Mon Sep 17 00:00:00 2001 From: FitseTLT Date: Thu, 5 Dec 2024 19:10:24 +0300 Subject: [PATCH 3/6] reset route on failure data --- src/libs/actions/IOU.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 63b4e04a184c..32aa8d908b7c 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -3253,6 +3253,7 @@ function updateMoneyRequestDistance({ }, }, modifiedWaypoints: onyxModifiedWaypoints, + routes: null, }, }); } From 8c1fc5eb797928323f758e60b80b1f39ea76c56f Mon Sep 17 00:00:00 2001 From: FitseTLT Date: Thu, 5 Dec 2024 19:26:09 +0300 Subject: [PATCH 4/6] updated comment --- src/libs/actions/IOU.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 32aa8d908b7c..506bfd06936d 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -3231,7 +3231,9 @@ function updateMoneyRequestDistance({ if (transactionBackup) { const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]; // We need to have all the keys of the original waypoint in the failure data for onyx merge to properly reset - // waypoints keys that do not exist in the waypoint of the reverting failure data. + // waypoints keys that do not exist in the waypoint of the reverting failure data. For instance, if a waypoint had + // three keys and the waypoint we we want to revert to has 2 keys then the third key that doesn't exist in the failureData + // waypoint should be explicitly reset otherwise onyx merge will leave it intact. const allWaypointKeys = [...new Set([...Object.keys(transactionBackup.comment?.waypoints ?? {}), ...Object.keys(transaction?.comment?.waypoints ?? {})])]; const onyxWaypoints = allWaypointKeys.reduce((acc: NullishDeep, key) => { acc[key] = transactionBackup.comment?.waypoints?.[key] ? {...transactionBackup.comment?.waypoints?.[key]} : null; From 641ea6b848073dcb5d059027563dccda359027e4 Mon Sep 17 00:00:00 2001 From: FitseTLT Date: Mon, 9 Dec 2024 17:05:09 +0300 Subject: [PATCH 5/6] updated comment --- 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 506bfd06936d..5310bf6e13b3 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -3230,10 +3230,10 @@ function updateMoneyRequestDistance({ if (transactionBackup) { const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]; - // We need to have all the keys of the original waypoint in the failure data for onyx merge to properly reset - // waypoints keys that do not exist in the waypoint of the reverting failure data. For instance, if a waypoint had - // three keys and the waypoint we we want to revert to has 2 keys then the third key that doesn't exist in the failureData - // waypoint should be explicitly reset otherwise onyx merge will leave it intact. + // We need to include all keys of the optimisticData's waypoints in the failureData for onyx merge to properly reset + // waypoint keys that do not exist in the failureData's waypoints. For instance, if the optimisticData waypoints had + // three keys and the failureData waypoint had only 2 keys then the third key that doesn't exist in the failureData + // waypoints should be explicitly reset otherwise onyx merge will leave it intact. const allWaypointKeys = [...new Set([...Object.keys(transactionBackup.comment?.waypoints ?? {}), ...Object.keys(transaction?.comment?.waypoints ?? {})])]; const onyxWaypoints = allWaypointKeys.reduce((acc: NullishDeep, key) => { acc[key] = transactionBackup.comment?.waypoints?.[key] ? {...transactionBackup.comment?.waypoints?.[key]} : null; From 6064126f81b775a56ddae1b3c15be81c5f560576 Mon Sep 17 00:00:00 2001 From: FitseTLT Date: Thu, 12 Dec 2024 19:17:19 +0300 Subject: [PATCH 6/6] minor fix --- src/libs/actions/IOU.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 53c158751514..40bf57ddadd6 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -3289,6 +3289,7 @@ function updateMoneyRequestDistance({ if (transactionBackup) { const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]; + // We need to include all keys of the optimisticData's waypoints in the failureData for onyx merge to properly reset // waypoint keys that do not exist in the failureData's waypoints. For instance, if the optimisticData waypoints had // three keys and the failureData waypoint had only 2 keys then the third key that doesn't exist in the failureData