Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename receiptFilename to filename #27203

Merged
merged 11 commits into from
Sep 21, 2023
2 changes: 1 addition & 1 deletion src/components/ReportActionItem/MoneyRequestPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function MoneyRequestPreview(props) {
!_.isEmpty(requestMerchant) && !props.isBillSplit && requestMerchant !== CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT && requestMerchant !== CONST.TRANSACTION.DEFAULT_MERCHANT;
const shouldShowDescription = !_.isEmpty(description) && !shouldShowMerchant;

const receiptImages = hasReceipt ? [ReceiptUtils.getThumbnailAndImageURIs(props.transaction.receipt.source, props.transaction.filename || props.transaction.receiptFilename || '')] : [];
const receiptImages = hasReceipt ? [ReceiptUtils.getThumbnailAndImageURIs(props.transaction.receipt.source, props.transaction.filename || '')] : [];

const getSettledMessage = () => {
switch (lodashGet(props.action, 'originalMessage.paymentType', '')) {
Expand Down
4 changes: 1 addition & 3 deletions src/components/ReportActionItem/ReportPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ function ReportPreview(props) {
const isScanning = hasReceipts && ReportUtils.areAllRequestsBeingSmartScanned(props.iouReportID, props.action);
const hasErrors = hasReceipts && ReportUtils.hasMissingSmartscanFields(props.iouReportID);
const lastThreeTransactionsWithReceipts = ReportUtils.getReportPreviewDisplayTransactions(props.action);
const lastThreeReceipts = _.map(lastThreeTransactionsWithReceipts, ({receipt, filename, receiptFilename}) =>
ReceiptUtils.getThumbnailAndImageURIs(receipt.source, filename || receiptFilename || ''),
);
const lastThreeReceipts = _.map(lastThreeTransactionsWithReceipts, ({receipt, filename}) => ReceiptUtils.getThumbnailAndImageURIs(receipt.source, filename || ''));

const hasOnlyOneReceiptRequest = numberOfRequests === 1 && hasReceipts;
const previewSubtitle = hasOnlyOneReceiptRequest
Expand Down
11 changes: 10 additions & 1 deletion src/libs/migrateOnyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@ import MoveToIndexedDB from './migrations/MoveToIndexedDB';
import RenameExpensifyNewsStatus from './migrations/RenameExpensifyNewsStatus';
import AddLastVisibleActionCreated from './migrations/AddLastVisibleActionCreated';
import PersonalDetailsByAccountID from './migrations/PersonalDetailsByAccountID';
import RenameReceiptFilename from './migrations/RenameReceiptFilename';

export default function () {
const startTime = Date.now();
Log.info('[Migrate Onyx] start');

return new Promise((resolve) => {
// Add all migrations to an array so they are executed in order
const migrationPromises = [MoveToIndexedDB, RenamePriorityModeKey, AddEncryptedAuthToken, RenameExpensifyNewsStatus, AddLastVisibleActionCreated, PersonalDetailsByAccountID];
const migrationPromises = [
MoveToIndexedDB,
RenamePriorityModeKey,
AddEncryptedAuthToken,
RenameExpensifyNewsStatus,
AddLastVisibleActionCreated,
PersonalDetailsByAccountID,
RenameReceiptFilename,
];

// Reduce all promises down to a single promise. All promises run in a linear fashion, waiting for the
// previous promise to finish before moving onto the next one.
Expand Down
57 changes: 57 additions & 0 deletions src/libs/migrations/RenameReceiptFilename.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import Onyx from 'react-native-onyx';
import _ from 'underscore';
import lodashHas from 'lodash/has';
import ONYXKEYS from '../../ONYXKEYS';
import Log from '../Log';

// 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 () {
return new Promise((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 connectionID = Onyx.connect({
key: ONYXKEYS.COLLECTION.TRANSACTION,
waitForCollectionCallback: true,
callback: (transactions) => {
Onyx.disconnect(connectionID);

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

if (!_.compact(_.pluck(transactions, 'receiptFilename')).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 = _.reduce(
transactions,
(result, transaction) => {
// Do nothing if there is no receiptFilename property
if (!lodashHas(transaction, 'receiptFilename')) {
return result;
}
Log.info(`[Migrate Onyx] Renaming receiptFilename ${transaction.receiptFilename} to filename`);
return {
...result,
[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`]: {
filename: transaction.receiptFilename,
receiptFilename: null,
},
};
},
{},
);

// eslint-disable-next-line rulesdir/prefer-actions-set-data
Onyx.mergeCollection(ONYXKEYS.COLLECTION.TRANSACTION, dataToSave).then(() => {
Log.info(`[Migrate Onyx] Ran migration RenameReceiptFilename and renamed ${_.size(dataToSave)} properties`);
resolve();
});
},
});
});
}
12 changes: 7 additions & 5 deletions src/types/onyx/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,27 @@ type Route = {
type Routes = Record<string, Route>;

type Transaction = {
transactionID: string;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's a little OCD but I like the alphabetize these.

amount: number;
category: string;
currency: string;
reportID: string;
comment: Comment;
merchant: string;
created: string;
pendingAction: OnyxCommon.PendingAction;
currency: string;
errors: OnyxCommon.Errors;
// The name of the file used for a receipt (formerly receiptFilename)
filename?: string;
merchant: string;
modifiedAmount?: number;
modifiedCreated?: string;
modifiedCurrency?: string;
pendingAction: OnyxCommon.PendingAction;
receipt: {
receiptID?: number;
source?: string;
state?: ValueOf<typeof CONST.IOU.RECEIPT_STATE>;
};
reportID: string;
routes?: Routes;
transactionID: string;
tag: string;
};

Expand Down
Loading