Skip to content

Commit

Permalink
Merge pull request #39829 from software-mansion-labs/@szymczak/deprec…
Browse files Browse the repository at this point in the history
…ate-card.isVirtual-references

Deprecate card.isVirtual references
  • Loading branch information
grgia authored Apr 16, 2024
2 parents c75c85f + fc9f595 commit 83d1166
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/libs/CardUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function maskCard(lastFour = ''): string {
* @returns a physical card object (or undefined if none is found)
*/
function findPhysicalCard(cards: Card[]) {
return cards.find((card) => !card.isVirtual);
return cards.find((card) => !card.nameValuePairs?.isVirtual);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/libs/migrateOnyx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import KeyReportActionsDraftByReportActionID from './migrations/KeyReportActions
import NVPMigration from './migrations/NVPMigration';
import PronounsMigration from './migrations/PronounsMigration';
import RemoveEmptyReportActionsDrafts from './migrations/RemoveEmptyReportActionsDrafts';
import RenameCardIsVirtual from './migrations/RenameCardIsVirtual';
import RenameReceiptFilename from './migrations/RenameReceiptFilename';
import TransactionBackupsToCollection from './migrations/TransactionBackupsToCollection';

Expand All @@ -14,6 +15,7 @@ export default function () {
return new Promise<void>((resolve) => {
// Add all migrations to an array so they are executed in order
const migrationPromises = [
RenameCardIsVirtual,
CheckForPreviousReportActionID,
RenameReceiptFilename,
KeyReportActionsDraftByReportActionID,
Expand Down
52 changes: 52 additions & 0 deletions src/libs/migrations/RenameCardIsVirtual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import Onyx from 'react-native-onyx';
import type {NullishDeep, OnyxEntry} from 'react-native-onyx';
import Log from '@libs/Log';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Card} from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';

type OldCard = Card & {isVirtual?: boolean};

// This migration changes the property name on each card from card list from isVirtual to nameValuePairs.isVirtual
export default function () {
return new Promise<void>((resolve) => {
const connectionID = Onyx.connect({
key: ONYXKEYS.CARD_LIST,
callback: (cardList: OnyxEntry<Record<string, OldCard>>) => {
Onyx.disconnect(connectionID);

if (!cardList || isEmptyObject(cardList)) {
Log.info('[Migrate Onyx] Skipped migration RenameCardIsVirtual because there are no cards linked to the account');
return resolve();
}
const cardsWithIsVirtualProp = Object.values(cardList).filter((card) => card.isVirtual !== undefined);
if (!cardsWithIsVirtualProp.length) {
Log.info('[Migrate Onyx] Skipped migration RenameCardIsVirtual because there were no cards with the isVirtual property');
return resolve();
}

Log.info('[Migrate Onyx] Running RenameCardIsVirtual migration');
const dataToSave: Record<string, NullishDeep<OldCard>> = cardsWithIsVirtualProp.reduce((result, card) => {
if (!card) {
return result;
}
return {
...result,
[card.cardID]: {
nameValuePairs: {
isVirtual: card.isVirtual,
},
isVirtual: undefined,
},
};
}, {});

// eslint-disable-next-line rulesdir/prefer-actions-set-data
Onyx.merge(ONYXKEYS.CARD_LIST, dataToSave).then(() => {
Log.info(`[Migrate Onyx] Ran migration RenameCardIsVirtual and renamed ${Object.keys(dataToSave)?.length} properties`);
resolve();
});
},
});
});
}
2 changes: 1 addition & 1 deletion src/pages/settings/Wallet/ActivatePhysicalCardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function ActivatePhysicalCardPage({
const [lastPressedDigit, setLastPressedDigit] = useState('');

const domainCards = CardUtils.getDomainCards(cardList)[domain] ?? [];
const physicalCard = domainCards.find((card) => !card.isVirtual);
const physicalCard = domainCards.find((card) => !card.nameValuePairs?.isVirtual);
const cardID = physicalCard?.cardID ?? 0;
const cardError = ErrorUtils.getLatestErrorMessage(physicalCard ?? {});

Expand Down
4 changes: 2 additions & 2 deletions src/pages/settings/Wallet/Card/BaseGetPhysicalCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function BaseGetPhysicalCard({
}

const domainCards = CardUtils.getDomainCards(cardList)[domain] || [];
const physicalCard = domainCards.find((card) => !card?.isVirtual);
const physicalCard = domainCards.find((card) => !card?.nameValuePairs?.isVirtual);

// When there are no cards for the specified domain, user is redirected to the wallet page
if (domainCards.length === 0) {
Expand Down Expand Up @@ -148,7 +148,7 @@ function BaseGetPhysicalCard({
// If the current step of the get physical card flow is the confirmation page
if (isConfirmation) {
const domainCards = CardUtils.getDomainCards(cardList)[domain];
const physicalCard = domainCards.find((card) => !card?.isVirtual);
const physicalCard = domainCards.find((card) => !card?.nameValuePairs?.isVirtual);
const cardID = physicalCard?.cardID ?? 0;

Wallet.requestPhysicalExpensifyCard(cardID, session?.authToken ?? '', updatedPrivatePersonalDetails);
Expand Down
4 changes: 2 additions & 2 deletions src/pages/settings/Wallet/ExpensifyCardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ function ExpensifyCardPage({
const {isOffline} = useNetwork();
const {translate} = useLocalize();
const domainCards = useMemo(() => cardList && CardUtils.getDomainCards(cardList)[domain], [cardList, domain]);
const virtualCard = useMemo(() => domainCards?.find((card) => card.isVirtual), [domainCards]);
const physicalCard = useMemo(() => domainCards?.find((card) => !card.isVirtual), [domainCards]);
const virtualCard = useMemo(() => domainCards?.find((card) => card.nameValuePairs?.isVirtual), [domainCards]);
const physicalCard = useMemo(() => domainCards?.find((card) => !card.nameValuePairs?.isVirtual), [domainCards]);

const [isLoading, setIsLoading] = useState(false);
const [isNotFound, setIsNotFound] = useState(false);
Expand Down
2 changes: 1 addition & 1 deletion src/pages/settings/Wallet/PaymentMethodList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ function PaymentMethodList({
if (shouldShowAssignedCards) {
const assignedCards = Object.values(cardList ?? {})
// Filter by physical, active cards associated with a domain
.filter((card) => !card.isVirtual && !!card.domainName && CONST.EXPENSIFY_CARD.ACTIVE_STATES.includes(card.state ?? 0));
.filter((card) => !card.nameValuePairs?.isVirtual && !!card.domainName && CONST.EXPENSIFY_CARD.ACTIVE_STATES.includes(card.state ?? 0));

const numberPhysicalExpensifyCards = assignedCards.filter((card) => CardUtils.isExpensifyCard(card.cardID)).length;

Expand Down
2 changes: 1 addition & 1 deletion src/pages/settings/Wallet/ReportVirtualCardFraudPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function ReportVirtualCardFraudPage({
const {translate} = useLocalize();

const domainCards = CardUtils.getDomainCards(cardList)[domain];
const virtualCard = domainCards?.find((card) => card.isVirtual);
const virtualCard = domainCards?.find((card) => card.nameValuePairs?.isVirtual);
const virtualCardError = ErrorUtils.getLatestErrorMessage(virtualCard?.errors ?? {});

const prevIsLoading = usePrevious(formData?.isLoading);
Expand Down
1 change: 0 additions & 1 deletion src/types/onyx/Card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ type Card = {
availableSpend: number;
domainName: string;
lastFourPAN?: string;
isVirtual: boolean; // Deprecating, use nameValuePairs.isVirtual
fraud: ValueOf<typeof CONST.EXPENSIFY_CARD.FRAUD_TYPES>;
errors?: OnyxCommon.Errors;
isLoading?: boolean;
Expand Down

0 comments on commit 83d1166

Please sign in to comment.