Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ const ONYXKEYS = {
/** Whether the user is a member of a policy other than their personal */
HAS_NON_PERSONAL_POLICY: 'hasNonPersonalPolicy',

/** Key under which selfDM id is stored. Returned by OpenApp */
SELF_DM_REPORT_ID: 'selfDMReportID',

/** NVP keys */

/** This NVP contains list of at most 5 recent attendees */
Expand Down Expand Up @@ -1297,6 +1300,7 @@ type OnyxValuesMapping = {
[ONYXKEYS.NVP_CSV_EXPORT_LAYOUTS]: Record<string, OnyxTypes.ExportTemplate>;
[ONYXKEYS.NVP_LAST_DISTANCE_EXPENSE_TYPE]: DistanceExpenseType;
[ONYXKEYS.HAS_DENIED_CONTACT_IMPORT_PROMPT]: boolean | undefined;
[ONYXKEYS.SELF_DM_REPORT_ID]: string | undefined;
};

type OnyxDerivedValuesMapping = {
Expand Down
27 changes: 20 additions & 7 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,17 @@ Onyx.connect({
},
});

let selfDMReportID: string | undefined;
Onyx.connectWithoutView({
key: ONYXKEYS.SELF_DM_REPORT_ID,
callback: (value) => {
if (!value) {
return;
}
selfDMReportID = value;
},
});

function getCurrentUserAvatar(): AvatarSource | undefined {
return currentUserPersonalDetails?.avatar;
}
Expand Down Expand Up @@ -1857,7 +1868,12 @@ function findSelfDMReportID(): string | undefined {
return;
}

if (selfDMReportID) {
return selfDMReportID;
}

const selfDMReport = Object.values(allReports).find((report) => isSelfDM(report) && !isThread(report));

return selfDMReport?.reportID;
}

Expand Down Expand Up @@ -2480,9 +2496,8 @@ function isIOURequest(report: OnyxInputOrEntry<Report>): boolean {
*/
function isTrackExpenseReport(report: OnyxInputOrEntry<Report>): boolean {
if (isThread(report)) {
const selfDMReportID = findSelfDMReportID();
const parentReportAction = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`]?.[report.parentReportActionID];
return !isEmptyObject(parentReportAction) && selfDMReportID === report.parentReportID && isTrackExpenseAction(parentReportAction);
return !isEmptyObject(parentReportAction) && findSelfDMReportID() === report.parentReportID && isTrackExpenseAction(parentReportAction);
}
return false;
}
Expand Down Expand Up @@ -6469,7 +6484,7 @@ function buildOptimisticTaskCommentReportAction(

function buildOptimisticSelfDMReport(created: string): Report {
return {
reportID: generateReportID(),
reportID: findSelfDMReportID() ?? generateReportID(),
participants: {
[currentUserAccountID ?? CONST.DEFAULT_NUMBER_ID]: {
notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE,
Expand Down Expand Up @@ -6832,8 +6847,7 @@ function getMovedTransactionMessage(report: OnyxEntry<Report>) {
}

function getUnreportedTransactionMessage() {
const selfDMReportID = findSelfDMReportID();
const reportUrl = `${environmentURL}/r/${selfDMReportID}`;
const reportUrl = `${environmentURL}/r/${findSelfDMReportID()}`;
// eslint-disable-next-line @typescript-eslint/no-deprecated
const message = translateLocal('iou.unreportedTransaction', {
reportUrl,
Expand Down Expand Up @@ -11690,8 +11704,7 @@ function prepareOnboardingOnyxData(

let selfDMParameters: SelfDMParameters = {};
if (engagementChoice === CONST.ONBOARDING_CHOICES.PERSONAL_SPEND || engagementChoice === CONST.ONBOARDING_CHOICES.TRACK_WORKSPACE) {
const selfDMReportID = findSelfDMReportID();
let selfDMReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${selfDMReportID}`];
let selfDMReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${findSelfDMReportID()}`];
let createdAction: ReportAction;
if (!selfDMReport) {
const currentTime = DateUtils.getDBTime();
Expand Down
17 changes: 8 additions & 9 deletions src/libs/actions/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
buildOptimisticUnHoldReportAction,
buildOptimisticUnreportedTransactionAction,
buildTransactionThread,
findSelfDMReportID,
getReportTransactions,
getTransactionDetails,
hasViolations as hasViolationsReportUtils,
Expand Down Expand Up @@ -701,6 +700,7 @@ function changeTransactionsReport(
policy?: OnyxEntry<Policy>,
reportNextStep?: OnyxEntry<ReportNextStepDeprecated>,
policyCategories?: OnyxEntry<PolicyCategories>,
selfDMReportIDParam?: string,
) {
const reportID = newReport?.reportID ?? CONST.REPORT.UNREPORTED_REPORT_ID;

Expand All @@ -720,15 +720,16 @@ function changeTransactionsReport(
const failureData: OnyxUpdate[] = [];
const successData: OnyxUpdate[] = [];

const existingSelfDMReportID = findSelfDMReportID();
let selfDMReport: Report | undefined;
let selfDMReportID = selfDMReportIDParam;
let selfDMReport: Report | undefined = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${selfDMReportID}`];
let selfDMCreatedReportAction: ReportAction | undefined;
const currentUserAccountID = getCurrentUserAccountID();

if (!existingSelfDMReportID && reportID === CONST.REPORT.UNREPORTED_REPORT_ID) {
if (!selfDMReport && reportID === CONST.REPORT.UNREPORTED_REPORT_ID) {
const currentTime = DateUtils.getDBTime();
selfDMReport = buildOptimisticSelfDMReport(currentTime);
selfDMCreatedReportAction = buildOptimisticCreatedReportAction(email ?? '', currentTime);
selfDMReportID = selfDMReport.reportID;

// Add optimistic updates for self DM report
optimisticData.push(
Expand Down Expand Up @@ -811,8 +812,6 @@ function changeTransactionsReport(
for (const transaction of transactions) {
const isUnreportedExpense = !transaction.reportID || transaction.reportID === CONST.REPORT.UNREPORTED_REPORT_ID;

const selfDMReportID = existingSelfDMReportID ?? selfDMReport?.reportID;

const oldIOUAction = getIOUActionForReportID(isUnreportedExpense ? selfDMReportID : transaction.reportID, transaction.transactionID);
if (!transaction.reportID || transaction.reportID === reportID) {
continue;
Expand Down Expand Up @@ -1179,11 +1178,11 @@ function changeTransactionsReport(
: {}),
};

if (!existingSelfDMReportID && reportID === CONST.REPORT.UNREPORTED_REPORT_ID && selfDMReport && selfDMCreatedReportAction) {
if (reportID === CONST.REPORT.UNREPORTED_REPORT_ID && selfDMReport && selfDMCreatedReportAction) {
// Add self DM data to transaction data
transactionIDToReportActionAndThreadData[transaction.transactionID] = {
...baseTransactionData,
selfDMReportID: selfDMReport.reportID,
selfDMReportID,
selfDMCreatedReportActionID: selfDMCreatedReportAction.reportActionID,
};
} else {
Expand Down Expand Up @@ -1295,7 +1294,7 @@ function changeTransactionsReport(
}

// 9. Update next step for report
const destinationReportID = reportID === CONST.REPORT.UNREPORTED_REPORT_ID ? (existingSelfDMReportID ?? selfDMReport?.reportID) : reportID;
const destinationReportID = reportID === CONST.REPORT.UNREPORTED_REPORT_ID ? selfDMReportID : reportID;
const destinationReport = destinationReportID
? (allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${destinationReportID}`] ??
(destinationReportID === newReport?.reportID ? newReport : undefined) ??
Expand Down
4 changes: 3 additions & 1 deletion src/pages/AddUnreportedExpense.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
import interceptAnonymousUser from '@libs/interceptAnonymousUser';
import type {AddUnreportedExpensesParamList} from '@libs/Navigation/types';
import {canSubmitPerDiemExpenseFromWorkspace, getPerDiemCustomUnit} from '@libs/PolicyUtils';
import {getTransactionDetails, isIOUReport} from '@libs/ReportUtils';
import {findSelfDMReportID, getTransactionDetails, isIOUReport} from '@libs/ReportUtils';
import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils';
import tokenizedSearch from '@libs/tokenizedSearch';
import {createUnreportedExpenseSections, getAmount, getCurrency, getDescription, getMerchant, isPerDiemRequest} from '@libs/TransactionUtils';
Expand Down Expand Up @@ -61,6 +61,7 @@ function AddUnreportedExpense({route}: AddUnreportedExpensePageType) {
const {isBetaEnabled} = usePermissions();
const isASAPSubmitBetaEnabled = isBetaEnabled(CONST.BETAS.ASAP_SUBMIT);
const session = useSession();
const selfDMReportID = useMemo(() => findSelfDMReportID(), []);
const shouldShowUnreportedTransactionsSkeletons = isLoadingUnreportedTransactions && hasMoreUnreportedTransactionsResults && !isOffline;

const getUnreportedTransactions = useCallback(
Expand Down Expand Up @@ -181,6 +182,7 @@ function AddUnreportedExpense({route}: AddUnreportedExpensePageType) {
policy,
reportNextStep,
policyCategories,
selfDMReportID,
);
}
});
Expand Down
4 changes: 3 additions & 1 deletion src/pages/NewReportWorkspaceSelectionPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import Navigation from '@libs/Navigation/Navigation';
import type {NewReportWorkspaceSelectionNavigatorParamList} from '@libs/Navigation/types';
import {getHeaderMessageForNonUserList} from '@libs/OptionsListUtils';
import {canSubmitPerDiemExpenseFromWorkspace, isPolicyAdmin, shouldShowPolicy} from '@libs/PolicyUtils';
import {getDefaultWorkspaceAvatar, getPolicyIDsWithEmptyReportsForAccount, hasViolations as hasViolationsReportUtils} from '@libs/ReportUtils';
import {findSelfDMReportID, getDefaultWorkspaceAvatar, getPolicyIDsWithEmptyReportsForAccount, hasViolations as hasViolationsReportUtils} from '@libs/ReportUtils';
import {buildCannedSearchQuery} from '@libs/SearchQueryUtils';
import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils';
import {isPerDiemRequest} from '@libs/TransactionUtils';
Expand Down Expand Up @@ -72,6 +72,7 @@ function NewReportWorkspaceSelectionPage({route}: NewReportWorkspaceSelectionPag
const shouldShowLoadingIndicator = isLoadingApp && !isOffline;
const [pendingPolicySelection, setPendingPolicySelection] = useState<{policy: WorkspaceListItem; shouldShowEmptyReportConfirmation: boolean} | null>(null);
const [accountID] = useOnyx(ONYXKEYS.SESSION, {selector: accountIDSelector, canBeMissing: true});
const selfDMReportID = useMemo(() => findSelfDMReportID(), []);

const policiesWithEmptyReportsSelector = useCallback(
(reports: OnyxCollection<OnyxTypes.Report>) => {
Expand Down Expand Up @@ -125,6 +126,7 @@ function NewReportWorkspaceSelectionPage({route}: NewReportWorkspaceSelectionPag
policies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`],
reportNextStep,
undefined,
selfDMReportID,
);

// eslint-disable-next-line rulesdir/no-default-id-values
Expand Down
6 changes: 4 additions & 2 deletions src/pages/ProfilePage.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-restricted-imports */
import {Str} from 'expensify-common';
import React, {useEffect, useMemo} from 'react';
import {View} from 'react-native';
Expand Down Expand Up @@ -74,17 +75,18 @@ function ProfilePage({route}: ProfilePageProps) {
const [account] = useOnyx(ONYXKEYS.ACCOUNT, {canBeMissing: true});
const [isDebugModeEnabled = false] = useOnyx(ONYXKEYS.IS_DEBUG_MODE_ENABLED, {canBeMissing: true});
const guideCalendarLink = account?.guideDetails?.calendarLink ?? '';
const selfDMReportID = useMemo(() => findSelfDMReportID(), []);

const accountID = Number(route.params?.accountID ?? CONST.DEFAULT_NUMBER_ID);
const isCurrentUser = session?.accountID === accountID;
const reportKey = useMemo(() => {
const reportID = isCurrentUser ? findSelfDMReportID() : getChatByParticipants(session?.accountID ? [accountID, session.accountID] : [], reports)?.reportID;
const reportID = isCurrentUser ? selfDMReportID : getChatByParticipants(session?.accountID ? [accountID, session.accountID] : [], reports)?.reportID;

if (isAnonymousUserSession() || !reportID) {
return `${ONYXKEYS.COLLECTION.REPORT}0` as const;
}
return `${ONYXKEYS.COLLECTION.REPORT}${reportID}` as const;
}, [accountID, isCurrentUser, reports, session]);
}, [accountID, selfDMReportID, isCurrentUser, reports, session]);
const [report] = useOnyx(reportKey, {canBeMissing: true});

const styles = useThemeStyles();
Expand Down
17 changes: 15 additions & 2 deletions src/pages/Search/SearchTransactionsChangeReport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {createNewReport} from '@libs/actions/Report';
import {changeTransactionsReport} from '@libs/actions/Transaction';
import setNavigationActionToMicrotaskQueue from '@libs/Navigation/helpers/setNavigationActionToMicrotaskQueue';
import Navigation from '@libs/Navigation/Navigation';
import {getReportOrDraftReport, hasViolations as hasViolationsReportUtils} from '@libs/ReportUtils';
import {findSelfDMReportID, getReportOrDraftReport, hasViolations as hasViolationsReportUtils} from '@libs/ReportUtils';
import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils';
import IOURequestEditReportCommon from '@pages/iou/request/step/IOURequestEditReportCommon';
import CONST from '@src/CONST';
Expand All @@ -39,6 +39,7 @@ function SearchTransactionsChangeReport() {
const session = useSession();
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
const policyForMovingExpenses = policyForMovingExpensesID ? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyForMovingExpensesID}`] : undefined;
const selfDMReportID = useMemo(() => findSelfDMReportID(), []);

const firstTransactionKey = selectedTransactionsKeys.at(0);
const firstTransactionReportID = firstTransactionKey ? selectedTransactions[firstTransactionKey]?.reportID : undefined;
Expand Down Expand Up @@ -84,6 +85,7 @@ function SearchTransactionsChangeReport() {
policyForMovingExpensesID ? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyForMovingExpensesID}`] : undefined,
reportNextStep,
undefined,
selfDMReportID,
);
clearSelectedTransactions();
});
Expand Down Expand Up @@ -125,6 +127,7 @@ function SearchTransactionsChangeReport() {
allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${item.policyID}`],
reportNextStep,
allPolicyCategories?.[`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${item.policyID}`],
selfDMReportID,
);
// eslint-disable-next-line @typescript-eslint/no-deprecated
InteractionManager.runAfterInteractions(() => {
Expand All @@ -138,7 +141,17 @@ function SearchTransactionsChangeReport() {
if (selectedTransactionsKeys.length === 0) {
return;
}
changeTransactionsReport(selectedTransactionsKeys, isASAPSubmitBetaEnabled, session?.accountID ?? CONST.DEFAULT_NUMBER_ID, session?.email ?? '');
changeTransactionsReport(
selectedTransactionsKeys,
isASAPSubmitBetaEnabled,
session?.accountID ?? CONST.DEFAULT_NUMBER_ID,
session?.email ?? '',
undefined,
undefined,
undefined,
undefined,
selfDMReportID,
);
clearSelectedTransactions();
Navigation.goBack();
};
Expand Down
14 changes: 7 additions & 7 deletions src/pages/TrackExpensePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import useOnyx from '@hooks/useOnyx';
import useThemeStyles from '@hooks/useThemeStyles';
import interceptAnonymousUser from '@libs/interceptAnonymousUser';
import Navigation from '@libs/Navigation/Navigation';
import * as ReportUtils from '@libs/ReportUtils';
import * as App from '@userActions/App';
import * as IOU from '@userActions/IOU';
import {findSelfDMReportID, generateReportID} from '@libs/ReportUtils';
import {confirmReadyToOpenApp} from '@userActions/App';
import {startMoneyRequest} from '@userActions/IOU';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
Expand All @@ -26,21 +26,21 @@ function TrackExpensePage() {
const styles = useThemeStyles();
const isUnmounted = useRef(false);
const {isOffline} = useNetwork();
const [hasSeenTrackTraining, hasSeenTrackTrainingResult] = useOnyx(ONYXKEYS.NVP_HAS_SEEN_TRACK_TRAINING);
const [hasSeenTrackTraining, hasSeenTrackTrainingResult] = useOnyx(ONYXKEYS.NVP_HAS_SEEN_TRACK_TRAINING, {canBeMissing: true});
const isLoadingHasSeenTrackTraining = isLoadingOnyxValue(hasSeenTrackTrainingResult);

useFocusEffect(() => {
interceptAnonymousUser(() => {
App.confirmReadyToOpenApp();
confirmReadyToOpenApp();
Navigation.isNavigationReady().then(() => {
if (isUnmounted.current || isLoadingHasSeenTrackTraining) {
return;
}
Navigation.goBack();
IOU.startMoneyRequest(
startMoneyRequest(
CONST.IOU.TYPE.TRACK,
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
ReportUtils.findSelfDMReportID() || ReportUtils.generateReportID(),
findSelfDMReportID() || generateReportID(),
);

if (!hasSeenTrackTraining && !isOffline) {
Expand Down
16 changes: 14 additions & 2 deletions src/pages/iou/request/step/IOURequestEditReport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {turnOffMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
import {changeTransactionsReport} from '@libs/actions/Transaction';
import setNavigationActionToMicrotaskQueue from '@libs/Navigation/helpers/setNavigationActionToMicrotaskQueue';
import Navigation from '@libs/Navigation/Navigation';
import {hasViolations as hasViolationsReportUtils} from '@libs/ReportUtils';
import {findSelfDMReportID, hasViolations as hasViolationsReportUtils} from '@libs/ReportUtils';
import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils';
import {isPerDiemRequest} from '@libs/TransactionUtils';
import {createNewReport} from '@userActions/Report';
Expand Down Expand Up @@ -58,6 +58,7 @@ function IOURequestEditReport({route}: IOURequestEditReportProps) {
const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, {canBeMissing: true});
const hasViolations = hasViolationsReportUtils(undefined, transactionViolations);
const policyForMovingExpenses = policyForMovingExpensesID ? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyForMovingExpensesID}`] : undefined;
const selfDMReportID = useMemo(() => findSelfDMReportID(), []);

const selectReport = (item: TransactionGroupListItem, report?: OnyxEntry<Report>) => {
if (selectedTransactionIDs.length === 0 || item.value === reportID) {
Expand All @@ -77,6 +78,7 @@ function IOURequestEditReport({route}: IOURequestEditReportProps) {
allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${item.policyID}`],
reportNextStep,
allPolicyCategories?.[`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${item.policyID}`],
selfDMReportID,
);
turnOffMobileSelectionMode();
clearSelectedTransactions(true);
Expand All @@ -89,7 +91,17 @@ function IOURequestEditReport({route}: IOURequestEditReportProps) {
if (!selectedReport || selectedTransactionIDs.length === 0) {
return;
}
changeTransactionsReport(selectedTransactionIDs, isASAPSubmitBetaEnabled, session?.accountID ?? CONST.DEFAULT_NUMBER_ID, session?.email ?? '');
changeTransactionsReport(
selectedTransactionIDs,
isASAPSubmitBetaEnabled,
session?.accountID ?? CONST.DEFAULT_NUMBER_ID,
session?.email ?? '',
undefined,
undefined,
undefined,
undefined,
selfDMReportID,
);
if (shouldTurnOffSelectionMode) {
turnOffMobileSelectionMode();
}
Expand Down
Loading