Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
8fae633
add new write param type
waterim Mar 10, 2025
bd88ffd
create new moveIOUReportToPolicy function
waterim Mar 10, 2025
b81a9f1
fix linter
waterim Mar 10, 2025
77ba24d
Merge remote-tracking branch 'upstream/main' into feature/57470-MoveI…
waterim Mar 10, 2025
55bf9e5
Merge remote-tracking branch 'upstream/main' into feature/57470-MoveI…
waterim Mar 11, 2025
88f1e1f
add new buildOptimisticChangePolicyReportAction
waterim Mar 17, 2025
b5af8b4
update types
waterim Mar 17, 2025
5b5e03f
update function with a feedback
waterim Mar 17, 2025
c937c07
Merge remote-tracking branch 'upstream/main' into feature/57470-MoveI…
waterim Mar 17, 2025
bab22b7
add autimatic action
waterim Mar 17, 2025
f1cd698
fix linter
waterim Mar 17, 2025
7a8da47
add new isPolicyMember function
waterim Mar 20, 2025
3af6cd2
update ReportChangeWorkspacePage with moveIOUReportToPolicy
waterim Mar 20, 2025
e79d326
Merge remote-tracking branch 'upstream/main' into feature/57470-MoveI…
waterim Mar 20, 2025
c0cfd4d
update to use transactionThreadReportID
waterim Mar 20, 2025
659d8c8
Merge remote-tracking branch 'upstream/main' into feature/57470-MoveI…
waterim Mar 24, 2025
2af66a9
update to expense chat report id
waterim Mar 24, 2025
ae87c15
update condition on changeWorkspace
waterim Mar 25, 2025
e9f1c2a
Merge remote-tracking branch 'upstream/main' into feature/57470-MoveI…
waterim Mar 25, 2025
fe04e0e
update names and add timestamp
waterim Mar 25, 2025
a6043e5
Merge remote-tracking branch 'upstream/main' into feature/57470-MoveI…
waterim Mar 25, 2025
dbf14a4
clear iouReportID
waterim Mar 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type MoveIOUReportToExistingPolicyParams = {
iouReportID: string;
policyID: string;
changePolicyReportActionID: string;
};

export default MoveIOUReportToExistingPolicyParams;
1 change: 1 addition & 0 deletions src/libs/API/parameters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ export type {default as GetCorpayOnboardingFieldsParams} from './GetCorpayOnboar
export type {SaveCorpayOnboardingCompanyDetailsParams} from './SaveCorpayOnboardingCompanyDetailsParams';
export type {default as AcceptSpotnanaTermsParams} from './AcceptSpotnanaTermsParams';
export type {default as SaveCorpayOnboardingBeneficialOwnerParams} from './SaveCorpayOnboardingBeneficialOwnerParams';
export type {default as MoveIOUReportToExistingPolicyParams} from './MoveIOUReportToExistingPolicyParams';
export type {default as ChangeReportPolicyParams} from './ChangeReportPolicyParams';
export type {default as ResetBankAccountSetupParams} from './ResetBankAccountSetupParams';
export type {default as SendRecapInAdminsRoomParams} from './SendRecapInAdminsRoomParams';
Expand Down
2 changes: 2 additions & 0 deletions src/libs/API/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ const WRITE_COMMANDS = {
EXPORT_TAGS_CSV: 'ExportTagsCSV',
EXPORT_PER_DIEM_CSV: 'ExportPerDiemCSV',
EXPORT_REPORT_TO_CSV: 'ExportReportToCSV',
MOVE_IOU_REPORT_TO_EXISTING_POLICY: 'MoveIOUReportToExistingPolicy',
EXPORT_REPORT_TO_PDF: 'ExportReportToPDF',
RENAME_WORKSPACE_CATEGORY: 'RenameWorkspaceCategory',
CREATE_POLICY_TAG: 'CreatePolicyTag',
Expand Down Expand Up @@ -597,6 +598,7 @@ type WriteCommandParameters = {
[WRITE_COMMANDS.UPDATE_WORKSPACE_MEMBERS_ROLE]: Parameters.UpdateWorkspaceMembersRoleParams;
[WRITE_COMMANDS.CREATE_WORKSPACE]: Parameters.CreateWorkspaceParams;
[WRITE_COMMANDS.CREATE_WORKSPACE_FROM_IOU_PAYMENT]: Parameters.CreateWorkspaceFromIOUPaymentParams;
[WRITE_COMMANDS.MOVE_IOU_REPORT_TO_EXISTING_POLICY]: Parameters.MoveIOUReportToExistingPolicyParams;
[WRITE_COMMANDS.SET_WORKSPACE_CATEGORIES_ENABLED]: Parameters.SetWorkspaceCategoriesEnabledParams;
[WRITE_COMMANDS.CREATE_WORKSPACE_CATEGORIES]: Parameters.CreateWorkspaceCategoriesParams;
[WRITE_COMMANDS.IMPORT_CATEGORIES_SPREADSHEET]: Parameters.ImportCategoriesSpreadsheetParams;
Expand Down
5 changes: 5 additions & 0 deletions src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ function shouldShowPolicy(policy: OnyxEntry<Policy>, shouldShowPendingDeletePoli
);
}

function isPolicyMember(currentUserLogin: string | undefined, policyID: string | undefined): boolean {
return !!currentUserLogin && !!policyID && !!allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]?.employeeList?.[currentUserLogin];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB but we improved the function with additional check for policy owner to determine if the user is a policy member. This would help in cases where employeeList is not available as observed in #69320

}

function isExpensifyTeam(email: string | undefined): boolean {
const emailDomain = Str.extractEmailDomain(email ?? '');
return emailDomain === CONST.EXPENSIFY_PARTNER_NAME || emailDomain === CONST.EMAIL.GUIDES_DOMAIN;
Expand Down Expand Up @@ -1449,6 +1453,7 @@ export {
isPolicyEmployee,
isPolicyFeatureEnabled,
isPolicyOwner,
isPolicyMember,
arePaymentsEnabled,
isSubmitAndClose,
isTaxTrackingEnabled,
Expand Down
3 changes: 2 additions & 1 deletion src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5837,10 +5837,11 @@ function buildOptimisticMovedReportAction(fromPolicyID: string | undefined, toPo
* Builds an optimistic CHANGEPOLICY report action with a randomly generated reportActionID.
* This action is used when we change the workspace of a report.
*/
function buildOptimisticChangePolicyReportAction(fromPolicyID: string | undefined, toPolicyID: string): ReportAction {
function buildOptimisticChangePolicyReportAction(fromPolicyID: string | undefined, toPolicyID: string, automaticAction = false): ReportAction {
const originalMessage = {
fromPolicy: fromPolicyID,
toPolicy: toPolicyID,
automaticAction,
};

const fromPolicy = getPolicy(fromPolicyID);
Expand Down
173 changes: 173 additions & 0 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import type {
LeaveRoomParams,
MarkAsExportedParams,
MarkAsUnreadParams,
MoveIOUReportToExistingPolicyParams,
OpenReportParams,
OpenRoomMembersPageParams,
ReadNewestActionParams,
Expand Down Expand Up @@ -127,13 +128,15 @@ import {
getReportLastVisibleActionCreated,
getReportMetadata,
getReportNotificationPreference,
getReportTransactions,
getReportViolations,
getRouteFromLink,
isChatThread as isChatThreadReportUtils,
isConciergeChatReport,
isExpenseReport,
isGroupChat as isGroupChatReportUtils,
isHiddenForCurrentUser,
isIOUReportUsingReport,
isMoneyRequestReport,
isSelfDM,
isValidReportIDFromPath,
Expand Down Expand Up @@ -167,6 +170,7 @@ import type {
ReportAction,
ReportActionReactions,
ReportUserIsTyping,
Transaction,
} from '@src/types/onyx';
import type {Decision} from '@src/types/onyx/OriginalMessage';
import type {ConnectionName} from '@src/types/onyx/Policy';
Expand Down Expand Up @@ -4871,6 +4875,174 @@ function clearDeleteTransactionNavigateBackUrl() {
Onyx.merge(ONYXKEYS.NVP_DELETE_TRANSACTION_NAVIGATE_BACK_URL, null);
}

/**
* Moves an IOU report to a policy by converting it to an expense report
* @param reportID - The ID of the IOU report to move
* @param policyID - The ID of the policy to move the report to
*/
function moveIOUReportToPolicy(reportID: string, policyID: string) {
const iouReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
const policy = getPolicy(policyID);

// This flow only works for IOU reports
if (!policy || !iouReport || !isIOUReportUsingReport(iouReport)) {
return;
}
// We do not want to create negative amount expenses
if (ReportActionsUtils.hasRequestFromCurrentAccount(iouReport.reportID, iouReport.managerID ?? CONST.DEFAULT_NUMBER_ID)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should filter out the deleted transactions to derive hasRequestFromCurrentAccount as it caused an issue #65323

return;
}

// Generate new variables for the policy
const policyName = policy.name ?? '';
const iouReportID = iouReport.reportID;
const employeeAccountID = iouReport.ownerAccountID;
const expenseChatReportId = getPolicyExpenseChat(employeeAccountID, policyID)?.reportID;

if (!expenseChatReportId) {
return;
}

const optimisticData: OnyxUpdate[] = [];

const successData: OnyxUpdate[] = [];

const failureData: OnyxUpdate[] = [];

// Next we need to convert the IOU report to Expense report.
// We need to change:
// - report type
// - change the sign of the report total
// - update its policyID and policyName
// - update the chatReportID to point to the workspace chat if the policy has policy expense chat enabled
const expenseReport = {
...iouReport,
chatReportID: policy.isPolicyExpenseChatEnabled ? expenseChatReportId : undefined,
policyID,
policyName,
parentReportID: iouReport.parentReportID,
type: CONST.REPORT.TYPE.EXPENSE,
total: -(iouReport?.total ?? 0),
Comment thread
waterim marked this conversation as resolved.
Resolved
};
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`,
value: expenseReport,
});
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`,
value: iouReport,
});

// The expense report transactions need to have the amount reversed to negative values
const reportTransactions = getReportTransactions(iouReportID);

// For performance reasons, we are going to compose a merge collection data for transactions
const transactionsOptimisticData: Record<string, Transaction> = {};
const transactionFailureData: Record<string, Transaction> = {};
reportTransactions.forEach((transaction) => {
transactionsOptimisticData[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = {
...transaction,
amount: -transaction.amount,
modifiedAmount: transaction.modifiedAmount ? -transaction.modifiedAmount : 0,
};

transactionFailureData[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = transaction;
});

optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE_COLLECTION,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}`,
value: transactionsOptimisticData,
});
failureData.push({
onyxMethod: Onyx.METHOD.MERGE_COLLECTION,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}`,
value: transactionFailureData,
});

// We need to move the report preview action from the DM to the workspace chat.
const parentReportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.parentReportID}`];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coming from: #64459. We shouldn't add the reportAction prefix when getting data from allReportActions

const parentReportActionID = iouReport.parentReportActionID;
const reportPreview = iouReport?.parentReportID && parentReportActionID ? parentReportActions?.[parentReportActionID] : undefined;
const oldChatReportID = iouReport.chatReportID;
Comment thread
waterim marked this conversation as resolved.
Resolved

if (reportPreview?.reportActionID) {
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${oldChatReportID}`,
value: {[reportPreview.reportActionID]: null},
});
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${oldChatReportID}`,
value: {[reportPreview.reportActionID]: reportPreview},
});

// Add the reportPreview action to workspace chat
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseChatReportId}`,
value: {[reportPreview.reportActionID]: {...reportPreview, created: DateUtils.getDBTime()}},
});
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseChatReportId}`,
value: {[reportPreview.reportActionID]: null},
});
}

Comment thread
waterim marked this conversation as resolved.
Resolved
// Create the CHANGE_POLICY report action and add it to the expense report which indicates to the user where the report has been moved
const changePolicyReportAction = buildOptimisticChangePolicyReportAction(iouReport.policyID, policyID, true);
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseChatReportId}`,
value: {[changePolicyReportAction.reportActionID]: changePolicyReportAction},
});
successData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseChatReportId}`,
value: {
[changePolicyReportAction.reportActionID]: {
...changePolicyReportAction,
pendingAction: null,
},
},
});
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseChatReportId}`,
value: {[changePolicyReportAction.reportActionID]: null},
});

// To optimistically remove the GBR from the DM we need to update the hasOutstandingChildRequest param to false
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${oldChatReportID}`,
value: {
hasOutstandingChildRequest: false,
iouReportID: null,
},
});
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${oldChatReportID}`,
value: {
hasOutstandingChildRequest: true,
iouReportID,
},
});

const parameters: MoveIOUReportToExistingPolicyParams = {
iouReportID,
policyID,
changePolicyReportActionID: changePolicyReportAction.reportActionID,
};

API.write(WRITE_COMMANDS.MOVE_IOU_REPORT_TO_EXISTING_POLICY, parameters, {optimisticData, successData, failureData});
}

/**
* Dismisses the change report policy educational modal so that it doesn't show up again.
*/
Expand Down Expand Up @@ -5218,6 +5390,7 @@ export {
getOptimisticChatReport,
saveReportDraft,
prepareOnboardingOnyxData,
moveIOUReportToPolicy,
dismissChangePolicyModal,
changeReportPolicy,
};
16 changes: 10 additions & 6 deletions src/pages/ReportChangeWorkspacePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import useNetwork from '@hooks/useNetwork';
import useThemeStyles from '@hooks/useThemeStyles';
import type {WorkspaceListItem} from '@hooks/useWorkspaceList';
import useWorkspaceList from '@hooks/useWorkspaceList';
import {changeReportPolicy} from '@libs/actions/Report';
import {changeReportPolicy, moveIOUReportToPolicy} from '@libs/actions/Report';
import Navigation from '@libs/Navigation/Navigation';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import type {ReportChangeWorkspaceNavigatorParamList} from '@libs/Navigation/types';
import {isWorkspaceEligibleForReportChange} from '@libs/PolicyUtils';
import {isExpenseReport, isMoneyRequestReportPendingDeletion} from '@libs/ReportUtils';
import {isPolicyMember, isWorkspaceEligibleForReportChange} from '@libs/PolicyUtils';
import {isIOUReport, isMoneyRequestReport, isMoneyRequestReportPendingDeletion} from '@libs/ReportUtils';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
Expand Down Expand Up @@ -45,9 +45,13 @@ function ReportChangeWorkspacePage({report}: ReportChangeWorkspacePageProps) {
return;
}
Navigation.goBack(ROUTES.REPORT_WITH_ID.getRoute(reportID));
changeReportPolicy(reportID, policyID);
if (isIOUReport(reportID) && isPolicyMember(currentUserLogin, policyID)) {
moveIOUReportToPolicy(reportID, policyID);
} else {
changeReportPolicy(reportID, policyID);
}
},
[reportID],
[currentUserLogin, reportID],
Comment thread
waterim marked this conversation as resolved.
Resolved
);

const {sections, shouldShowNoResultsFoundMessage, shouldShowSearchInput} = useWorkspaceList({
Expand All @@ -59,7 +63,7 @@ function ReportChangeWorkspacePage({report}: ReportChangeWorkspacePageProps) {
additionalFilter: (newPolicy) => isWorkspaceEligibleForReportChange(newPolicy, report, oldPolicy, currentUserLogin),
});

if (!isExpenseReport(report) || isMoneyRequestReportPendingDeletion(report) || (!report.total && !report.unheldTotal)) {
if (!isMoneyRequestReport(report) || isMoneyRequestReportPendingDeletion(report) || (!report.total && !report.unheldTotal)) {
return <NotFoundPage />;
}

Expand Down