Skip to content

Commit 0c69f5f

Browse files
Merge pull request Expensify#58174 from Shahidullah-Muffakir/refactor/57958-buildOptimisticChatReport-param-object
refactor buildOptimisticChatReport to use a params object
2 parents cda61b1 + 8a9925f commit 0c69f5f

File tree

8 files changed

+172
-164
lines changed

8 files changed

+172
-164
lines changed

src/libs/ReportUtils.ts

+69-73
Original file line numberDiff line numberDiff line change
@@ -6135,23 +6135,41 @@ function isWorkspaceChat(chatType: string) {
61356135
/**
61366136
* Builds an optimistic chat report with a randomly generated reportID and as much information as we currently have
61376137
*/
6138-
function buildOptimisticChatReport(
6139-
participantList: number[],
6140-
reportName: string = CONST.REPORT.DEFAULT_REPORT_NAME,
6141-
chatType?: ValueOf<typeof CONST.REPORT.CHAT_TYPE>,
6142-
policyID: string = CONST.POLICY.OWNER_EMAIL_FAKE,
6143-
ownerAccountID: number = CONST.REPORT.OWNER_ACCOUNT_ID_FAKE,
6138+
type BuildOptimisticChatReportParams = {
6139+
participantList: number[];
6140+
reportName?: string;
6141+
chatType?: ValueOf<typeof CONST.REPORT.CHAT_TYPE>;
6142+
policyID?: string;
6143+
ownerAccountID?: number;
6144+
isOwnPolicyExpenseChat?: boolean;
6145+
oldPolicyName?: string;
6146+
visibility?: ValueOf<typeof CONST.REPORT.VISIBILITY>;
6147+
writeCapability?: ValueOf<typeof CONST.REPORT.WRITE_CAPABILITIES>;
6148+
notificationPreference?: NotificationPreference;
6149+
parentReportActionID?: string;
6150+
parentReportID?: string;
6151+
description?: string;
6152+
avatarUrl?: string;
6153+
optimisticReportID?: string;
6154+
};
6155+
6156+
function buildOptimisticChatReport({
6157+
participantList,
6158+
reportName = CONST.REPORT.DEFAULT_REPORT_NAME,
6159+
chatType,
6160+
policyID = CONST.POLICY.OWNER_EMAIL_FAKE,
6161+
ownerAccountID = CONST.REPORT.OWNER_ACCOUNT_ID_FAKE,
61446162
isOwnPolicyExpenseChat = false,
61456163
oldPolicyName = '',
6146-
visibility?: ValueOf<typeof CONST.REPORT.VISIBILITY>,
6147-
writeCapability?: ValueOf<typeof CONST.REPORT.WRITE_CAPABILITIES>,
6148-
notificationPreference: NotificationPreference = CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS,
6164+
visibility,
6165+
writeCapability,
6166+
notificationPreference = CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS,
61496167
parentReportActionID = '',
61506168
parentReportID = '',
61516169
description = '',
61526170
avatarUrl = '',
61536171
optimisticReportID = '',
6154-
): OptimisticChatReport {
6172+
}: BuildOptimisticChatReportParams): OptimisticChatReport {
61556173
const isWorkspaceChatType = chatType && isWorkspaceChat(chatType);
61566174
const participants = participantList.reduce((reportParticipants: Participants, accountID: number) => {
61576175
const participant: ReportParticipant = {
@@ -6207,23 +6225,14 @@ function buildOptimisticGroupChatReport(
62076225
optimisticReportID?: string,
62086226
notificationPreference?: NotificationPreference,
62096227
) {
6210-
return buildOptimisticChatReport(
6211-
participantAccountIDs,
6228+
return buildOptimisticChatReport({
6229+
participantList: participantAccountIDs,
62126230
reportName,
6213-
CONST.REPORT.CHAT_TYPE.GROUP,
6214-
undefined,
6215-
undefined,
6216-
undefined,
6217-
undefined,
6218-
undefined,
6219-
undefined,
6231+
chatType: CONST.REPORT.CHAT_TYPE.GROUP,
62206232
notificationPreference,
6221-
undefined,
6222-
undefined,
6223-
undefined,
6224-
avatarUri,
6233+
avatarUrl: avatarUri,
62256234
optimisticReportID,
6226-
);
6235+
});
62276236
}
62286237

62296238
/**
@@ -6623,18 +6632,17 @@ function buildOptimisticAnnounceChat(policyID: string, accountIDs: number[]): Op
66236632
};
66246633
}
66256634

6626-
const announceChatData = buildOptimisticChatReport(
6627-
accountIDs,
6628-
CONST.REPORT.WORKSPACE_CHAT_ROOMS.ANNOUNCE,
6629-
CONST.REPORT.CHAT_TYPE.POLICY_ANNOUNCE,
6635+
const announceChatData = buildOptimisticChatReport({
6636+
participantList: accountIDs,
6637+
reportName: CONST.REPORT.WORKSPACE_CHAT_ROOMS.ANNOUNCE,
6638+
chatType: CONST.REPORT.CHAT_TYPE.POLICY_ANNOUNCE,
66306639
policyID,
6631-
CONST.POLICY.OWNER_ACCOUNT_ID_FAKE,
6632-
false,
6633-
policy?.name,
6634-
undefined,
6635-
CONST.REPORT.WRITE_CAPABILITIES.ADMINS,
6636-
CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS,
6637-
);
6640+
ownerAccountID: CONST.POLICY.OWNER_ACCOUNT_ID_FAKE,
6641+
oldPolicyName: policy?.name,
6642+
writeCapability: CONST.REPORT.WRITE_CAPABILITIES.ADMINS,
6643+
notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS,
6644+
});
6645+
66386646
const announceCreatedAction = buildOptimisticCreatedReportAction(CONST.POLICY.OWNER_EMAIL_FAKE);
66396647
announceRoomOnyxData.onyxOptimisticData.push(
66406648
{
@@ -6726,39 +6734,32 @@ function buildOptimisticAnnounceChat(policyID: string, accountIDs: number[]): Op
67266734
function buildOptimisticWorkspaceChats(policyID: string, policyName: string, expenseReportId?: string): OptimisticWorkspaceChats {
67276735
const pendingChatMembers = getPendingChatMembers(currentUserAccountID ? [currentUserAccountID] : [], [], CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD);
67286736
const adminsChatData = {
6729-
...buildOptimisticChatReport(
6730-
currentUserAccountID ? [currentUserAccountID] : [],
6731-
CONST.REPORT.WORKSPACE_CHAT_ROOMS.ADMINS,
6732-
CONST.REPORT.CHAT_TYPE.POLICY_ADMINS,
6737+
...buildOptimisticChatReport({
6738+
participantList: currentUserAccountID ? [currentUserAccountID] : [],
6739+
reportName: CONST.REPORT.WORKSPACE_CHAT_ROOMS.ADMINS,
6740+
chatType: CONST.REPORT.CHAT_TYPE.POLICY_ADMINS,
67336741
policyID,
6734-
CONST.POLICY.OWNER_ACCOUNT_ID_FAKE,
6735-
false,
6736-
policyName,
6737-
),
6742+
ownerAccountID: CONST.POLICY.OWNER_ACCOUNT_ID_FAKE,
6743+
oldPolicyName: policyName,
6744+
}),
67386745
};
67396746
const adminsChatReportID = adminsChatData.reportID;
67406747
const adminsCreatedAction = buildOptimisticCreatedReportAction(CONST.POLICY.OWNER_EMAIL_FAKE);
67416748
const adminsReportActionData = {
67426749
[adminsCreatedAction.reportActionID]: adminsCreatedAction,
67436750
};
67446751

6745-
const expenseChatData = buildOptimisticChatReport(
6746-
currentUserAccountID ? [currentUserAccountID] : [],
6747-
'',
6748-
CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
6752+
const expenseChatData = buildOptimisticChatReport({
6753+
participantList: currentUserAccountID ? [currentUserAccountID] : [],
6754+
reportName: '',
6755+
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
67496756
policyID,
6750-
currentUserAccountID,
6751-
true,
6752-
policyName,
6753-
undefined,
6754-
undefined,
6755-
undefined,
6756-
undefined,
6757-
undefined,
6758-
undefined,
6759-
undefined,
6760-
expenseReportId,
6761-
);
6757+
ownerAccountID: currentUserAccountID,
6758+
isOwnPolicyExpenseChat: true,
6759+
oldPolicyName: policyName,
6760+
optimisticReportID: expenseReportId,
6761+
});
6762+
67626763
const expenseChatReportID = expenseChatData.reportID;
67636764
const expenseReportCreatedAction = buildOptimisticCreatedReportAction(currentUserEmail ?? '');
67646765
const expenseReportActionData = {
@@ -6884,20 +6885,15 @@ function buildTransactionThread(
68846885
};
68856886
}
68866887

6887-
return buildOptimisticChatReport(
6888-
participantAccountIDs,
6889-
getTransactionReportName({reportAction}),
6890-
undefined,
6891-
moneyRequestReport?.policyID,
6892-
CONST.POLICY.OWNER_ACCOUNT_ID_FAKE,
6893-
false,
6894-
'',
6895-
undefined,
6896-
undefined,
6897-
CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN,
6898-
reportAction?.reportActionID,
6899-
moneyRequestReport?.reportID,
6900-
);
6888+
return buildOptimisticChatReport({
6889+
participantList: participantAccountIDs,
6890+
reportName: getTransactionReportName({reportAction}),
6891+
policyID: moneyRequestReport?.policyID,
6892+
ownerAccountID: CONST.POLICY.OWNER_ACCOUNT_ID_FAKE,
6893+
notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN,
6894+
parentReportActionID: reportAction?.reportActionID,
6895+
parentReportID: moneyRequestReport?.reportID,
6896+
});
69016897
}
69026898

69036899
/**

src/libs/actions/IOU.ts

+34-19
Original file line numberDiff line numberDiff line change
@@ -2565,7 +2565,11 @@ function getSendInvoiceInformation(
25652565

25662566
if (!chatReport) {
25672567
isNewChatReport = true;
2568-
chatReport = buildOptimisticChatReport([receiverAccountID, currentUserAccountID], CONST.REPORT.DEFAULT_REPORT_NAME, CONST.REPORT.CHAT_TYPE.INVOICE, senderWorkspaceID);
2568+
chatReport = buildOptimisticChatReport({
2569+
participantList: [receiverAccountID, currentUserAccountID],
2570+
chatType: CONST.REPORT.CHAT_TYPE.INVOICE,
2571+
policyID: senderWorkspaceID,
2572+
});
25692573
}
25702574

25712575
// STEP 2: Create a new optimistic invoice report.
@@ -2713,7 +2717,9 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma
27132717
// If we still don't have a report, it likely doens't exist and we need to build an optimistic one
27142718
if (!chatReport) {
27152719
isNewChatReport = true;
2716-
chatReport = buildOptimisticChatReport([payerAccountID, payeeAccountID]);
2720+
chatReport = buildOptimisticChatReport({
2721+
participantList: [payerAccountID, payeeAccountID],
2722+
});
27172723
}
27182724

27192725
// STEP 2: Get the Expense/IOU report. If the moneyRequestReportID has been provided, we want to add the transaction to this specific report.
@@ -2964,7 +2970,9 @@ function getPerDiemExpenseInformation(perDiemExpenseInformation: PerDiemExpenseI
29642970
// If we still don't have a report, it likely doens't exist and we need to build an optimistic one
29652971
if (!chatReport) {
29662972
isNewChatReport = true;
2967-
chatReport = buildOptimisticChatReport([payerAccountID, payeeAccountID]);
2973+
chatReport = buildOptimisticChatReport({
2974+
participantList: [payerAccountID, payeeAccountID],
2975+
});
29682976
}
29692977

29702978
// STEP 2: Get the Expense/IOU report. If the moneyRequestReportID has been provided, we want to add the transaction to this specific report.
@@ -5115,23 +5123,20 @@ function getOrCreateOptimisticSplitChatReport(existingSplitChatReportID: string
51155123

51165124
// Create a Group Chat if we have multiple participants
51175125
if (participants.length > 1) {
5118-
const splitChatReport = buildOptimisticChatReport(
5119-
allParticipantsAccountIDs,
5120-
'',
5121-
CONST.REPORT.CHAT_TYPE.GROUP,
5122-
undefined,
5123-
undefined,
5124-
undefined,
5125-
undefined,
5126-
undefined,
5127-
undefined,
5128-
CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS,
5129-
);
5126+
const splitChatReport = buildOptimisticChatReport({
5127+
participantList: allParticipantsAccountIDs,
5128+
reportName: '',
5129+
chatType: CONST.REPORT.CHAT_TYPE.GROUP,
5130+
notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS,
5131+
});
5132+
51305133
return {existingSplitChatReport: null, splitChatReport};
51315134
}
51325135

51335136
// Otherwise, create a new 1:1 chat report
5134-
const splitChatReport = buildOptimisticChatReport(participantAccountIDs);
5137+
const splitChatReport = buildOptimisticChatReport({
5138+
participantList: participantAccountIDs,
5139+
});
51355140
return {existingSplitChatReport: null, splitChatReport};
51365141
}
51375142

@@ -5393,7 +5398,11 @@ function createSplitsAndOnyxData({
53935398
const existingChatReport = getChatByParticipants([accountID, currentUserAccountID]);
53945399
isNewOneOnOneChatReport = !existingChatReport;
53955400
shouldCreateOptimisticPersonalDetails = isNewOneOnOneChatReport && !personalDetailExists;
5396-
oneOnOneChatReport = existingChatReport ?? buildOptimisticChatReport([accountID, currentUserAccountID]);
5401+
oneOnOneChatReport =
5402+
existingChatReport ??
5403+
buildOptimisticChatReport({
5404+
participantList: [accountID, currentUserAccountID],
5405+
});
53975406
}
53985407

53995408
// STEP 2: Get existing IOU/Expense report and update its total OR build a new optimistic one
@@ -6183,7 +6192,11 @@ function completeSplitBill(
61836192
} else {
61846193
const existingChatReport = getChatByParticipants(participant.accountID ? [participant.accountID, sessionAccountID] : []);
61856194
isNewOneOnOneChatReport = !existingChatReport;
6186-
oneOnOneChatReport = existingChatReport ?? buildOptimisticChatReport(participant.accountID ? [participant.accountID, sessionAccountID] : []);
6195+
oneOnOneChatReport =
6196+
existingChatReport ??
6197+
buildOptimisticChatReport({
6198+
participantList: participant.accountID ? [participant.accountID, sessionAccountID] : [],
6199+
});
61876200
}
61886201

61896202
let oneOnOneIOUReport: OneOnOneIOUReport = oneOnOneChatReport?.iouReportID ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oneOnOneChatReport.iouReportID}`] : null;
@@ -7296,7 +7309,9 @@ function getSendMoneyParams(
72967309
let chatReport = !isEmptyObject(report) && report?.reportID ? report : getChatByParticipants([recipientAccountID, managerID]);
72977310
let isNewChat = false;
72987311
if (!chatReport) {
7299-
chatReport = buildOptimisticChatReport([recipientAccountID, managerID]);
7312+
chatReport = buildOptimisticChatReport({
7313+
participantList: [recipientAccountID, managerID],
7314+
});
73007315
isNewChat = true;
73017316
}
73027317
const optimisticIOUReport = buildOptimisticIOUReport(recipientAccountID, managerID, amount, chatReport.reportID, currency, true);

src/libs/actions/Policy/Policy.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,12 @@ function createPolicyExpenseChats(policyID: string, invitedEmailsToAccountIDs: I
10861086
});
10871087
return;
10881088
}
1089-
const optimisticReport = ReportUtils.buildOptimisticChatReport([sessionAccountID, cleanAccountID], undefined, CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT, policyID, cleanAccountID);
1089+
const optimisticReport = ReportUtils.buildOptimisticChatReport({
1090+
participantList: [sessionAccountID, cleanAccountID],
1091+
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
1092+
policyID,
1093+
ownerAccountID: cleanAccountID,
1094+
});
10901095
const optimisticCreatedAction = ReportUtils.buildOptimisticCreatedReportAction(login);
10911096

10921097
workspaceMembersChats.reportCreationData[login] = {

0 commit comments

Comments
 (0)