Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/libs/migrations/RenameReceiptFilename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,27 @@ import ONYXKEYS from '@src/ONYXKEYS';
import type 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
// This migration moves filename from the transaction root to transaction.receipt.filename to match the database structure.
export default function () {
return new Promise<void>((resolve) => {
// Connect to the TRANSACTION collection key in Onyx to get all of the stored transactions.
// Go through each transaction and change the property name
const connection = Onyx.connect({
const connection = Onyx.connectWithoutView({
key: ONYXKEYS.COLLECTION.TRANSACTION,
waitForCollectionCallback: true,
callback: (transactions: OnyxCollection<OldTransaction>) => {
callback: (transactions: OnyxCollection<Transaction>) => {
Onyx.disconnect(connection);

if (!transactions || isEmptyObject(transactions)) {
Log.info('[Migrate Onyx] Skipped migration RenameReceiptFilename because there are no transactions');
return resolve();
}

const transactionsWithReceipt: Array<OnyxEntry<OldTransaction>> = Object.values(transactions).filter((transaction) => transaction?.receiptFilename);
const transactionsWithReceipt: Array<OnyxEntry<Transaction>> = Object.values(transactions).filter((transaction) => transaction?.filename);
if (!transactionsWithReceipt?.length) {
Log.info('[Migrate Onyx] Skipped migration RenameReceiptFilename because there were no transactions with the receiptFilename property');
Log.info('[Migrate Onyx] Skipped migration RenameReceiptFilename because there were no transactions with the filename property');
return resolve();
}
Log.info('[Migrate Onyx] Running RenameReceiptFilename migration');
Expand All @@ -35,14 +34,16 @@ export default function () {
if (!transaction) {
return acc;
}
Log.info(`[Migrate Onyx] Renaming receiptFilename ${transaction.receiptFilename} to filename`);
Log.info(`[Migrate Onyx] Renaming filename ${transaction.filename} to receipt.filename`);
acc[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = {
filename: transaction.receiptFilename,
receiptFilename: null,
receipt: {
filename: transaction.filename,
},
filename: null,
};
return acc;
},
{} as Record<TransactionKey, NullishDeep<OldTransaction>>,
{} as Record<TransactionKey, NullishDeep<Transaction>>,
);

// eslint-disable-next-line rulesdir/prefer-actions-set-data
Expand Down
Loading