diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 359582b7977c..70dadec83653 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -950,6 +950,53 @@ function buildOptimisticCreatedReportAction(ownerEmail) { }; } +/** + * Returns the necessary reportAction onyx data to indicate that a chat has been archived + * + * @param {Number} sequenceNumber + * @param {String} ownerEmail + * @param {String} policyName + * @param {String} reason - A 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, + }; +} + /** * @param {String} policyID * @param {String} policyName @@ -1258,6 +1305,7 @@ export { isUnread, buildOptimisticWorkspaceChats, buildOptimisticChatReport, + buildOptimisticClosedReportAction, buildOptimisticCreatedReportAction, buildOptimisticIOUReport, buildOptimisticIOUReportAction, diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index f46285a76c67..7716c36c7f21 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 * as Report from './Report'; import Log from '../Log'; const allPolicies = {}; @@ -71,8 +72,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, @@ -92,6 +94,24 @@ function deleteWorkspace(policyID, reports) { oldPolicyName: allPolicies[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`].name, }, })), + + // Add closed actions to all chat reports linked to this policy + ..._.map(reports, ({reportID, ownerEmail}) => { + const highestSequenceNumber = Report.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/home/report/ReportActionsList.js b/src/pages/home/report/ReportActionsList.js index 36638f521dc7..5e61a042193a 100644 --- a/src/pages/home/report/ReportActionsList.js +++ b/src/pages/home/report/ReportActionsList.js @@ -175,7 +175,7 @@ class ReportActionsList extends React.Component { // Make sure the oldest report action loaded is not the first. This is so we do not show the // skeleton view above the created action in a newly generated optimistic chat or one with not // that many comments. - const lastReportAction = _.last(this.props.sortedReportActions); + const lastReportAction = _.last(this.props.sortedReportActions) || {}; if (this.props.report.isLoadingReportActions && lastReportAction.sequenceNumber > 0) { return ( 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); }