Skip to content

Commit

Permalink
Clean migration files code
Browse files Browse the repository at this point in the history
  • Loading branch information
blazejkustra committed Dec 8, 2023
1 parent c106a01 commit 8f06dad
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/libs/migrations/KeyReportActionsDraftByReportActionID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import ONYXKEYS from '@src/ONYXKEYS';
import {ReportActionsDrafts} from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';

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

/**
* This migration updates reportActionsDrafts data to be keyed by reportActionID.
*
Expand All @@ -24,7 +26,6 @@ export default function () {
return resolve();
}

type ReportActionsDraftsKey = `${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${string}`;
const newReportActionsDrafts: Record<ReportActionsDraftsKey, OnyxEntry<ReportActionsDrafts>> = {};
Object.entries(allReportActionsDrafts).forEach(([onyxKey, reportActionDraft]) => {
if (typeof reportActionDraft !== 'string') {
Expand Down
27 changes: 13 additions & 14 deletions src/libs/migrations/RenameReceiptFilename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Transaction from '@src/types/onyx/Transaction';
import {isEmptyObject} from '@src/types/utils/EmptyObject';

type OldTransaction = Transaction & {receiptFilename?: string};
type TransactionKey = `${typeof ONYXKEYS.COLLECTION.TRANSACTION}${string}`;

// This migration changes the property name on a transaction from receiptFilename to filename so that it matches what is stored in the database
export default function () {
Expand All @@ -23,25 +24,23 @@ export default function () {
return resolve();
}

const transactionArray: Array<OldTransaction | null> = Object.values(transactions);
if (!transactionArray?.map((transaction) => transaction?.receiptFilename).filter(Boolean).length) {
const transactionsWithReceipt: Array<OldTransaction | null> = Object.values(transactions).filter((transaction) => transaction?.receiptFilename);
if (!transactionsWithReceipt?.length) {
Log.info('[Migrate Onyx] Skipped migration RenameReceiptFilename because there were no transactions with the receiptFilename property');
return resolve();
}

Log.info('[Migrate Onyx] Running RenameReceiptFilename migration');
const dataToSave: Record<`${typeof ONYXKEYS.COLLECTION.TRANSACTION}${string}`, NullishDeep<OldTransaction>> = {};

transactionArray?.forEach((transaction) => {
// Do nothing if there is no receiptFilename property
if (!transaction || !('receiptFilename' in transaction)) {
return;
const dataToSave: Record<TransactionKey, NullishDeep<OldTransaction>> = transactionsWithReceipt?.reduce((result, transaction) => {
if (!transaction) {
return result;
}

Log.info(`[Migrate Onyx] Renaming receiptFilename ${transaction?.receiptFilename} to filename`);
dataToSave[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = {
filename: transaction?.receiptFilename,
receiptFilename: null,
Log.info(`[Migrate Onyx] Renaming receiptFilename ${transaction.receiptFilename} to filename`);
return {
...result,
[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`]: {
filename: transaction.receiptFilename,
receiptFilename: null,
},
};
}, {});

Expand Down

0 comments on commit 8f06dad

Please sign in to comment.