Skip to content

Commit

Permalink
Migrated CheckForPreviousReportActionID.js with Log.js dependency.
Browse files Browse the repository at this point in the history
  • Loading branch information
fvlvte committed Oct 5, 2023
1 parent 60b8dae commit ae362c2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
28 changes: 15 additions & 13 deletions src/libs/Log.js → src/libs/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown>
*/
function LogCommand(parameters) {
function LogCommand(parameters: Record<string, unknown>) {
const commandName = 'Log';
requireParameters(['logPacket', 'expensifyCashAppVersion'], parameters, commandName);

Expand All @@ -27,21 +27,23 @@ 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<string, unknown>) {
const requestParams = params;
requestParams.shouldProcessImmediately = false;
requestParams.shouldRetry = false;
requestParams.expensifyCashAppVersion = `expensifyCash[${getPlatform()}]${pkg.version}`;
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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Object>}
* @returns
*/
function getReportActionsFromOnyx() {
function getReportActionsFromOnyx(): Promise<OnyxCollection<ReportAction>> {
return new Promise((resolve) => {
const connectionID = Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
waitForCollectionCallback: true,
callback: (allReportActions) => {
callback: (allReportActions: OnyxCollection<ReportAction>) => {
Onyx.disconnect(connectionID);
return resolve(allReportActions);
},
Expand All @@ -23,28 +25,33 @@ 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<void>}
* @returns
*/
export default function () {
export default function (): Promise<void> {
return getReportActionsFromOnyx().then((allReportActions) => {
if (_.isEmpty(allReportActions)) {
Log.info(`[Migrate Onyx] Skipped migration CheckForPreviousReportActionID because there were no reportActions`);
return;
}

let firstValidValue;
_.some(_.values(allReportActions), (reportActions) =>
_.some(_.values(reportActions), (reportActionData) => {
if (_.has(reportActionData, 'reportActionID')) {

const records = allReportActions as Record<string, ReportAction>;

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;
}
Expand All @@ -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<string, ReportAction> = {};

Object.entries(records).forEach(([onyxKey]) => {
onyxData[onyxKey] = {} as ReportAction;
});

return Onyx.multiSet(onyxData);
return Onyx.multiSet(onyxData as never);
});
}

0 comments on commit ae362c2

Please sign in to comment.