Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@
};

let conciergeReportIDOnyxConnect: OnyxEntry<string>;
Onyx.connect({

Check warning on line 1040 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.CONCIERGE_REPORT_ID,
callback: (value) => {
conciergeReportIDOnyxConnect = value;
Expand All @@ -1045,7 +1045,7 @@
});

const defaultAvatarBuildingIconTestID = 'SvgDefaultAvatarBuilding Icon';
Onyx.connect({

Check warning on line 1048 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.SESSION,
callback: (value) => {
// When signed out, val is undefined
Expand All @@ -1063,7 +1063,7 @@
let allPersonalDetails: OnyxEntry<PersonalDetailsList>;
let allPersonalDetailLogins: string[];
let currentUserPersonalDetails: OnyxEntry<PersonalDetails>;
Onyx.connect({

Check warning on line 1066 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (value) => {
if (deprecatedCurrentUserAccountID) {
Expand All @@ -1075,7 +1075,7 @@
});

let allReportsDraft: OnyxCollection<Report>;
Onyx.connect({

Check warning on line 1078 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_DRAFT,
waitForCollectionCallback: true,
callback: (value) => (allReportsDraft = value),
Expand All @@ -1083,7 +1083,7 @@

let allPolicies: OnyxCollection<Policy>;
let policiesArray: Policy[] = [];
Onyx.connect({

Check warning on line 1086 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.POLICY,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -1093,7 +1093,7 @@
});

let allPolicyDrafts: OnyxCollection<Policy>;
Onyx.connect({

Check warning on line 1096 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.POLICY_DRAFTS,
waitForCollectionCallback: true,
callback: (value) => (allPolicyDrafts = value),
Expand All @@ -1101,7 +1101,7 @@

let deprecatedAllReports: OnyxCollection<Report>;
let deprecatedReportsByPolicyID: ReportByPolicyMap;
Onyx.connect({

Check warning on line 1104 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
Expand Down Expand Up @@ -1137,14 +1137,14 @@
});

let betaConfiguration: OnyxEntry<BetaConfiguration> = {};
Onyx.connect({

Check warning on line 1140 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.BETA_CONFIGURATION,
callback: (value) => (betaConfiguration = value ?? {}),
});

let deprecatedAllTransactions: OnyxCollection<Transaction> = {};
let deprecatedReportsTransactions: Record<string, Transaction[]> = {};
Onyx.connect({

Check warning on line 1147 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.TRANSACTION,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -1170,7 +1170,7 @@
});

let allReportActions: OnyxCollection<ReportActions>;
Onyx.connect({

Check warning on line 1173 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
waitForCollectionCallback: true,
callback: (actions) => {
Expand Down Expand Up @@ -4169,9 +4169,9 @@
/**
* Returns the unresolved card fraud alert action for a given report.
*/
function getUnresolvedCardFraudAlertAction(reportID: string): OnyxEntry<ReportAction> {
const reportActions = getAllReportActions(reportID);
return Object.values(reportActions).find((action): action is ReportAction => isActionableCardFraudAlert(action) && !getOriginalMessage(action)?.resolution);
function getUnresolvedCardFraudAlertAction(reportID: string, reportActions?: OnyxEntry<ReportActions>): OnyxEntry<ReportAction> {
const actions = reportActions ?? getAllReportActions(reportID);
return Object.values(actions).find((action): action is ReportAction => isActionableCardFraudAlert(action) && !getOriginalMessage(action)?.resolution);
}

/**
Expand Down Expand Up @@ -13694,6 +13694,7 @@
hasMissingInvoiceBankAccount,
reasonForReportToBeInOptionList,
getReasonAndReportActionThatRequiresAttention,
getUnresolvedCardFraudAlertAction,
buildOptimisticChangeFieldAction,
isPolicyRelatedReport,
hasReportErrorsOtherThanFailedReceipt,
Expand Down
36 changes: 32 additions & 4 deletions src/libs/actions/Card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {SpendRuleForm} from '@src/types/form';
import type {Card, CompanyCardFeedWithDomainID, Report, Transaction} from '@src/types/onyx';
import type {CardLimitType, ExpensifyCardDetails, IssueNewCardData, IssueNewCardStep} from '@src/types/onyx/Card';
import type {CardLimitType, ExpensifyCardDetails, IssueNewCardData, IssueNewCardStep, PossibleFraudData} from '@src/types/onyx/Card';
import type {ExpensifyCardRule} from '@src/types/onyx/ExpensifyCardSettings';
import type {SelectedTimezone} from '@src/types/onyx/PersonalDetails';
import type {ConnectionName} from '@src/types/onyx/Policy';
Expand Down Expand Up @@ -1669,15 +1669,21 @@ function deleteExpensifyCardRule(domainAccountID: number, cardRuleID: string, ex
* Resolves a fraud alert for a given card.
* When the user clicks on the whisper it sets the optimistic data to the resolution and calls the API
*/
function resolveFraudAlert(cardID: number | undefined, isFraud: boolean, reportID: string | undefined, reportActionID: string | undefined) {
function resolveFraudAlert(
cardID: number | undefined,
isFraud: boolean,
reportID: string | undefined,
reportActionID: string | undefined,
previousPossibleFraud: PossibleFraudData | null = null,
) {
if (!reportID || !reportActionID || !cardID) {
Log.hmmm('[resolveFraudAlert] Missing required parameters');
return;
}

const resolution = isFraud ? CONST.CARD_FRAUD_ALERT_RESOLUTION.FRAUD : CONST.CARD_FRAUD_ALERT_RESOLUTION.RECOGNIZED;

const optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS>> = [
const optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS | typeof ONYXKEYS.CARD_LIST>> = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
Expand All @@ -1690,6 +1696,17 @@ function resolveFraudAlert(cardID: number | undefined, isFraud: boolean, reportI
},
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.CARD_LIST,
value: {
[cardID]: {
nameValuePairs: {
possibleFraud: null,
},
},
},
},
];

const successData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS>> = [
Expand All @@ -1704,7 +1721,7 @@ function resolveFraudAlert(cardID: number | undefined, isFraud: boolean, reportI
},
];

const failureData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS>> = [
const failureData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS | typeof ONYXKEYS.CARD_LIST>> = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
Expand All @@ -1718,6 +1735,17 @@ function resolveFraudAlert(cardID: number | undefined, isFraud: boolean, reportI
},
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.CARD_LIST,
value: {
[cardID]: {
nameValuePairs: {
possibleFraud: previousPossibleFraud,
},
},
},
},
];

const parameters: ResolveFraudAlertParams = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import useOnyx from '@hooks/useOnyx';
import {isCard, isCardPendingActivate, isCardPendingIssue, isCardWithCustomZeroLimit, isCardWithPotentialFraud, isExpensifyCard} from '@libs/CardUtils';
import {getUnresolvedCardFraudAlertAction} from '@libs/ReportUtils';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Card} from '@src/types/onyx';

function useTimeSensitiveCards() {
const [cards] = useOnyx(ONYXKEYS.CARD_LIST);
const [allReportActions] = useOnyx(ONYXKEYS.COLLECTION.REPORT_ACTIONS);

const cardsNeedingShippingAddress: Card[] = [];
const cardsNeedingActivation: Card[] = [];
Expand All @@ -19,7 +21,11 @@ function useTimeSensitiveCards() {
continue;
}

if (isCardWithPotentialFraud(card) && card.nameValuePairs?.possibleFraud?.fraudAlertReportID) {
const fraudAlertReportID = card.nameValuePairs?.possibleFraud?.fraudAlertReportID;
const reportActions = fraudAlertReportID ? allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${fraudAlertReportID}`] : undefined;
const hasUnresolvedFraudAction = !!fraudAlertReportID && !!getUnresolvedCardFraudAlertAction(String(fraudAlertReportID), reportActions);

if (isCardWithPotentialFraud(card) && !!fraudAlertReportID && hasUnresolvedFraudAction) {
cardsWithFraud.push(card);
}

Expand Down
9 changes: 7 additions & 2 deletions src/pages/inbox/report/actionContents/FraudAlertContent.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {cardByIdSelector} from '@selectors/Card';
import React from 'react';
import {View} from 'react-native';
import type {ActionableItem} from '@components/ReportActionItem/ActionableItemButtons';
import ActionableItemButtons from '@components/ReportActionItem/ActionableItemButtons';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import {getActionableCardFraudAlertMessage, getOriginalMessage} from '@libs/ReportActionsUtils';
import ReportActionItemBasicMessage from '@pages/inbox/report/ReportActionItemBasicMessage';
import {resolveFraudAlert} from '@userActions/Card';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {ReportAction} from '@src/types/onyx';

type FraudAlertContentProps = {
Expand All @@ -20,6 +23,8 @@ function FraudAlertContent({action, reportID}: FraudAlertContentProps) {
const reportActionID = action?.reportActionID;
const originalMessage = getOriginalMessage(action);
const cardID = originalMessage?.cardID;
const [card] = useOnyx(ONYXKEYS.CARD_LIST, {selector: cardByIdSelector(String(cardID))});
const possibleFraud = card?.nameValuePairs?.possibleFraud ?? null;

const buttons: ActionableItem[] = originalMessage?.resolution
? []
Expand All @@ -28,15 +33,15 @@ function FraudAlertContent({action, reportID}: FraudAlertContentProps) {
text: 'cardPage.cardFraudAlert.confirmButtonText',
key: `${action.reportActionID}-cardFraudAlert-confirm`,
onPress: () => {
resolveFraudAlert(cardID, false, reportID, reportActionID);
resolveFraudAlert(cardID, false, reportID, reportActionID, possibleFraud);
},
isPrimary: true,
},
{
text: 'cardPage.cardFraudAlert.reportFraudButtonText',
key: `${action.reportActionID}-cardFraudAlert-reportFraud`,
onPress: () => {
resolveFraudAlert(cardID, true, reportID, reportActionID);
resolveFraudAlert(cardID, true, reportID, reportActionID, possibleFraud);
},
},
];
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/hooks/useTimeSensitiveCards.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Card, CardList} from '@src/types/onyx';
import {createRandomExpensifyCard} from '../../utils/collections/card';
import createRandomReportAction from '../../utils/collections/reportActions';
import waitForBatchedUpdates from '../../utils/waitForBatchedUpdates';

describe('useTimeSensitiveCards', () => {
Expand Down Expand Up @@ -178,8 +179,15 @@ describe('useTimeSensitiveCards', () => {
possibleFraud: {triggerAmount: 5663, triggerMerchant: 'WAL-MART #2366', currency: 'USD', fraudAlertReportID: 123456},
});
const cardList: CardList = {'1': cardWithFraud};
const unresolvedFraudAction = {
...createRandomReportAction(1),
actionName: CONST.REPORT.ACTIONS.TYPE.ACTIONABLE_CARD_FRAUD_ALERT,
};

await Onyx.merge(ONYXKEYS.CARD_LIST, cardList);
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${cardWithFraud.nameValuePairs?.possibleFraud?.fraudAlertReportID}`, {
[unresolvedFraudAction.reportActionID]: unresolvedFraudAction,
});
await waitForBatchedUpdates();

const {result} = renderHook(() => useTimeSensitiveCards());
Expand Down Expand Up @@ -210,8 +218,15 @@ describe('useTimeSensitiveCards', () => {
possibleFraud: {triggerAmount: 5663, triggerMerchant: 'WAL-MART #2366', currency: 'USD', fraudAlertReportID: 5230242215684213},
});
const cardList: CardList = {'1': cardWithPendingFraudAlert};
const unresolvedFraudAction = {
...createRandomReportAction(2),
actionName: CONST.REPORT.ACTIONS.TYPE.ACTIONABLE_CARD_FRAUD_ALERT,
};

await Onyx.merge(ONYXKEYS.CARD_LIST, cardList);
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${cardWithPendingFraudAlert.nameValuePairs?.possibleFraud?.fraudAlertReportID}`, {
[unresolvedFraudAction.reportActionID]: unresolvedFraudAction,
});
await waitForBatchedUpdates();

const {result} = renderHook(() => useTimeSensitiveCards());
Expand Down Expand Up @@ -283,8 +298,15 @@ describe('useTimeSensitiveCards', () => {
} as Card['nameValuePairs'],
};
const cardList: CardList = {'1': zeroLimitFraudCard};
const unresolvedFraudAction = {
...createRandomReportAction(3),
actionName: CONST.REPORT.ACTIONS.TYPE.ACTIONABLE_CARD_FRAUD_ALERT,
};

await Onyx.merge(ONYXKEYS.CARD_LIST, cardList);
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${zeroLimitFraudCard.nameValuePairs?.possibleFraud?.fraudAlertReportID}`, {
[unresolvedFraudAction.reportActionID]: unresolvedFraudAction,
});
await waitForBatchedUpdates();

const {result} = renderHook(() => useTimeSensitiveCards());
Expand Down
Loading