Skip to content

Commit

Permalink
refactor: migrate to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
samh-nl committed Dec 23, 2023
1 parent 285a312 commit 97a1be4
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 72 deletions.
69 changes: 0 additions & 69 deletions src/libs/migrations/RemoveEmptyReportActionsDrafts.js

This file was deleted.

76 changes: 76 additions & 0 deletions src/libs/migrations/RemoveEmptyReportActionsDrafts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import _ from 'lodash';
import Onyx, {OnyxEntry} from 'react-native-onyx';
import Log from '@libs/Log';
import ONYXKEYS from '@src/ONYXKEYS';
import {ReportActionsDraft, ReportActionsDrafts} from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';

type ReportActionsDraftsKey = `${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${string}`;

/**
* This migration removes empty drafts from reportActionsDrafts, which was previously used to mark a draft as being non-existent (e.g. upon cancel).
*/
export default function (): Promise<void> {
return new Promise<void>((resolve) => {
const connectionID = Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
waitForCollectionCallback: true,
callback: (allReportActionsDrafts) => {
Onyx.disconnect(connectionID);

if (!allReportActionsDrafts) {
Log.info('[Migrate Onyx] Skipped migration RemoveEmptyReportActionsDrafts because there were no reportActionsDrafts');
return resolve();
}

const newReportActionsDrafts: Record<ReportActionsDraftsKey, OnyxEntry<ReportActionsDrafts>> = {};
Object.entries(allReportActionsDrafts).forEach(([onyxKey, reportActionDrafts]) => {
const newReportActionsDraftsForReport: Record<string, ReportActionsDraft> = {};

// Whether there is at least one draft in this report that has to be migrated
let hasUnmigratedDraft = false;

if (reportActionDrafts) {
Object.entries(reportActionDrafts).forEach(([reportActionID, reportActionDraft]) => {
// If the draft is a string, it means it hasn't been migrated yet
if (typeof reportActionDraft === 'string') {
hasUnmigratedDraft = true;
Log.info(`[Migrate Onyx] Migrating draft for report action ${reportActionID}`);

if (_.isEmpty(reportActionDraft)) {
Log.info(`[Migrate Onyx] Removing draft for report action ${reportActionID}`);
return;
}

newReportActionsDraftsForReport[reportActionID] = {message: reportActionDraft};
} else {
// We've already migrated this draft, so keep the existing value
newReportActionsDraftsForReport[reportActionID] = reportActionDraft;
}
});
}

if (isEmptyObject(newReportActionsDraftsForReport)) {
Log.info('[Migrate Onyx] NO REMAINING');
// Clear if there are no drafts remaining
newReportActionsDrafts[onyxKey as ReportActionsDraftsKey] = null;
} else if (hasUnmigratedDraft) {
// Only migrate if there are unmigrated drafts, there's no need to overwrite this onyx key with the same data
newReportActionsDrafts[onyxKey as ReportActionsDraftsKey] = newReportActionsDraftsForReport;
}
});

if (isEmptyObject(newReportActionsDrafts)) {
Log.info('[Migrate Onyx] Skipped migration RemoveEmptyReportActionsDrafts because there are no actions drafts to migrate');
return resolve();
}

Log.info(`[Migrate Onyx] Updating drafts for ${Object.keys(newReportActionsDrafts).length} reports`);
Onyx.multiSet(newReportActionsDrafts).then(() => {
Log.info('[Migrate Onyx] Ran migration RemoveEmptyReportActionsDrafts successfully');
resolve();
});
},
});
});
}
7 changes: 7 additions & 0 deletions src/types/onyx/ReportActionsDraft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type ReportActionsDraft =
| {
message: string;
}
| string;

export default ReportActionsDraft;
4 changes: 1 addition & 3 deletions src/types/onyx/ReportActionsDrafts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
type ReportActionsDraft = {
message: string;
};
import ReportActionsDraft from './ReportActionsDraft';

type ReportActionsDrafts = Record<string, ReportActionsDraft>;

Expand Down
2 changes: 2 additions & 0 deletions src/types/onyx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import ReimbursementAccountDraft from './ReimbursementAccountDraft';
import Report from './Report';
import ReportAction, {ReportActions} from './ReportAction';
import ReportActionReactions from './ReportActionReactions';
import ReportActionsDraft from './ReportActionsDraft';
import ReportActionsDrafts from './ReportActionsDrafts';
import ReportMetadata from './ReportMetadata';
import ReportNextStep from './ReportNextStep';
Expand Down Expand Up @@ -107,6 +108,7 @@ export type {
ReportAction,
ReportActionReactions,
ReportActions,
ReportActionsDraft,
ReportActionsDrafts,
ReportMetadata,
ReportNextStep,
Expand Down

0 comments on commit 97a1be4

Please sign in to comment.