From ae362c2540ad8f49c63de64d00a5b7355e43a60c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Fa=C5=82at?= Date: Mon, 18 Sep 2023 11:37:06 +0200 Subject: [PATCH] Migrated CheckForPreviousReportActionID.js with Log.js dependency. --- src/libs/{Log.js => Log.ts} | 28 +++++++------ ...D.js => CheckForPreviousReportActionID.ts} | 42 +++++++++++-------- 2 files changed, 40 insertions(+), 30 deletions(-) rename src/libs/{Log.js => Log.ts} (81%) rename src/libs/migrations/{CheckForPreviousReportActionID.js => CheckForPreviousReportActionID.ts} (62%) diff --git a/src/libs/Log.js b/src/libs/Log.ts similarity index 81% rename from src/libs/Log.js rename to src/libs/Log.ts index e51fb74aedd5..e9874e0b739d 100644 --- a/src/libs/Log.js +++ b/src/libs/Log.ts @@ -7,15 +7,15 @@ import pkg from '../../package.json'; import requireParameters from './requireParameters'; import * as Network from './Network'; -let timeout = null; +let timeout: NodeJS.Timeout | null = null; /** - * @param {Object} parameters - * @param {String} parameters.expensifyCashAppVersion - * @param {Object[]} parameters.logPacket - * @returns {Promise} + * @param parameters + * @param parameters.expensifyCashAppVersion + * @param parameters.logPacket + * @returns Promise */ -function LogCommand(parameters) { +function LogCommand(parameters: Record) { const commandName = 'Log'; requireParameters(['logPacket', 'expensifyCashAppVersion'], parameters, commandName); @@ -27,13 +27,13 @@ function LogCommand(parameters) { /** * Network interface for logger. * - * @param {Logger} logger - * @param {Object} params - * @param {Object} params.parameters - * @param {String} params.message - * @return {Promise} + * @param logger + * @param params + * @param params.parameters + * @param params.message + * @return */ -function serverLoggingCallback(logger, params) { +function serverLoggingCallback(logger: Logger, params: Record) { const requestParams = params; requestParams.shouldProcessImmediately = false; requestParams.shouldRetry = false; @@ -41,7 +41,9 @@ function serverLoggingCallback(logger, params) { if (requestParams.parameters) { requestParams.parameters = JSON.stringify(params.parameters); } - clearTimeout(timeout); + if(timeout) { + clearTimeout(timeout); + } timeout = setTimeout(() => logger.info('Flushing logs older than 10 minutes', true, {}, true), 10 * 60 * 1000); return LogCommand(requestParams); } diff --git a/src/libs/migrations/CheckForPreviousReportActionID.js b/src/libs/migrations/CheckForPreviousReportActionID.ts similarity index 62% rename from src/libs/migrations/CheckForPreviousReportActionID.js rename to src/libs/migrations/CheckForPreviousReportActionID.ts index a61d9bfb08ec..ea1c2e2ad5b9 100644 --- a/src/libs/migrations/CheckForPreviousReportActionID.js +++ b/src/libs/migrations/CheckForPreviousReportActionID.ts @@ -1,17 +1,19 @@ -import _ from 'underscore'; -import Onyx from 'react-native-onyx'; +import Onyx, {OnyxCollection} from 'react-native-onyx'; +import _ from "lodash"; +import lodashHas from "lodash/has"; import Log from '../Log'; import ONYXKEYS from '../../ONYXKEYS'; +import {ReportAction} from "../../types/onyx"; /** - * @returns {Promise} + * @returns */ -function getReportActionsFromOnyx() { +function getReportActionsFromOnyx(): Promise> { return new Promise((resolve) => { const connectionID = Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, waitForCollectionCallback: true, - callback: (allReportActions) => { + callback: (allReportActions: OnyxCollection) => { Onyx.disconnect(connectionID); return resolve(allReportActions); }, @@ -23,9 +25,9 @@ function getReportActionsFromOnyx() { * This migration checks for the 'previousReportActionID' key in the first valid reportAction of a report in Onyx. * If the key is not found then all reportActions for all reports are removed from Onyx. * - * @returns {Promise} + * @returns */ -export default function () { +export default function (): Promise { return getReportActionsFromOnyx().then((allReportActions) => { if (_.isEmpty(allReportActions)) { Log.info(`[Migrate Onyx] Skipped migration CheckForPreviousReportActionID because there were no reportActions`); @@ -33,18 +35,23 @@ export default function () { } let firstValidValue; - _.some(_.values(allReportActions), (reportActions) => - _.some(_.values(reportActions), (reportActionData) => { - if (_.has(reportActionData, 'reportActionID')) { + + const records = allReportActions as Record; + + Object.values(records).some((reportAction: ReportAction) => { + Object.values(reportAction).some((reportActionData: unknown) => { + if (lodashHas(reportActionData, 'reportActionID')) { firstValidValue = reportActionData; return true; } return false; - }), - ); + }); - if (_.isUndefined(firstValidValue)) { + return true; + }); + + if (firstValidValue === undefined) { Log.info(`[Migrate Onyx] Skipped migration CheckForPreviousReportActionID because there were no valid reportActions`); return; } @@ -57,11 +64,12 @@ export default function () { // If previousReportActionID not found: Log.info(`[Migrate Onyx] CheckForPreviousReportActionID Migration: removing all reportActions because previousReportActionID not found in the first valid reportAction`); - const onyxData = {}; - _.each(allReportActions, (reportAction, onyxKey) => { - onyxData[onyxKey] = {}; + const onyxData: Record = {}; + + Object.entries(records).forEach(([onyxKey]) => { + onyxData[onyxKey] = {} as ReportAction; }); - return Onyx.multiSet(onyxData); + return Onyx.multiSet(onyxData as never); }); }