diff --git a/src/libs/API/parameters/AddMembersToWorkspaceParams.ts b/src/libs/API/parameters/AddMembersToWorkspaceParams.ts index 4e96fd07d301..abfed55e2df3 100644 --- a/src/libs/API/parameters/AddMembersToWorkspaceParams.ts +++ b/src/libs/API/parameters/AddMembersToWorkspaceParams.ts @@ -3,6 +3,8 @@ type AddMembersToWorkspaceParams = { welcomeNote: string; policyID: string; reportCreationData?: string; + announceChatReportID?: string; + announceCreatedReportActionID?: string; }; export default AddMembersToWorkspaceParams; diff --git a/src/libs/API/parameters/CreateWorkspaceFromIOUPaymentParams.ts b/src/libs/API/parameters/CreateWorkspaceFromIOUPaymentParams.ts index 761a6c2f5008..a1256f5ad051 100644 --- a/src/libs/API/parameters/CreateWorkspaceFromIOUPaymentParams.ts +++ b/src/libs/API/parameters/CreateWorkspaceFromIOUPaymentParams.ts @@ -1,13 +1,11 @@ type CreateWorkspaceFromIOUPaymentParams = { policyID: string; - announceChatReportID: string; adminsChatReportID: string; expenseChatReportID: string; ownerEmail: string; makeMeAdmin: boolean; policyName: string; type: string; - announceCreatedReportActionID: string; adminsCreatedReportActionID: string; expenseCreatedReportActionID: string; customUnitID: string; diff --git a/src/libs/API/parameters/CreateWorkspaceParams.ts b/src/libs/API/parameters/CreateWorkspaceParams.ts index c86598b48953..18ef4a0e763f 100644 --- a/src/libs/API/parameters/CreateWorkspaceParams.ts +++ b/src/libs/API/parameters/CreateWorkspaceParams.ts @@ -1,13 +1,11 @@ type CreateWorkspaceParams = { policyID: string; - announceChatReportID: string; adminsChatReportID: string; expenseChatReportID: string; ownerEmail: string; makeMeAdmin: boolean; policyName: string; type: string; - announceCreatedReportActionID: string; adminsCreatedReportActionID: string; expenseCreatedReportActionID: string; customUnitID: string; diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index c95f6b5a371a..e7479920b260 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -219,7 +219,7 @@ const isPolicyOwner = (policy: OnyxInputOrEntry, currentUserAccountID: n * * If includeMemberWithErrors is false, We only return members without errors. Otherwise, the members with errors would immediately be removed before the user has a chance to read the error. */ -function getMemberAccountIDsForWorkspace(employeeList: PolicyEmployeeList | undefined, includeMemberWithErrors = false): MemberEmailsToAccountIDs { +function getMemberAccountIDsForWorkspace(employeeList: PolicyEmployeeList | undefined, includeMemberWithErrors = false, includeMemberWithPendingDelete = true): MemberEmailsToAccountIDs { const members = employeeList ?? {}; const memberEmailsToAccountIDs: MemberEmailsToAccountIDs = {}; Object.keys(members).forEach((email) => { @@ -229,6 +229,12 @@ function getMemberAccountIDsForWorkspace(employeeList: PolicyEmployeeList | unde return; } } + if (!includeMemberWithPendingDelete) { + const member = members?.[email]; + if (member.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) { + return; + } + } const personalDetail = getPersonalDetailByEmail(email); if (!personalDetail?.login) { return; diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 684274bc0079..21cc02f19237 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -351,11 +351,19 @@ type OptimisticTaskReportAction = Pick< | 'delegateAccountID' >; -type OptimisticWorkspaceChats = { +type AnnounceRoomOnyxData = { + onyxOptimisticData: OnyxUpdate[]; + onyxSuccessData: OnyxUpdate[]; + onyxFailureData: OnyxUpdate[]; +}; + +type OptimisticAnnounceChat = { announceChatReportID: string; - announceChatData: OptimisticChatReport; - announceReportActionData: Record; - announceCreatedReportActionID: string; + announceChatReportActionID: string; + announceChatData: AnnounceRoomOnyxData; +}; + +type OptimisticWorkspaceChats = { adminsChatReportID: string; adminsChatData: OptimisticChatReport; adminsReportActionData: Record; @@ -5603,24 +5611,113 @@ function buildOptimisticDismissedViolationReportAction( }; } -function buildOptimisticWorkspaceChats(policyID: string, policyName: string, expenseReportId?: string): OptimisticWorkspaceChats { +function buildOptimisticAnnounceChat(policyID: string, accountIDs: number[]): OptimisticAnnounceChat { + const announceReport = getRoom(CONST.REPORT.CHAT_TYPE.POLICY_ANNOUNCE, policyID); + const policy = getPolicy(policyID); + const announceRoomOnyxData: AnnounceRoomOnyxData = { + onyxOptimisticData: [], + onyxSuccessData: [], + onyxFailureData: [], + }; + + // Do not create #announce room if the room already exists or if there are less than 3 participants in workspace + if (accountIDs.length < 3 || announceReport) { + return { + announceChatReportID: '', + announceChatReportActionID: '', + announceChatData: announceRoomOnyxData, + }; + } + const announceChatData = buildOptimisticChatReport( - currentUserAccountID ? [currentUserAccountID] : [], + accountIDs, CONST.REPORT.WORKSPACE_CHAT_ROOMS.ANNOUNCE, CONST.REPORT.CHAT_TYPE.POLICY_ANNOUNCE, policyID, CONST.POLICY.OWNER_ACCOUNT_ID_FAKE, false, - policyName, + policy?.name, undefined, CONST.REPORT.WRITE_CAPABILITIES.ADMINS, CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, ); - const announceChatReportID = announceChatData.reportID; const announceCreatedAction = buildOptimisticCreatedReportAction(CONST.POLICY.OWNER_EMAIL_FAKE); - const announceReportActionData = { - [announceCreatedAction.reportActionID]: announceCreatedAction, + announceRoomOnyxData.onyxOptimisticData.push( + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT}${announceChatData.reportID}`, + value: { + pendingFields: { + addWorkspaceRoom: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + }, + ...announceChatData, + }, + }, + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT_DRAFT}${announceChatData.reportID}`, + value: null, + }, + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${announceChatData.reportID}`, + value: { + [announceCreatedAction.reportActionID]: announceCreatedAction, + }, + }, + ); + announceRoomOnyxData.onyxSuccessData.push( + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${announceChatData.reportID}`, + value: { + pendingFields: { + addWorkspaceRoom: null, + }, + pendingAction: null, + isOptimisticReport: false, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${announceChatData.reportID}`, + value: { + [announceCreatedAction.reportActionID]: { + pendingAction: null, + }, + }, + }, + ); + announceRoomOnyxData.onyxFailureData.push( + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${announceChatData.reportID}`, + value: { + pendingFields: { + addWorkspaceRoom: null, + }, + pendingAction: null, + isOptimisticReport: false, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${announceChatData.reportID}`, + value: { + [announceCreatedAction.reportActionID]: { + pendingAction: null, + }, + }, + }, + ); + return { + announceChatReportID: announceChatData.reportID, + announceChatReportActionID: announceCreatedAction.reportActionID, + announceChatData: announceRoomOnyxData, }; +} + +function buildOptimisticWorkspaceChats(policyID: string, policyName: string, expenseReportId?: string): OptimisticWorkspaceChats { const pendingChatMembers = getPendingChatMembers(currentUserAccountID ? [currentUserAccountID] : [], [], CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD); const adminsChatData = { ...buildOptimisticChatReport( @@ -5665,10 +5762,6 @@ function buildOptimisticWorkspaceChats(policyID: string, policyName: string, exp }; return { - announceChatReportID, - announceChatData, - announceReportActionData, - announceCreatedReportActionID: announceCreatedAction.reportActionID, adminsChatReportID, adminsChatData, adminsReportActionData, @@ -7942,6 +8035,7 @@ export { buildOptimisticTaskReport, buildOptimisticTaskReportAction, buildOptimisticUnHoldReportAction, + buildOptimisticAnnounceChat, buildOptimisticWorkspaceChats, buildParticipantsFromAccountIDs, buildTransactionThread, diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index bc553ea86d70..5262cc4dc4ff 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -3398,8 +3398,6 @@ function categorizeTrackedExpense( receipt, policyExpenseChatReportID: createdWorkspaceParams?.expenseChatReportID, policyExpenseCreatedReportActionID: createdWorkspaceParams?.expenseCreatedReportActionID, - announceChatReportID: createdWorkspaceParams?.announceChatReportID, - announceCreatedReportActionID: createdWorkspaceParams?.announceCreatedReportActionID, adminsChatReportID: createdWorkspaceParams?.adminsChatReportID, adminsCreatedReportActionID: createdWorkspaceParams?.adminsCreatedReportActionID, }; @@ -3475,8 +3473,6 @@ function shareTrackedExpense( receipt, policyExpenseChatReportID: createdWorkspaceParams?.expenseChatReportID, policyExpenseCreatedReportActionID: createdWorkspaceParams?.expenseCreatedReportActionID, - announceChatReportID: createdWorkspaceParams?.announceChatReportID, - announceCreatedReportActionID: createdWorkspaceParams?.announceCreatedReportActionID, adminsChatReportID: createdWorkspaceParams?.adminsChatReportID, adminsCreatedReportActionID: createdWorkspaceParams?.adminsCreatedReportActionID, }; @@ -6643,23 +6639,10 @@ function getPayMoneyRequestParams( successData: policySuccessData, params, } = Policy.buildPolicyData(currentUserEmail, true, undefined, payerPolicyID); - const { - announceChatReportID, - announceCreatedReportActionID, - adminsChatReportID, - adminsCreatedReportActionID, - expenseChatReportID, - expenseCreatedReportActionID, - customUnitRateID, - customUnitID, - ownerEmail, - policyName, - } = params; + const {adminsChatReportID, adminsCreatedReportActionID, expenseChatReportID, expenseCreatedReportActionID, customUnitRateID, customUnitID, ownerEmail, policyName} = params; policyParams = { policyID: payerPolicyID, - announceChatReportID, - announceCreatedReportActionID, adminsChatReportID, adminsCreatedReportActionID, expenseChatReportID, @@ -7581,8 +7564,6 @@ function payInvoice(paymentMethodType: PaymentMethodType, chatReport: OnyxTypes. params: { reportActionID, policyID, - announceChatReportID, - announceCreatedReportActionID, adminsChatReportID, adminsCreatedReportActionID, expenseChatReportID, @@ -7608,8 +7589,6 @@ function payInvoice(paymentMethodType: PaymentMethodType, chatReport: OnyxTypes. params = { ...params, policyID, - announceChatReportID, - announceCreatedReportActionID, adminsChatReportID, adminsCreatedReportActionID, expenseChatReportID, diff --git a/src/libs/actions/Policy/Member.ts b/src/libs/actions/Policy/Member.ts index 8c2a66a8ccf6..f4d2287aca4c 100644 --- a/src/libs/actions/Policy/Member.ts +++ b/src/libs/actions/Policy/Member.ts @@ -605,7 +605,7 @@ function clearWorkspaceOwnerChangeFlow(policyID: string) { * Adds members to the specified workspace/policyID * Please see https://github.com/Expensify/App/blob/main/README.md#Security for more details */ -function addMembersToWorkspace(invitedEmailsToAccountIDs: InvitedEmailsToAccountIDs, welcomeNote: string, policyID: string) { +function addMembersToWorkspace(invitedEmailsToAccountIDs: InvitedEmailsToAccountIDs, welcomeNote: string, policyID: string, policyMemberAccountIDs: number[]) { const policyKey = `${ONYXKEYS.COLLECTION.POLICY}${policyID}` as const; const logins = Object.keys(invitedEmailsToAccountIDs).map((memberLogin) => PhoneNumber.addSMSDomainIfPhoneNumber(memberLogin)); const accountIDs = Object.values(invitedEmailsToAccountIDs); @@ -614,6 +614,8 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs: InvitedEmailsToAccount const newPersonalDetailsOnyxData = PersonalDetailsUtils.getPersonalDetailsOnyxDataForOptimisticUsers(newLogins, newAccountIDs); const announceRoomMembers = buildAnnounceRoomMembersOnyxData(policyID, accountIDs); + const optimisticAnnounceChat = ReportUtils.buildOptimisticAnnounceChat(policyID, [...policyMemberAccountIDs, ...accountIDs]); + const announceRoomChat = optimisticAnnounceChat.announceChatData; // create onyx data for policy expense chats for each new member const membersChats = createPolicyExpenseChats(policyID, invitedEmailsToAccountIDs); @@ -640,7 +642,7 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs: InvitedEmailsToAccount }, }, ]; - optimisticData.push(...newPersonalDetailsOnyxData.optimisticData, ...membersChats.onyxOptimisticData, ...announceRoomMembers.onyxOptimisticData); + optimisticData.push(...newPersonalDetailsOnyxData.optimisticData, ...membersChats.onyxOptimisticData, ...announceRoomChat.onyxOptimisticData, ...announceRoomMembers.onyxOptimisticData); const successData: OnyxUpdate[] = [ { @@ -651,7 +653,7 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs: InvitedEmailsToAccount }, }, ]; - successData.push(...newPersonalDetailsOnyxData.finallyData, ...membersChats.onyxSuccessData, ...announceRoomMembers.onyxSuccessData); + successData.push(...newPersonalDetailsOnyxData.finallyData, ...membersChats.onyxSuccessData, ...announceRoomChat.onyxSuccessData, ...announceRoomMembers.onyxSuccessData); const failureData: OnyxUpdate[] = [ { @@ -665,10 +667,12 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs: InvitedEmailsToAccount }, }, ]; - failureData.push(...membersChats.onyxFailureData, ...announceRoomMembers.onyxFailureData); + failureData.push(...membersChats.onyxFailureData, ...announceRoomChat.onyxFailureData, ...announceRoomMembers.onyxFailureData); const params: AddMembersToWorkspaceParams = { employees: JSON.stringify(logins.map((login) => ({email: login}))), + ...(optimisticAnnounceChat.announceChatReportID ? {announceChatReportID: optimisticAnnounceChat.announceChatReportID} : {}), + ...(optimisticAnnounceChat.announceChatReportActionID ? {announceCreatedReportActionID: optimisticAnnounceChat.announceChatReportActionID} : {}), welcomeNote: Parser.replace(welcomeNote), policyID, }; diff --git a/src/libs/actions/Policy/Policy.ts b/src/libs/actions/Policy/Policy.ts index 8f8bba8e916f..60cab1787700 100644 --- a/src/libs/actions/Policy/Policy.ts +++ b/src/libs/actions/Policy/Policy.ts @@ -1532,10 +1532,6 @@ function buildPolicyData(policyOwnerEmail = '', makeMeAdmin = false, policyName const {customUnits, customUnitID, customUnitRateID, outputCurrency} = buildOptimisticCustomUnits(); const { - announceChatReportID, - announceChatData, - announceReportActionData, - announceCreatedReportActionID, adminsChatReportID, adminsChatData, adminsReportActionData, @@ -1593,26 +1589,6 @@ function buildPolicyData(policyOwnerEmail = '', makeMeAdmin = false, policyName }, }, }, - { - onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.REPORT}${announceChatReportID}`, - value: { - pendingFields: { - addWorkspaceRoom: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, - }, - ...announceChatData, - }, - }, - { - onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.REPORT_DRAFT}${announceChatReportID}`, - value: null, - }, - { - onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${announceChatReportID}`, - value: announceReportActionData, - }, { onyxMethod: Onyx.METHOD.SET, key: `${ONYXKEYS.COLLECTION.REPORT}${adminsChatReportID}`, @@ -1672,26 +1648,6 @@ function buildPolicyData(policyOwnerEmail = '', makeMeAdmin = false, policyName }, }, }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${announceChatReportID}`, - value: { - pendingFields: { - addWorkspaceRoom: null, - }, - pendingAction: null, - isOptimisticReport: false, - }, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${announceChatReportID}`, - value: { - [announceCreatedReportActionID]: { - pendingAction: null, - }, - }, - }, { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${adminsChatReportID}`, @@ -1741,16 +1697,6 @@ function buildPolicyData(policyOwnerEmail = '', makeMeAdmin = false, policyName key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, value: {employeeList: null}, }, - { - onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.REPORT}${announceChatReportID}`, - value: null, - }, - { - onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${announceChatReportID}`, - value: null, - }, { onyxMethod: Onyx.METHOD.SET, key: `${ONYXKEYS.COLLECTION.REPORT}${adminsChatReportID}`, @@ -1787,14 +1733,12 @@ function buildPolicyData(policyOwnerEmail = '', makeMeAdmin = false, policyName const params: CreateWorkspaceParams = { policyID, - announceChatReportID, adminsChatReportID, expenseChatReportID, ownerEmail: policyOwnerEmail, makeMeAdmin, policyName: workspaceName, type: CONST.POLICY.TYPE.TEAM, - announceCreatedReportActionID, adminsCreatedReportActionID, expenseCreatedReportActionID, customUnitID, @@ -1832,8 +1776,10 @@ function createDraftWorkspace(policyOwnerEmail = '', makeMeAdmin = false, policy const {customUnits, customUnitID, customUnitRateID, outputCurrency} = buildOptimisticCustomUnits(); - const {expenseChatData, announceChatReportID, announceCreatedReportActionID, adminsChatReportID, adminsCreatedReportActionID, expenseChatReportID, expenseCreatedReportActionID} = - ReportUtils.buildOptimisticWorkspaceChats(policyID, workspaceName); + const {expenseChatData, adminsChatReportID, adminsCreatedReportActionID, expenseChatReportID, expenseCreatedReportActionID} = ReportUtils.buildOptimisticWorkspaceChats( + policyID, + workspaceName, + ); const optimisticData: OnyxUpdate[] = [ { @@ -1897,14 +1843,12 @@ function createDraftWorkspace(policyOwnerEmail = '', makeMeAdmin = false, policy const params: CreateWorkspaceParams = { policyID, - announceChatReportID, adminsChatReportID, expenseChatReportID, ownerEmail: policyOwnerEmail, makeMeAdmin, policyName: workspaceName, type: CONST.POLICY.TYPE.TEAM, - announceCreatedReportActionID, adminsCreatedReportActionID, expenseCreatedReportActionID, customUnitID, @@ -2168,10 +2112,6 @@ function createWorkspaceFromIOUPayment(iouReport: OnyxEntry): WorkspaceF const iouReportID = iouReport.reportID; const { - announceChatReportID, - announceChatData, - announceReportActionData, - announceCreatedReportActionID, adminsChatReportID, adminsChatData, adminsReportActionData, @@ -2238,21 +2178,6 @@ function createWorkspaceFromIOUPayment(iouReport: OnyxEntry): WorkspaceF key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, value: newWorkspace, }, - { - onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.REPORT}${announceChatReportID}`, - value: { - pendingFields: { - addWorkspaceRoom: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, - }, - ...announceChatData, - }, - }, - { - onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${announceChatReportID}`, - value: announceReportActionData, - }, { onyxMethod: Onyx.METHOD.SET, key: `${ONYXKEYS.COLLECTION.REPORT}${adminsChatReportID}`, @@ -2309,25 +2234,6 @@ function createWorkspaceFromIOUPayment(iouReport: OnyxEntry): WorkspaceF }, }, }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${announceChatReportID}`, - value: { - pendingFields: { - addWorkspaceRoom: null, - }, - pendingAction: null, - }, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${announceChatReportID}`, - value: { - [Object.keys(announceChatData)[0]]: { - pendingAction: null, - }, - }, - }, { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${adminsChatReportID}`, @@ -2370,23 +2276,6 @@ function createWorkspaceFromIOUPayment(iouReport: OnyxEntry): WorkspaceF successData.push(...employeeWorkspaceChat.onyxSuccessData); const failureData: OnyxUpdate[] = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${announceChatReportID}`, - value: { - pendingFields: { - addWorkspaceRoom: null, - }, - pendingAction: null, - }, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${announceChatReportID}`, - value: { - pendingAction: null, - }, - }, { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${adminsChatReportID}`, @@ -2589,14 +2478,12 @@ function createWorkspaceFromIOUPayment(iouReport: OnyxEntry): WorkspaceF const params: CreateWorkspaceFromIOUPaymentParams = { policyID, - announceChatReportID, adminsChatReportID, expenseChatReportID: workspaceChatReportID, ownerEmail: '', makeMeAdmin: false, policyName: workspaceName, type: CONST.POLICY.TYPE.TEAM, - announceCreatedReportActionID, adminsCreatedReportActionID, expenseCreatedReportActionID: workspaceChatCreatedReportActionID, customUnitID, diff --git a/src/pages/workspace/WorkspaceInviteMessagePage.tsx b/src/pages/workspace/WorkspaceInviteMessagePage.tsx index ff5b7326af84..3899656424ac 100644 --- a/src/pages/workspace/WorkspaceInviteMessagePage.tsx +++ b/src/pages/workspace/WorkspaceInviteMessagePage.tsx @@ -1,9 +1,8 @@ import type {StackScreenProps} from '@react-navigation/stack'; import lodashDebounce from 'lodash/debounce'; -import React, {useEffect, useMemo, useState} from 'react'; +import React, {useCallback, useEffect, useMemo, useState} from 'react'; import {Keyboard, View} from 'react-native'; -import {withOnyx} from 'react-native-onyx'; -import type {OnyxEntry} from 'react-native-onyx'; +import {useOnyx} from 'react-native-onyx'; import type {GestureResponderEvent} from 'react-native/Libraries/Types/CoreEventTypes'; import FormProvider from '@components/Form/FormProvider'; import InputWrapper from '@components/Form/InputWrapper'; @@ -35,36 +34,16 @@ import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; import INPUT_IDS from '@src/types/form/WorkspaceInviteMessageForm'; -import type {InvitedEmailsToAccountIDs, PersonalDetailsList} from '@src/types/onyx'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import AccessOrNotFoundWrapper from './AccessOrNotFoundWrapper'; import withPolicyAndFullscreenLoading from './withPolicyAndFullscreenLoading'; import type {WithPolicyAndFullscreenLoadingProps} from './withPolicyAndFullscreenLoading'; -type WorkspaceInviteMessagePageOnyxProps = { - /** All of the personal details for everyone */ - allPersonalDetails: OnyxEntry; - - /** An object containing the accountID for every invited user email */ - invitedEmailsToAccountIDsDraft: OnyxEntry; - - /** Updated workspace invite message */ - workspaceInviteMessageDraft: OnyxEntry; -}; - type WorkspaceInviteMessagePageProps = WithPolicyAndFullscreenLoadingProps & WithCurrentUserPersonalDetailsProps & - WorkspaceInviteMessagePageOnyxProps & StackScreenProps; -function WorkspaceInviteMessagePage({ - workspaceInviteMessageDraft, - invitedEmailsToAccountIDsDraft, - policy, - route, - allPersonalDetails, - currentUserPersonalDetails, -}: WorkspaceInviteMessagePageProps) { +function WorkspaceInviteMessagePage({policy, route, currentUserPersonalDetails}: WorkspaceInviteMessagePageProps) { const styles = useThemeStyles(); const {translate} = useLocalize(); @@ -72,21 +51,28 @@ function WorkspaceInviteMessagePage({ const {inputCallbackRef, inputRef} = useAutoFocusInput(); + const [invitedEmailsToAccountIDsDraft] = useOnyx(`${ONYXKEYS.COLLECTION.WORKSPACE_INVITE_MEMBERS_DRAFT}${route.params.policyID.toString()}`); + const [workspaceInviteMessageDraft] = useOnyx(`${ONYXKEYS.COLLECTION.WORKSPACE_INVITE_MESSAGE_DRAFT}${route.params.policyID.toString()}`); + const [allPersonalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST); + const welcomeNoteSubject = useMemo( () => `# ${currentUserPersonalDetails?.displayName ?? ''} invited you to ${policy?.name ?? 'a workspace'}`, [policy?.name, currentUserPersonalDetails?.displayName], ); - const getDefaultWelcomeNote = () => - // workspaceInviteMessageDraft can be an empty string - // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing - workspaceInviteMessageDraft || - // policy?.description can be an empty string - // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing - Parser.htmlToMarkdown(policy?.description ?? '') || - translate('workspace.common.welcomeNote', { - workspaceName: policy?.name ?? '', - }); + const getDefaultWelcomeNote = useCallback(() => { + return ( + // workspaceInviteMessageDraft can be an empty string + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing + workspaceInviteMessageDraft || + // policy?.description can be an empty string + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing + Parser.htmlToMarkdown(policy?.description ?? '') || + translate('workspace.common.welcomeNote', { + workspaceName: policy?.name ?? '', + }) + ); + }, [workspaceInviteMessageDraft, policy, translate]); useEffect(() => { if (!isEmptyObject(invitedEmailsToAccountIDsDraft)) { @@ -100,14 +86,22 @@ function WorkspaceInviteMessagePage({ // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps }, []); + useEffect(() => { + if (isEmptyObject(invitedEmailsToAccountIDsDraft)) { + return; + } + setWelcomeNote(getDefaultWelcomeNote()); + }, [getDefaultWelcomeNote, invitedEmailsToAccountIDsDraft]); + const debouncedSaveDraft = lodashDebounce((newDraft: string | null) => { Policy.setWorkspaceInviteMessageDraft(route.params.policyID, newDraft); }); const sendInvitation = () => { Keyboard.dismiss(); + const policyMemberAccountIDs = Object.values(PolicyUtils.getMemberAccountIDsForWorkspace(policy?.employeeList, false, false)); // Please see https://github.com/Expensify/App/blob/main/README.md#Security for more details - Member.addMembersToWorkspace(invitedEmailsToAccountIDsDraft ?? {}, `${welcomeNoteSubject}\n\n${welcomeNote}`, route.params.policyID); + Member.addMembersToWorkspace(invitedEmailsToAccountIDsDraft ?? {}, `${welcomeNoteSubject}\n\n${welcomeNote}`, route.params.policyID, policyMemberAccountIDs); debouncedSaveDraft(null); Navigation.dismissModal(); }; @@ -220,18 +214,4 @@ function WorkspaceInviteMessagePage({ WorkspaceInviteMessagePage.displayName = 'WorkspaceInviteMessagePage'; -export default withPolicyAndFullscreenLoading( - withCurrentUserPersonalDetails( - withOnyx({ - allPersonalDetails: { - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - }, - invitedEmailsToAccountIDsDraft: { - key: ({route}) => `${ONYXKEYS.COLLECTION.WORKSPACE_INVITE_MEMBERS_DRAFT}${route.params.policyID.toString()}`, - }, - workspaceInviteMessageDraft: { - key: ({route}) => `${ONYXKEYS.COLLECTION.WORKSPACE_INVITE_MESSAGE_DRAFT}${route.params.policyID.toString()}`, - }, - })(WorkspaceInviteMessagePage), - ), -); +export default withPolicyAndFullscreenLoading(withCurrentUserPersonalDetails(WorkspaceInviteMessagePage)); diff --git a/src/pages/workspace/WorkspacesListPage.tsx b/src/pages/workspace/WorkspacesListPage.tsx index 435c62bbe73d..1dd3ee4a74d3 100755 --- a/src/pages/workspace/WorkspacesListPage.tsx +++ b/src/pages/workspace/WorkspacesListPage.tsx @@ -341,7 +341,7 @@ function WorkspacesListPage() { fallbackIcon: Expensicons.FallbackWorkspaceAvatar, policyID: policy.id, adminRoom: policyRooms?.[policy.id]?.adminRoom ?? policy.chatReportIDAdmins?.toString(), - announceRoom: policyRooms?.[policy.id]?.announceRoom ?? policy.chatReportIDAnnounce?.toString(), + announceRoom: policyRooms?.[policy.id]?.announceRoom ?? (policy.chatReportIDAnnounce ? policy.chatReportIDAnnounce?.toString() : ''), ownerAccountID: policy.ownerAccountID, role: policy.role, type: policy.type, diff --git a/tests/actions/PolicyTest.ts b/tests/actions/PolicyTest.ts index 01842b48b6f4..2ede9f5e5228 100644 --- a/tests/actions/PolicyTest.ts +++ b/tests/actions/PolicyTest.ts @@ -12,7 +12,6 @@ import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; const ESH_EMAIL = 'eshgupta1217@gmail.com'; const ESH_ACCOUNT_ID = 1; -const ESH_PARTICIPANT_ANNOUNCE_ROOM: Participant = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}; const ESH_PARTICIPANT_ADMINS_ROOM: Participant = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}; const ESH_PARTICIPANT_EXPENSE_CHAT = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}; const WORKSPACE_NAME = "Esh's Workspace"; @@ -37,7 +36,6 @@ describe('actions/Policy', () => { await waitForBatchedUpdates(); let adminReportID; - let announceReportID; let expenseReportID; const policyID = Policy.generatePolicyID(); @@ -75,9 +73,9 @@ describe('actions/Policy', () => { }); }); - // Three reports should be created: #announce, #admins and expense report + // Two reports should be created: #admins and expense report const workspaceReports = Object.values(allReports ?? {}).filter((report) => report?.policyID === policyID); - expect(workspaceReports.length).toBe(3); + expect(workspaceReports.length).toBe(2); workspaceReports.forEach((report) => { expect(report?.pendingFields?.addWorkspaceRoom).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD); switch (report?.chatType) { @@ -86,11 +84,6 @@ describe('actions/Policy', () => { adminReportID = report.reportID; break; } - case CONST.REPORT.CHAT_TYPE.POLICY_ANNOUNCE: { - expect(report?.participants).toEqual({[ESH_ACCOUNT_ID]: ESH_PARTICIPANT_ANNOUNCE_ROOM}); - announceReportID = report.reportID; - break; - } case CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT: { expect(report?.participants).toEqual({[ESH_ACCOUNT_ID]: ESH_PARTICIPANT_EXPENSE_CHAT}); expenseReportID = report.reportID; @@ -114,13 +107,12 @@ describe('actions/Policy', () => { // Each of the three reports should have a a `CREATED` action. let adminReportActions: ReportAction[] = Object.values(reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${adminReportID}`] ?? {}); - let announceReportActions: ReportAction[] = Object.values(reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${announceReportID}`] ?? {}); let expenseReportActions: ReportAction[] = Object.values(reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReportID}`] ?? {}); - let workspaceReportActions: ReportAction[] = adminReportActions.concat(announceReportActions, expenseReportActions); - [adminReportActions, announceReportActions, expenseReportActions].forEach((actions) => { + let workspaceReportActions: ReportAction[] = adminReportActions.concat(expenseReportActions); + [adminReportActions, expenseReportActions].forEach((actions) => { expect(actions.length).toBe(1); }); - [...adminReportActions, ...announceReportActions, ...expenseReportActions].forEach((reportAction) => { + [...adminReportActions, ...expenseReportActions].forEach((reportAction) => { expect(reportAction.actionName).toBe(CONST.REPORT.ACTIONS.TYPE.CREATED); expect(reportAction.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD); expect(reportAction.actorAccountID).toBe(ESH_ACCOUNT_ID); @@ -174,9 +166,8 @@ describe('actions/Policy', () => { // Check if the report action pending action was cleared adminReportActions = Object.values(reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${adminReportID}`] ?? {}); - announceReportActions = Object.values(reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${announceReportID}`] ?? {}); expenseReportActions = Object.values(reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReportID}`] ?? {}); - workspaceReportActions = adminReportActions.concat(announceReportActions, expenseReportActions); + workspaceReportActions = adminReportActions.concat(expenseReportActions); workspaceReportActions.forEach((reportAction) => { expect(reportAction.pendingAction).toBeFalsy(); });