Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ function ActionContentRouter({
parentReportActionID={report?.parentReportActionID}
actionReportID={action.reportID}
action={action}
originalReport={originalReport}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,57 @@ import {useSession} from '@components/OnyxListItemProvider';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
import {getOriginalMessage} from '@libs/ReportActionsUtils';
import {getOriginalMessage, hasReasoning} from '@libs/ReportActionsUtils';
import ReportActionItemBasicMessage from '@pages/inbox/report/ReportActionItemBasicMessage';
import ReportActionItemMessageWithExplain from '@pages/inbox/report/ReportActionItemMessageWithExplain';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type * as OnyxTypes from '@src/types/onyx';
import type {Report, ReportAction, ReportActions} from '@src/types/onyx';

type ReceiptScanFailedContentProps = {
reportID: string | undefined;
actionReportID: string | undefined;
parentReportID: string | undefined;
parentReportActionID: string | undefined;
reportType: string | undefined;
action: OnyxTypes.ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.RECEIPT_SCAN_FAILED>;
action: ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.RECEIPT_SCAN_FAILED>;

/** Original report from which the given reportAction is first created */
originalReport: OnyxEntry<Report>;
};

function ReceiptScanFailedContent({reportID, actionReportID, parentReportID, parentReportActionID, reportType, action}: ReceiptScanFailedContentProps) {
function ReceiptScanFailedContent({reportID, actionReportID, parentReportID, parentReportActionID, reportType, action, originalReport}: ReceiptScanFailedContentProps) {
const {translate} = useLocalize();
const session = useSession();
// IOU action lives in the IOU report's actions — `report` itself if it's IOU/Expense/Invoice, else its parent.
const isIOUReport = reportType === CONST.REPORT.TYPE.IOU || reportType === CONST.REPORT.TYPE.EXPENSE || reportType === CONST.REPORT.TYPE.INVOICE;
const IOUReportID = isIOUReport ? reportID : parentReportID;

const receiptScanFailedIOUActionDataSelector = (reportActions: OnyxEntry<OnyxTypes.ReportActions>) =>
const receiptScanFailedIOUActionDataSelector = (reportActions: OnyxEntry<ReportActions>) =>
getReceiptScanFailedIOUActionDataSelector(reportActions, isIOUReport, parentReportActionID, actionReportID);
const [receiptScanFailedIOUActionData] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${getNonEmptyStringOnyxID(IOUReportID)}`, {
selector: receiptScanFailedIOUActionDataSelector,
});
const [childReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${getNonEmptyStringOnyxID(action.childReportID)}`);
const {actorAccountID} = receiptScanFailedIOUActionData ?? {};
const canEdit = !!actorAccountID && actorAccountID === session?.accountID;
const missingFields = getOriginalMessage(action)?.missingFields;
const message = translate('violations.smartscanFailed', {canEdit, missingFields});

if (hasReasoning(action)) {
// `AskToExplain` already supplies its own leading period; strip the trailing period
// from the localized message so the rendered string reads "…manually. Explain" not "…manually.. Explain".
return (
<ReportActionItemMessageWithExplain
message={message.replace(/\.\s*$/, '')}
action={action}
childReport={childReport}
originalReport={originalReport}
/>
);
}

return <ReportActionItemBasicMessage message={translate('violations.smartscanFailed', {canEdit, missingFields})} />;
return <ReportActionItemBasicMessage message={message} />;
}

export default ReceiptScanFailedContent;
3 changes: 3 additions & 0 deletions src/types/onyx/OriginalMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ type Decision = {
type OriginalMessageSmartScanFailed = {
/** Fields that are missing */
missingFields: string[];

/** LLM-friendly explanation of the scan failure that activates the Explain button */
reasoning?: string;
};

/** Model of `add comment` report action */
Expand Down
258 changes: 258 additions & 0 deletions tests/ui/ReportActionItemTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,264 @@ describe('ReportActionItem', () => {
expect(screen.getByText(translateLocal('violations.smartscanFailed', {canEdit: false}))).toBeOnTheScreen();
});

it('RECEIPT_SCAN_FAILED action shows Explain link when action has reasoning (submitter)', async () => {
const parentReportID = 'parentReport3';
const parentReportActionID = 'iouAction3';

await act(async () => {
await Onyx.merge(ONYXKEYS.SESSION, {accountID: ACTOR_ACCOUNT_ID});
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, {
[parentReportActionID]: {
reportActionID: parentReportActionID,
actorAccountID: ACTOR_ACCOUNT_ID,
actionName: CONST.REPORT.ACTIONS.TYPE.IOU,
created: '2025-07-12 09:03:17.653',
message: [{type: 'COMMENT', html: '', text: ''}],
originalMessage: {type: CONST.IOU.REPORT_ACTION_TYPE.CREATE, amount: 100, currency: 'USD'},
},
});
});
await waitForBatchedUpdatesWithAct();

const report = {reportID: 'scanReport3', parentReportID, parentReportActionID};
const action = createReportAction(CONST.REPORT.ACTIONS.TYPE.RECEIPT_SCAN_FAILED, {
reasoning: "The date couldn't be read from this receipt.",
});

render(
<ComposeProviders components={[OnyxListItemProvider, LocaleContextProvider, HTMLEngineProvider]}>
<OptionsListContextProvider>
<ScreenWrapper testID="test">
<PortalProvider>
<ReportActionItem
chatReport={undefined}
report={report}
transactionThreadReport={undefined}
parentReportAction={undefined}
action={action}
displayAsGroup={false}
shouldDisplayNewMarker={false}
isFirstVisibleReportAction={false}
/>
</PortalProvider>
</ScreenWrapper>
</OptionsListContextProvider>
</ComposeProviders>,
);
await waitForBatchedUpdatesWithAct();

expect(screen.getByText(translateLocal('violations.smartscanFailed', {canEdit: true}))).toBeOnTheScreen();
expect(screen.getByText('Explain')).toBeOnTheScreen();
});

it('RECEIPT_SCAN_FAILED action shows Explain link when action has reasoning (non-submitter)', async () => {
// Guards both canEdit branches: the Explain affordance must render regardless of
// whether the viewer is the submitter, because hasReasoning() is independent of canEdit.
const parentReportID = 'parentReport4';
const parentReportActionID = 'iouAction4';
const OTHER_ACCOUNT_ID = 999999;

await act(async () => {
await Onyx.merge(ONYXKEYS.SESSION, {accountID: ACTOR_ACCOUNT_ID});
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, {
[parentReportActionID]: {
reportActionID: parentReportActionID,
actorAccountID: OTHER_ACCOUNT_ID,
actionName: CONST.REPORT.ACTIONS.TYPE.IOU,
created: '2025-07-12 09:03:17.653',
message: [{type: 'COMMENT', html: '', text: ''}],
originalMessage: {type: CONST.IOU.REPORT_ACTION_TYPE.CREATE, amount: 100, currency: 'USD'},
},
});
});
await waitForBatchedUpdatesWithAct();

const report = {reportID: 'scanReport4', parentReportID, parentReportActionID};
const action = createReportAction(CONST.REPORT.ACTIONS.TYPE.RECEIPT_SCAN_FAILED, {
reasoning: "The merchant couldn't be read from this receipt.",
});

render(
<ComposeProviders components={[OnyxListItemProvider, LocaleContextProvider, HTMLEngineProvider]}>
<OptionsListContextProvider>
<ScreenWrapper testID="test">
<PortalProvider>
<ReportActionItem
chatReport={undefined}
report={report}
transactionThreadReport={undefined}
parentReportAction={undefined}
action={action}
displayAsGroup={false}
shouldDisplayNewMarker={false}
isFirstVisibleReportAction={false}
/>
</PortalProvider>
</ScreenWrapper>
</OptionsListContextProvider>
</ComposeProviders>,
);
await waitForBatchedUpdatesWithAct();

expect(screen.getByText(translateLocal('violations.smartscanFailed', {canEdit: false}))).toBeOnTheScreen();
expect(screen.getByText('Explain')).toBeOnTheScreen();
});

it('RECEIPT_SCAN_FAILED action does not show Explain link when action has no reasoning (backward compat)', async () => {
// Backward-compat guard: pre-Auth-21123 actions had no `reasoning` field on originalMessage.
// Those actions must continue to render the plain message with no inline Explain affordance.
const parentReportID = 'parentReport5';
const parentReportActionID = 'iouAction5';

await act(async () => {
await Onyx.merge(ONYXKEYS.SESSION, {accountID: ACTOR_ACCOUNT_ID});
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, {
[parentReportActionID]: {
reportActionID: parentReportActionID,
actorAccountID: ACTOR_ACCOUNT_ID,
actionName: CONST.REPORT.ACTIONS.TYPE.IOU,
created: '2025-07-12 09:03:17.653',
message: [{type: 'COMMENT', html: '', text: ''}],
originalMessage: {type: CONST.IOU.REPORT_ACTION_TYPE.CREATE, amount: 100, currency: 'USD'},
},
});
});
await waitForBatchedUpdatesWithAct();

const report = {reportID: 'scanReport5', parentReportID, parentReportActionID};
const action = createReportAction(CONST.REPORT.ACTIONS.TYPE.RECEIPT_SCAN_FAILED, {});

render(
<ComposeProviders components={[OnyxListItemProvider, LocaleContextProvider, HTMLEngineProvider]}>
<OptionsListContextProvider>
<ScreenWrapper testID="test">
<PortalProvider>
<ReportActionItem
chatReport={undefined}
report={report}
transactionThreadReport={undefined}
parentReportAction={undefined}
action={action}
displayAsGroup={false}
shouldDisplayNewMarker={false}
isFirstVisibleReportAction={false}
/>
</PortalProvider>
</ScreenWrapper>
</OptionsListContextProvider>
</ComposeProviders>,
);
await waitForBatchedUpdatesWithAct();

expect(screen.getByText(translateLocal('violations.smartscanFailed', {canEdit: true}))).toBeOnTheScreen();
expect(screen.queryByText('Explain')).not.toBeOnTheScreen();
});

it('RECEIPT_SCAN_FAILED action with missingFields and reasoning shows field-specific message plus Explain', async () => {
const parentReportID = 'parentReport6';
const parentReportActionID = 'iouAction6';

await act(async () => {
await Onyx.merge(ONYXKEYS.SESSION, {accountID: ACTOR_ACCOUNT_ID});
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, {
[parentReportActionID]: {
reportActionID: parentReportActionID,
actorAccountID: ACTOR_ACCOUNT_ID,
actionName: CONST.REPORT.ACTIONS.TYPE.IOU,
created: '2025-07-12 09:03:17.653',
message: [{type: 'COMMENT', html: '', text: ''}],
originalMessage: {type: CONST.IOU.REPORT_ACTION_TYPE.CREATE, amount: 100, currency: 'USD'},
},
});
});
await waitForBatchedUpdatesWithAct();

const report = {reportID: 'scanReport6', parentReportID, parentReportActionID};
const action = createReportAction(CONST.REPORT.ACTIONS.TYPE.RECEIPT_SCAN_FAILED, {
missingFields: ['merchant', 'date'],
reasoning: 'The merchant and date could not be read from this receipt.',
});

render(
<ComposeProviders components={[OnyxListItemProvider, LocaleContextProvider, HTMLEngineProvider]}>
<OptionsListContextProvider>
<ScreenWrapper testID="test">
<PortalProvider>
<ReportActionItem
chatReport={undefined}
report={report}
transactionThreadReport={undefined}
parentReportAction={undefined}
action={action}
displayAsGroup={false}
shouldDisplayNewMarker={false}
isFirstVisibleReportAction={false}
/>
</PortalProvider>
</ScreenWrapper>
</OptionsListContextProvider>
</ComposeProviders>,
);
await waitForBatchedUpdatesWithAct();

// Partial match because the WithExplain branch strips the trailing period
// before the AskToExplain link suffix is rendered into a separate text node.
expect(screen.getByText(/missing merchant and date/)).toBeOnTheScreen();
expect(screen.getByText('Explain')).toBeOnTheScreen();
});

it('RECEIPT_SCAN_FAILED action with reasoning does not produce double period before Explain', async () => {
const parentReportID = 'parentReport7';
const parentReportActionID = 'iouAction7';

await act(async () => {
await Onyx.merge(ONYXKEYS.SESSION, {accountID: ACTOR_ACCOUNT_ID});
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, {
[parentReportActionID]: {
reportActionID: parentReportActionID,
actorAccountID: ACTOR_ACCOUNT_ID,
actionName: CONST.REPORT.ACTIONS.TYPE.IOU,
created: '2025-07-12 09:03:17.653',
message: [{type: 'COMMENT', html: '', text: ''}],
originalMessage: {type: CONST.IOU.REPORT_ACTION_TYPE.CREATE, amount: 100, currency: 'USD'},
},
});
});
await waitForBatchedUpdatesWithAct();

const report = {reportID: 'scanReport7', parentReportID, parentReportActionID};
const action = createReportAction(CONST.REPORT.ACTIONS.TYPE.RECEIPT_SCAN_FAILED, {
missingFields: ['amount'],
reasoning: "The amount couldn't be read from this receipt.",
});

render(
<ComposeProviders components={[OnyxListItemProvider, LocaleContextProvider, HTMLEngineProvider]}>
<OptionsListContextProvider>
<ScreenWrapper testID="test">
<PortalProvider>
<ReportActionItem
chatReport={undefined}
report={report}
transactionThreadReport={undefined}
parentReportAction={undefined}
action={action}
displayAsGroup={false}
shouldDisplayNewMarker={false}
isFirstVisibleReportAction={false}
/>
</PortalProvider>
</ScreenWrapper>
</OptionsListContextProvider>
</ComposeProviders>,
);
await waitForBatchedUpdatesWithAct();

// The rendered text must not contain ".." before "Explain"
expect(screen.getByText('Explain')).toBeOnTheScreen();
expect(document.body.textContent).not.toMatch(/\.\.\s*Explain/);
});

it('HOLD_COMMENT action renders via ReportActionItemBasicMessage', async () => {
const action = createReportAction(CONST.REPORT.ACTIONS.TYPE.HOLD_COMMENT, {});
action.message = [{type: 'COMMENT', html: 'Hold reason text', text: 'Hold reason text'}];
Expand Down
Loading