Repository navigation
Feature: Add moveIOUReportToPolicy #58078
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8fae633
bd88ffd
b81a9f1
77ba24d
55bf9e5
88f1e1f
b5af8b4
5b5e03f
c937c07
bab22b7
f1cd698
7a8da47
3af6cd2
e79d326
c0cfd4d
659d8c8
2af66a9
ae87c15
e9f1c2a
fe04e0e
a6043e5
dbf14a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ import type { | |
| LeaveRoomParams, | ||
| MarkAsExportedParams, | ||
| MarkAsUnreadParams, | ||
| MoveIOUReportToExistingPolicyParams, | ||
| OpenReportParams, | ||
| OpenRoomMembersPageParams, | ||
| ReadNewestActionParams, | ||
|
|
@@ -127,13 +128,15 @@ import { | |
| getReportLastVisibleActionCreated, | ||
| getReportMetadata, | ||
| getReportNotificationPreference, | ||
| getReportTransactions, | ||
| getReportViolations, | ||
| getRouteFromLink, | ||
| isChatThread as isChatThreadReportUtils, | ||
| isConciergeChatReport, | ||
| isExpenseReport, | ||
| isGroupChat as isGroupChatReportUtils, | ||
| isHiddenForCurrentUser, | ||
| isIOUReportUsingReport, | ||
| isMoneyRequestReport, | ||
| isSelfDM, | ||
| isValidReportIDFromPath, | ||
|
|
@@ -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'; | ||
|
|
@@ -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)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should filter out the deleted transactions to derive |
||
| 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), | ||
|
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}`]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| const parentReportActionID = iouReport.parentReportActionID; | ||
| const reportPreview = iouReport?.parentReportID && parentReportActionID ? parentReportActions?.[parentReportActionID] : undefined; | ||
| const oldChatReportID = iouReport.chatReportID; | ||
|
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}, | ||
| }); | ||
| } | ||
|
|
||
|
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. | ||
| */ | ||
|
|
@@ -5218,6 +5390,7 @@ export { | |
| getOptimisticChatReport, | ||
| saveReportDraft, | ||
| prepareOnboardingOnyxData, | ||
| moveIOUReportToPolicy, | ||
| dismissChangePolicyModal, | ||
| changeReportPolicy, | ||
| }; | ||
There was a problem hiding this comment.
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
employeeListis not available as observed in #69320