From c01469768b88fc43f50466c23cfd85192fe08d4d Mon Sep 17 00:00:00 2001 From: Yuwen Memon Date: Wed, 22 May 2024 16:54:48 -0700 Subject: [PATCH 1/6] Make sure that the connection sync progress times out after 20 minutes using timestamp --- src/CONST.ts | 1 + src/libs/actions/connections/QuickBooksOnline.ts | 1 + src/pages/workspace/accounting/PolicyAccountingPage.tsx | 4 +++- src/types/onyx/Policy.ts | 1 + 4 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/CONST.ts b/src/CONST.ts index 6517ece4276d..dbb9ec5fcaf7 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -1806,6 +1806,7 @@ const CONST = { XERO_CHECK_CONNECTION: 'xeroCheckConnection', XERO_SYNC_TITLE: 'xeroSyncTitle', }, + SYNC_STAGE_TIMEOUT: 1_200_000, // 20 minutes (20m * 60s * 1000ms) }, ACCESS_VARIANTS: { PAID: 'paid', diff --git a/src/libs/actions/connections/QuickBooksOnline.ts b/src/libs/actions/connections/QuickBooksOnline.ts index f507758e8d38..e0b11fbdc94c 100644 --- a/src/libs/actions/connections/QuickBooksOnline.ts +++ b/src/libs/actions/connections/QuickBooksOnline.ts @@ -21,6 +21,7 @@ function syncConnection(policyID: string) { value: { stageInProgress: CONST.POLICY.CONNECTIONS.SYNC_STAGE_NAME.STARTING_IMPORT, connectionName: CONST.POLICY.CONNECTIONS.NAME.QBO, + timestamp: Date.now(), }, }, ]; diff --git a/src/pages/workspace/accounting/PolicyAccountingPage.tsx b/src/pages/workspace/accounting/PolicyAccountingPage.tsx index d6ef27c7b00d..763a0ee5c7d6 100644 --- a/src/pages/workspace/accounting/PolicyAccountingPage.tsx +++ b/src/pages/workspace/accounting/PolicyAccountingPage.tsx @@ -112,7 +112,9 @@ function PolicyAccountingPage({policy, connectionSyncProgress, isConnectionDataF const [isDisconnectModalOpen, setIsDisconnectModalOpen] = useState(false); const threeDotsMenuContainerRef = useRef(null); - const isSyncInProgress = !!connectionSyncProgress?.stageInProgress && connectionSyncProgress.stageInProgress !== CONST.POLICY.CONNECTIONS.SYNC_STAGE_NAME.JOB_DONE; + const isSyncInProgress = !!connectionSyncProgress?.stageInProgress + && connectionSyncProgress.stageInProgress !== CONST.POLICY.CONNECTIONS.SYNC_STAGE_NAME.JOB_DONE + && (Date.now() - (connectionSyncProgress.timestamp ?? 0)) < CONST.POLICY.CONNECTIONS.SYNC_STAGE_TIMEOUT; const accountingIntegrations = Object.values(CONST.POLICY.CONNECTIONS.NAME).filter((name) => !(name === CONST.POLICY.CONNECTIONS.NAME.XERO && !canUseXeroIntegration)); const connectedIntegration = accountingIntegrations.find((integration) => !!policy?.connections?.[integration]) ?? connectionSyncProgress?.connectionName; diff --git a/src/types/onyx/Policy.ts b/src/types/onyx/Policy.ts index 53c76286985d..1611e059a6fd 100644 --- a/src/types/onyx/Policy.ts +++ b/src/types/onyx/Policy.ts @@ -545,6 +545,7 @@ type PolicyConnectionName = ValueOf; type PolicyConnectionSyncProgress = { stageInProgress: PolicyConnectionSyncStage; connectionName: PolicyConnectionName; + timestamp: number; }; export default Policy; From 3b4af719500e5f236e3620fded7d0efba50db87f Mon Sep 17 00:00:00 2001 From: Yuwen Memon Date: Wed, 22 May 2024 21:45:03 -0700 Subject: [PATCH 2/6] Prettier --- src/pages/workspace/accounting/PolicyAccountingPage.tsx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/pages/workspace/accounting/PolicyAccountingPage.tsx b/src/pages/workspace/accounting/PolicyAccountingPage.tsx index 763a0ee5c7d6..6cdb05f3148c 100644 --- a/src/pages/workspace/accounting/PolicyAccountingPage.tsx +++ b/src/pages/workspace/accounting/PolicyAccountingPage.tsx @@ -112,9 +112,10 @@ function PolicyAccountingPage({policy, connectionSyncProgress, isConnectionDataF const [isDisconnectModalOpen, setIsDisconnectModalOpen] = useState(false); const threeDotsMenuContainerRef = useRef(null); - const isSyncInProgress = !!connectionSyncProgress?.stageInProgress - && connectionSyncProgress.stageInProgress !== CONST.POLICY.CONNECTIONS.SYNC_STAGE_NAME.JOB_DONE - && (Date.now() - (connectionSyncProgress.timestamp ?? 0)) < CONST.POLICY.CONNECTIONS.SYNC_STAGE_TIMEOUT; + const isSyncInProgress = + !!connectionSyncProgress?.stageInProgress && + connectionSyncProgress.stageInProgress !== CONST.POLICY.CONNECTIONS.SYNC_STAGE_NAME.JOB_DONE && + Date.now() - (connectionSyncProgress.timestamp ?? 0) < CONST.POLICY.CONNECTIONS.SYNC_STAGE_TIMEOUT; const accountingIntegrations = Object.values(CONST.POLICY.CONNECTIONS.NAME).filter((name) => !(name === CONST.POLICY.CONNECTIONS.NAME.XERO && !canUseXeroIntegration)); const connectedIntegration = accountingIntegrations.find((integration) => !!policy?.connections?.[integration]) ?? connectionSyncProgress?.connectionName; From debf40ea95275c403f8978061840a6f4fe5e59ba Mon Sep 17 00:00:00 2001 From: Yuwen Memon Date: Thu, 23 May 2024 12:23:41 -0700 Subject: [PATCH 3/6] Use ISO timestamp --- src/CONST.ts | 2 +- src/libs/actions/connections/QuickBooksOnline.ts | 2 +- src/pages/workspace/accounting/PolicyAccountingPage.tsx | 4 +++- src/types/onyx/Policy.ts | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/CONST.ts b/src/CONST.ts index dbb9ec5fcaf7..28e619bbd2bf 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -1806,7 +1806,7 @@ const CONST = { XERO_CHECK_CONNECTION: 'xeroCheckConnection', XERO_SYNC_TITLE: 'xeroSyncTitle', }, - SYNC_STAGE_TIMEOUT: 1_200_000, // 20 minutes (20m * 60s * 1000ms) + SYNC_STAGE_TIMEOUT_MINUTES: 20, }, ACCESS_VARIANTS: { PAID: 'paid', diff --git a/src/libs/actions/connections/QuickBooksOnline.ts b/src/libs/actions/connections/QuickBooksOnline.ts index e0b11fbdc94c..17920f27033b 100644 --- a/src/libs/actions/connections/QuickBooksOnline.ts +++ b/src/libs/actions/connections/QuickBooksOnline.ts @@ -21,7 +21,7 @@ function syncConnection(policyID: string) { value: { stageInProgress: CONST.POLICY.CONNECTIONS.SYNC_STAGE_NAME.STARTING_IMPORT, connectionName: CONST.POLICY.CONNECTIONS.NAME.QBO, - timestamp: Date.now(), + timestamp: new Date().toISOString(), }, }, ]; diff --git a/src/pages/workspace/accounting/PolicyAccountingPage.tsx b/src/pages/workspace/accounting/PolicyAccountingPage.tsx index 6cdb05f3148c..95cd395dd628 100644 --- a/src/pages/workspace/accounting/PolicyAccountingPage.tsx +++ b/src/pages/workspace/accounting/PolicyAccountingPage.tsx @@ -39,6 +39,7 @@ import type {Policy, PolicyConnectionSyncProgress} from '@src/types/onyx'; import type {PolicyConnectionName} from '@src/types/onyx/Policy'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import type IconAsset from '@src/types/utils/IconAsset'; +import {differenceInMinutes, parseISO, isValid} from "date-fns"; type PolicyAccountingPageOnyxProps = { connectionSyncProgress: OnyxEntry; @@ -112,10 +113,11 @@ function PolicyAccountingPage({policy, connectionSyncProgress, isConnectionDataF const [isDisconnectModalOpen, setIsDisconnectModalOpen] = useState(false); const threeDotsMenuContainerRef = useRef(null); + const lastSyncProgressDate = parseISO(connectionSyncProgress?.timestamp ?? ''); const isSyncInProgress = !!connectionSyncProgress?.stageInProgress && connectionSyncProgress.stageInProgress !== CONST.POLICY.CONNECTIONS.SYNC_STAGE_NAME.JOB_DONE && - Date.now() - (connectionSyncProgress.timestamp ?? 0) < CONST.POLICY.CONNECTIONS.SYNC_STAGE_TIMEOUT; + (isValid(lastSyncProgressDate) && differenceInMinutes(new Date(), lastSyncProgressDate) < CONST.POLICY.CONNECTIONS.SYNC_STAGE_TIMEOUT_MINUTES); const accountingIntegrations = Object.values(CONST.POLICY.CONNECTIONS.NAME).filter((name) => !(name === CONST.POLICY.CONNECTIONS.NAME.XERO && !canUseXeroIntegration)); const connectedIntegration = accountingIntegrations.find((integration) => !!policy?.connections?.[integration]) ?? connectionSyncProgress?.connectionName; diff --git a/src/types/onyx/Policy.ts b/src/types/onyx/Policy.ts index 1611e059a6fd..e61770aebc27 100644 --- a/src/types/onyx/Policy.ts +++ b/src/types/onyx/Policy.ts @@ -545,7 +545,7 @@ type PolicyConnectionName = ValueOf; type PolicyConnectionSyncProgress = { stageInProgress: PolicyConnectionSyncStage; connectionName: PolicyConnectionName; - timestamp: number; + timestamp: string; }; export default Policy; From daf4da7ce659b89df131ddd6b6d78f3541c92c87 Mon Sep 17 00:00:00 2001 From: Yuwen Memon Date: Thu, 23 May 2024 12:26:01 -0700 Subject: [PATCH 4/6] Prettier --- src/pages/workspace/accounting/PolicyAccountingPage.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/pages/workspace/accounting/PolicyAccountingPage.tsx b/src/pages/workspace/accounting/PolicyAccountingPage.tsx index 95cd395dd628..285de304a26c 100644 --- a/src/pages/workspace/accounting/PolicyAccountingPage.tsx +++ b/src/pages/workspace/accounting/PolicyAccountingPage.tsx @@ -1,3 +1,4 @@ +import {differenceInMinutes, isValid, parseISO} from 'date-fns'; import React, {useMemo, useRef, useState} from 'react'; import {ActivityIndicator, View} from 'react-native'; import {withOnyx} from 'react-native-onyx'; @@ -39,7 +40,6 @@ import type {Policy, PolicyConnectionSyncProgress} from '@src/types/onyx'; import type {PolicyConnectionName} from '@src/types/onyx/Policy'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import type IconAsset from '@src/types/utils/IconAsset'; -import {differenceInMinutes, parseISO, isValid} from "date-fns"; type PolicyAccountingPageOnyxProps = { connectionSyncProgress: OnyxEntry; @@ -117,7 +117,8 @@ function PolicyAccountingPage({policy, connectionSyncProgress, isConnectionDataF const isSyncInProgress = !!connectionSyncProgress?.stageInProgress && connectionSyncProgress.stageInProgress !== CONST.POLICY.CONNECTIONS.SYNC_STAGE_NAME.JOB_DONE && - (isValid(lastSyncProgressDate) && differenceInMinutes(new Date(), lastSyncProgressDate) < CONST.POLICY.CONNECTIONS.SYNC_STAGE_TIMEOUT_MINUTES); + isValid(lastSyncProgressDate) && + differenceInMinutes(new Date(), lastSyncProgressDate) < CONST.POLICY.CONNECTIONS.SYNC_STAGE_TIMEOUT_MINUTES; const accountingIntegrations = Object.values(CONST.POLICY.CONNECTIONS.NAME).filter((name) => !(name === CONST.POLICY.CONNECTIONS.NAME.XERO && !canUseXeroIntegration)); const connectedIntegration = accountingIntegrations.find((integration) => !!policy?.connections?.[integration]) ?? connectionSyncProgress?.connectionName; From 361770f9f86acb2f60d29778085b4bf312c9ee78 Mon Sep 17 00:00:00 2001 From: Yuwen Memon Date: Tue, 4 Jun 2024 11:19:46 -0700 Subject: [PATCH 5/6] Add timestamp to unified sync call --- src/libs/actions/connections/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libs/actions/connections/index.ts b/src/libs/actions/connections/index.ts index 185cd089d1e9..45a4e4f6819c 100644 --- a/src/libs/actions/connections/index.ts +++ b/src/libs/actions/connections/index.ts @@ -137,6 +137,7 @@ function syncConnection(policyID: string, connectionName: PolicyConnectionName | value: { stageInProgress: isQBOConnection ? CONST.POLICY.CONNECTIONS.SYNC_STAGE_NAME.STARTING_IMPORT_QBO : CONST.POLICY.CONNECTIONS.SYNC_STAGE_NAME.STARTING_IMPORT_XERO, connectionName, + timestamp: new Date().toISOString(), }, }, ]; From 90c63f5280111e3edf7fbf0cd0a18c21c63d6858 Mon Sep 17 00:00:00 2001 From: Yuwen Memon Date: Tue, 4 Jun 2024 12:18:00 -0700 Subject: [PATCH 6/6] prettier --- src/pages/workspace/accounting/PolicyAccountingPage.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/workspace/accounting/PolicyAccountingPage.tsx b/src/pages/workspace/accounting/PolicyAccountingPage.tsx index c5edff3f4c51..bd7aa5cb4c1a 100644 --- a/src/pages/workspace/accounting/PolicyAccountingPage.tsx +++ b/src/pages/workspace/accounting/PolicyAccountingPage.tsx @@ -1,4 +1,4 @@ -import {formatDistanceToNow, differenceInMinutes, isValid, parseISO} from 'date-fns'; +import {differenceInMinutes, formatDistanceToNow, isValid, parseISO} from 'date-fns'; import React, {useEffect, useMemo, useRef, useState} from 'react'; import {ActivityIndicator, View} from 'react-native'; import {withOnyx} from 'react-native-onyx';