Skip to content
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

create announce room when 3 or more participants #48660

Merged
merged 14 commits into from
Sep 27, 2024
Merged
2 changes: 2 additions & 0 deletions src/libs/API/parameters/AddMembersToWorkspaceParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ type AddMembersToWorkspaceParams = {
welcomeNote: string;
policyID: string;
reportCreationData?: string;
announceChatReportID?: string;
announceCreatedReportActionID?: string;
};

export default AddMembersToWorkspaceParams;
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 0 additions & 2 deletions src/libs/API/parameters/CreateWorkspaceParams.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
8 changes: 7 additions & 1 deletion src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ const isPolicyOwner = (policy: OnyxInputOrEntry<Policy>, 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) => {
Expand All @@ -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;
Expand Down
122 changes: 108 additions & 14 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, OptimisticCreatedReportAction>;
announceCreatedReportActionID: string;
announceChatReportActionID: string;
announceChatData: AnnounceRoomOnyxData;
};

type OptimisticWorkspaceChats = {
adminsChatReportID: string;
adminsChatData: OptimisticChatReport;
adminsReportActionData: Record<string, OptimisticCreatedReportAction>;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -5665,10 +5762,6 @@ function buildOptimisticWorkspaceChats(policyID: string, policyName: string, exp
};

return {
announceChatReportID,
announceChatData,
announceReportActionData,
announceCreatedReportActionID: announceCreatedAction.reportActionID,
adminsChatReportID,
adminsChatData,
adminsReportActionData,
Expand Down Expand Up @@ -7942,6 +8035,7 @@ export {
buildOptimisticTaskReport,
buildOptimisticTaskReportAction,
buildOptimisticUnHoldReportAction,
buildOptimisticAnnounceChat,
buildOptimisticWorkspaceChats,
buildParticipantsFromAccountIDs,
buildTransactionThread,
Expand Down
23 changes: 1 addition & 22 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -7581,8 +7564,6 @@ function payInvoice(paymentMethodType: PaymentMethodType, chatReport: OnyxTypes.
params: {
reportActionID,
policyID,
announceChatReportID,
announceCreatedReportActionID,
adminsChatReportID,
adminsCreatedReportActionID,
expenseChatReportID,
Expand All @@ -7608,8 +7589,6 @@ function payInvoice(paymentMethodType: PaymentMethodType, chatReport: OnyxTypes.
params = {
...params,
policyID,
announceChatReportID,
announceCreatedReportActionID,
adminsChatReportID,
adminsCreatedReportActionID,
expenseChatReportID,
Expand Down
12 changes: 8 additions & 4 deletions src/libs/actions/Policy/Member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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[] = [
{
Expand All @@ -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[] = [
{
Expand All @@ -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,
};
Expand Down
Loading
Loading