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 @@ -323,11 +323,19 @@ type OptimisticTaskReportAction = Pick<
| 'linkMetadata'
>;

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 @@ -5513,24 +5521,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 @@ -5575,10 +5672,6 @@ function buildOptimisticWorkspaceChats(policyID: string, policyName: string, exp
};

return {
announceChatReportID,
announceChatData,
announceReportActionData,
announceCreatedReportActionID: announceCreatedAction.reportActionID,
adminsChatReportID,
adminsChatData,
adminsReportActionData,
Expand Down Expand Up @@ -7852,6 +7945,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 @@ -3397,8 +3397,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 @@ -3474,8 +3472,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 @@ -6642,23 +6638,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 @@ -7580,8 +7563,6 @@ function payInvoice(paymentMethodType: PaymentMethodType, chatReport: OnyxTypes.
params: {
reportActionID,
policyID,
announceChatReportID,
announceCreatedReportActionID,
adminsChatReportID,
adminsCreatedReportActionID,
expenseChatReportID,
Expand All @@ -7607,8 +7588,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 @@ -602,7 +602,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 @@ -611,6 +611,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 @@ -637,7 +639,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 @@ -648,7 +650,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 @@ -662,10 +664,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