-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31280 from Tony-MK/backup-transactions-onyx-migra…
…tion Onyx migration for transactions backups
- Loading branch information
Showing
6 changed files
with
69 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import Onyx, {OnyxCollection} from 'react-native-onyx'; | ||
import Log from '@libs/Log'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
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 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 by most functions, e.g, getAllReportTransactions (src/libs/TransactionUtils.ts). 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 pending map. | ||
* 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) => { | ||
const connectionID = Onyx.connect({ | ||
key: ONYXKEYS.COLLECTION.TRANSACTION, | ||
waitForCollectionCallback: true, | ||
callback: (transactions: OnyxCollection<Transaction>) => { | ||
Onyx.disconnect(connectionID); | ||
|
||
// Determine whether any transactions were stored | ||
if (!transactions || Object.keys(transactions).length === 0) { | ||
Log.info('[Migrate Onyx] Skipped TransactionBackupsToCollection migration because there are no transactions'); | ||
return resolve(); | ||
} | ||
|
||
const onyxData: OnyxCollection<Transaction> = {}; | ||
|
||
// Find all the transaction backups available | ||
Object.keys(transactions).forEach((transactionOnyxKey: string) => { | ||
const transaction: Transaction | null = transactions[transactionOnyxKey]; | ||
|
||
// Determine whether or not the transaction is a backup | ||
if (transactionOnyxKey.endsWith('-backup') && transaction) { | ||
// 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; | ||
} | ||
}); | ||
|
||
// Determine whether any transaction backups are found | ||
if (Object.keys(onyxData).length === 0) { | ||
Log.info('[Migrate Onyx] Skipped TransactionBackupsToCollection migration because there are no transaction backups'); | ||
return resolve(); | ||
} | ||
|
||
// Move the transaction backups to the draft transaction collection | ||
Onyx.multiSet(onyxData as Partial<{string: [Transaction | null]}>).then(() => { | ||
Log.info('[Migrate Onyx] TransactionBackupsToCollection migration: Successfully moved all the transaction backups to the draft transaction collection'); | ||
resolve(); | ||
}); | ||
}, | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters