diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 45b9319a430..68a9714a80e 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -950,6 +950,77 @@ function buildOptimisticCreatedReportAction(ownerEmail) { }; } +/** + * Returns the necessary reportAction onyx data to indicate that a chat has been + * @param {Number} sequenceNumber + * @param {String} ownerEmail + * @param {String} policyName + * @param {String} reason - Reason why the chat has been archived + * @returns {Object} + */ +function buildOptimisticClosedReportAction(sequenceNumber, ownerEmail, policyName, reason = CONST.REPORT.ARCHIVE_REASON.DEFAULT) { + return { + actionName: CONST.REPORT.ACTIONS.TYPE.CLOSED, + actorAccountID: currentUserAccountID, + automatic: false, + avatar: lodashGet(allPersonalDetails, [currentUserEmail, 'avatar'], getDefaultAvatar(currentUserEmail)), + clientID: NumberUtils.generateReportActionClientID(), + created: DateUtils.getDBTime(), + message: [ + { + type: CONST.REPORT.MESSAGE.TYPE.TEXT, + style: 'strong', + text: ownerEmail === currentUserEmail ? 'You' : ownerEmail, + }, + { + type: CONST.REPORT.MESSAGE.TYPE.TEXT, + style: 'normal', + text: ' closed this report', + }, + ], + originalMessage: { + policyName, + reason, + }, + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + person: [ + { + type: CONST.REPORT.MESSAGE.TYPE.TEXT, + style: 'strong', + text: lodashGet(allPersonalDetails, [currentUserEmail, 'displayName'], currentUserEmail), + }, + ], + reportActionID: NumberUtils.rand64(), + sequenceNumber, + shouldShow: true, + }; +} + +// actionName: "CLOSED" +// actorAccountID: 32 +// actorEmail: "test3@test.com" +// automatic: false +// avatar: "https://d2g02b6ed2w9fz.cloudfront.net/dea8ae7de1308911b35e0b1189ca85d379dbdb83_128.jpeg" +// clientID: "" +// created: "2023-01-16 19:18:52.966" +// message: Array(2) +// 0: +// {type: 'TEXT', style: 'strong', text: 'You'} +// 1: +// {type: 'TEXT', style: 'normal', text: ' closed this report'} + +// originalMessage: +// policyName: "Test's Workspace" +// reason: "policyDeleted" +// person: Array(1) +// 0: {type: 'TEXT', style: 'strong', text: 'Test User'} +// length: 1 +// reportActionID: "8939705822345818810" +// reportActionTimestamp: 1673896732966 +// sequenceNumber: 1 +// shouldShow: true +// timestamp: 1673896732 + /** * @param {String} policyID * @param {String} policyName @@ -1250,6 +1321,7 @@ export { isUnread, buildOptimisticWorkspaceChats, buildOptimisticChatReport, + buildOptimisticClosedReportAction, buildOptimisticCreatedReportAction, buildOptimisticIOUReport, buildOptimisticIOUReportAction, diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index 5a2de0e7e1f..f6081486cda 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -13,6 +13,7 @@ import ROUTES from '../../ROUTES'; import * as OptionsListUtils from '../OptionsListUtils'; import DateUtils from '../DateUtils'; import * as ReportUtils from '../ReportUtils'; +import {getMaxSequenceNumber} from './Report'; import Log from '../Log'; const allPolicies = {}; @@ -59,8 +60,9 @@ function updateLastAccessedWorkspace(policyID) { * * @param {String} policyID * @param {Array} reports + * @param {String} policyName */ -function deleteWorkspace(policyID, reports) { +function deleteWorkspace(policyID, reports, policyName) { const optimisticData = [ { onyxMethod: CONST.ONYX.METHOD.MERGE, @@ -80,6 +82,22 @@ function deleteWorkspace(policyID, reports) { oldPolicyName: allPolicies[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`].name, }, })), + ..._.map(reports, ({reportID, ownerEmail}) => { + const highestSequenceNumber = getMaxSequenceNumber(reportID); + const optimisticClosedReportAction = ReportUtils.buildOptimisticClosedReportAction( + highestSequenceNumber + 1, + ownerEmail, + policyName, + CONST.REPORT.ARCHIVE_REASON.POLICY_DELETED + ); + const optimisticReportActions = {}; + optimisticReportActions[optimisticClosedReportAction.clientID] = optimisticClosedReportAction; + return { + onyxMethod: CONST.ONYX.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, + value: optimisticReportActions, + } + }), ]; // Restore the old report stateNum and statusNum diff --git a/src/pages/workspace/WorkspaceInitialPage.js b/src/pages/workspace/WorkspaceInitialPage.js index 9ee7b141c5b..74d3706e31e 100644 --- a/src/pages/workspace/WorkspaceInitialPage.js +++ b/src/pages/workspace/WorkspaceInitialPage.js @@ -77,7 +77,7 @@ class WorkspaceInitialPage extends React.Component { */ confirmDeleteAndHideModal() { const policyReports = _.filter(this.props.reports, report => report && report.policyID === this.props.policy.id); - Policy.deleteWorkspace(this.props.policy.id, policyReports); + Policy.deleteWorkspace(this.props.policy.id, policyReports, this.props.policy.name); this.toggleDeleteModal(false); Navigation.navigate(ROUTES.SETTINGS_WORKSPACES); }