diff --git a/src/components/Search/SearchFiltersChatsSelector.tsx b/src/components/Search/SearchFiltersChatsSelector.tsx index 34b532b1b58b..1daa8f660556 100644 --- a/src/components/Search/SearchFiltersChatsSelector.tsx +++ b/src/components/Search/SearchFiltersChatsSelector.tsx @@ -74,7 +74,7 @@ function SearchFiltersChatsSelector({initialReportIDs, onFiltersUpdate, isScreen const isReportArchived = !!privateIsArchived; const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${reportData?.policyID}`]; const reportPolicyTags = policyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${getNonEmptyStringOnyxID(report?.policyID)}`]; - const alternateText = getAlternateText(report, {}, isReportArchived, currentUserEmail, policy, {}, undefined, undefined, reportAttributesDerived, reportPolicyTags); + const alternateText = getAlternateText(report, {}, isReportArchived, currentUserAccountID, policy, {}, undefined, undefined, reportAttributesDerived, reportPolicyTags); return {...report, alternateText}; }); diff --git a/src/libs/ModifiedExpenseMessage.ts b/src/libs/ModifiedExpenseMessage.ts index d9ae8647a71a..3067f4cc609a 100644 --- a/src/libs/ModifiedExpenseMessage.ts +++ b/src/libs/ModifiedExpenseMessage.ts @@ -1,10 +1,8 @@ import isEmpty from 'lodash/isEmpty'; -import Onyx from 'react-native-onyx'; -import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; +import type {OnyxEntry} from 'react-native-onyx'; import type {Entries, ValueOf} from 'type-fest'; import type {LocalizedTranslate} from '@components/LocaleContextProvider'; import CONST from '@src/CONST'; -import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type {Policy, PolicyTagLists, Report, ReportAction} from '@src/types/onyx'; import type {PersonalRulesModifiedFields, PolicyRulesModifiedFields} from '@src/types/onyx/OriginalMessage'; @@ -13,12 +11,11 @@ import {getDecodedCategoryName, isCategoryMissing} from './CategoryUtils'; import {convertToDisplayString} from './CurrencyUtils'; import DateUtils from './DateUtils'; import {getEnvironmentURL} from './Environment/Environment'; -// eslint-disable-next-line @typescript-eslint/no-deprecated -import {formatList, translateLocal} from './Localize'; +import {formatList} from './Localize'; import Log from './Log'; import Parser from './Parser'; import {getPersonalDetailByEmail} from './PersonalDetailsUtils'; -import {getCleanedTagName, getCommaSeparatedTagNameWithSanitizedColons, getPolicy, getSortedTagKeys, isPolicyAdmin} from './PolicyUtils'; +import {getCleanedTagName, getCommaSeparatedTagNameWithSanitizedColons, getSortedTagKeys, isPolicyAdmin} from './PolicyUtils'; import {getOriginalMessage, isModifiedExpenseAction} from './ReportActionsUtils'; // This cycle import is safe because ReportNameUtils was extracted from ReportUtils to separate report name computation logic. // The functions imported here are pure utility functions that don't create initialization-time dependencies. @@ -30,20 +27,6 @@ import {getPolicyName, getReportName, getRootParentReport, isPolicyExpenseChat, import {getFormattedAttendees, getTagArrayFromName} from './TransactionUtils'; import {isInvalidMerchantValue} from './ValidationUtils'; -let allPolicyTags: OnyxCollection = {}; -// eslint-disable-next-line @typescript-eslint/no-deprecated -- Onyx.connectWithoutView is being removed in https://github.com/Expensify/App/issues/66336 -Onyx.connectWithoutView({ - key: ONYXKEYS.COLLECTION.POLICY_TAGS, - waitForCollectionCallback: true, - callback: (value) => { - if (!value) { - allPolicyTags = {}; - return; - } - allPolicyTags = value; - }, -}); - let environmentURL: string; getEnvironmentURL().then((url: string) => (environmentURL = url)); @@ -147,7 +130,7 @@ function getForDistanceRequest(translate: LocalizedTranslate, newMerchant: strin }); } -function getForExpenseMovedFromSelfDM(translate: LocalizedTranslate, destinationReport: OnyxEntry, currentUserLogin: string): string { +function getForExpenseMovedFromSelfDM(translate: LocalizedTranslate, destinationReport: OnyxEntry, currentUserLogin: string) { const rootParentReport = getRootParentReport({report: destinationReport}); // In OldDot, expenses could be moved to a self-DM. Return the corresponding message for this case. if (isSelfDM(rootParentReport)) { @@ -261,320 +244,6 @@ function getRulesModifiedMessage( * If we change this function be sure to update the backend as well. */ function getForReportAction({ - reportAction, - policyID, - movedFromReport, - movedToReport, - policyForMovingExpensesID, - currentUserLogin, -}: { - reportAction: OnyxEntry; - policyID: string | undefined; - movedFromReport?: OnyxEntry; - movedToReport?: OnyxEntry; - policyForMovingExpensesID?: string; - currentUserLogin: string; -}): string { - if (!isModifiedExpenseAction(reportAction)) { - return ''; - } - - // eslint-disable-next-line @typescript-eslint/no-deprecated - const movedFromOrToReportMessage = getMovedFromOrToReportMessage(translateLocal, movedFromReport, movedToReport, currentUserLogin); - if (movedFromOrToReportMessage) { - return movedFromOrToReportMessage; - } - - const reportActionOriginalMessage = getOriginalMessage(reportAction); - - const removalFragments: string[] = []; - const setFragments: string[] = []; - const changeFragments: string[] = []; - - const isReportActionOriginalMessageAnObject = reportActionOriginalMessage && typeof reportActionOriginalMessage === 'object'; - const hasModifiedAmount = - isReportActionOriginalMessageAnObject && - 'oldAmount' in reportActionOriginalMessage && - 'oldCurrency' in reportActionOriginalMessage && - 'amount' in reportActionOriginalMessage && - 'currency' in reportActionOriginalMessage; - - const hasModifiedMerchant = isReportActionOriginalMessageAnObject && 'oldMerchant' in reportActionOriginalMessage && 'merchant' in reportActionOriginalMessage; - - if (hasModifiedAmount) { - const oldCurrency = reportActionOriginalMessage?.oldCurrency; - const oldAmountValue = reportActionOriginalMessage?.oldAmount ?? 0; - const oldAmount = convertToDisplayString(oldAmountValue, oldCurrency); - - const currency = reportActionOriginalMessage?.currency; - const amount = convertToDisplayString(reportActionOriginalMessage?.amount ?? 0, currency); - - // Only Distance edits should modify amount and merchant (which stores distance) in a single transaction. - // We check the merchant is in distance format (includes @) as a sanity check - if (hasModifiedMerchant && (reportActionOriginalMessage?.merchant ?? '').includes('@')) { - // eslint-disable-next-line @typescript-eslint/no-deprecated - return getForDistanceRequest(translateLocal, reportActionOriginalMessage?.merchant ?? '', reportActionOriginalMessage?.oldMerchant ?? '', amount, oldAmount); - } - // eslint-disable-next-line @typescript-eslint/no-deprecated - buildMessageFragmentForValue(translateLocal, amount, oldAmount, translateLocal('iou.amount'), false, setFragments, removalFragments, changeFragments); - } - - const hasModifiedComment = isReportActionOriginalMessageAnObject && 'oldComment' in reportActionOriginalMessage && 'newComment' in reportActionOriginalMessage; - if (hasModifiedComment) { - // eslint-disable-next-line @typescript-eslint/no-deprecated - let descriptionLabel = translateLocal('common.description'); - - // Add attribution suffix based on AI-generated descriptions - if (reportActionOriginalMessage?.aiGenerated) { - // eslint-disable-next-line @typescript-eslint/no-deprecated - descriptionLabel += ` ${translateLocal('iou.basedOnAI')}`; - } - - buildMessageFragmentForValue( - // eslint-disable-next-line @typescript-eslint/no-deprecated - translateLocal, - reportActionOriginalMessage?.newComment ?? '', - reportActionOriginalMessage?.oldComment ?? '', - descriptionLabel, - true, - setFragments, - removalFragments, - changeFragments, - ); - } - - if (reportActionOriginalMessage?.oldCreated && reportActionOriginalMessage?.created) { - const formattedOldCreated = DateUtils.formatWithUTCTimeZone(reportActionOriginalMessage.oldCreated, CONST.DATE.FNS_FORMAT_STRING); - buildMessageFragmentForValue( - // eslint-disable-next-line @typescript-eslint/no-deprecated - translateLocal, - reportActionOriginalMessage.created, - formattedOldCreated, - // eslint-disable-next-line @typescript-eslint/no-deprecated - translateLocal('common.date'), - false, - setFragments, - removalFragments, - changeFragments, - ); - } - - if (hasModifiedMerchant) { - buildMessageFragmentForValue( - // eslint-disable-next-line @typescript-eslint/no-deprecated - translateLocal, - reportActionOriginalMessage?.merchant ?? '', - reportActionOriginalMessage?.oldMerchant ?? '', - // eslint-disable-next-line @typescript-eslint/no-deprecated - translateLocal('common.merchant'), - true, - setFragments, - removalFragments, - changeFragments, - ); - } - - const hasModifiedCategory = isReportActionOriginalMessageAnObject && 'oldCategory' in reportActionOriginalMessage && 'category' in reportActionOriginalMessage; - if (hasModifiedCategory) { - // eslint-disable-next-line @typescript-eslint/no-deprecated - let categoryLabel = translateLocal('common.category').toLowerCase(); - - // Add attribution suffix based on source - if (reportActionOriginalMessage?.source === CONST.CATEGORY_SOURCE.AI) { - // eslint-disable-next-line @typescript-eslint/no-deprecated - categoryLabel += ` ${translateLocal('iou.basedOnAI')}`; - } else if (reportActionOriginalMessage?.source === CONST.CATEGORY_SOURCE.MCC) { - // eslint-disable-next-line @typescript-eslint/no-deprecated - const policy = getPolicy(policyID); - const isAdmin = isPolicyAdmin(policy, currentUserLogin); - - // For admins, create a hyperlink to the workspace rules page - if (isAdmin && policy?.id) { - const rulesLink = `${environmentURL}/${ROUTES.WORKSPACE_RULES.getRoute(policy.id)}`; - // eslint-disable-next-line @typescript-eslint/no-deprecated - categoryLabel += ` ${translateLocal('iou.basedOnMCC', {rulesLink})}`; - } else { - // eslint-disable-next-line @typescript-eslint/no-deprecated - categoryLabel += ` ${translateLocal('iou.basedOnMCC', {rulesLink: ''})}`; - } - } - - buildMessageFragmentForValue( - // eslint-disable-next-line @typescript-eslint/no-deprecated - translateLocal, - getDecodedCategoryName(reportActionOriginalMessage?.category ?? ''), - getDecodedCategoryName(reportActionOriginalMessage?.oldCategory ?? ''), - categoryLabel, - true, - setFragments, - removalFragments, - changeFragments, - // Don't convert to lowercase when we have source attribution (to preserve any HTML links) - false, - ); - } - - const hasModifiedTag = isReportActionOriginalMessageAnObject && 'oldTag' in reportActionOriginalMessage && 'tag' in reportActionOriginalMessage; - if (hasModifiedTag) { - const policyIDForTags = policyID === CONST.POLICY.OWNER_EMAIL_FAKE && policyForMovingExpensesID ? policyForMovingExpensesID : policyID; - const policyTags = allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyIDForTags}`] ?? CONST.POLICY.DEFAULT_TAG_LIST; - const transactionTag = reportActionOriginalMessage?.tag ?? ''; - const oldTransactionTag = reportActionOriginalMessage?.oldTag ?? ''; - const splittedTag = getTagArrayFromName(transactionTag); - const splittedOldTag = getTagArrayFromName(oldTransactionTag); - // eslint-disable-next-line @typescript-eslint/no-deprecated - const localizedTagListName = translateLocal('common.tag'); - const sortedTagKeys = getSortedTagKeys(policyTags); - - for (const [index, policyTagKey] of sortedTagKeys.entries()) { - const policyTagListName = policyTags[policyTagKey].name || localizedTagListName; - - const newTag = splittedTag.at(index) ?? ''; - const oldTag = splittedOldTag.at(index) ?? ''; - - if (newTag !== oldTag) { - buildMessageFragmentForValue( - // eslint-disable-next-line @typescript-eslint/no-deprecated - translateLocal, - getCleanedTagName(newTag), - getCleanedTagName(oldTag), - policyTagListName, - true, - setFragments, - removalFragments, - changeFragments, - policyTagListName === localizedTagListName, - ); - } - } - } - - const hasModifiedTaxAmount = isReportActionOriginalMessageAnObject && 'oldTaxAmount' in reportActionOriginalMessage && 'taxAmount' in reportActionOriginalMessage; - if (hasModifiedTaxAmount) { - const currency = reportActionOriginalMessage?.currency; - - const taxAmount = convertToDisplayString(getTaxAmountAbsValue(reportActionOriginalMessage?.taxAmount ?? 0), currency); - const oldTaxAmountValue = getTaxAmountAbsValue(reportActionOriginalMessage?.oldTaxAmount ?? 0); - const oldTaxAmount = convertToDisplayString(oldTaxAmountValue, currency); - // eslint-disable-next-line @typescript-eslint/no-deprecated - buildMessageFragmentForValue(translateLocal, taxAmount, oldTaxAmount, translateLocal('iou.taxAmount'), false, setFragments, removalFragments, changeFragments); - } - - const hasModifiedTaxRate = isReportActionOriginalMessageAnObject && 'oldTaxRate' in reportActionOriginalMessage && 'taxRate' in reportActionOriginalMessage; - if (hasModifiedTaxRate) { - buildMessageFragmentForValue( - // eslint-disable-next-line @typescript-eslint/no-deprecated - translateLocal, - reportActionOriginalMessage?.taxRate ?? '', - reportActionOriginalMessage?.oldTaxRate ?? '', - // eslint-disable-next-line @typescript-eslint/no-deprecated - translateLocal('iou.taxRate'), - false, - setFragments, - removalFragments, - changeFragments, - ); - } - - const hasModifiedBillable = isReportActionOriginalMessageAnObject && 'oldBillable' in reportActionOriginalMessage && 'billable' in reportActionOriginalMessage; - if (hasModifiedBillable) { - // eslint-disable-next-line @typescript-eslint/no-deprecated - const oldBillable = reportActionOriginalMessage?.oldBillable === 'billable' ? translateLocal('common.billable').toLowerCase() : translateLocal('common.nonBillable').toLowerCase(); - // eslint-disable-next-line @typescript-eslint/no-deprecated - const newBillable = reportActionOriginalMessage?.billable === 'billable' ? translateLocal('common.billable').toLowerCase() : translateLocal('common.nonBillable').toLowerCase(); - buildMessageFragmentForValue( - // eslint-disable-next-line @typescript-eslint/no-deprecated - translateLocal, - newBillable, - oldBillable, - // eslint-disable-next-line @typescript-eslint/no-deprecated - translateLocal('iou.expense'), - true, - setFragments, - removalFragments, - changeFragments, - ); - } - - const hasModifiedReimbursable = isReportActionOriginalMessageAnObject && 'oldReimbursable' in reportActionOriginalMessage && 'reimbursable' in reportActionOriginalMessage; - if (hasModifiedReimbursable) { - const oldReimbursable = - // eslint-disable-next-line @typescript-eslint/no-deprecated - reportActionOriginalMessage?.oldReimbursable === 'reimbursable' ? translateLocal('iou.reimbursable').toLowerCase() : translateLocal('iou.nonReimbursable').toLowerCase(); - const newReimbursable = - // eslint-disable-next-line @typescript-eslint/no-deprecated - reportActionOriginalMessage?.reimbursable === 'reimbursable' ? translateLocal('iou.reimbursable').toLowerCase() : translateLocal('iou.nonReimbursable').toLowerCase(); - buildMessageFragmentForValue( - // eslint-disable-next-line @typescript-eslint/no-deprecated - translateLocal, - newReimbursable, - oldReimbursable, - // eslint-disable-next-line @typescript-eslint/no-deprecated - translateLocal('iou.expense'), - true, - setFragments, - removalFragments, - changeFragments, - ); - } - - const hasModifiedAttendees = isReportActionOriginalMessageAnObject && 'oldAttendees' in reportActionOriginalMessage && 'newAttendees' in reportActionOriginalMessage; - if (hasModifiedAttendees) { - const [oldAttendees, attendees] = getFormattedAttendees(reportActionOriginalMessage.newAttendees, reportActionOriginalMessage.oldAttendees); - // eslint-disable-next-line @typescript-eslint/no-deprecated - buildMessageFragmentForValue(translateLocal, oldAttendees, attendees, translateLocal('iou.attendees'), false, setFragments, removalFragments, changeFragments); - } - - const hasPersonalRulesModifiedFields = isReportActionOriginalMessageAnObject && 'personalRulesModifiedFields' in reportActionOriginalMessage; - if (hasPersonalRulesModifiedFields) { - const personalRulesModifiedFields = reportActionOriginalMessage.personalRulesModifiedFields; - if (personalRulesModifiedFields) { - // eslint-disable-next-line @typescript-eslint/no-deprecated - return getRulesModifiedMessage(translateLocal, personalRulesModifiedFields, true); - } - } - - const hasPolicyRulesModifiedFields = isReportActionOriginalMessageAnObject && 'policyRulesModifiedFields' in reportActionOriginalMessage && 'policyID' in reportActionOriginalMessage; - if (hasPolicyRulesModifiedFields) { - const rulePolicyID = reportActionOriginalMessage.policyID; - const policyRulesModifiedFields = reportActionOriginalMessage.policyRulesModifiedFields; - - if (policyRulesModifiedFields && rulePolicyID) { - // eslint-disable-next-line @typescript-eslint/no-deprecated - const rulePolicy = getPolicy(rulePolicyID); - const hasPolicyRuleAccess = !!rulePolicy?.areRulesEnabled && isPolicyAdmin(rulePolicy, currentUserLogin); - // eslint-disable-next-line @typescript-eslint/no-deprecated - return getRulesModifiedMessage(translateLocal, policyRulesModifiedFields, false, rulePolicyID, hasPolicyRuleAccess); - } - } - - const message = - // eslint-disable-next-line @typescript-eslint/no-deprecated - getMessageLine(translateLocal, `\n${translateLocal('iou.changed')}`, changeFragments) + - // eslint-disable-next-line @typescript-eslint/no-deprecated - getMessageLine(translateLocal, `\n${translateLocal('iou.set')}`, setFragments) + - // eslint-disable-next-line @typescript-eslint/no-deprecated - getMessageLine(translateLocal, `\n${translateLocal('iou.removed')}`, removalFragments); - if (message === '') { - // If we don't have enough structured information to build a detailed message but we - // know the change was AI-generated, fall back to an AI-attributed generic summary so - // users can still understand that Concierge updated the expense automatically. - if (reportActionOriginalMessage?.aiGenerated) { - // eslint-disable-next-line @typescript-eslint/no-deprecated - return `${translateLocal('iou.changedTheExpense')} ${translateLocal('iou.basedOnAI')}`; - } - - // eslint-disable-next-line @typescript-eslint/no-deprecated - return translateLocal('iou.changedTheExpense'); - } - return `${message.substring(1, message.length)}`; -} - -/** - * Temporary function with same implementation as getForReportAction but without policyID and with policyTags - * to gradually migrate from Onyx.connect - */ -function getForReportActionTemp({ translate, reportAction, policy, @@ -588,7 +257,10 @@ function getForReportActionTemp({ policy?: OnyxEntry; movedFromReport?: OnyxEntry; movedToReport?: OnyxEntry; - policyTags: OnyxEntry; + // Optional because the deprecated getReportName in ReportUtils.ts calls this without policyTags. + // getReportName itself will be migrated away from Onyx.connect in a follow-up PR. + // See https://github.com/Expensify/App/pull/75562 + policyTags?: OnyxEntry; currentUserLogin: string; }): string { if (!isModifiedExpenseAction(reportAction)) { @@ -643,8 +315,8 @@ function getForReportActionTemp({ buildMessageFragmentForValue( translate, - reportActionOriginalMessage?.newComment ?? '', - reportActionOriginalMessage?.oldComment ?? '', + Parser.htmlToMarkdown(reportActionOriginalMessage?.newComment ?? ''), + Parser.htmlToMarkdown(reportActionOriginalMessage?.oldComment ?? ''), descriptionLabel, true, setFragments, @@ -817,4 +489,4 @@ function getForReportActionTemp({ return `${message.substring(1, message.length)}`; } -export {getForReportAction, getMovedReportID, getMovedFromOrToReportMessage, getForReportActionTemp}; +export {getForReportAction, getMovedReportID, getMovedFromOrToReportMessage}; diff --git a/src/libs/Notification/LocalNotification/BrowserNotifications.ts b/src/libs/Notification/LocalNotification/BrowserNotifications.ts index a237a854efd0..71819a40aaab 100644 --- a/src/libs/Notification/LocalNotification/BrowserNotifications.ts +++ b/src/libs/Notification/LocalNotification/BrowserNotifications.ts @@ -5,7 +5,7 @@ import EXPENSIFY_ICON_URL from '@assets/images/expensify-logo-round-clearspace.p import * as AppUpdate from '@libs/actions/AppUpdate'; // eslint-disable-next-line @typescript-eslint/no-deprecated -- translateLocal is deprecated; BrowserNotifications is non-React code that cannot use the translate hook import {translateLocal} from '@libs/Localize'; -import {getForReportActionTemp} from '@libs/ModifiedExpenseMessage'; +import {getForReportAction} from '@libs/ModifiedExpenseMessage'; import {getTextFromHtml} from '@libs/ReportActionsUtils'; import * as ReportUtils from '@libs/ReportUtils'; import playSound, {SOUNDS} from '@libs/Sound'; @@ -144,7 +144,7 @@ export default { currentUserLogin, }: LocalNotificationModifiedExpensePushParams) { const title = reportAction.person?.map((f) => f.text).join(', ') ?? ''; - const bodyWithHTML = getForReportActionTemp({ + const bodyWithHTML = getForReportAction({ // eslint-disable-next-line @typescript-eslint/no-deprecated -- translateLocal is deprecated; BrowserNotifications is non-React code that cannot use the translate hook translate: translateLocal, reportAction, diff --git a/src/libs/OptionsListUtils/index.ts b/src/libs/OptionsListUtils/index.ts index 3d6fe65537de..30486acf9285 100644 --- a/src/libs/OptionsListUtils/index.ts +++ b/src/libs/OptionsListUtils/index.ts @@ -19,7 +19,7 @@ import {translateLocal} from '@libs/Localize'; import {appendCountryCode, getPhoneNumberWithoutSpecialChars} from '@libs/LoginUtils'; import {MaxHeap} from '@libs/MaxHeap'; import {MinHeap} from '@libs/MinHeap'; -import {getForReportAction, getForReportActionTemp} from '@libs/ModifiedExpenseMessage'; +import {getForReportAction} from '@libs/ModifiedExpenseMessage'; import Navigation from '@libs/Navigation/Navigation'; import Parser from '@libs/Parser'; import Permissions from '@libs/Permissions'; @@ -96,7 +96,6 @@ import { isPendingRemove, isReimbursementDeQueuedOrCanceledAction, isReimbursementQueuedAction, - isRejectedAction, isRenamedAction, isReportActionVisible, isReportPreviewAction, @@ -421,7 +420,7 @@ function getAlternateText( option: OptionData, {showChatPreviewLine = false, forcePolicyNamePreview = false}: PreviewConfig, isReportArchived: boolean | undefined, - currentUserLogin: string, + currentUserAccountID: number, // We'll make it required in the next PR. Ref: https://github.com/Expensify/App/issues/66415 policy?: OnyxEntry, lastActorDetails: Partial | null = {}, @@ -448,7 +447,6 @@ function getAlternateText( visibleReportActionsDataParam: visibleReportActionsData, reportAttributesDerived, policyTags, - currentUserLogin, }); const reportPrefix = getReportSubtitlePrefix(report); const formattedLastMessageTextWithPrefix = reportPrefix + formattedLastMessageText; @@ -594,7 +592,6 @@ function getLastMessageTextForReport({ movedToReport, policy, isReportArchived = false, - policyForMovingExpensesID, reportMetadata, visibleReportActionsDataParam, lastAction, @@ -615,7 +612,7 @@ function getLastMessageTextForReport({ lastAction?: OnyxEntry; reportAttributesDerived?: ReportAttributesDerivedValue['reports']; policyTags?: OnyxEntry; - currentUserLogin: string; + currentUserLogin?: string; }): string { const reportID = report?.reportID; const canUserPerformWrite = canUserPerformWriteAction(report, isReportArchived); @@ -725,24 +722,15 @@ function getLastMessageTextForReport({ } else if (isReportMessageAttachment({text: report?.lastMessageText ?? '', html: report?.lastMessageHtml, type: ''})) { lastMessageTextFromReport = `[${translate('common.attachment')}]`; } else if (isModifiedExpenseAction(lastReportAction)) { - const properSchemaForModifiedExpenseMessageWithHTML = policyTags - ? getForReportActionTemp({ - translate, - reportAction: lastReportAction, - policy, - movedFromReport, - movedToReport, - policyTags, - currentUserLogin, - }) - : getForReportAction({ - reportAction: lastReportAction, - policyID: report?.policyID, - movedFromReport, - movedToReport, - policyForMovingExpensesID, - currentUserLogin, - }); + const properSchemaForModifiedExpenseMessageWithHTML = getForReportAction({ + translate, + reportAction: lastReportAction, + policy, + movedFromReport, + movedToReport, + policyTags, + currentUserLogin: currentUserLogin ?? '', + }); // Strip HTML tags for plain text display in options list const properSchemaForModifiedExpenseMessage = Parser.htmlToText(properSchemaForModifiedExpenseMessageWithHTML); lastMessageTextFromReport = formatReportLastMessageText(properSchemaForModifiedExpenseMessage, true); @@ -791,7 +779,7 @@ function getLastMessageTextForReport({ } else { lastMessageTextFromReport = translate('iou.forwarded'); } - } else if (isRejectedAction(lastReportAction)) { + } else if (lastReportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.REJECTED) { lastMessageTextFromReport = translate('iou.rejectedThisReport'); } else if (lastReportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.POLICY_CHANGE_LOG.CORPORATE_UPGRADE) { lastMessageTextFromReport = translate('workspaceActions.upgradedWorkspace'); @@ -1032,7 +1020,6 @@ function createOption( // If displaying chat preview line is needed, let's overwrite the default alternate text const lastActorDetails = personalDetails?.[report?.lastActorAccountID ?? String(CONST.DEFAULT_NUMBER_ID)] ?? {}; - const currentUserLogin = personalDetails?.[currentUserAccountID]?.login ?? ''; // eslint-disable-next-line @typescript-eslint/no-deprecated const translateFn = translate ?? translateLocal; result.lastMessageText = getLastMessageTextForReport({ @@ -1043,7 +1030,6 @@ function createOption( visibleReportActionsDataParam: visibleReportActionsData, reportAttributesDerived, policyTags, - currentUserLogin, }); result.alternateText = showPersonalDetails && personalDetail?.login @@ -1052,7 +1038,7 @@ function createOption( result, {showChatPreviewLine, forcePolicyNamePreview}, !!result.private_isArchived, - currentUserLogin, + currentUserAccountID, // TODO: Remove this in the next PR that will refactor prepareReportOptionsForDisplay. Ref: https://github.com/Expensify/App/issues/66415 undefined, lastActorDetails, @@ -2236,7 +2222,7 @@ function isValidReport(option: SearchOption, policy: OnyxEntry, function prepareReportOptionsForDisplay( options: Array>, policiesCollection: OnyxCollection, - currentUserLogin: string, + currentUserAccountID: number, config: GetValidReportsConfig, visibleReportActionsData: VisibleReportActionsDerivedValue = {}, reportAttributesDerived?: ReportAttributesDerivedValue['reports'], @@ -2277,7 +2263,7 @@ function prepareReportOptionsForDisplay( option, {showChatPreviewLine, forcePolicyNamePreview}, !!option.private_isArchived, - currentUserLogin, + currentUserAccountID, // TODO: Remove this in the next PR that will refactor prepareReportOptionsForDisplay. Ref: https://github.com/Expensify/App/issues/66415 undefined, null, @@ -2522,7 +2508,7 @@ function getValidOptions( selfDMChat = prepareReportOptionsForDisplay( selfDMChats, policiesCollection, - currentUserEmail, + currentUserAccountID, { ...getValidReportsConfig, selectedOptions, @@ -2544,7 +2530,7 @@ function getValidOptions( recentReportOptions = prepareReportOptionsForDisplay( recentReportOptions, policiesCollection, - currentUserEmail, + currentUserAccountID, { ...getValidReportsConfig, selectedOptions, @@ -2562,7 +2548,7 @@ function getValidOptions( workspaceChats = prepareReportOptionsForDisplay( workspaceChats, policiesCollection, - currentUserEmail, + currentUserAccountID, { ...getValidReportsConfig, selectedOptions, diff --git a/src/libs/ReportNameUtils.ts b/src/libs/ReportNameUtils.ts index 909a05d6b99f..bfc3ffd2ebd5 100644 --- a/src/libs/ReportNameUtils.ts +++ b/src/libs/ReportNameUtils.ts @@ -27,7 +27,7 @@ import {formatPhoneNumber as formatPhoneNumberPhoneUtils} from './LocalePhoneNum // eslint-disable-next-line @typescript-eslint/no-deprecated import {translateLocal} from './Localize'; // eslint-disable-next-line import/no-cycle -import {getForReportAction, getForReportActionTemp, getMovedReportID} from './ModifiedExpenseMessage'; +import {getForReportAction, getMovedReportID} from './ModifiedExpenseMessage'; import Parser from './Parser'; import {getDisplayNameOrDefault} from './PersonalDetailsUtils'; import {getCleanedTagName, isPolicyAdmin, isPolicyFieldListEmpty} from './PolicyUtils'; @@ -747,27 +747,17 @@ function computeChatThreadReportName( return generateArchivedReportName(reportActionMessage); } if (!isEmptyObject(parentReportAction) && isModifiedExpenseAction(parentReportAction)) { - const policyID = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${report?.reportID}`]?.policyID; - const movedFromReport = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${getMovedReportID(parentReportAction, CONST.REPORT.MOVE_TYPE.FROM)}`]; const movedToReport = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${getMovedReportID(parentReportAction, CONST.REPORT.MOVE_TYPE.TO)}`]; - const modifiedMessageWithHTML = policyTags - ? getForReportActionTemp({ - translate, - reportAction: parentReportAction, - movedFromReport, - movedToReport, - policyTags, - policy, - currentUserLogin, - }) - : getForReportAction({ - reportAction: parentReportAction, - policyID, - movedFromReport, - movedToReport, - currentUserLogin, - }); + const modifiedMessageWithHTML = getForReportAction({ + translate, + reportAction: parentReportAction, + movedFromReport, + movedToReport, + policyTags, + policy, + currentUserLogin, + }); // Strip HTML tags for plain text display in report previews const modifiedMessage = Parser.htmlToText(modifiedMessageWithHTML); return formatReportLastMessageText(modifiedMessage); diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index bf7da4cfcdd0..fc0b902aa73c 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -5912,13 +5912,12 @@ function getReportName(reportNameInformation: GetReportNameParams): string { return generateArchivedReportName(reportActionMessage); } if (!isEmptyObject(parentReportAction) && isModifiedExpenseAction(parentReportAction)) { - const policyID = report?.policyID; - const movedFromReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${getMovedReportID(parentReportAction, CONST.REPORT.MOVE_TYPE.FROM)}`]; const movedToReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${getMovedReportID(parentReportAction, CONST.REPORT.MOVE_TYPE.TO)}`]; const modifiedMessageWithHTML = getForReportAction({ + // eslint-disable-next-line @typescript-eslint/no-deprecated -- translateLocal is deprecated; getReportName is non-React code that cannot use the translate hook + translate: translateLocal, reportAction: parentReportAction, - policyID, movedFromReport, movedToReport, // Temporarily retrieves current user email from getCurrentUserEmail, since getReportName is deprecated and no longer requires this parameter to be passed explicitly. diff --git a/src/libs/actions/OnyxDerived/configs/reportAttributes.ts b/src/libs/actions/OnyxDerived/configs/reportAttributes.ts index 0e51a8278c03..5608c2b9ce0a 100644 --- a/src/libs/actions/OnyxDerived/configs/reportAttributes.ts +++ b/src/libs/actions/OnyxDerived/configs/reportAttributes.ts @@ -117,6 +117,8 @@ export default createOnyxDerivedValueConfig({ const reportNameValuePairsUpdates = sourceValues?.[ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS] ?? {}; const transactionsUpdates = sourceValues?.[ONYXKEYS.COLLECTION.TRANSACTION]; const transactionViolationsUpdates = sourceValues?.[ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS]; + const policyTagsUpdates = sourceValues?.[ONYXKEYS.COLLECTION.POLICY_TAGS]; + let dataToIterate = Object.keys(reports); // check if there are any report-related updates @@ -144,7 +146,7 @@ export default createOnyxDerivedValueConfig({ if (useIncrementalUpdates) { // if there are report-related updates, iterate over the updates - if (updates.length > 0 || !!transactionsUpdates || !!transactionViolationsUpdates) { + if (updates.length > 0 || !!transactionsUpdates || !!transactionViolationsUpdates || !!policyTagsUpdates) { if (updates.length > 0) { dataToIterate = prepareReportKeys(updates); @@ -183,6 +185,13 @@ export default createOnyxDerivedValueConfig({ } dataToIterate.push(...prepareReportKeys(transactionReportIDs)); } + if (policyTagsUpdates) { + const changedPolicyIDs = new Set(Object.keys(policyTagsUpdates).map((key) => key.replace(ONYXKEYS.COLLECTION.POLICY_TAGS, ''))); + const affectedReportKeys = Object.values(reports) + .filter((report) => !!report?.policyID && changedPolicyIDs.has(report.policyID)) + .map((report) => `${ONYXKEYS.COLLECTION.REPORT}${report?.reportID}`); + dataToIterate.push(...prepareReportKeys(affectedReportKeys)); + } } else { // No updates to process, return current value to prevent unnecessary computation return currentValue ?? {reports: {}, locale: null}; diff --git a/src/pages/inbox/report/ContextMenu/ContextMenuActions.tsx b/src/pages/inbox/report/ContextMenu/ContextMenuActions.tsx index 57609718b924..87d679e61d85 100644 --- a/src/pages/inbox/report/ContextMenu/ContextMenuActions.tsx +++ b/src/pages/inbox/report/ContextMenu/ContextMenuActions.tsx @@ -18,7 +18,7 @@ import {getEnvironmentURL} from '@libs/Environment/Environment'; import fileDownload from '@libs/fileDownload'; import getAttachmentDetails from '@libs/fileDownload/getAttachmentDetails'; import {formatPhoneNumber as formatPhoneNumberPhoneUtils} from '@libs/LocalePhoneNumber'; -import {getForReportActionTemp} from '@libs/ModifiedExpenseMessage'; +import {getForReportAction} from '@libs/ModifiedExpenseMessage'; import Navigation from '@libs/Navigation/Navigation'; import Parser from '@libs/Parser'; import {getCleanedTagName, isPolicyAdmin} from '@libs/PolicyUtils'; @@ -774,7 +774,7 @@ const ContextMenuActions: ContextMenuAction[] = [ const displayMessage = html ?? text; setClipboardMessage(displayMessage); } else if (isModifiedExpenseAction(reportAction)) { - const modifyExpenseMessageWithHTML = getForReportActionTemp({ + const modifyExpenseMessageWithHTML = getForReportAction({ translate, reportAction, policy, diff --git a/src/pages/inbox/report/ReportActionItem.tsx b/src/pages/inbox/report/ReportActionItem.tsx index 53fe3d633593..3b4c4f49fcc2 100644 --- a/src/pages/inbox/report/ReportActionItem.tsx +++ b/src/pages/inbox/report/ReportActionItem.tsx @@ -9,7 +9,7 @@ import usePolicyForMovingExpenses from '@hooks/usePolicyForMovingExpenses'; import useReportIsArchived from '@hooks/useReportIsArchived'; import useReportTransactions from '@hooks/useReportTransactions'; import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID'; -import {getForReportActionTemp, getMovedReportID} from '@libs/ModifiedExpenseMessage'; +import {getForReportAction, getMovedReportID} from '@libs/ModifiedExpenseMessage'; import {getIOUReportIDFromReportActionPreview, getOriginalMessage, isMoneyRequestAction} from '@libs/ReportActionsUtils'; import { chatIncludesChronosWithID, @@ -169,7 +169,7 @@ function ReportActionItem({ action as OnyxEntry>, report, )} - modifiedExpenseMessage={getForReportActionTemp({ + modifiedExpenseMessage={getForReportAction({ translate, reportAction: action, policy, diff --git a/tests/perf-test/ModifiedExpenseMessage.perf-test.ts b/tests/perf-test/ModifiedExpenseMessage.perf-test.ts index 25127123ccd0..5b6b2c363cb2 100644 --- a/tests/perf-test/ModifiedExpenseMessage.perf-test.ts +++ b/tests/perf-test/ModifiedExpenseMessage.perf-test.ts @@ -2,21 +2,27 @@ import {randAmount} from '@ngneat/falso'; import Onyx from 'react-native-onyx'; import {measureFunction} from 'reassure'; import CONST from '@src/CONST'; +import IntlStore from '@src/languages/IntlStore'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Policy, Report} from '@src/types/onyx'; import {getForReportAction} from '../../src/libs/ModifiedExpenseMessage'; import createCollection from '../utils/collections/createCollection'; import createRandomPolicy from '../utils/collections/policies'; +import createRandomPolicyTags from '../utils/collections/policyTags'; import createRandomReportAction from '../utils/collections/reportActions'; import {createRandomReport} from '../utils/collections/reports'; +import {translateLocal} from '../utils/TestHelper'; import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; -beforeAll(() => - Onyx.init({ +const CURRENT_USER_LOGIN = 'test@example.com'; + +beforeAll(() => { + IntlStore.load(CONST.LOCALES.EN); + return Onyx.init({ keys: ONYXKEYS, evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS], - }), -); + }); +}); // Clear out Onyx after each test so that each test starts with a clean state afterEach(() => { @@ -39,9 +45,9 @@ const getMockedPolicies = (length = 500) => const mockedReportsMap = getMockedReports(1000) as Record<`${typeof ONYXKEYS.COLLECTION.REPORT}`, Report>; const mockedPoliciesMap = getMockedPolicies(1000) as Record<`${typeof ONYXKEYS.COLLECTION.POLICY}`, Policy>; +const mockedPolicyTags = createRandomPolicyTags('Department', 5); test('[ModifiedExpenseMessage] getForReportAction on 1k reports and policies', async () => { - const report = createRandomReport(1, undefined); const reportAction = { ...createRandomReportAction(1), actionName: CONST.REPORT.ACTIONS.TYPE.MODIFIED_EXPENSE, @@ -59,5 +65,5 @@ test('[ModifiedExpenseMessage] getForReportAction on 1k reports and policies', a }); await waitForBatchedUpdates(); - await measureFunction(() => getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: 'test@example.com'})); + await measureFunction(() => getForReportAction({translate: translateLocal, reportAction, policyTags: mockedPolicyTags, currentUserLogin: CURRENT_USER_LOGIN})); }); diff --git a/tests/unit/ModifiedExpenseMessageTest.ts b/tests/unit/ModifiedExpenseMessageTest.ts index b47f2dcb4c90..15ce68ddcbae 100644 --- a/tests/unit/ModifiedExpenseMessageTest.ts +++ b/tests/unit/ModifiedExpenseMessageTest.ts @@ -1,6 +1,6 @@ import Onyx from 'react-native-onyx'; import {getEnvironmentURL} from '@libs/Environment/Environment'; -import {getForReportAction, getForReportActionTemp, getMovedFromOrToReportMessage, getMovedReportID} from '@libs/ModifiedExpenseMessage'; +import {getForReportAction, getMovedFromOrToReportMessage, getMovedReportID} from '@libs/ModifiedExpenseMessage'; // eslint-disable-next-line no-restricted-syntax -- this is required to allow mocking import * as PolicyUtils from '@libs/PolicyUtils'; // eslint-disable-next-line no-restricted-syntax, no-restricted-imports -- this is required to allow mocking @@ -197,7 +197,6 @@ describe('ModifiedExpenseMessage', () => { }); describe('getForAction', () => { - const report = createRandomReport(1, undefined); describe('when the amount is changed', () => { const reportAction = { ...createRandomReportAction(1), @@ -213,7 +212,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message', () => { const expectedResult = `changed the amount to $18.00 (previously $12.55)`; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -234,7 +233,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message', () => { const expectedResult = `changed the amount to $18.00 (previously $0.00)`; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -257,7 +256,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message', () => { const expectedResult = 'changed the amount to $18.00 (previously $12.55)\nremoved the description (previously "this is for the shuttle")'; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -282,7 +281,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message', () => { const expectedResult = 'changed the amount to $18.00 (previously $12.55)\nset the category to "Benefits"\nremoved the description (previously "this is for the shuttle")'; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -305,7 +304,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message', () => { const expectedResult = 'changed the amount to $18.00 (previously $12.55) and the merchant to "Taco Bell" (previously "Big Belly")'; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -333,7 +332,7 @@ describe('ModifiedExpenseMessage', () => { const expectedResult = 'changed the amount to $18.00 (previously $12.55) and the merchant to "Taco Bell" (previously "Big Belly")\nset the category to "Benefits"\nremoved the description (previously "this is for the shuttle")'; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -359,7 +358,7 @@ describe('ModifiedExpenseMessage', () => { const expectedResult = 'changed the amount to $18.00 (previously $12.55), the description to "I bought it on the way" (previously "from the business trip"), and the merchant to "Taco Bell" (previously "Big Belly")'; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -378,7 +377,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message', () => { const expectedResult = `removed the merchant (previously "Big Belly")`; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -397,7 +396,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message', () => { const expectedResult = `set the merchant to "KFC"`; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -416,7 +415,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message', () => { const expectedResult = `set the merchant to "KFC"`; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -435,7 +434,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct "removed" text message', () => { const expectedResult = `removed the merchant (previously "Old Merchant")`; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -454,7 +453,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct "changed" text message', () => { const expectedResult = `changed the merchant to "New Merchant" (previously "Old Merchant")`; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -475,7 +474,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message', () => { const expectedResult = `removed the description (previously "mini shore") and the merchant (previously "Big Belly")`; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -498,7 +497,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message', () => { const expectedResult = `removed the description (previously "mini shore"), the merchant (previously "Big Belly"), and the category (previously "Benefits")`; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -517,7 +516,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message', () => { const expectedResult = `set the merchant to "Big Belly"`; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -538,7 +537,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message', () => { const expectedResult = `set the description to "mini shore" and the merchant to "Big Belly"`; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -561,7 +560,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message', () => { const expectedResult = `set the description to "mini shore", the merchant to "Big Belly", and the category to "Benefits"`; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -580,7 +579,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message', () => { const expectedResult = 'changed the date to 2023-12-27 (previously 2023-12-26)'; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -598,7 +597,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message', () => { const expectedResult = 'changed the expense'; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -620,7 +619,7 @@ describe('ModifiedExpenseMessage', () => { it('then the message says the distance is changed and shows the new and old merchant and amount', () => { const expectedResult = `changed the distance to ${reportAction.originalMessage.merchant} (previously ${reportAction.originalMessage.oldMerchant}), which updated the amount to $7.00 (previously $0.70)`; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); }); @@ -641,7 +640,7 @@ describe('ModifiedExpenseMessage', () => { it('then the message says the rate is changed and shows the new and old merchant and amount', () => { const expectedResult = `changed the rate to ${reportAction.originalMessage.merchant} (previously ${reportAction.originalMessage.oldMerchant}), which updated the amount to $55.80 (previously $39.45)`; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); }); @@ -660,7 +659,7 @@ describe('ModifiedExpenseMessage', () => { reportName: '', }; - const result = getForReportAction({reportAction, policyID: report.policyID, movedFromReport, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN, movedFromReport}); expect(result).toEqual(expectedResult); }); }); @@ -679,7 +678,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message with AI attribution when setting description', () => { const expectedResult = 'set the description based on past activity to "Flight to client meeting"'; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -699,7 +698,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message with AI attribution when changing description', () => { const expectedResult = 'changed the description based on past activity to "New description" (previously "Old description")'; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -719,7 +718,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message with AI attribution', () => { const expectedResult = `changed the category based on past activity to "Travel" (previously "Food")`; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -739,7 +738,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message with MCC attribution for non-admin', () => { const expectedResult = `changed the category based on workspace rule to "Travel" (previously "Food")`; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -759,10 +758,9 @@ describe('ModifiedExpenseMessage', () => { isPolicyExpenseChatEnabled: true, }; - jest.spyOn(PolicyUtils, 'getPolicy').mockReturnValue(mockPolicy); jest.spyOn(PolicyUtils, 'isPolicyAdmin').mockReturnValue(true); - const result = getForReportAction({reportAction, policyID: mockPolicy.id, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policy: mockPolicy, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); // Verify the policyID in the URL exactly matches the policy.id (case-preserved) expect(result).toContain(`workspaces/${mockPolicy.id}/rules`); @@ -785,7 +783,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message with AI attribution', () => { const expectedResult = `set the category based on past activity to "Travel"`; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -805,7 +803,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message without showing previously uncategorized', () => { const expectedResult = `set the category based on past activity to "6403 Travel - Member Services"`; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -824,7 +822,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the generic changed expense message since no meaningful change occurred', () => { const expectedResult = `changed the expense`; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -844,7 +842,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message with AI attribution', () => { const expectedResult = `removed the category based on past activity (previously "Travel")`; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -858,13 +856,10 @@ describe('ModifiedExpenseMessage', () => { environmentURL = await getEnvironmentURL(); }); + const policyRulesPolicy = {id: policyRulesPolicyId, areRulesEnabled: true} as Policy; + beforeEach(() => { // Default: current user has policy rule access (admin + rules enabled), so link points to workspace rules - // eslint-disable-next-line @typescript-eslint/no-deprecated - (PolicyUtils.getPolicy as jest.Mock).mockReturnValue({ - id: policyRulesPolicyId, - areRulesEnabled: true, - } as Policy); (PolicyUtils.isPolicyAdmin as jest.Mock).mockReturnValue(true); }); @@ -883,7 +878,7 @@ describe('ModifiedExpenseMessage', () => { } as OriginalMessageModifiedExpense, }; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policy: policyRulesPolicy, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); const expectedResult = `marked the expense as "billable", marked the expense as "reimbursable", set the category to "Travel", and merchant to "McDonald's" via workspace rules`; @@ -909,7 +904,7 @@ describe('ModifiedExpenseMessage', () => { } as OriginalMessageModifiedExpense, }; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policy: policyRulesPolicy, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); const expectedResult = `set the tax rate to "New Tax Rate" via workspace rules`; @@ -929,7 +924,7 @@ describe('ModifiedExpenseMessage', () => { } as OriginalMessageModifiedExpense, }; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policy: policyRulesPolicy, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); const expectedResult = `set the category to "Travel" and merchant to "McDonald's" via workspace rules`; @@ -948,7 +943,7 @@ describe('ModifiedExpenseMessage', () => { } as OriginalMessageModifiedExpense, }; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policy: policyRulesPolicy, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); const expectedResult = `marked the expense as "billable" via workspace rules`; @@ -968,7 +963,7 @@ describe('ModifiedExpenseMessage', () => { } as OriginalMessageModifiedExpense, }; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policy: policyRulesPolicy, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); const expectedResult = `marked the expense as "reimbursable" and marked the expense as "billable" via workspace rules`; @@ -990,7 +985,7 @@ describe('ModifiedExpenseMessage', () => { } as OriginalMessageModifiedExpense, }; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policy: policyRulesPolicy, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toContain(CONST.CONFIGURE_EXPENSE_REPORT_RULES_HELP_URL); expect(result).toContain('workspace rules'); @@ -1011,7 +1006,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message without attribution', () => { const expectedResult = `changed the category to "Travel" (previously "Food")`; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); @@ -1031,18 +1026,14 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message without attribution', () => { const expectedResult = `changed the category to "Travel" (previously "Food")`; - const result = getForReportAction({reportAction, policyID: report.policyID, currentUserLogin: CURRENT_USER_LOGIN}); + const result = getForReportAction({translate: translateLocal, reportAction, policyTags: undefined, currentUserLogin: CURRENT_USER_LOGIN}); expect(result).toEqual(expectedResult); }); }); }); - describe('getForReportActionTemp', () => { - // getForReportActionTemp is a temporary function that takes translate, policy, policyTags, and currentUserLogin as parameters - // instead of using module-level Onyx connections. This allows React components to properly re-render when the underlying data changes. - // These tests mirror the getForReportAction tests to ensure the same behavior. - + describe('getForReportAction', () => { describe('when the amount is changed', () => { const reportAction = { ...createRandomReportAction(1), @@ -1058,7 +1049,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message', () => { const expectedResult = `changed the amount to $18.00 (previously $12.55)`; - const result = getForReportActionTemp({ + const result = getForReportAction({ translate: translateLocal, reportAction, policyTags: undefined, @@ -1086,7 +1077,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message', () => { const expectedResult = 'changed the amount to $18.00 (previously $12.55)\nremoved the description (previously "this is for the shuttle")'; - const result = getForReportActionTemp({ + const result = getForReportAction({ translate: translateLocal, reportAction, policyTags: undefined, @@ -1110,7 +1101,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message', () => { const expectedResult = `set the merchant to "Big Belly"`; - const result = getForReportActionTemp({ + const result = getForReportAction({ translate: translateLocal, reportAction, policyTags: undefined, @@ -1134,7 +1125,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message', () => { const expectedResult = `removed the merchant (previously "Big Belly")`; - const result = getForReportActionTemp({ + const result = getForReportAction({ translate: translateLocal, reportAction, policyTags: undefined, @@ -1158,7 +1149,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message', () => { const expectedResult = `changed the category to "Travel" (previously "Food")`; - const result = getForReportActionTemp({ + const result = getForReportAction({ translate: translateLocal, reportAction, policyTags: undefined, @@ -1183,7 +1174,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message with AI attribution', () => { const expectedResult = `changed the category based on past activity to "Travel" (previously "Food")`; - const result = getForReportActionTemp({ + const result = getForReportAction({ translate: translateLocal, reportAction, policyTags: undefined, @@ -1208,7 +1199,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct text message with MCC attribution for non-admin', () => { const expectedResult = `changed the category based on workspace rule to "Travel" (previously "Food")`; - const result = getForReportActionTemp({ + const result = getForReportAction({ translate: translateLocal, reportAction, policyTags: undefined, @@ -1231,7 +1222,7 @@ describe('ModifiedExpenseMessage', () => { jest.spyOn(PolicyUtils, 'isPolicyAdmin').mockReturnValue(true); - const result = getForReportActionTemp({ + const result = getForReportAction({ translate: translateLocal, reportAction, policy: mockPolicy, @@ -1262,7 +1253,7 @@ describe('ModifiedExpenseMessage', () => { it('then the message says the distance is changed and shows the new and old merchant and amount', () => { const expectedResult = `changed the distance to ${reportAction.originalMessage.merchant} (previously ${reportAction.originalMessage.oldMerchant}), which updated the amount to $7.00 (previously $0.70)`; - const result = getForReportActionTemp({ + const result = getForReportAction({ translate: translateLocal, reportAction, policyTags: undefined, @@ -1286,7 +1277,7 @@ describe('ModifiedExpenseMessage', () => { reportName: '', }; - const result = getForReportActionTemp({ + const result = getForReportAction({ translate: translateLocal, reportAction, movedFromReport, @@ -1304,7 +1295,7 @@ describe('ModifiedExpenseMessage', () => { }; it('returns an empty string', () => { - const result = getForReportActionTemp({ + const result = getForReportAction({ translate: translateLocal, reportAction, policyTags: undefined, @@ -1325,7 +1316,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the generic changed expense message', () => { const expectedResult = 'changed the expense'; - const result = getForReportActionTemp({ + const result = getForReportAction({ translate: translateLocal, reportAction, policyTags: undefined, @@ -1349,7 +1340,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct translated text message', () => { const expectedResult = 'changed the expense to "billable" (previously "non-billable")'; - const result = getForReportActionTemp({ + const result = getForReportAction({ translate: translateLocal, reportAction, policyTags: undefined, @@ -1373,7 +1364,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the correct translated text message', () => { const expectedResult = 'changed the expense to "non-reimbursable" (previously "reimbursable")'; - const result = getForReportActionTemp({ + const result = getForReportAction({ translate: translateLocal, reportAction, policyTags: undefined, @@ -1394,7 +1385,7 @@ describe('ModifiedExpenseMessage', () => { it('returns the AI-attributed message', () => { const expectedResult = 'changed the expense based on past activity'; - const result = getForReportActionTemp({ + const result = getForReportAction({ translate: translateLocal, reportAction, policyTags: undefined,