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
2 changes: 1 addition & 1 deletion src/components/LHNOptionsList/OptionRowLHNData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function OptionRowLHNData({
currentUserAccountID,
reportAttributesDerived,
policyTags,
currentUserLogin: login,
currentUserLogin: login ?? '',
});
if (deepEqual(item, optionItemRef.current)) {
return optionItemRef.current;
Expand Down
2 changes: 1 addition & 1 deletion src/components/Search/SearchFiltersChatsSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function SearchFiltersChatsSelector({initialReportIDs, onFiltersUpdate, isScreen
createOptionFromReport({...reportData, reportID: id}, personalDetails, currentUserAccountID, chatReport, privateIsArchived, reportAttributesDerived),
);
const isReportArchived = !!privateIsArchived;
const alternateText = getAlternateText(report, {}, isReportArchived, currentUserAccountID, {}, undefined, undefined, reportAttributesDerived);
const alternateText = getAlternateText(report, {}, isReportArchived, currentUserEmail, {}, undefined, undefined, reportAttributesDerived);
return {...report, alternateText};
});

Expand Down
19 changes: 12 additions & 7 deletions src/libs/ModifiedExpenseMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function getForDistanceRequest(translate: LocalizedTranslate, newMerchant: strin
});
}

function getForExpenseMovedFromSelfDM(translate: LocalizedTranslate, destinationReport: OnyxEntry<Report>) {
function getForExpenseMovedFromSelfDM(translate: LocalizedTranslate, destinationReport: OnyxEntry<Report>, currentUserLogin: string): 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)) {
Expand All @@ -169,7 +169,7 @@ function getForExpenseMovedFromSelfDM(translate: LocalizedTranslate, destination
// In NewDot, the "Move report" flow only supports moving expenses from self-DM to:
// - A policy expense chat
// - A 1:1 DM
const currentUserAccountID = getPersonalDetailByEmail(storedCurrentUserLogin)?.accountID;
const currentUserAccountID = getPersonalDetailByEmail(currentUserLogin)?.accountID;
const reportName = isPolicyExpenseChat(rootParentReport)
? getPolicyExpenseChatName({report: rootParentReport})
: buildReportNameFromParticipantNames({report: rootParentReport, currentUserAccountID});
Expand All @@ -190,9 +190,14 @@ function getMovedReportID(reportAction: OnyxEntry<ReportAction>, type: ValueOf<t
return type === CONST.REPORT.MOVE_TYPE.TO ? reportActionOriginalMessage?.movedToReportID : reportActionOriginalMessage?.movedFromReport;
}

function getMovedFromOrToReportMessage(translate: LocalizedTranslate, movedFromReport: OnyxEntry<Report> | undefined, movedToReport: OnyxEntry<Report> | undefined): string | undefined {
function getMovedFromOrToReportMessage(
translate: LocalizedTranslate,
movedFromReport: OnyxEntry<Report> | undefined,
movedToReport: OnyxEntry<Report> | undefined,
currentUserLogin: string,
): string | undefined {
if (movedToReport) {
return getForExpenseMovedFromSelfDM(translate, movedToReport);
return getForExpenseMovedFromSelfDM(translate, movedToReport, currentUserLogin);
}

if (movedFromReport) {
Expand Down Expand Up @@ -294,7 +299,7 @@ function getForReportAction({
}

// eslint-disable-next-line @typescript-eslint/no-deprecated
const movedFromOrToReportMessage = getMovedFromOrToReportMessage(translateLocal, movedFromReport, movedToReport);
const movedFromOrToReportMessage = getMovedFromOrToReportMessage(translateLocal, movedFromReport, movedToReport, currentUserLogin);
if (movedFromOrToReportMessage) {
return movedFromOrToReportMessage;
}
Expand Down Expand Up @@ -556,7 +561,7 @@ function getForReportAction({
if (policyRulesModifiedFields && rulePolicyID) {
// eslint-disable-next-line @typescript-eslint/no-deprecated
const rulePolicy = getPolicy(rulePolicyID);
const hasPolicyRuleAccess = !!rulePolicy?.areRulesEnabled && isPolicyAdmin(rulePolicy, storedCurrentUserLogin);
const hasPolicyRuleAccess = !!rulePolicy?.areRulesEnabled && isPolicyAdmin(rulePolicy, currentUserLogin);
// eslint-disable-next-line @typescript-eslint/no-deprecated
return getRulesModifiedMessage(translateLocal, policyRulesModifiedFields, false, rulePolicyID, hasPolicyRuleAccess);
}
Expand Down Expand Up @@ -615,7 +620,7 @@ function getForReportActionTemp({
return '';
}

const movedFromOrToReportMessage = getMovedFromOrToReportMessage(translate, movedFromReport, movedToReport);
const movedFromOrToReportMessage = getMovedFromOrToReportMessage(translate, movedFromReport, movedToReport, currentUserLogin);
if (movedFromOrToReportMessage) {
return movedFromOrToReportMessage;
}
Expand Down
23 changes: 13 additions & 10 deletions src/libs/OptionsListUtils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,13 @@
*/

let allPersonalDetails: OnyxEntry<PersonalDetailsList>;
Onyx.connect({

Check warning on line 208 in src/libs/OptionsListUtils/index.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

Check warning on line 208 in src/libs/OptionsListUtils/index.ts

View workflow job for this annotation

GitHub Actions / 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) => (allPersonalDetails = isEmptyObject(value) ? {} : value),
});

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

Check warning on line 214 in src/libs/OptionsListUtils/index.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

Check warning on line 214 in src/libs/OptionsListUtils/index.ts

View workflow job for this annotation

GitHub Actions / 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 All @@ -220,7 +220,7 @@
});

let allReportNameValuePairsOnyxConnect: OnyxCollection<ReportNameValuePairs>;
Onyx.connect({

Check warning on line 223 in src/libs/OptionsListUtils/index.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

Check warning on line 223 in src/libs/OptionsListUtils/index.ts

View workflow job for this annotation

GitHub Actions / ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -231,7 +231,7 @@
const lastReportActions: ReportActions = {};
const allSortedReportActions: Record<string, ReportAction[]> = {};
let allReportActions: OnyxCollection<ReportActions>;
Onyx.connect({

Check warning on line 234 in src/libs/OptionsListUtils/index.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 @@ -273,7 +273,7 @@
});

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

Check warning on line 276 in src/libs/OptionsListUtils/index.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.NVP_ACTIVE_POLICY_ID,
callback: (value) => (activePolicyID = value),
});
Expand Down Expand Up @@ -429,7 +429,7 @@
option: OptionData,
{showChatPreviewLine = false, forcePolicyNamePreview = false}: PreviewConfig,
isReportArchived: boolean | undefined,
currentUserAccountID: number,
currentUserLogin: string,
lastActorDetails: Partial<PersonalDetails> | null = {},
visibleReportActionsData: VisibleReportActionsDerivedValue = {},
translate?: LocalizedTranslate,
Expand All @@ -454,6 +454,7 @@
chatReport,
visibleReportActionsDataParam: visibleReportActionsData,
reportAttributesDerived,
currentUserLogin,
});
const reportPrefix = getReportSubtitlePrefix(report);
const formattedLastMessageTextWithPrefix = reportPrefix + formattedLastMessageText;
Expand Down Expand Up @@ -620,7 +621,7 @@
lastAction?: OnyxEntry<ReportAction>;
reportAttributesDerived?: ReportAttributesDerivedValue['reports'];
policyTags?: OnyxEntry<PolicyTagLists>;
currentUserLogin?: string;
currentUserLogin: string;
}): string {
const reportID = report?.reportID;
const canUserPerformWrite = canUserPerformWriteAction(report, isReportArchived);
Expand Down Expand Up @@ -739,14 +740,15 @@
movedFromReport,
movedToReport,
policyTags,
currentUserLogin: currentUserLogin ?? '',
currentUserLogin,
})
: getForReportAction({
reportAction: lastReportAction,
policyID: report?.policyID,
movedFromReport,
movedToReport,
policyForMovingExpensesID,
currentUserLogin,
});
lastMessageTextFromReport = formatReportLastMessageText(properSchemaForModifiedExpenseMessage, true);
} else if (isMovedTransactionAction(lastReportAction)) {
Expand Down Expand Up @@ -1034,6 +1036,7 @@

// 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({
Expand All @@ -1044,6 +1047,7 @@
chatReport,
visibleReportActionsDataParam: visibleReportActionsData,
reportAttributesDerived,
currentUserLogin,
});
result.alternateText =
showPersonalDetails && personalDetail?.login
Expand All @@ -1052,7 +1056,7 @@
result,
{showChatPreviewLine, forcePolicyNamePreview},
!!result.private_isArchived,
currentUserAccountID,
currentUserLogin,
lastActorDetails,
visibleReportActionsData,
translateFn,
Expand Down Expand Up @@ -2213,7 +2217,7 @@
function prepareReportOptionsForDisplay(
options: Array<SearchOption<Report>>,
policiesCollection: OnyxCollection<Policy>,
currentUserAccountID: number,
currentUserLogin: string,
config: GetValidReportsConfig,
visibleReportActionsData: VisibleReportActionsDerivedValue = {},
reportAttributesDerived?: ReportAttributesDerivedValue['reports'],
Expand Down Expand Up @@ -2242,7 +2246,6 @@
continue;
}
const report = option.item;

/**
* By default, generated options does not have the chat preview line enabled.
* If showChatPreviewLine or forcePolicyNamePreview are true, let's generate and overwrite the alternate text.
Expand All @@ -2251,7 +2254,7 @@
option,
{showChatPreviewLine, forcePolicyNamePreview},
!!option.private_isArchived,
currentUserAccountID,
currentUserLogin,
null,
visibleReportActionsData,
undefined,
Expand Down Expand Up @@ -2489,7 +2492,7 @@
selfDMChat = prepareReportOptionsForDisplay(
selfDMChats,
policiesCollection,
currentUserAccountID,
currentUserEmail,
{
...getValidReportsConfig,
selectedOptions,
Expand All @@ -2510,7 +2513,7 @@
recentReportOptions = prepareReportOptionsForDisplay(
recentReportOptions,
policiesCollection,
currentUserAccountID,
currentUserEmail,
{
...getValidReportsConfig,
selectedOptions,
Expand All @@ -2527,7 +2530,7 @@
workspaceChats = prepareReportOptionsForDisplay(
workspaceChats,
policiesCollection,
currentUserAccountID,
currentUserEmail,
{
...getValidReportsConfig,
selectedOptions,
Expand Down
2 changes: 1 addition & 1 deletion src/libs/SidebarUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ function getOptionData({
visibleReportActionsData?: VisibleReportActionsDerivedValue;
reportAttributesDerived?: ReportAttributesDerivedValue['reports'];
policyTags?: OnyxEntry<PolicyTagLists>;
currentUserLogin?: string;
currentUserLogin: string;
}): OptionData | undefined {
// When a user signs out, Onyx is cleared. Due to the lazy rendering with a virtual list, it's possible for
// this method to be called after the Onyx data has been cleared out. In that case, it's fine to do
Expand Down
3 changes: 2 additions & 1 deletion tests/perf-test/SidebarUtils.perf-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
const REPORTS_COUNT = 15000;
const REPORT_THRESHOLD = 5;
const PERSONAL_DETAILS_LIST_COUNT = 1000;
const CURRENT_USER_LOGIN = 'test@example.com';

const allReports = createCollection<Report>(
(item) => `${ONYXKEYS.COLLECTION.REPORT}${item.reportID}`,
Expand Down Expand Up @@ -49,7 +50,6 @@ const mockedBetas = Object.values(CONST.BETAS);

const currentReportId = '1';
const transactionViolations = {} as OnyxCollection<TransactionViolation[]>;

describe('SidebarUtils', () => {
beforeAll(() => {
Onyx.init({
Expand Down Expand Up @@ -93,6 +93,7 @@ describe('SidebarUtils', () => {
lastActionReport: undefined,
isReportArchived: undefined,
currentUserAccountID: 1,
currentUserLogin: CURRENT_USER_LOGIN,
}),
);
});
Expand Down
46 changes: 38 additions & 8 deletions tests/unit/ModifiedExpenseMessageTest.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import Onyx from 'react-native-onyx';
import {getEnvironmentURL} from '@libs/Environment/Environment';
import {getForReportAction, getForReportActionTemp, 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
import * as ReportNameUtils from '@libs/ReportNameUtils';
// eslint-disable-next-line no-restricted-syntax -- this is required to allow mocking
import * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import IntlStore from '@src/languages/IntlStore';
import {translate} from '@src/libs/Localize';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy} from '@src/types/onyx';
import type {OriginalMessageModifiedExpense} from '@src/types/onyx/OriginalMessage';
import createRandomReportAction from '../utils/collections/reportActions';
Expand All @@ -22,9 +26,15 @@ jest.mock('@libs/PolicyUtils', () => ({
isPolicyAdmin: jest.fn(),
}));

// Mock ReportNameUtils so buildReportNameFromParticipantNames is controllable.
jest.mock('@libs/ReportNameUtils', () => ({
...jest.requireActual<typeof ReportNameUtils>('@libs/ReportNameUtils'),
buildReportNameFromParticipantNames: jest.fn(),
}));

const MOVED_TO_REPORT_ID = '1';
const MOVED_FROM_REPORT_ID = '2';

const CURRENT_USER_LOGIN = 'test@example.com';
describe('ModifiedExpenseMessage', () => {
beforeAll(() => {
IntlStore.load(CONST.LOCALES.EN);
Expand Down Expand Up @@ -104,13 +114,13 @@ describe('ModifiedExpenseMessage', () => {
describe('when moving to a report', () => {
it('returns "moved expense to personal space" message when moving an expense to selfDM', () => {
const selfDMReport = createRandomReport(1, CONST.REPORT.CHAT_TYPE.SELF_DM);
const result = getMovedFromOrToReportMessage(translateLocal, undefined, selfDMReport);
const result = getMovedFromOrToReportMessage(translateLocal, undefined, selfDMReport, CURRENT_USER_LOGIN);
const expectedResult = translate(CONST.LOCALES.EN as 'en', 'iou.movedToPersonalSpace');
expect(result).toEqual(expectedResult);
});
it('returns "moved expense from personal space to chat with reportName" message when moving an expense to policy expense chat with only reportName', () => {
const policyExpenseReport = createRandomReport(1, CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT);
const result = getMovedFromOrToReportMessage(translateLocal, undefined, policyExpenseReport);
const result = getMovedFromOrToReportMessage(translateLocal, undefined, policyExpenseReport, CURRENT_USER_LOGIN);
const expectedResult = translate(CONST.LOCALES.EN as 'en', 'iou.movedFromPersonalSpace', {reportName: policyExpenseReport.reportName});
expect(result).toEqual(expectedResult);
});
Expand All @@ -119,7 +129,7 @@ describe('ModifiedExpenseMessage', () => {
...createRandomReport(1, CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT),
policyName: 'Policy',
};
const result = getMovedFromOrToReportMessage(translateLocal, undefined, policyExpenseReport);
const result = getMovedFromOrToReportMessage(translateLocal, undefined, policyExpenseReport, CURRENT_USER_LOGIN);
const expectedResult = translate(CONST.LOCALES.EN as 'en', 'iou.movedFromPersonalSpace', {
reportName: policyExpenseReport.reportName,
workspaceName: policyExpenseReport.policyName,
Expand All @@ -131,10 +141,30 @@ describe('ModifiedExpenseMessage', () => {
...createRandomReport(1, CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT),
reportName: '',
};
const result = getMovedFromOrToReportMessage(translateLocal, undefined, policyExpenseReport);
const result = getMovedFromOrToReportMessage(translateLocal, undefined, policyExpenseReport, CURRENT_USER_LOGIN);
const expectedResult = translate(CONST.LOCALES.EN as 'en', 'iou.changedTheExpense');
expect(result).toEqual(expectedResult);
});
it('returns "moved from personal space to reportName" message when moving an expense to a 1:1 DM', async () => {
const mockAccountID = 12345;
const dmReportName = 'John Doe';
const dmReport = createRandomReport(1, undefined);

await Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {
[mockAccountID]: {accountID: mockAccountID, login: CURRENT_USER_LOGIN},
});

(ReportNameUtils.buildReportNameFromParticipantNames as jest.Mock).mockImplementation(({currentUserAccountID}) => {
if (currentUserAccountID === mockAccountID) {
return dmReportName;
}
return '';
});

const result = getMovedFromOrToReportMessage(translateLocal, undefined, dmReport, CURRENT_USER_LOGIN);
const expectedResult = translate(CONST.LOCALES.EN as 'en', 'iou.movedFromPersonalSpace', {reportName: dmReportName});
expect(result).toEqual(expectedResult);
});
});

describe('when moving from a report', () => {
Expand All @@ -143,7 +173,7 @@ describe('ModifiedExpenseMessage', () => {
};

it('returns "moved expense from reportName" message', () => {
const result = getMovedFromOrToReportMessage(translateLocal, movedFromReport, undefined);
const result = getMovedFromOrToReportMessage(translateLocal, movedFromReport, undefined, CURRENT_USER_LOGIN);
const expectedResult = translate(CONST.LOCALES.EN as 'en', 'iou.movedFromReport', movedFromReport.reportName ?? '');
expect(result).toEqual(expectedResult);
});
Expand All @@ -153,15 +183,15 @@ describe('ModifiedExpenseMessage', () => {
...createRandomReport(1, undefined),
reportName: '',
};
const result = getMovedFromOrToReportMessage(translateLocal, reportWithoutName, undefined);
const result = getMovedFromOrToReportMessage(translateLocal, reportWithoutName, undefined, CURRENT_USER_LOGIN);
const expectedResult = translate(CONST.LOCALES.EN as 'en', 'iou.movedFromReport', '');

expect(result).toEqual(expectedResult);
});
});

it('returns undefined when neither movedToReport nor movedFromReport is provided', () => {
const result = getMovedFromOrToReportMessage(translateLocal, undefined, undefined);
const result = getMovedFromOrToReportMessage(translateLocal, undefined, undefined, CURRENT_USER_LOGIN);
expect(result).toBeUndefined();
});
});
Expand Down
Loading
Loading