Skip to content

Commit ad99c20

Browse files
authored
Merge pull request #54247 from callstack-internal/chore/migrate-pendingChatMembers
migrate pendingChatMembers to reportMetadata
2 parents 5c750c5 + be6c47b commit ad99c20

17 files changed

+441
-187
lines changed

src/libs/DebugUtils.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -529,12 +529,6 @@ function validateReportDraftProperty(key: keyof Report, value: string) {
529529
},
530530
'number',
531531
);
532-
case 'pendingChatMembers':
533-
return validateArray<ArrayElement<Report, 'pendingChatMembers'>>(value, {
534-
accountID: 'string',
535-
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION,
536-
errors: 'object',
537-
});
538532
case 'fieldList':
539533
return validateObject<ObjectElement<Report, 'fieldList'>>(
540534
value,
@@ -616,7 +610,6 @@ function validateReportDraftProperty(key: keyof Report, value: string) {
616610
iouReportID: CONST.RED_BRICK_ROAD_PENDING_ACTION,
617611
preexistingReportID: CONST.RED_BRICK_ROAD_PENDING_ACTION,
618612
nonReimbursableTotal: CONST.RED_BRICK_ROAD_PENDING_ACTION,
619-
pendingChatMembers: CONST.RED_BRICK_ROAD_PENDING_ACTION,
620613
fieldList: CONST.RED_BRICK_ROAD_PENDING_ACTION,
621614
permissions: CONST.RED_BRICK_ROAD_PENDING_ACTION,
622615
tripData: CONST.RED_BRICK_ROAD_PENDING_ACTION,

src/libs/PolicyUtils.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,13 @@ const isPolicyUser = (policy: OnyxInputOrEntry<Policy>, currentUserLogin?: strin
246246
const isPolicyAuditor = (policy: OnyxInputOrEntry<Policy>, currentUserLogin?: string): boolean =>
247247
(policy?.role ?? (currentUserLogin && policy?.employeeList?.[currentUserLogin]?.role)) === CONST.POLICY.ROLE.AUDITOR;
248248

249-
const isPolicyEmployee = (policyID: string, policies: OnyxCollection<Policy>): boolean => Object.values(policies ?? {}).some((policy) => policy?.id === policyID);
249+
const isPolicyEmployee = (policyID: string | undefined, policies: OnyxCollection<Policy>): boolean => {
250+
if (!policyID) {
251+
return false;
252+
}
253+
254+
return Object.values(policies ?? {}).some((policy) => policy?.id === policyID);
255+
};
250256

251257
/**
252258
* Checks if the current user is an owner (creator) of the policy.
@@ -1112,6 +1118,10 @@ function hasVBBA(policyID: string) {
11121118
}
11131119

11141120
function getTagApproverRule(policyOrID: string | SearchPolicy | OnyxEntry<Policy>, tagName: string) {
1121+
if (!policyOrID) {
1122+
return;
1123+
}
1124+
11151125
const policy = typeof policyOrID === 'string' ? getPolicy(policyOrID) : policyOrID;
11161126

11171127
const approvalRules = policy?.rules?.approvalRules ?? [];

src/libs/ReportActionsUtils.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,11 @@ function getLinkedTransactionID(reportActionOrID: string | OnyxEntry<ReportActio
927927
return getOriginalMessage(reportAction)?.IOUTransactionID ?? null;
928928
}
929929

930-
function getReportAction(reportID: string, reportActionID: string): ReportAction | undefined {
930+
function getReportAction(reportID: string | undefined, reportActionID: string | undefined): ReportAction | undefined {
931+
if (!reportID || !reportActionID) {
932+
return undefined;
933+
}
934+
931935
return allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`]?.[reportActionID];
932936
}
933937

@@ -1599,7 +1603,11 @@ function getIOUActionForReportID(reportID: string, transactionID: string): OnyxE
15991603
/**
16001604
* Get the track expense actionable whisper of the corresponding track expense
16011605
*/
1602-
function getTrackExpenseActionableWhisper(transactionID: string, chatReportID: string) {
1606+
function getTrackExpenseActionableWhisper(transactionID: string | undefined, chatReportID: string | undefined) {
1607+
if (!transactionID || !chatReportID) {
1608+
return undefined;
1609+
}
1610+
16031611
const chatReportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReportID}`] ?? {};
16041612
return Object.values(chatReportActions).find((action: ReportAction) => isActionableTrackExpense(action) && getOriginalMessage(action)?.transactionID === transactionID);
16051613
}

src/libs/ReportUtils.ts

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ import type {ErrorFields, Errors, Icon, PendingAction} from '@src/types/onyx/Ony
5252
import type {OriginalMessageChangeLog, PaymentMethodType} from '@src/types/onyx/OriginalMessage';
5353
import type {Status} from '@src/types/onyx/PersonalDetails';
5454
import type {ConnectionName} from '@src/types/onyx/Policy';
55-
import type {NotificationPreference, Participants, PendingChatMember, Participant as ReportParticipant} from '@src/types/onyx/Report';
55+
import type {NotificationPreference, Participants, Participant as ReportParticipant} from '@src/types/onyx/Report';
5656
import type {Message, OldDotReportAction, ReportActions} from '@src/types/onyx/ReportAction';
57+
import type {PendingChatMember} from '@src/types/onyx/ReportMetadata';
5758
import type {SearchPolicy, SearchReport, SearchTransaction} from '@src/types/onyx/SearchResults';
5859
import type {Comment, TransactionChanges, WaypointCollection} from '@src/types/onyx/Transaction';
5960
import {isEmptyObject} from '@src/types/utils/EmptyObject';
@@ -384,6 +385,7 @@ type OptimisticWorkspaceChats = {
384385
expenseChatData: OptimisticChatReport;
385386
expenseReportActionData: Record<string, OptimisticCreatedReportAction>;
386387
expenseCreatedReportActionID: string;
388+
pendingChatMembers: PendingChatMember[];
387389
};
388390

389391
type OptimisticModifiedExpenseReportAction = Pick<
@@ -701,6 +703,7 @@ Onyx.connect({
701703
});
702704

703705
let allReportMetadata: OnyxCollection<ReportMetadata>;
706+
const allReportMetadataKeyValue: Record<string, ReportMetadata> = {};
704707
Onyx.connect({
705708
key: ONYXKEYS.COLLECTION.REPORT_METADATA,
706709
waitForCollectionCallback: true,
@@ -709,6 +712,15 @@ Onyx.connect({
709712
return;
710713
}
711714
allReportMetadata = value;
715+
716+
Object.entries(value).forEach(([reportID, reportMetadata]) => {
717+
if (!reportMetadata) {
718+
return;
719+
}
720+
721+
const [, id] = reportID.split('_');
722+
allReportMetadataKeyValue[id] = reportMetadata;
723+
});
712724
},
713725
});
714726

@@ -1759,7 +1771,11 @@ function isPayAtEndExpenseReport(reportID: string, transactions: Transaction[] |
17591771
/**
17601772
* Checks if a report is a transaction thread associated with a report that has only one transaction
17611773
*/
1762-
function isOneTransactionThread(reportID: string, parentReportID: string, threadParentReportAction: OnyxEntry<ReportAction>): boolean {
1774+
function isOneTransactionThread(reportID: string | undefined, parentReportID: string | undefined, threadParentReportAction: OnyxEntry<ReportAction>): boolean {
1775+
if (!reportID || !parentReportID) {
1776+
return false;
1777+
}
1778+
17631779
const parentReportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`] ?? ([] as ReportAction[]);
17641780
const transactionThreadReportID = ReportActionsUtils.getOneTransactionThreadReportID(parentReportID, parentReportActions);
17651781
return reportID === transactionThreadReportID && !ReportActionsUtils.isSentMoneyReportAction(threadParentReportAction);
@@ -2213,6 +2229,7 @@ function getDisplayNameForParticipant(
22132229

22142230
function getParticipantsAccountIDsForDisplay(report: OnyxEntry<Report>, shouldExcludeHidden = false, shouldExcludeDeleted = false, shouldForceExcludeCurrentUser = false): number[] {
22152231
const reportParticipants = report?.participants ?? {};
2232+
const reportMetadata = getReportMetadata(report?.reportID);
22162233
let participantsEntries = Object.entries(reportParticipants);
22172234

22182235
// We should not show participants that have an optimistic entry with the same login in the personal details
@@ -2252,7 +2269,7 @@ function getParticipantsAccountIDsForDisplay(report: OnyxEntry<Report>, shouldEx
22522269

22532270
if (
22542271
shouldExcludeDeleted &&
2255-
report?.pendingChatMembers?.findLast((member) => Number(member.accountID) === accountID)?.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE
2272+
reportMetadata?.pendingChatMembers?.findLast((member) => Number(member.accountID) === accountID)?.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE
22562273
) {
22572274
return false;
22582275
}
@@ -2313,8 +2330,10 @@ function getGroupChatName(participants?: SelectedParticipant[], shouldApplyLimit
23132330
return report.reportName;
23142331
}
23152332

2333+
const reportMetadata = getReportMetadata(report?.reportID);
2334+
23162335
const pendingMemberAccountIDs = new Set(
2317-
report?.pendingChatMembers?.filter((member) => member.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE).map((member) => member.accountID),
2336+
reportMetadata?.pendingChatMembers?.filter((member) => member.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE).map((member) => member.accountID),
23182337
);
23192338
let participantAccountIDs =
23202339
participants?.map((participant) => participant.accountID) ??
@@ -3017,7 +3036,11 @@ function getTitleReportField(reportFields: Record<string, PolicyReportField>) {
30173036
/**
30183037
* Get the key for a report field
30193038
*/
3020-
function getReportFieldKey(reportFieldId: string) {
3039+
function getReportFieldKey(reportFieldId: string | undefined) {
3040+
if (!reportFieldId) {
3041+
return '';
3042+
}
3043+
30213044
// We don't need to add `expensify_` prefix to the title field key, because backend stored title under a unique key `text_title`,
30223045
// and all the other report field keys are stored under `expensify_FIELD_ID`.
30233046
if (reportFieldId === CONST.REPORT_FIELD_TITLE_FIELD_ID) {
@@ -6071,7 +6094,6 @@ function buildOptimisticWorkspaceChats(policyID: string, policyName: string, exp
60716094
false,
60726095
policyName,
60736096
),
6074-
pendingChatMembers,
60756097
};
60766098
const adminsChatReportID = adminsChatData.reportID;
60776099
const adminsCreatedAction = buildOptimisticCreatedReportAction(CONST.POLICY.OWNER_EMAIL_FAKE);
@@ -6111,6 +6133,7 @@ function buildOptimisticWorkspaceChats(policyID: string, policyName: string, exp
61116133
expenseChatData,
61126134
expenseReportActionData,
61136135
expenseCreatedReportActionID: expenseReportCreatedAction.reportActionID,
6136+
pendingChatMembers,
61146137
};
61156138
}
61166139

@@ -8225,7 +8248,16 @@ function createDraftWorkspaceAndNavigateToConfirmationScreen(transactionID: stri
82258248
}
82268249
}
82278250

8228-
function createDraftTransactionAndNavigateToParticipantSelector(transactionID: string, reportID: string, actionName: IOUAction, reportActionID: string): void {
8251+
function createDraftTransactionAndNavigateToParticipantSelector(
8252+
transactionID: string | undefined,
8253+
reportID: string | undefined,
8254+
actionName: IOUAction,
8255+
reportActionID: string | undefined,
8256+
): void {
8257+
if (!transactionID || !reportID || !reportActionID) {
8258+
return;
8259+
}
8260+
82298261
const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] ?? ({} as Transaction);
82308262
const reportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`] ?? ([] as ReportAction[]);
82318263

@@ -8538,7 +8570,7 @@ function hasInvoiceReports() {
85388570
}
85398571

85408572
function getReportMetadata(reportID?: string) {
8541-
return allReportMetadata?.[`${ONYXKEYS.COLLECTION.REPORT_METADATA}${reportID}`];
8573+
return allReportMetadataKeyValue[reportID ?? '-1'];
85428574
}
85438575

85448576
export {

src/libs/actions/IOU.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5748,7 +5748,11 @@ function prepareToCleanUpMoneyRequest(transactionID: string, reportAction: OnyxT
57485748
* @param isSingleTransactionView - whether we are in the transaction thread report
57495749
* @returns The URL to navigate to
57505750
*/
5751-
function getNavigationUrlOnMoneyRequestDelete(transactionID: string, reportAction: OnyxTypes.ReportAction, isSingleTransactionView = false): Route | undefined {
5751+
function getNavigationUrlOnMoneyRequestDelete(transactionID: string | undefined, reportAction: OnyxTypes.ReportAction, isSingleTransactionView = false): Route | undefined {
5752+
if (!transactionID) {
5753+
return undefined;
5754+
}
5755+
57525756
const {shouldDeleteTransactionThread, shouldDeleteIOUReport, iouReport} = prepareToCleanUpMoneyRequest(transactionID, reportAction);
57535757

57545758
// Determine which report to navigate back to
@@ -5771,7 +5775,16 @@ function getNavigationUrlOnMoneyRequestDelete(transactionID: string, reportActio
57715775
* @param isSingleTransactionView - Whether we're in single transaction view
57725776
* @returns The URL to navigate to
57735777
*/
5774-
function getNavigationUrlAfterTrackExpenseDelete(chatReportID: string, transactionID: string, reportAction: OnyxTypes.ReportAction, isSingleTransactionView = false): Route | undefined {
5778+
function getNavigationUrlAfterTrackExpenseDelete(
5779+
chatReportID: string | undefined,
5780+
transactionID: string | undefined,
5781+
reportAction: OnyxTypes.ReportAction,
5782+
isSingleTransactionView = false,
5783+
): Route | undefined {
5784+
if (!chatReportID || !transactionID) {
5785+
return undefined;
5786+
}
5787+
57755788
const chatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${chatReportID}`] ?? null;
57765789

57775790
// If not a self DM, handle it as a regular money request
@@ -5946,7 +5959,11 @@ function cleanUpMoneyRequest(transactionID: string, reportAction: OnyxTypes.Repo
59465959
* @param isSingleTransactionView - whether we are in the transaction thread report
59475960
* @return the url to navigate back once the money request is deleted
59485961
*/
5949-
function deleteMoneyRequest(transactionID: string, reportAction: OnyxTypes.ReportAction, isSingleTransactionView = false) {
5962+
function deleteMoneyRequest(transactionID: string | undefined, reportAction: OnyxTypes.ReportAction, isSingleTransactionView = false) {
5963+
if (!transactionID) {
5964+
return;
5965+
}
5966+
59505967
// STEP 1: Calculate and prepare the data
59515968
const {
59525969
shouldDeleteTransactionThread,
@@ -6207,7 +6224,11 @@ function deleteMoneyRequest(transactionID: string, reportAction: OnyxTypes.Repor
62076224
return urlToNavigateBack;
62086225
}
62096226

6210-
function deleteTrackExpense(chatReportID: string, transactionID: string, reportAction: OnyxTypes.ReportAction, isSingleTransactionView = false) {
6227+
function deleteTrackExpense(chatReportID: string | undefined, transactionID: string | undefined, reportAction: OnyxTypes.ReportAction, isSingleTransactionView = false) {
6228+
if (!chatReportID || !transactionID) {
6229+
return;
6230+
}
6231+
62116232
const urlToNavigateBack = getNavigationUrlAfterTrackExpenseDelete(chatReportID, transactionID, reportAction, isSingleTransactionView);
62126233

62136234
// STEP 1: Get all collections we're updating

0 commit comments

Comments
 (0)