Skip to content

Commit

Permalink
Merge pull request #2 from Tony-MK/backup-transactions-onyx-migration…
Browse files Browse the repository at this point in the history
…-patch-1

Fix: Return resolve promise and removing console log while improve co…
  • Loading branch information
Tony-MK authored Nov 18, 2023
2 parents 55ac5f7 + b989b9d commit 0504cf5
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions src/libs/migrations/TransactionBackupsToCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import {Transaction} from '@src/types/onyx';

/**
* This migration moves all the transaction backups stored in the transaction collection, ONYXKEYS.COLLECTION.TRANSACTION,
* to a reserved collection which only stores draft transactions, ONYXKEYS.COLLECTION.TRANSACTION_DRAFT.
* The purpose of the migration is that there is a possibility of transaction backups are not filtered from
* to a reserved collection that only stores draft transactions, ONYXKEYS.COLLECTION.TRANSACTION_DRAFT.
* The purpose of the migration is that there is a possibility that transaction backups are not filtered from
* by most functions when all transactions are fetched. Eg: getAllReportTransactions (src/libs/TransactionUtils.ts)
* One problem which arose from storing transaction backups with the other transactions is that for every distance request
* One problem that arose from storing transaction backups with the other transactions is that for every distance request
* which have their waypoints updated offline, we expect the ReportPreview component to display the default image of a pending map.
* However, due to the presence of the transaction backup, the previous map image will be displayed alongside the current pending map.
* The problem was further discussed in this PR -> https://github.com/Expensify/App/pull/30232#issuecomment-178110172
* The problem was further discussed in this PR. https://github.com/Expensify/App/pull/30232#issuecomment-178110172
*/
export default function (): Promise<void> {
return new Promise<void>((resolve) => {
Expand All @@ -22,38 +22,35 @@ export default function (): Promise<void> {
callback: (transactions: OnyxCollection<Transaction>) => {
Onyx.disconnect(connectionID);

// Check if transaction collection is empty
// Determine whether or not any transactions were found
if (isEmpty(transactions) || !transactions) {
Log.info('[Migrate Onyx] Skipped TransactionBackupsToCollection migration because there are no transactions');
return resolve;
return resolve();
}
const onyxData: OnyxCollection<Transaction> = {};

// Find all the transaction backups available
Object.keys(transactions).forEach((transactionOnyxKey: string) => {
const transaction: Transaction | null = transactions[transactionOnyxKey];

// Check if transaction is a backup
// Determine whether or not the transaction is a backup
if (!transactionOnyxKey.endsWith('-backup') || !transaction) {
return;
}

// Create the transaction backup in the draft transaction collection
onyxData[`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction.transactionID}`] = transaction;

// Delete the transaction backup stored the transaction collection
onyxData[transactionOnyxKey] = null;
// Create the transaction backup in the draft transaction collection
onyxData[`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction.transactionID}`] = transaction;

// Delete the transaction backup stored in the transaction collection
onyxData[transactionOnyxKey] = null;
}
});

// Check if any transaction backups were found
// Determine whether or not any transaction backups were found
if (isEmpty(onyxData)) {
Log.info('[Migrate Onyx] Skipped TransactionBackupsToCollection migration because there are no transaction backups');
return;
return resolve();
}

// eslint-disable-next-line no-console
console.info(onyxData);

// Move the transaction backups to the draft transaction collection
Onyx.multiSet(onyxData as Partial<{string: [Transaction | null]}>).then(() => {
Log.info('[Migrate Onyx] TransactionBackupsToCollection migration: Moved the transaction backups to the draft transaction collection');
resolve();
Expand Down

0 comments on commit 0504cf5

Please sign in to comment.