From 4c41990a93ec26fd840c2974e2e7af3fec615652 Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Fri, 16 Aug 2024 22:41:19 +0530 Subject: [PATCH 01/16] Migrating notification preference into participants --- src/libs/OptionsListUtils.ts | 6 +- src/libs/ReportUtils.ts | 85 +++++++++++-------- src/libs/SidebarUtils.ts | 4 +- src/libs/UnreadIndicatorUpdater/index.ts | 4 +- src/libs/actions/IOU.ts | 8 +- src/libs/actions/Report.ts | 73 +++++++++------- src/libs/actions/Task.ts | 19 +++-- src/libs/actions/User.ts | 5 +- src/pages/ProfilePage.tsx | 7 +- src/pages/ReportDetailsPage.tsx | 2 +- src/pages/RoomInvitePage.tsx | 2 +- src/pages/home/ReportScreen.tsx | 6 +- .../Report/NotificationPreferencePage.tsx | 7 +- .../settings/Report/ReportSettingsPage.tsx | 7 +- src/types/onyx/Report.ts | 5 +- tests/actions/IOUTest.ts | 31 ++++--- tests/actions/PolicyTest.ts | 2 +- tests/actions/ReportTest.ts | 18 +++- tests/ui/PaginationTest.tsx | 4 +- tests/ui/UnreadIndicatorsTest.tsx | 4 +- tests/unit/ReportUtilsTest.ts | 26 ++++-- tests/unit/UnreadIndicatorUpdaterTest.ts | 18 +++- 22 files changed, 213 insertions(+), 130 deletions(-) diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index 98274e829001..c0e3396cd57c 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -805,7 +805,7 @@ function createOption( result.isWaitingOnBankAccount = report.isWaitingOnBankAccount; result.policyID = report.policyID; result.isSelfDM = ReportUtils.isSelfDM(report); - result.notificationPreference = report.notificationPreference; + result.notificationPreference = ReportUtils.getReportNotificationPreference(report); const visibleParticipantAccountIDs = ReportUtils.getParticipantsAccountIDsForDisplay(report, true); @@ -899,7 +899,7 @@ function getPolicyExpenseReportOption(participant: Participant | ReportUtils.Opt const expenseReport = ReportUtils.isPolicyExpenseChat(participant) ? getReportOrDraftReport(participant.reportID) : null; const visibleParticipantAccountIDs = Object.entries(expenseReport?.participants ?? {}) - .filter(([, reportParticipant]) => reportParticipant && !reportParticipant.hidden) + .filter(([, reportParticipant]) => reportParticipant && reportParticipant.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) .map(([accountID]) => Number(accountID)); const option = createOption( @@ -2578,7 +2578,7 @@ function getEmptyOptions(): Options { } function shouldUseBoldText(report: ReportUtils.OptionData): boolean { - return report.isUnread === true && report.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE; + return report.isUnread === true && ReportUtils.getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE; } export { diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 28ede28bf06b..34fcbbef259b 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -154,10 +154,10 @@ type OptimisticExpenseReport = Pick< | 'statusNum' | 'total' | 'nonReimbursableTotal' - | 'notificationPreference' | 'parentReportID' | 'lastVisibleActionCreated' | 'parentReportActionID' + | 'participants' | 'fieldList' >; @@ -274,7 +274,6 @@ type OptimisticChatReport = Pick< | 'lastMessageText' | 'lastReadTime' | 'lastVisibleActionCreated' - | 'notificationPreference' | 'oldPolicyName' | 'ownerAccountID' | 'pendingFields' @@ -355,7 +354,6 @@ type OptimisticTaskReport = Pick< | 'policyID' | 'stateNum' | 'statusNum' - | 'notificationPreference' | 'parentReportActionID' | 'lastVisibleActionCreated' | 'hasParentAccess' @@ -395,7 +393,6 @@ type OptimisticIOUReport = Pick< | 'statusNum' | 'total' | 'reportName' - | 'notificationPreference' | 'parentReportID' | 'lastVisibleActionCreated' | 'fieldList' @@ -1153,6 +1150,32 @@ function isSystemChat(report: OnyxEntry): boolean { return getChatType(report) === CONST.REPORT.CHAT_TYPE.SYSTEM; } +function getDefaultNotificationPreferenceForReport(report: OnyxEntry) { + if (isAnnounceRoom(report)) { + return CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS; + } + if (isPublicRoom(report)) { + return CONST.REPORT.NOTIFICATION_PREFERENCE.DAILY; + } + if (!getChatType(report) || isGroupChat(report)) { + return CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS; + } + if (isAdminRoom(report) || isPolicyExpenseChat(report) || isInvoiceRoom(report)) { + return CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS; + } + if (isSelfDM(report)) { + return CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE; + } + return CONST.REPORT.NOTIFICATION_PREFERENCE.DAILY; +} + +/** + * Get the notification preference given a report + */ +function getReportNotificationPreference(report: OnyxEntry) { + return report?.participants?.[currentUserAccountID ?? -1]?.notificationPreference ?? getDefaultNotificationPreferenceForReport(report); +} + const CONCIERGE_ACCOUNT_ID_STRING = CONST.ACCOUNT_ID.CONCIERGE.toString(); /** * Only returns true if this is our main 1:1 DM report with Concierge. @@ -1258,7 +1281,7 @@ function hasExpensifyGuidesEmails(accountIDs: number[]): boolean { function getMostRecentlyVisitedReport(reports: Array>, reportMetadata: OnyxCollection): OnyxEntry { const filteredReports = reports.filter((report) => { - const shouldKeep = !isChatThread(report) || report?.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; + const shouldKeep = !isChatThread(report) || getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; return shouldKeep && !!report?.reportID && !!(reportMetadata?.[`${ONYXKEYS.COLLECTION.REPORT_METADATA}${report.reportID}`]?.lastVisitTime ?? report?.lastReadTime); }); return lodashMaxBy(filteredReports, (a) => new Date(reportMetadata?.[`${ONYXKEYS.COLLECTION.REPORT_METADATA}${a?.reportID}`]?.lastVisitTime ?? a?.lastReadTime ?? '').valueOf()); @@ -1634,13 +1657,6 @@ function isPayer(session: OnyxEntry, iouReport: OnyxEntry) { return isAdmin || (isMoneyRequestReport(iouReport) && isManager); } -/** - * Get the notification preference given a report - */ -function getReportNotificationPreference(report: OnyxEntry): string | number { - return report?.notificationPreference ?? ''; -} - /** * Checks if the current user is the action's author */ @@ -2070,7 +2086,7 @@ function getParticipantsAccountIDsForDisplay(report: OnyxEntry, shouldEx return false; } - if (shouldExcludeHidden && participant.hidden) { + if (shouldExcludeHidden && participant.notificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) { return false; } @@ -2120,7 +2136,7 @@ function buildParticipantsFromAccountIDs(accountIDs: number[]): Participants { const finalParticipants: Participants = {}; return accountIDs.reduce((participants, accountID) => { // eslint-disable-next-line no-param-reassign - participants[accountID] = {hidden: false}; + participants[accountID] = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}; return participants; }, finalParticipants); } @@ -4224,8 +4240,8 @@ function buildOptimisticIOUReport(payeeAccountID: number, payerAccountID: number const policy = getPolicy(policyID); const participants: Participants = { - [payeeAccountID]: {hidden: true}, - [payerAccountID]: {hidden: true}, + [payeeAccountID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN}, + [payerAccountID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN}, }; return { @@ -4243,7 +4259,6 @@ function buildOptimisticIOUReport(payeeAccountID: number, payerAccountID: number // We don't translate reportName because the server response is always in English reportName: `${payerEmail} owes ${formattedTotal}`, - notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, parentReportID: chatReportID, lastVisibleActionCreated: DateUtils.getDBTime(), fieldList: policy?.fieldList, @@ -4298,7 +4313,11 @@ function buildOptimisticInvoiceReport(chatReportID: string, policyID: string, re stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, statusNum: CONST.REPORT.STATUS_NUM.OPEN, total, - notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, + participants: { + [currentUserAccountID ?? -1]: { + notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, + }, + }, parentReportID: chatReportID, lastVisibleActionCreated: DateUtils.getDBTime(), }; @@ -4348,7 +4367,11 @@ function buildOptimisticExpenseReport( statusNum, total: storedTotal, nonReimbursableTotal: reimbursable ? 0 : storedTotal, - notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, + participants: { + [payeeAccountID]: { + notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, + }, + }, parentReportID: chatReportID, lastVisibleActionCreated: DateUtils.getDBTime(), parentReportActionID, @@ -4977,11 +5000,10 @@ function buildOptimisticChatReport( description = '', avatarUrl = '', optimisticReportID = '', - shouldShowParticipants = true, ): OptimisticChatReport { const participants = participantList.reduce((reportParticipants: Participants, accountID: number) => { const participant: ReportParticipant = { - hidden: !shouldShowParticipants, + notificationPreference, role: accountID === currentUserAccountID ? CONST.REPORT.ROLE.ADMIN : CONST.REPORT.ROLE.MEMBER, }; // eslint-disable-next-line no-param-reassign @@ -5002,7 +5024,6 @@ function buildOptimisticChatReport( lastMessageText: undefined, lastReadTime: currentTime, lastVisibleActionCreated: currentTime, - notificationPreference, oldPolicyName, ownerAccountID: ownerAccountID || CONST.REPORT.OWNER_ACCOUNT_ID_FAKE, parentReportActionID, @@ -5509,12 +5530,12 @@ function buildOptimisticTaskReport( ): OptimisticTaskReport { const participants: Participants = { [ownerAccountID]: { - hidden: false, + notificationPreference, }, }; if (assigneeAccountID) { - participants[assigneeAccountID] = {hidden: false}; + participants[assigneeAccountID] = {notificationPreference}; } return { @@ -5529,7 +5550,6 @@ function buildOptimisticTaskReport( policyID, stateNum: CONST.REPORT.STATE_NUM.OPEN, statusNum: CONST.REPORT.STATUS_NUM.OPEN, - notificationPreference, lastVisibleActionCreated: DateUtils.getDBTime(), hasParentAccess: true, }; @@ -5607,10 +5627,6 @@ function buildTransactionThread( CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, reportAction?.reportActionID, moneyRequestReport?.reportID, - '', - '', - '', - false, ); } @@ -5975,7 +5991,7 @@ function shouldReportBeInOptionList({ // All unread chats (even archived ones) in GSD mode will be shown. This is because GSD mode is specifically for focusing the user on the most relevant chats, primarily, the unread ones if (isInFocusMode) { - return isUnread(report) && report.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE; + return isUnread(report) && getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE; } // Archived reports should always be shown when in default (most recent) mode. This is because you should still be able to access and search for the chats to find them. @@ -7362,7 +7378,7 @@ function isAdminOwnerApproverOrReportOwner(report: OnyxEntry, policy: On /** * Whether the user can join a report */ -function canJoinChat(report: OnyxInputOrEntry, parentReportAction: OnyxInputOrEntry, policy: OnyxInputOrEntry): boolean { +function canJoinChat(report: OnyxEntry, parentReportAction: OnyxInputOrEntry, policy: OnyxInputOrEntry): boolean { // We disabled thread functions for whisper action // So we should not show join option for existing thread on whisper message that has already been left, or manually leave it if (ReportActionsUtils.isWhisperAction(parentReportAction)) { @@ -7370,7 +7386,7 @@ function canJoinChat(report: OnyxInputOrEntry, parentReportAction: OnyxI } // If the notification preference of the chat is not hidden that means we have already joined the chat - if (report?.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) { + if (getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) { return false; } @@ -7404,7 +7420,7 @@ function canLeaveChat(report: OnyxEntry, policy: OnyxEntry): boo return false; } - if (report?.notificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) { + if (getReportNotificationPreference(report) === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) { return false; } @@ -7422,7 +7438,7 @@ function canLeaveChat(report: OnyxEntry, policy: OnyxEntry): boo return canLeaveInvoiceRoom(report); } - return (isChatThread(report) && !!report?.notificationPreference?.length) || isUserCreatedPolicyRoom(report) || isNonAdminOrOwnerOfPolicyExpenseChat(report, policy); + return (isChatThread(report) && !!getReportNotificationPreference(report)) || isUserCreatedPolicyRoom(report) || isNonAdminOrOwnerOfPolicyExpenseChat(report, policy); } function getReportActionActorAccountID(reportAction: OnyxInputOrEntry, iouReport: OnyxInputOrEntry | undefined): number | undefined { @@ -7933,6 +7949,7 @@ export { isInvoiceRoomWithID, isInvoiceReport, isOpenInvoiceReport, + getDefaultNotificationPreferenceForReport, canWriteInReport, navigateToDetailsPage, navigateToPrivateNotes, diff --git a/src/libs/SidebarUtils.ts b/src/libs/SidebarUtils.ts index 9ce8feb26868..033b947d7dc4 100644 --- a/src/libs/SidebarUtils.ts +++ b/src/libs/SidebarUtils.ts @@ -103,7 +103,7 @@ function getOrderedReportIDs( const reportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`] ?? {}; const parentReportAction = ReportActionsUtils.getReportAction(report?.parentReportID ?? '-1', report?.parentReportActionID ?? '-1'); const doesReportHaveViolations = OptionsListUtils.shouldShowViolations(report, transactionViolations); - const isHidden = report.notificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; + const isHidden = ReportUtils.getReportNotificationPreference(report) === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; const isFocused = report.reportID === currentReportId; const allReportErrors = OptionsListUtils.getAllReportErrors(report, reportActions) ?? {}; const transactionReportActions = ReportActionsUtils.getAllReportActions(report.reportID); @@ -340,7 +340,7 @@ function getOptionData({ result.hasOutstandingChildRequest = report.hasOutstandingChildRequest; result.parentReportID = report.parentReportID ?? '-1'; result.isWaitingOnBankAccount = report.isWaitingOnBankAccount; - result.notificationPreference = report.notificationPreference; + result.notificationPreference = ReportUtils.getReportNotificationPreference(report); result.isAllowedToComment = ReportUtils.canUserPerformWriteAction(report); result.chatType = report.chatType; result.isDeletedParentAction = report.isDeletedParentAction; diff --git a/src/libs/UnreadIndicatorUpdater/index.ts b/src/libs/UnreadIndicatorUpdater/index.ts index d6a65ee85aac..40af0759f06a 100644 --- a/src/libs/UnreadIndicatorUpdater/index.ts +++ b/src/libs/UnreadIndicatorUpdater/index.ts @@ -29,8 +29,8 @@ function getUnreadReportsForUnreadIndicator(reports: OnyxCollection, cur * Furthermore, muted reports may or may not appear in the LHN depending on priority mode, * but they should not be considered in the unread indicator count. */ - report?.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN && - report?.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE, + ReportUtils.getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN && + ReportUtils.getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE, ); } diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 9cc844849c89..c433d9922f4b 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -3942,7 +3942,7 @@ function createSplitsAndOnyxData( splitChatReport.lastActorAccountID = currentUserAccountID; splitChatReport.lastVisibleActionCreated = splitIOUReportAction.created; - let splitChatReportNotificationPreference = splitChatReport.notificationPreference; + let splitChatReportNotificationPreference = ReportUtils.getReportNotificationPreference(splitChatReport); if (splitChatReportNotificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) { splitChatReportNotificationPreference = CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS; } @@ -3962,7 +3962,11 @@ function createSplitsAndOnyxData( key: `${ONYXKEYS.COLLECTION.REPORT}${splitChatReport.reportID}`, value: { ...splitChatReport, - notificationPreference: splitChatReportNotificationPreference, + participants: { + [currentUserAccountID]: { + notificationPreference: splitChatReportNotificationPreference, + }, + }, }, }, { diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 1ab622adcdc7..9dd000c6ab5f 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -102,7 +102,7 @@ import type { } from '@src/types/onyx'; import type {Decision} from '@src/types/onyx/OriginalMessage'; import type {ConnectionName} from '@src/types/onyx/Policy'; -import type {NotificationPreference, Participants, Participant as ReportParticipant, RoomVisibility, WriteCapability} from '@src/types/onyx/Report'; +import type {NotificationPreference, Participant, Participants, Participant as ReportParticipant, RoomVisibility, WriteCapability} from '@src/types/onyx/Report'; import type Report from '@src/types/onyx/Report'; import type {Message, ReportActions} from '@src/types/onyx/ReportAction'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; @@ -490,7 +490,9 @@ function addActions(reportID: string, text = '', file?: FileObject) { const shouldUpdateNotificationPrefernece = !isEmptyObject(report) && ReportUtils.getReportNotificationPreference(report) === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; if (shouldUpdateNotificationPrefernece) { - optimisticReport.notificationPreference = CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS; + optimisticReport.participants = { + [currentUserAccountID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + }; } // Optimistically add the new actions to the store before waiting to save them to the server @@ -550,7 +552,9 @@ function addActions(reportID: string, text = '', file?: FileObject) { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, value: { - notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, + participants: { + [currentUserAccountID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + }, }, }); } @@ -1688,7 +1692,6 @@ function updateNotificationPreference( parentReportID?: string, parentReportActionID?: string, report?: OnyxEntry, - isJoiningRoom?: boolean, ) { if (previousValue === newValue) { if (navigate && !isEmptyObject(report) && report.reportID) { @@ -1701,7 +1704,13 @@ function updateNotificationPreference( { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, - value: {notificationPreference: newValue}, + value: { + participants: { + [currentUserAccountID]: { + notificationPreference: newValue, + }, + }, + }, }, ]; @@ -1709,7 +1718,13 @@ function updateNotificationPreference( { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, - value: {notificationPreference: previousValue}, + value: { + participants: { + [currentUserAccountID]: { + notificationPreference: previousValue, + }, + }, + }, }, ]; @@ -1726,20 +1741,6 @@ function updateNotificationPreference( }); } - if (isJoiningRoom) { - optimisticData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, - value: { - participants: { - [currentUserAccountID]: { - hidden: false, - }, - }, - }, - }); - } - const parameters: UpdateReportNotificationPreferenceParams = {reportID, notificationPreference: newValue}; API.write(WRITE_COMMANDS.UPDATE_REPORT_NOTIFICATION_PREFERENCE, parameters, {optimisticData, failureData}); @@ -2424,7 +2425,7 @@ function shouldShowReportActionNotification(reportID: string, action: ReportActi } // We don't want to send a local notification if the user preference is daily, mute or hidden. - const notificationPreference = ReportConnection.getAllReports()?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]?.notificationPreference ?? CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS; + const notificationPreference = ReportUtils.getReportNotificationPreference(ReportConnection.getAllReports()?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]); if (notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS) { Log.info(`${tag} No notification because user preference is to be notified: ${notificationPreference}`); return false; @@ -2747,13 +2748,12 @@ function joinRoom(report: OnyxEntry) { } updateNotificationPreference( report.reportID, - report.notificationPreference, + CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, false, report.parentReportID, report.parentReportActionID, report, - true, ); } @@ -2808,10 +2808,9 @@ function leaveRoom(reportID: string, isWorkspaceMemberLeavingWorkspaceRoom = fal value: isWorkspaceMemberLeavingWorkspaceRoom || isChatThread ? { - notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, participants: { [currentUserAccountID]: { - hidden: true, + notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, }, }, } @@ -2819,7 +2818,11 @@ function leaveRoom(reportID: string, isWorkspaceMemberLeavingWorkspaceRoom = fal reportID: null, stateNum: CONST.REPORT.STATE_NUM.APPROVED, statusNum: CONST.REPORT.STATUS_NUM.CLOSED, - notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, + participants: { + [currentUserAccountID]: { + notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, + }, + }, }, }, ]; @@ -2830,7 +2833,13 @@ function leaveRoom(reportID: string, isWorkspaceMemberLeavingWorkspaceRoom = fal key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, value: isWorkspaceMemberLeavingWorkspaceRoom || isChatThread - ? {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN} + ? { + participants: { + [currentUserAccountID]: { + notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, + }, + }, + } : Object.keys(report).reduce>((acc, key) => { acc[key] = null; return acc; @@ -2860,7 +2869,7 @@ function leaveRoom(reportID: string, isWorkspaceMemberLeavingWorkspaceRoom = fal failureData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`, - value: {[report.parentReportActionID]: {childReportNotificationPreference: report.notificationPreference}}, + value: {[report.parentReportActionID]: {childReportNotificationPreference: ReportUtils.getReportNotificationPreference(report)}}, }); } @@ -2886,6 +2895,8 @@ function inviteToRoom(reportID: string, inviteeEmailsToAccountIDs: InvitedEmails return; } + const defaultNotificationPreference = ReportUtils.getDefaultNotificationPreferenceForReport(report); + const inviteeEmails = Object.keys(inviteeEmailsToAccountIDs); const inviteeAccountIDs = Object.values(inviteeEmailsToAccountIDs); @@ -2895,7 +2906,7 @@ function inviteToRoom(reportID: string, inviteeEmailsToAccountIDs: InvitedEmails const participantsAfterInvitation = inviteeAccountIDs.reduce( (reportParticipants: Participants, accountID: number) => { const participant: ReportParticipant = { - hidden: false, + notificationPreference: defaultNotificationPreference, role: CONST.REPORT.ROLE.MEMBER, }; // eslint-disable-next-line no-param-reassign @@ -2998,8 +3009,8 @@ function clearAddRoomMemberError(reportID: string, invitedAccountID: string) { function updateGroupChatMemberRoles(reportID: string, accountIDList: number[], role: ValueOf) { const memberRoles: Record = {}; - const optimisticParticipants: Participants = {}; - const successParticipants: Participants = {}; + const optimisticParticipants: Record> = {}; + const successParticipants: Record> = {}; accountIDList.forEach((accountID) => { memberRoles[accountID] = role; diff --git a/src/libs/actions/Task.ts b/src/libs/actions/Task.ts index 6105b7cffd9e..a32437f2039b 100644 --- a/src/libs/actions/Task.ts +++ b/src/libs/actions/Task.ts @@ -25,7 +25,7 @@ import type ReportAction from '@src/types/onyx/ReportAction'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import * as Report from './Report'; -type OptimisticReport = Pick; +type OptimisticReport = Pick; type Assignee = { icons: Icon[]; displayName: string; @@ -581,9 +581,13 @@ function editTaskAssignee(report: OnyxTypes.Report, ownerAccountID: number, assi pendingFields: { ...(assigneeAccountID && {managerID: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}), }, - notificationPreference: [assigneeAccountID, ownerAccountID].includes(currentUserAccountID) - ? CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS - : CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, + participants: { + [currentUserAccountID]: { + notificationPreference: [assigneeAccountID, ownerAccountID].includes(currentUserAccountID) + ? CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS + : CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, + }, + }, }; const successReport: NullishDeep = {pendingFields: {...(assigneeAccountID && {managerID: null})}}; @@ -662,7 +666,12 @@ function editTaskAssignee(report: OnyxTypes.Report, ownerAccountID: number, assi // If we make a change to the assignee, we want to add a comment to the assignee's chat // Check if the assignee actually changed if (assigneeAccountID && assigneeAccountID !== report.managerID && assigneeAccountID !== ownerAccountID && assigneeChatReport) { - optimisticReport.participants = {[assigneeAccountID]: {hidden: false}}; + optimisticReport.participants = { + ...(optimisticReport.participants ?? {}), + [assigneeAccountID]: { + notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, + }, + }; assigneeChatReportOnyxData = ReportUtils.getTaskAssigneeChatOnyxData( currentUserAccountID, diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index 5e0b83f8521d..49bea8e058a8 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -556,11 +556,10 @@ const isChannelMuted = (reportId: string) => key: `${ONYXKEYS.COLLECTION.REPORT}${reportId}`, callback: (report) => { Onyx.disconnect(connectionId); + const notificationPreference = report?.participants?.[currentUserAccountID]?.notificationPreference; resolve( - !report?.notificationPreference || - report?.notificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE || - report?.notificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, + !notificationPreference || notificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE || notificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, ); }, }); diff --git a/src/pages/ProfilePage.tsx b/src/pages/ProfilePage.tsx index d172e2089983..b414afb887cc 100755 --- a/src/pages/ProfilePage.tsx +++ b/src/pages/ProfilePage.tsx @@ -155,10 +155,11 @@ function ProfilePage({route}: ProfilePageProps) { const navigateBackTo = route?.params?.backTo; - const shouldShowNotificationPreference = - !isEmptyObject(report) && !isCurrentUser && !!report.notificationPreference && report.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; + const notificationPreferenceValue = ReportUtils.getReportNotificationPreference(report); + + const shouldShowNotificationPreference = !isEmptyObject(report) && !isCurrentUser && notificationPreferenceValue !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; const notificationPreference = shouldShowNotificationPreference - ? translate(`notificationPreferencesPage.notificationPreferences.${report.notificationPreference}` as TranslationPaths) + ? translate(`notificationPreferencesPage.notificationPreferences.${notificationPreferenceValue}` as TranslationPaths) : ''; // eslint-disable-next-line rulesdir/prefer-early-return diff --git a/src/pages/ReportDetailsPage.tsx b/src/pages/ReportDetailsPage.tsx index de81d661cfff..500619bfb9c7 100644 --- a/src/pages/ReportDetailsPage.tsx +++ b/src/pages/ReportDetailsPage.tsx @@ -257,7 +257,7 @@ function ReportDetailsPage({policies, report, session, personalDetails}: ReportD roomDescription = translate('newRoomPage.roomName'); } - const shouldShowNotificationPref = !isMoneyRequestReport && report?.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; + const shouldShowNotificationPref = !isMoneyRequestReport && ReportUtils.getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; const shouldShowWriteCapability = !isMoneyRequestReport; const shouldShowMenuItem = shouldShowNotificationPref || shouldShowWriteCapability || (!!report?.visibility && report.chatType !== CONST.REPORT.CHAT_TYPE.INVOICE); diff --git a/src/pages/RoomInvitePage.tsx b/src/pages/RoomInvitePage.tsx index 17239e6d4fb5..5dc281d8013e 100644 --- a/src/pages/RoomInvitePage.tsx +++ b/src/pages/RoomInvitePage.tsx @@ -66,7 +66,7 @@ function RoomInvitePage({ // Any existing participants and Expensify emails should not be eligible for invitation const excludedUsers = useMemo(() => { const visibleParticipantAccountIDs = Object.entries(report.participants ?? {}) - .filter(([, participant]) => participant && !participant.hidden) + .filter(([, participant]) => participant && participant.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) .map(([accountID]) => Number(accountID)); return [...PersonalDetailsUtils.getLoginsByAccountIDs(visibleParticipantAccountIDs), ...CONST.EXPENSIFY_EMAILS].map((participant) => PhoneNumber.addSMSDomainIfPhoneNumber(participant), diff --git a/src/pages/home/ReportScreen.tsx b/src/pages/home/ReportScreen.tsx index 979dd6166c78..4fe54385d97b 100644 --- a/src/pages/home/ReportScreen.tsx +++ b/src/pages/home/ReportScreen.tsx @@ -196,7 +196,6 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro isWaitingOnBankAccount: reportOnyx?.isWaitingOnBankAccount, iouReportID: reportOnyx?.iouReportID, isOwnPolicyExpenseChat: reportOnyx?.isOwnPolicyExpenseChat, - notificationPreference: reportOnyx?.notificationPreference, isPinned: reportOnyx?.isPinned, chatReportID: reportOnyx?.chatReportID, visibility: reportOnyx?.visibility, @@ -239,7 +238,6 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro reportOnyx?.isWaitingOnBankAccount, reportOnyx?.iouReportID, reportOnyx?.isOwnPolicyExpenseChat, - reportOnyx?.notificationPreference, reportOnyx?.isPinned, reportOnyx?.chatReportID, reportOnyx?.visibility, @@ -556,7 +554,7 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro !isFocused || prevIsFocused || !ReportUtils.isChatThread(report) || - report.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN || + ReportUtils.getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN || isSingleTransactionView ) { return; @@ -566,7 +564,7 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro // We don't want to run this useEffect every time `report` is changed // Excluding shouldUseNarrowLayout from the dependency list to prevent re-triggering on screen resize events. // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps - }, [prevIsFocused, report.notificationPreference, isFocused, isSingleTransactionView]); + }, [prevIsFocused, report.participants, isFocused, isSingleTransactionView]); useEffect(() => { // We don't want this effect to run on the first render. diff --git a/src/pages/settings/Report/NotificationPreferencePage.tsx b/src/pages/settings/Report/NotificationPreferencePage.tsx index 3a149e1ed377..fd256d685139 100644 --- a/src/pages/settings/Report/NotificationPreferencePage.tsx +++ b/src/pages/settings/Report/NotificationPreferencePage.tsx @@ -22,17 +22,18 @@ function NotificationPreferencePage({report}: NotificationPreferencePageProps) { const {translate} = useLocalize(); const [reportNameValuePairs] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report.reportID || -1}`); const isMoneyRequestReport = ReportUtils.isMoneyRequestReport(report); + const currentNotificationPreference = ReportUtils.getReportNotificationPreference(report); const shouldDisableNotificationPreferences = ReportUtils.isArchivedRoom(report, reportNameValuePairs) || ReportUtils.isSelfDM(report) || - (!isMoneyRequestReport && report?.notificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN); + (!isMoneyRequestReport && currentNotificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN); const notificationPreferenceOptions = Object.values(CONST.REPORT.NOTIFICATION_PREFERENCE) .filter((pref) => pref !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) .map((preference) => ({ value: preference, text: translate(`notificationPreferencesPage.notificationPreferences.${preference}`), keyForList: preference, - isSelected: preference === report?.notificationPreference, + isSelected: preference === currentNotificationPreference, })); return ( @@ -49,7 +50,7 @@ function NotificationPreferencePage({report}: NotificationPreferencePageProps) { sections={[{data: notificationPreferenceOptions}]} ListItem={RadioListItem} onSelectRow={(option) => - report && ReportActions.updateNotificationPreference(report.reportID, report.notificationPreference, option.value, true, undefined, undefined, report) + report && ReportActions.updateNotificationPreference(report.reportID, currentNotificationPreference, option.value, true, undefined, undefined, report) } shouldSingleExecuteRowSelect initiallyFocusedOptionKey={notificationPreferenceOptions.find((locale) => locale.isSelected)?.keyForList} diff --git a/src/pages/settings/Report/ReportSettingsPage.tsx b/src/pages/settings/Report/ReportSettingsPage.tsx index 6e1ab9a61737..6a9986b5550f 100644 --- a/src/pages/settings/Report/ReportSettingsPage.tsx +++ b/src/pages/settings/Report/ReportSettingsPage.tsx @@ -33,9 +33,10 @@ function ReportSettingsPage({report, policies}: ReportSettingsPageProps) { const isMoneyRequestReport = ReportUtils.isMoneyRequestReport(report); const shouldDisableSettings = isEmptyObject(report) || ReportUtils.isArchivedRoom(report, reportNameValuePairs) || ReportUtils.isSelfDM(report); + const notificationPreferenceValue = ReportUtils.getReportNotificationPreference(report); const notificationPreference = - report?.notificationPreference && report.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN - ? translate(`notificationPreferencesPage.notificationPreferences.${report.notificationPreference}`) + notificationPreferenceValue && notificationPreferenceValue !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN + ? translate(`notificationPreferencesPage.notificationPreferences.${notificationPreferenceValue}`) : ''; const writeCapability = ReportUtils.isAdminRoom(report) ? CONST.REPORT.WRITE_CAPABILITIES.ADMINS : report?.writeCapability ?? CONST.REPORT.WRITE_CAPABILITIES.ALL; @@ -43,7 +44,7 @@ function ReportSettingsPage({report, policies}: ReportSettingsPageProps) { const shouldAllowWriteCapabilityEditing = useMemo(() => ReportUtils.canEditWriteCapability(report, linkedWorkspace), [report, linkedWorkspace]); const shouldAllowChangeVisibility = useMemo(() => ReportUtils.canEditRoomVisibility(report, linkedWorkspace), [report, linkedWorkspace]); - const shouldShowNotificationPref = !isMoneyRequestReport && report?.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; + const shouldShowNotificationPref = !isMoneyRequestReport && notificationPreferenceValue !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; const shouldShowWriteCapability = !isMoneyRequestReport; diff --git a/src/types/onyx/Report.ts b/src/types/onyx/Report.ts index 14fd2bd3ac81..4f781897375b 100644 --- a/src/types/onyx/Report.ts +++ b/src/types/onyx/Report.ts @@ -39,7 +39,7 @@ type PendingChatMember = { /** Report participant properties */ type Participant = OnyxCommon.OnyxValueWithOfflineFeedback<{ /** Whether the participant is visible in the report */ - hidden?: boolean; + notificationPreference: NotificationPreference; /** What is the role of the participant in the report */ role?: 'admin' | 'member'; @@ -113,9 +113,6 @@ type Report = OnyxCommon.OnyxValueWithOfflineFeedback< /** The time of the last mention of the report */ lastMentionedTime?: string | null; - /** The current user's notification preference for this report */ - notificationPreference?: NotificationPreference; - /** The policy name to use */ policyName?: string | null; diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index 405760435d14..45179f3ba79d 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -36,16 +36,16 @@ jest.mock('@src/libs/Navigation/Navigation', () => ({ const CARLOS_EMAIL = 'cmartins@expensifail.com'; const CARLOS_ACCOUNT_ID = 1; -const CARLOS_PARTICIPANT: Participant = {hidden: false, role: 'member'}; +const CARLOS_PARTICIPANT: Participant = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, role: 'member'}; const JULES_EMAIL = 'jules@expensifail.com'; const JULES_ACCOUNT_ID = 2; -const JULES_PARTICIPANT: Participant = {hidden: false, role: 'member'}; +const JULES_PARTICIPANT: Participant = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, role: 'member'}; const RORY_EMAIL = 'rory@expensifail.com'; const RORY_ACCOUNT_ID = 3; -const RORY_PARTICIPANT: Participant = {hidden: false, role: 'admin'}; +const RORY_PARTICIPANT: Participant = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, role: 'admin'}; const VIT_EMAIL = 'vit@expensifail.com'; const VIT_ACCOUNT_ID = 4; -const VIT_PARTICIPANT: Participant = {hidden: false, role: 'member'}; +const VIT_PARTICIPANT: Participant = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, role: 'member'}; OnyxUpdateManager(); describe('actions/IOU', () => { @@ -100,7 +100,10 @@ describe('actions/IOU', () => { iouReportID = iouReport?.reportID; transactionThread = transactionThreadReport; - expect(iouReport?.notificationPreference).toBe(CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN); + expect(iouReport?.participants).toBe({ + [RORY_ACCOUNT_ID]: {notification: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN}, + [CARLOS_ACCOUNT_ID]: {notification: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN}, + }); // They should be linked together expect(chatReport?.participants).toEqual({[RORY_ACCOUNT_ID]: RORY_PARTICIPANT, [CARLOS_ACCOUNT_ID]: CARLOS_PARTICIPANT}); @@ -293,7 +296,10 @@ describe('actions/IOU', () => { const iouReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.IOU); iouReportID = iouReport?.reportID; - expect(iouReport?.notificationPreference).toBe(CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN); + expect(iouReport?.participants).toBe({ + [RORY_ACCOUNT_ID]: {notification: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN}, + [CARLOS_ACCOUNT_ID]: {notification: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN}, + }); // They should be linked together expect(chatReport.iouReportID).toBe(iouReportID); @@ -638,7 +644,7 @@ describe('actions/IOU', () => { const iouReport = iouReports[0]; iouReportID = iouReport?.reportID; - expect(iouReport?.notificationPreference).toBe(CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN); + expect(chatReport?.participants).toEqual({[RORY_ACCOUNT_ID]: RORY_PARTICIPANT, [CARLOS_ACCOUNT_ID]: CARLOS_PARTICIPANT}); // They should be linked together expect(chatReport?.participants).toEqual({[RORY_ACCOUNT_ID]: RORY_PARTICIPANT, [CARLOS_ACCOUNT_ID]: CARLOS_PARTICIPANT}); @@ -1159,14 +1165,13 @@ describe('actions/IOU', () => { // The 1:1 chat reports and the IOU reports should be linked together expect(carlosChatReport?.iouReportID).toBe(carlosIOUReport?.reportID); expect(carlosIOUReport?.chatReportID).toBe(carlosChatReport?.reportID); - expect(carlosIOUReport?.notificationPreference).toBe(CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN); + expect(carlosIOUReport?.participants).toBe({[CARLOS_ACCOUNT_ID]: CARLOS_PARTICIPANT}); expect(julesChatReport?.iouReportID).toBe(julesIOUReport?.reportID); expect(julesIOUReport?.chatReportID).toBe(julesChatReport?.reportID); expect(vitChatReport?.iouReportID).toBe(vitIOUReport?.reportID); expect(vitIOUReport?.chatReportID).toBe(vitChatReport?.reportID); - expect(carlosIOUReport?.notificationPreference).toBe(CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN); resolve(); }, @@ -2437,7 +2442,7 @@ describe('actions/IOU', () => { // Given a transaction thread thread = ReportUtils.buildTransactionThread(createIOUAction, iouReport); - expect(thread.notificationPreference).toBe(CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN); + expect(thread.participants).toBe({[RORY_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN}}); Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, @@ -2626,7 +2631,7 @@ describe('actions/IOU', () => { // Given a transaction thread thread = ReportUtils.buildTransactionThread(createIOUAction, iouReport); - expect(thread.notificationPreference).toBe(CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN); + expect(thread.participants).toBe({[RORY_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN}}); const participantAccountIDs = Object.keys(thread.participants ?? {}).map(Number); const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(participantAccountIDs); @@ -2714,7 +2719,7 @@ describe('actions/IOU', () => { jest.advanceTimersByTime(10); thread = ReportUtils.buildTransactionThread(createIOUAction, iouReport); - expect(thread.notificationPreference).toBe(CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN); + expect(thread.participants).toBe({[RORY_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN}}); Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, @@ -2954,7 +2959,7 @@ describe('actions/IOU', () => { jest.advanceTimersByTime(10); thread = ReportUtils.buildTransactionThread(createIOUAction, iouReport); - expect(thread.notificationPreference).toBe(CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN); + expect(thread.participants).toBe(CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN); Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, diff --git a/tests/actions/PolicyTest.ts b/tests/actions/PolicyTest.ts index 17e9c09acfd3..2bbd19ce9dea 100644 --- a/tests/actions/PolicyTest.ts +++ b/tests/actions/PolicyTest.ts @@ -12,7 +12,7 @@ import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; const ESH_EMAIL = 'eshgupta1217@gmail.com'; const ESH_ACCOUNT_ID = 1; -const ESH_PARTICIPANT: Participant = {hidden: false, role: 'admin'}; +const ESH_PARTICIPANT: Participant = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.DAILY, role: 'admin'}; const WORKSPACE_NAME = "Esh's Workspace"; OnyxUpdateManager(); diff --git a/tests/actions/ReportTest.ts b/tests/actions/ReportTest.ts index e6ab31334bb1..a099348257f0 100644 --- a/tests/actions/ReportTest.ts +++ b/tests/actions/ReportTest.ts @@ -103,7 +103,11 @@ describe('actions/Report', () => { key: `${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, value: { reportID: REPORT_ID, - notificationPreference: 'always', + participants: { + [TEST_USER_ACCOUNT_ID]: { + notificationPreference: 'always', + }, + }, lastVisibleActionCreated: '2022-11-22 03:48:27.267', lastMessageText: 'Testing a comment', lastActorAccountID: TEST_USER_ACCOUNT_ID, @@ -230,7 +234,11 @@ describe('actions/Report', () => { key: `${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, value: { reportID: REPORT_ID, - notificationPreference: 'always', + participants: { + [USER_1_ACCOUNT_ID]: { + notificationPreference: 'always', + }, + }, lastMessageText: 'Comment 1', lastActorAccountID: USER_2_ACCOUNT_ID, lastVisibleActionCreated: reportActionCreatedDate, @@ -380,7 +388,11 @@ describe('actions/Report', () => { key: `${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, value: { reportID: REPORT_ID, - notificationPreference: 'always', + participants: { + [USER_1_ACCOUNT_ID]: { + notificationPreference: 'always', + }, + }, lastMessageText: 'Current User Comment 3', lastActorAccountID: 1, lastVisibleActionCreated: reportActionCreatedDate, diff --git a/tests/ui/PaginationTest.tsx b/tests/ui/PaginationTest.tsx index 277f6b88e78f..250264235172 100644 --- a/tests/ui/PaginationTest.tsx +++ b/tests/ui/PaginationTest.tsx @@ -198,7 +198,7 @@ async function signInAndGetApp(): Promise { reportID: REPORT_ID, reportName: CONST.REPORT.DEFAULT_REPORT_NAME, lastMessageText: 'Test', - participants: {[USER_B_ACCOUNT_ID]: {hidden: false}}, + participants: {[USER_B_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}}, lastActorAccountID: USER_B_ACCOUNT_ID, type: CONST.REPORT.TYPE.CHAT, }); @@ -212,7 +212,7 @@ async function signInAndGetApp(): Promise { reportID: COMMENT_LINKING_REPORT_ID, reportName: CONST.REPORT.DEFAULT_REPORT_NAME, lastMessageText: 'Test', - participants: {[USER_A_ACCOUNT_ID]: {hidden: false}}, + participants: {[USER_A_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}}, lastActorAccountID: USER_A_ACCOUNT_ID, type: CONST.REPORT.TYPE.CHAT, }); diff --git a/tests/ui/UnreadIndicatorsTest.tsx b/tests/ui/UnreadIndicatorsTest.tsx index 1d31a707d81d..011f1e01668f 100644 --- a/tests/ui/UnreadIndicatorsTest.tsx +++ b/tests/ui/UnreadIndicatorsTest.tsx @@ -140,7 +140,7 @@ function signInAndGetAppWithUnreadChat(): Promise { lastReadTime: reportAction3CreatedDate, lastVisibleActionCreated: reportAction9CreatedDate, lastMessageText: 'Test', - participants: {[USER_B_ACCOUNT_ID]: {hidden: false}}, + participants: {[USER_B_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}}, lastActorAccountID: USER_B_ACCOUNT_ID, type: CONST.REPORT.TYPE.CHAT, }); @@ -301,7 +301,7 @@ describe('Unread Indicators', () => { lastVisibleActionCreated: DateUtils.getDBTime(utcToZonedTime(NEW_REPORT_FIST_MESSAGE_CREATED_DATE, 'UTC').valueOf()), lastMessageText: 'Comment 1', lastActorAccountID: USER_C_ACCOUNT_ID, - participants: {[USER_C_ACCOUNT_ID]: {hidden: false}}, + participants: {[USER_C_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}}, type: CONST.REPORT.TYPE.CHAT, }, }, diff --git a/tests/unit/ReportUtilsTest.ts b/tests/unit/ReportUtilsTest.ts index 12bcabc92b19..a8fecd11e87f 100644 --- a/tests/unit/ReportUtilsTest.ts +++ b/tests/unit/ReportUtilsTest.ts @@ -946,28 +946,44 @@ describe('ReportUtils', () => { const invoiceReport: Report = { reportID: '1', type: CONST.REPORT.TYPE.INVOICE, - participants: {[userAccountID]: {hidden: false}, [currentUserAccountID]: {hidden: false}}, + participants: { + [userAccountID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + [currentUserAccountID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + }, }; const taskReport: Report = { reportID: '2', type: CONST.REPORT.TYPE.TASK, - participants: {[userAccountID]: {hidden: false}, [currentUserAccountID]: {hidden: false}}, + participants: { + [userAccountID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + [currentUserAccountID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + }, }; const iouReport: Report = { reportID: '3', type: CONST.REPORT.TYPE.IOU, - participants: {[userAccountID]: {hidden: false}, [currentUserAccountID]: {hidden: false}}, + participants: { + [userAccountID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + [currentUserAccountID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + }, }; groupChatReport = { reportID: '4', type: CONST.REPORT.TYPE.CHAT, chatType: CONST.REPORT.CHAT_TYPE.GROUP, - participants: {[userAccountID]: {hidden: false}, [userAccountID2]: {hidden: false}, [currentUserAccountID]: {hidden: false}}, + participants: { + [userAccountID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + [userAccountID2]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + [currentUserAccountID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + }, }; oneOnOneChatReport = { reportID: '5', type: CONST.REPORT.TYPE.CHAT, - participants: {[userAccountID]: {hidden: false}, [currentUserAccountID]: {hidden: false}}, + participants: { + [userAccountID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + [currentUserAccountID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + }, }; const reportCollectionDataSet = toCollectionDataSet( ONYXKEYS.COLLECTION.REPORT, diff --git a/tests/unit/UnreadIndicatorUpdaterTest.ts b/tests/unit/UnreadIndicatorUpdaterTest.ts index 9cf65bcb69d4..cf8119f3784a 100644 --- a/tests/unit/UnreadIndicatorUpdaterTest.ts +++ b/tests/unit/UnreadIndicatorUpdaterTest.ts @@ -1,6 +1,10 @@ /* eslint-disable @typescript-eslint/naming-convention */ import CONST from '../../src/CONST'; import * as UnreadIndicatorUpdater from '../../src/libs/UnreadIndicatorUpdater'; +import * as TestHelper from '../utils/TestHelper'; + +const TEST_USER_ACCOUNT_ID = 1; +const TEST_USER_LOGIN = 'test@test.com'; describe('UnreadIndicatorUpdaterTest', () => { describe('should return correct number of unread reports', () => { @@ -24,7 +28,9 @@ describe('UnreadIndicatorUpdaterTest', () => { }, 3: {reportID: '3', reportName: 'test', type: CONST.REPORT.TYPE.TASK, lastMessageText: 'test'}, }; - expect(UnreadIndicatorUpdater.getUnreadReportsForUnreadIndicator(reportsToBeUsed, '3').length).toBe(2); + TestHelper.setPersonalDetails(TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID).then(() => { + expect(UnreadIndicatorUpdater.getUnreadReportsForUnreadIndicator(reportsToBeUsed, '3').length).toBe(2); + }); }); it('given some reports are incomplete', () => { @@ -42,7 +48,11 @@ describe('UnreadIndicatorUpdaterTest', () => { reportID: '1', reportName: 'test', type: CONST.REPORT.TYPE.EXPENSE, - notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, + participants: { + [TEST_USER_ACCOUNT_ID]: { + notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, + }, + }, lastReadTime: '2023-07-08 07:15:44.030', lastVisibleActionCreated: '2023-08-08 07:15:44.030', lastMessageText: 'test', @@ -57,7 +67,9 @@ describe('UnreadIndicatorUpdaterTest', () => { }, 3: {reportID: '3', reportName: 'test', type: CONST.REPORT.TYPE.TASK, lastMessageText: 'test'}, }; - expect(UnreadIndicatorUpdater.getUnreadReportsForUnreadIndicator(reportsToBeUsed, '3').length).toBe(1); + TestHelper.setPersonalDetails(TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID).then(() => { + expect(UnreadIndicatorUpdater.getUnreadReportsForUnreadIndicator(reportsToBeUsed, '3').length).toBe(1); + }); }); }); }); From 8504a1df1f3480852fd96a716482309d76565131 Mon Sep 17 00:00:00 2001 From: Ishpaul Singh Date: Sun, 18 Aug 2024 23:57:55 +0530 Subject: [PATCH 02/16] fix/tests --- src/libs/ReportUtils.ts | 4 ++-- src/libs/actions/Report.ts | 4 ++-- tests/actions/IOUTest.ts | 20 ++++++++++---------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 34fcbbef259b..e4d8c29d7323 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1150,7 +1150,7 @@ function isSystemChat(report: OnyxEntry): boolean { return getChatType(report) === CONST.REPORT.CHAT_TYPE.SYSTEM; } -function getDefaultNotificationPreferenceForReport(report: OnyxEntry) { +function getDefaultNotificationPreferenceForReport(report: OnyxEntry): ValueOf { if (isAnnounceRoom(report)) { return CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS; } @@ -1172,7 +1172,7 @@ function getDefaultNotificationPreferenceForReport(report: OnyxEntry) { /** * Get the notification preference given a report */ -function getReportNotificationPreference(report: OnyxEntry) { +function getReportNotificationPreference(report: OnyxEntry): ValueOf { return report?.participants?.[currentUserAccountID ?? -1]?.notificationPreference ?? getDefaultNotificationPreferenceForReport(report); } diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 9dd000c6ab5f..26dc000fe6a1 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -3009,8 +3009,8 @@ function clearAddRoomMemberError(reportID: string, invitedAccountID: string) { function updateGroupChatMemberRoles(reportID: string, accountIDList: number[], role: ValueOf) { const memberRoles: Record = {}; - const optimisticParticipants: Record> = {}; - const successParticipants: Record> = {}; + const optimisticParticipants: Record> = {}; + const successParticipants: Record> = {}; accountIDList.forEach((accountID) => { memberRoles[accountID] = role; diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index 45179f3ba79d..36ebf4684fae 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -101,8 +101,8 @@ describe('actions/IOU', () => { transactionThread = transactionThreadReport; expect(iouReport?.participants).toBe({ - [RORY_ACCOUNT_ID]: {notification: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN}, - [CARLOS_ACCOUNT_ID]: {notification: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN}, + [RORY_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN}, + [CARLOS_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN}, }); // They should be linked together @@ -297,8 +297,8 @@ describe('actions/IOU', () => { iouReportID = iouReport?.reportID; expect(iouReport?.participants).toBe({ - [RORY_ACCOUNT_ID]: {notification: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN}, - [CARLOS_ACCOUNT_ID]: {notification: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN}, + [RORY_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN}, + [CARLOS_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN}, }); // They should be linked together @@ -644,10 +644,10 @@ describe('actions/IOU', () => { const iouReport = iouReports[0]; iouReportID = iouReport?.reportID; - expect(chatReport?.participants).toEqual({[RORY_ACCOUNT_ID]: RORY_PARTICIPANT, [CARLOS_ACCOUNT_ID]: CARLOS_PARTICIPANT}); + expect(chatReport?.participants).toStrictEqual({[RORY_ACCOUNT_ID]: RORY_PARTICIPANT, [CARLOS_ACCOUNT_ID]: CARLOS_PARTICIPANT}); // They should be linked together - expect(chatReport?.participants).toEqual({[RORY_ACCOUNT_ID]: RORY_PARTICIPANT, [CARLOS_ACCOUNT_ID]: CARLOS_PARTICIPANT}); + expect(chatReport?.participants).toStrictEqual({[RORY_ACCOUNT_ID]: RORY_PARTICIPANT, [CARLOS_ACCOUNT_ID]: CARLOS_PARTICIPANT}); expect(chatReport?.iouReportID).toBe(iouReport?.reportID); resolve(); @@ -1165,7 +1165,7 @@ describe('actions/IOU', () => { // The 1:1 chat reports and the IOU reports should be linked together expect(carlosChatReport?.iouReportID).toBe(carlosIOUReport?.reportID); expect(carlosIOUReport?.chatReportID).toBe(carlosChatReport?.reportID); - expect(carlosIOUReport?.participants).toBe({[CARLOS_ACCOUNT_ID]: CARLOS_PARTICIPANT}); + expect(carlosIOUReport?.participants).toStrictEqual({[CARLOS_ACCOUNT_ID]: CARLOS_PARTICIPANT}); expect(julesChatReport?.iouReportID).toBe(julesIOUReport?.reportID); expect(julesIOUReport?.chatReportID).toBe(julesChatReport?.reportID); @@ -2442,7 +2442,7 @@ describe('actions/IOU', () => { // Given a transaction thread thread = ReportUtils.buildTransactionThread(createIOUAction, iouReport); - expect(thread.participants).toBe({[RORY_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN}}); + expect(thread.participants).toStrictEqual({[RORY_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN}}); Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, @@ -2719,7 +2719,7 @@ describe('actions/IOU', () => { jest.advanceTimersByTime(10); thread = ReportUtils.buildTransactionThread(createIOUAction, iouReport); - expect(thread.participants).toBe({[RORY_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN}}); + expect(thread.participants).toStrictEqual({[RORY_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN}}); Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, @@ -2959,7 +2959,7 @@ describe('actions/IOU', () => { jest.advanceTimersByTime(10); thread = ReportUtils.buildTransactionThread(createIOUAction, iouReport); - expect(thread.participants).toBe(CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN); + expect(thread.participants).toStrictEqual({[RORY_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN}}); Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, From 4d34462c094a86fd489fa082881659b16c94df01 Mon Sep 17 00:00:00 2001 From: Ishpaul Singh Date: Mon, 19 Aug 2024 00:43:05 +0530 Subject: [PATCH 03/16] FIXES MORE TESTS --- tests/actions/IOUTest.ts | 6 +++--- tests/actions/PolicyTest.ts | 8 ++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index 36ebf4684fae..0b599824dba9 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -100,7 +100,7 @@ describe('actions/IOU', () => { iouReportID = iouReport?.reportID; transactionThread = transactionThreadReport; - expect(iouReport?.participants).toBe({ + expect(iouReport?.participants).toEqual({ [RORY_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN}, [CARLOS_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN}, }); @@ -296,7 +296,7 @@ describe('actions/IOU', () => { const iouReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.IOU); iouReportID = iouReport?.reportID; - expect(iouReport?.participants).toBe({ + expect(iouReport?.participants).toEqual({ [RORY_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN}, [CARLOS_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN}, }); @@ -2631,7 +2631,7 @@ describe('actions/IOU', () => { // Given a transaction thread thread = ReportUtils.buildTransactionThread(createIOUAction, iouReport); - expect(thread.participants).toBe({[RORY_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN}}); + expect(thread.participants).toEqual({[RORY_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN}}); const participantAccountIDs = Object.keys(thread.participants ?? {}).map(Number); const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(participantAccountIDs); diff --git a/tests/actions/PolicyTest.ts b/tests/actions/PolicyTest.ts index 2bbd19ce9dea..2a3d82c8a8ec 100644 --- a/tests/actions/PolicyTest.ts +++ b/tests/actions/PolicyTest.ts @@ -12,7 +12,9 @@ import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; const ESH_EMAIL = 'eshgupta1217@gmail.com'; const ESH_ACCOUNT_ID = 1; -const ESH_PARTICIPANT: Participant = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.DAILY, role: 'admin'}; +const ESH_PARTICIPANT_ANNOUNCE_ROOM: Participant = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.DAILY, role: 'admin'}; +const ESH_PARTICIPANT_ADMINS_ROOM: Participant = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, role: 'admin'}; +const ESH_PARTICIPANT_EXPENSE_CHAT = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, role: 'admin'} const WORKSPACE_NAME = "Esh's Workspace"; OnyxUpdateManager(); @@ -78,17 +80,19 @@ describe('actions/Policy', () => { expect(workspaceReports.length).toBe(3); workspaceReports.forEach((report) => { expect(report?.pendingFields?.addWorkspaceRoom).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD); - expect(report?.participants).toEqual({[ESH_ACCOUNT_ID]: ESH_PARTICIPANT}); switch (report?.chatType) { case CONST.REPORT.CHAT_TYPE.POLICY_ADMINS: { + expect(report?.participants).toEqual({ [ESH_ACCOUNT_ID]: ESH_PARTICIPANT_ADMINS_ROOM }); 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; break; } From 24fe82850deb9887a952fb45230c6595943d5c88 Mon Sep 17 00:00:00 2001 From: Shubham Agrawal <58412969+shubham1206agra@users.noreply.github.com> Date: Mon, 19 Aug 2024 10:28:58 +0530 Subject: [PATCH 04/16] Apply suggestions from code review Co-authored-by: Ishpaul Singh <104348397+ishpaul777@users.noreply.github.com> --- src/libs/actions/Report.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 26dc000fe6a1..0044b40e1912 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -102,7 +102,7 @@ import type { } from '@src/types/onyx'; import type {Decision} from '@src/types/onyx/OriginalMessage'; import type {ConnectionName} from '@src/types/onyx/Policy'; -import type {NotificationPreference, Participant, Participants, Participant as ReportParticipant, RoomVisibility, WriteCapability} from '@src/types/onyx/Report'; +import type {NotificationPreference, Participants, Participant as ReportParticipant, RoomVisibility, WriteCapability} from '@src/types/onyx/Report'; import type Report from '@src/types/onyx/Report'; import type {Message, ReportActions} from '@src/types/onyx/ReportAction'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; From cc71ee510ab75833099eae9257d34f64d2956a78 Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Mon, 19 Aug 2024 10:29:49 +0530 Subject: [PATCH 05/16] Fix lint --- tests/actions/PolicyTest.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/actions/PolicyTest.ts b/tests/actions/PolicyTest.ts index 2a3d82c8a8ec..79906534eb2c 100644 --- a/tests/actions/PolicyTest.ts +++ b/tests/actions/PolicyTest.ts @@ -14,7 +14,7 @@ const ESH_EMAIL = 'eshgupta1217@gmail.com'; const ESH_ACCOUNT_ID = 1; const ESH_PARTICIPANT_ANNOUNCE_ROOM: Participant = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.DAILY, role: 'admin'}; const ESH_PARTICIPANT_ADMINS_ROOM: Participant = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, role: 'admin'}; -const ESH_PARTICIPANT_EXPENSE_CHAT = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, role: 'admin'} +const ESH_PARTICIPANT_EXPENSE_CHAT = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, role: 'admin'}; const WORKSPACE_NAME = "Esh's Workspace"; OnyxUpdateManager(); @@ -82,17 +82,17 @@ describe('actions/Policy', () => { expect(report?.pendingFields?.addWorkspaceRoom).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD); switch (report?.chatType) { case CONST.REPORT.CHAT_TYPE.POLICY_ADMINS: { - expect(report?.participants).toEqual({ [ESH_ACCOUNT_ID]: ESH_PARTICIPANT_ADMINS_ROOM }); + expect(report?.participants).toEqual({[ESH_ACCOUNT_ID]: ESH_PARTICIPANT_ADMINS_ROOM}); adminReportID = report.reportID; break; } case CONST.REPORT.CHAT_TYPE.POLICY_ANNOUNCE: { - expect(report?.participants).toEqual({ [ESH_ACCOUNT_ID]: ESH_PARTICIPANT_ANNOUNCE_ROOM }); + 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 }); + expect(report?.participants).toEqual({[ESH_ACCOUNT_ID]: ESH_PARTICIPANT_EXPENSE_CHAT}); expenseReportID = report.reportID; break; } From 3aba64b0b20d5eb0596c742aa3cb899a68c4d3b2 Mon Sep 17 00:00:00 2001 From: Shubham Agrawal <58412969+shubham1206agra@users.noreply.github.com> Date: Wed, 21 Aug 2024 06:54:26 +0530 Subject: [PATCH 06/16] Apply suggestions from code review Co-authored-by: Ishpaul Singh <104348397+ishpaul777@users.noreply.github.com> --- tests/actions/IOUTest.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index 0b599824dba9..2b45443f8cac 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -1165,7 +1165,9 @@ describe('actions/IOU', () => { // The 1:1 chat reports and the IOU reports should be linked together expect(carlosChatReport?.iouReportID).toBe(carlosIOUReport?.reportID); expect(carlosIOUReport?.chatReportID).toBe(carlosChatReport?.reportID); - expect(carlosIOUReport?.participants).toStrictEqual({[CARLOS_ACCOUNT_ID]: CARLOS_PARTICIPANT}); +Object.values(carlosIOUReport?.participants ?? {}).forEach((participant) => { + expect(participant.notificationPreference).toBe(CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN); +}); expect(julesChatReport?.iouReportID).toBe(julesIOUReport?.reportID); expect(julesIOUReport?.chatReportID).toBe(julesChatReport?.reportID); @@ -2442,7 +2444,7 @@ describe('actions/IOU', () => { // Given a transaction thread thread = ReportUtils.buildTransactionThread(createIOUAction, iouReport); - expect(thread.participants).toStrictEqual({[RORY_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN}}); + expect(thread.participants).toStrictEqual({[CARLOS_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN}}); Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, @@ -2631,7 +2633,7 @@ describe('actions/IOU', () => { // Given a transaction thread thread = ReportUtils.buildTransactionThread(createIOUAction, iouReport); - expect(thread.participants).toEqual({[RORY_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN}}); + expect(thread.participants).toEqual({[CARLOS_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN}}); const participantAccountIDs = Object.keys(thread.participants ?? {}).map(Number); const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(participantAccountIDs); @@ -2719,7 +2721,7 @@ describe('actions/IOU', () => { jest.advanceTimersByTime(10); thread = ReportUtils.buildTransactionThread(createIOUAction, iouReport); - expect(thread.participants).toStrictEqual({[RORY_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN}}); + expect(thread.participants).toStrictEqual({[CARLOS_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN}}); Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, @@ -2959,7 +2961,7 @@ describe('actions/IOU', () => { jest.advanceTimersByTime(10); thread = ReportUtils.buildTransactionThread(createIOUAction, iouReport); - expect(thread.participants).toStrictEqual({[RORY_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN}}); + expect(thread.participants).toStrictEqual({[CARLOS_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN}}); Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, From 6967e8dc193f74ec324e8ce00481bffcae55ede8 Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Wed, 21 Aug 2024 06:55:58 +0530 Subject: [PATCH 07/16] Fix lint --- tests/actions/IOUTest.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index 2b45443f8cac..5684619945f1 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -1165,9 +1165,9 @@ describe('actions/IOU', () => { // The 1:1 chat reports and the IOU reports should be linked together expect(carlosChatReport?.iouReportID).toBe(carlosIOUReport?.reportID); expect(carlosIOUReport?.chatReportID).toBe(carlosChatReport?.reportID); -Object.values(carlosIOUReport?.participants ?? {}).forEach((participant) => { + Object.values(carlosIOUReport?.participants ?? {}).forEach((participant) => { expect(participant.notificationPreference).toBe(CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN); -}); + }); expect(julesChatReport?.iouReportID).toBe(julesIOUReport?.reportID); expect(julesIOUReport?.chatReportID).toBe(julesChatReport?.reportID); From cf6ab27f9ac6622024642036cbe2f1127bb4b74f Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Wed, 21 Aug 2024 09:37:33 +0530 Subject: [PATCH 08/16] Fix jest test --- tests/actions/IOUTest.ts | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index 5684619945f1..f698ac5c7979 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -1148,17 +1148,27 @@ describe('actions/IOU', () => { expect(isEmptyObject(vitIOUReport)).toBe(false); expect(vitIOUReport?.total).toBe(amount / 4); + const groupParticipants = { + [CARLOS_ACCOUNT_ID]: { + notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, + role: CONST.REPORT.ROLE.MEMBER, + }, + [JULES_ACCOUNT_ID]: { + notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, + role: CONST.REPORT.ROLE.MEMBER, + }, + [VIT_ACCOUNT_ID]: { + notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, + role: CONST.REPORT.ROLE.MEMBER, + }, + [RORY_ACCOUNT_ID]: { + notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, + role: CONST.REPORT.ROLE.ADMIN, + }, + }; + // 7. The group chat with everyone - groupChat = Object.values(allReports ?? {}).find( - (report) => - report?.type === CONST.REPORT.TYPE.CHAT && - isEqual(report.participants, { - [CARLOS_ACCOUNT_ID]: CARLOS_PARTICIPANT, - [JULES_ACCOUNT_ID]: JULES_PARTICIPANT, - [VIT_ACCOUNT_ID]: VIT_PARTICIPANT, - [RORY_ACCOUNT_ID]: RORY_PARTICIPANT, - }), - ); + groupChat = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.CHAT && isEqual(report.participants, groupParticipants)); expect(isEmptyObject(groupChat)).toBe(false); expect(groupChat?.pendingFields).toStrictEqual({createChat: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD}); From 7733be8a4f4d6ffa7bc7c5979727b3ae0d36886f Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Wed, 21 Aug 2024 09:39:15 +0530 Subject: [PATCH 09/16] Fix jest test --- src/libs/actions/IOU.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index c433d9922f4b..7e9ec5cd3c55 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -3844,6 +3844,7 @@ function getOrCreateOptimisticSplitChatReport(existingSplitChatReportID: string, undefined, CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, ); + console.debug('Created a new group chat report', splitChatReport); return {existingSplitChatReport: null, splitChatReport}; } @@ -3954,6 +3955,16 @@ function createSplitsAndOnyxData( }; } + const updatedParticipants = fastMerge( + splitChatReport.participants, + { + [currentUserAccountID]: { + notificationPreference: splitChatReportNotificationPreference, + }, + }, + false, + ); + const optimisticData: OnyxUpdate[] = [ { // Use set for new reports because it doesn't exist yet, is faster, @@ -3962,11 +3973,7 @@ function createSplitsAndOnyxData( key: `${ONYXKEYS.COLLECTION.REPORT}${splitChatReport.reportID}`, value: { ...splitChatReport, - participants: { - [currentUserAccountID]: { - notificationPreference: splitChatReportNotificationPreference, - }, - }, + participants: updatedParticipants, }, }, { From b8d8979bd9d310a38ae1755eb95761210b0cffec Mon Sep 17 00:00:00 2001 From: Shubham Agrawal <58412969+shubham1206agra@users.noreply.github.com> Date: Wed, 21 Aug 2024 09:51:51 +0530 Subject: [PATCH 10/16] Apply suggestions from code review --- src/libs/actions/IOU.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index cf76b0069e96..2f382cb7829a 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -3848,7 +3848,6 @@ function getOrCreateOptimisticSplitChatReport(existingSplitChatReportID: string, undefined, CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, ); - console.debug('Created a new group chat report', splitChatReport); return {existingSplitChatReport: null, splitChatReport}; } From 8fe8e8332a0110a967c80e6a57a3678413275dfc Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Thu, 22 Aug 2024 08:30:55 +0530 Subject: [PATCH 11/16] Fix ts --- tests/unit/OptionsListUtilsTest.ts | 72 +++++++++++++++--------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/tests/unit/OptionsListUtilsTest.ts b/tests/unit/OptionsListUtilsTest.ts index 34a0a9af7383..6122dcd5c877 100644 --- a/tests/unit/OptionsListUtilsTest.ts +++ b/tests/unit/OptionsListUtilsTest.ts @@ -22,9 +22,9 @@ describe('OptionsListUtils', () => { isPinned: false, reportID: '1', participants: { - 2: {}, - 1: {}, - 5: {}, + 2: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + 1: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + 5: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, }, reportName: 'Iron Man, Mister Fantastic, Invisible Woman', type: CONST.REPORT.TYPE.CHAT, @@ -35,8 +35,8 @@ describe('OptionsListUtils', () => { isPinned: false, reportID: '2', participants: { - 2: {}, - 3: {}, + 2: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + 3: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, }, reportName: 'Spider-Man', type: CONST.REPORT.TYPE.CHAT, @@ -49,8 +49,8 @@ describe('OptionsListUtils', () => { isPinned: true, reportID: '3', participants: { - 2: {}, - 1: {}, + 2: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + 1: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, }, reportName: 'Mister Fantastic', type: CONST.REPORT.TYPE.CHAT, @@ -61,8 +61,8 @@ describe('OptionsListUtils', () => { isPinned: false, reportID: '4', participants: { - 2: {}, - 4: {}, + 2: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + 4: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, }, reportName: 'Black Panther', type: CONST.REPORT.TYPE.CHAT, @@ -73,8 +73,8 @@ describe('OptionsListUtils', () => { isPinned: false, reportID: '5', participants: { - 2: {}, - 5: {}, + 2: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + 5: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, }, reportName: 'Invisible Woman', type: CONST.REPORT.TYPE.CHAT, @@ -85,8 +85,8 @@ describe('OptionsListUtils', () => { isPinned: false, reportID: '6', participants: { - 2: {}, - 6: {}, + 2: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + 6: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, }, reportName: 'Thor', type: CONST.REPORT.TYPE.CHAT, @@ -99,8 +99,8 @@ describe('OptionsListUtils', () => { isPinned: false, reportID: '7', participants: { - 2: {}, - 7: {}, + 2: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + 7: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, }, reportName: 'Captain America', type: CONST.REPORT.TYPE.CHAT, @@ -113,8 +113,8 @@ describe('OptionsListUtils', () => { isPinned: false, reportID: '8', participants: { - 2: {}, - 12: {}, + 2: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + 12: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, }, reportName: 'Silver Surfer', type: CONST.REPORT.TYPE.CHAT, @@ -127,8 +127,8 @@ describe('OptionsListUtils', () => { isPinned: false, reportID: '9', participants: { - 2: {}, - 8: {}, + 2: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + 8: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, }, reportName: 'Mister Sinister', iouReportID: '100', @@ -142,8 +142,8 @@ describe('OptionsListUtils', () => { reportID: '10', isPinned: false, participants: { - 2: {}, - 7: {}, + 2: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + 7: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, }, reportName: '', oldPolicyName: "SHIELD's workspace", @@ -236,8 +236,8 @@ describe('OptionsListUtils', () => { isPinned: false, reportID: '11', participants: { - 2: {}, - 999: {}, + 2: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + 999: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, }, reportName: 'Concierge', type: CONST.REPORT.TYPE.CHAT, @@ -252,8 +252,8 @@ describe('OptionsListUtils', () => { isPinned: false, reportID: '12', participants: { - 2: {}, - 1000: {}, + 2: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + 1000: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, }, reportName: 'Chronos', type: CONST.REPORT.TYPE.CHAT, @@ -268,8 +268,8 @@ describe('OptionsListUtils', () => { isPinned: false, reportID: '13', participants: { - 2: {}, - 1001: {}, + 2: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + 1001: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, }, reportName: 'Receipts', type: CONST.REPORT.TYPE.CHAT, @@ -284,10 +284,10 @@ describe('OptionsListUtils', () => { isPinned: false, reportID: '14', participants: { - 2: {}, - 1: {}, - 10: {}, - 3: {}, + 2: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + 1: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + 10: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + 3: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, }, reportName: '', oldPolicyName: 'Avengers Room', @@ -306,8 +306,8 @@ describe('OptionsListUtils', () => { isChatRoom: false, reportID: '15', participants: { - 1: {}, - 2: {}, + 1: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + 2: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, }, reportName: 'Test Workspace', type: CONST.REPORT.TYPE.CHAT, @@ -323,9 +323,9 @@ describe('OptionsListUtils', () => { isPinned: false, reportID: '15', participants: { - 2: {}, - 3: {}, - 4: {}, + 2: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + 3: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, + 4: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, }, reportName: 'Spider-Man, Black Panther', type: CONST.REPORT.TYPE.CHAT, From fb066c112ea78ac1566e4525e1472062f5318f26 Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Tue, 27 Aug 2024 18:44:39 +0530 Subject: [PATCH 12/16] Fix lint --- src/types/onyx/Report.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/onyx/Report.ts b/src/types/onyx/Report.ts index 6287acc14850..b92f38f29ee1 100644 --- a/src/types/onyx/Report.ts +++ b/src/types/onyx/Report.ts @@ -115,7 +115,7 @@ type Report = OnyxCommon.OnyxValueWithOfflineFeedback< /** The policy avatar to use, if any */ policyAvatar?: string | null; - + /** The policy name to use */ policyName?: string | null; From dbbf085b2cb1e9e1a7cb43b29f4f02ef7aaffeb3 Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Thu, 29 Aug 2024 19:15:21 +0530 Subject: [PATCH 13/16] Fixed some optimistic cases --- src/libs/UnreadIndicatorUpdater/index.ts | 12 ++++++---- src/libs/actions/IOU.ts | 22 ++--------------- src/libs/actions/Report.ts | 6 ++--- tests/actions/IOUTest.ts | 30 ++++++++---------------- 4 files changed, 22 insertions(+), 48 deletions(-) diff --git a/src/libs/UnreadIndicatorUpdater/index.ts b/src/libs/UnreadIndicatorUpdater/index.ts index 40af0759f06a..f4b8e3281308 100644 --- a/src/libs/UnreadIndicatorUpdater/index.ts +++ b/src/libs/UnreadIndicatorUpdater/index.ts @@ -9,8 +9,9 @@ import type {Report} from '@src/types/onyx'; import updateUnread from './updateUnread'; function getUnreadReportsForUnreadIndicator(reports: OnyxCollection, currentReportID: string) { - return Object.values(reports ?? {}).filter( - (report) => + return Object.values(reports ?? {}).filter((report) => { + const notificationPreference = ReportUtils.getReportNotificationPreference(report); + return ( ReportUtils.isUnread(report) && ReportUtils.shouldReportBeInOptionList({ report, @@ -29,9 +30,10 @@ function getUnreadReportsForUnreadIndicator(reports: OnyxCollection, cur * Furthermore, muted reports may or may not appear in the LHN depending on priority mode, * but they should not be considered in the unread indicator count. */ - ReportUtils.getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN && - ReportUtils.getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE, - ); + notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN && + notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE + ); + }); } const memoizedGetUnreadReportsForUnreadIndicator = memoize(getUnreadReportsForUnreadIndicator, {maxArgs: 1}); diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index e00f8149aab5..5f4fdaca88f5 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -3851,7 +3851,7 @@ function getOrCreateOptimisticSplitChatReport(existingSplitChatReportID: string, undefined, undefined, undefined, - CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, + CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, ); return {existingSplitChatReport: null, splitChatReport}; } @@ -3951,11 +3951,6 @@ function createSplitsAndOnyxData( splitChatReport.lastActorAccountID = currentUserAccountID; splitChatReport.lastVisibleActionCreated = splitIOUReportAction.created; - let splitChatReportNotificationPreference = ReportUtils.getReportNotificationPreference(splitChatReport); - if (splitChatReportNotificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) { - splitChatReportNotificationPreference = CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS; - } - // If we have an existing splitChatReport (group chat or workspace) use it's pending fields, otherwise indicate that we are adding a chat if (!existingSplitChatReport) { splitChatReport.pendingFields = { @@ -3963,26 +3958,13 @@ function createSplitsAndOnyxData( }; } - const updatedParticipants = fastMerge( - splitChatReport.participants, - { - [currentUserAccountID]: { - notificationPreference: splitChatReportNotificationPreference, - }, - }, - false, - ); - const optimisticData: OnyxUpdate[] = [ { // Use set for new reports because it doesn't exist yet, is faster, // and we need the data to be available when we navigate to the chat page onyxMethod: existingSplitChatReport ? Onyx.METHOD.MERGE : Onyx.METHOD.SET, key: `${ONYXKEYS.COLLECTION.REPORT}${splitChatReport.reportID}`, - value: { - ...splitChatReport, - participants: updatedParticipants, - }, + value: splitChatReport, }, { onyxMethod: Onyx.METHOD.SET, diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 5e375d7692d6..34583e3ac1fa 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -1008,7 +1008,7 @@ function navigateToAndOpenReport( if (isEmptyObject(chat)) { if (isGroupChat) { // If we are creating a group chat then participantAccountIDs is expected to contain currentUserAccountID - newChat = ReportUtils.buildOptimisticGroupChatReport(participantAccountIDs, reportName ?? '', avatarUri ?? '', optimisticReportID, CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN); + newChat = ReportUtils.buildOptimisticGroupChatReport(participantAccountIDs, reportName ?? '', avatarUri ?? '', optimisticReportID, CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS); } else { newChat = ReportUtils.buildOptimisticChatReport( [...participantAccountIDs, currentUserAccountID], @@ -1020,7 +1020,7 @@ function navigateToAndOpenReport( undefined, undefined, undefined, - CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, + CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, ); } } @@ -2749,7 +2749,7 @@ function joinRoom(report: OnyxEntry) { } updateNotificationPreference( report.reportID, - CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, + ReportUtils.getReportNotificationPreference(report), CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, false, report.parentReportID, diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index 74fca9c176a8..662eae8d7b21 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -1150,27 +1150,17 @@ describe('actions/IOU', () => { expect(isEmptyObject(vitIOUReport)).toBe(false); expect(vitIOUReport?.total).toBe(amount / 4); - const groupParticipants = { - [CARLOS_ACCOUNT_ID]: { - notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, - role: CONST.REPORT.ROLE.MEMBER, - }, - [JULES_ACCOUNT_ID]: { - notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, - role: CONST.REPORT.ROLE.MEMBER, - }, - [VIT_ACCOUNT_ID]: { - notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, - role: CONST.REPORT.ROLE.MEMBER, - }, - [RORY_ACCOUNT_ID]: { - notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, - role: CONST.REPORT.ROLE.ADMIN, - }, - }; - // 7. The group chat with everyone - groupChat = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.CHAT && isEqual(report.participants, groupParticipants)); + groupChat = Object.values(allReports ?? {}).find( + (report) => + report?.type === CONST.REPORT.TYPE.CHAT && + isEqual(report.participants, { + [CARLOS_ACCOUNT_ID]: CARLOS_PARTICIPANT, + [JULES_ACCOUNT_ID]: JULES_PARTICIPANT, + [VIT_ACCOUNT_ID]: VIT_PARTICIPANT, + [RORY_ACCOUNT_ID]: RORY_PARTICIPANT, + }), + ); expect(isEmptyObject(groupChat)).toBe(false); expect(groupChat?.pendingFields).toStrictEqual({createChat: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD}); From ddb38a00feac88fa96ee3a79082140ec9feb15dc Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Thu, 29 Aug 2024 19:20:00 +0530 Subject: [PATCH 14/16] Fixed some more optimistic cases --- src/libs/ReportUtils.ts | 4 +--- tests/actions/PolicyTest.ts | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index f0f0e4c4048e..bd61d0ce110d 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -5457,9 +5457,7 @@ function buildOptimisticWorkspaceChats(policyID: string, policyName: string, exp policyName, undefined, undefined, - - // #announce contains all policy members so notifying always should be opt-in only. - CONST.REPORT.NOTIFICATION_PREFERENCE.DAILY, + CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, ); const announceChatReportID = announceChatData.reportID; const announceCreatedAction = buildOptimisticCreatedReportAction(CONST.POLICY.OWNER_EMAIL_FAKE); diff --git a/tests/actions/PolicyTest.ts b/tests/actions/PolicyTest.ts index ad7c1d996c64..2009ce53c3fd 100644 --- a/tests/actions/PolicyTest.ts +++ b/tests/actions/PolicyTest.ts @@ -12,7 +12,7 @@ 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.DAILY, role: 'admin'}; +const ESH_PARTICIPANT_ANNOUNCE_ROOM: Participant = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, role: 'admin'}; const ESH_PARTICIPANT_ADMINS_ROOM: Participant = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, role: 'admin'}; const ESH_PARTICIPANT_EXPENSE_CHAT = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, role: 'admin'}; const WORKSPACE_NAME = "Esh's Workspace"; From 152e09ac0b5b0b6cec0957a67fda292707268fa7 Mon Sep 17 00:00:00 2001 From: Shubham Agrawal <58412969+shubham1206agra@users.noreply.github.com> Date: Sun, 1 Sep 2024 12:52:07 +0530 Subject: [PATCH 15/16] Fix test --- tests/actions/PolicyTest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/actions/PolicyTest.ts b/tests/actions/PolicyTest.ts index 2009ce53c3fd..afffb9e387aa 100644 --- a/tests/actions/PolicyTest.ts +++ b/tests/actions/PolicyTest.ts @@ -12,7 +12,7 @@ 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, role: 'admin'}; +const ESH_PARTICIPANT_ANNOUNCE_ROOM: Participant = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}; const ESH_PARTICIPANT_ADMINS_ROOM: Participant = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, role: 'admin'}; const ESH_PARTICIPANT_EXPENSE_CHAT = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, role: 'admin'}; const WORKSPACE_NAME = "Esh's Workspace"; From 26f869525124b8083b045ff695349cf424082e57 Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Sun, 1 Sep 2024 13:22:36 +0530 Subject: [PATCH 16/16] Fixed jest tests --- tests/actions/PolicyTest.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/actions/PolicyTest.ts b/tests/actions/PolicyTest.ts index afffb9e387aa..01842b48b6f4 100644 --- a/tests/actions/PolicyTest.ts +++ b/tests/actions/PolicyTest.ts @@ -13,8 +13,8 @@ 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, role: 'admin'}; -const ESH_PARTICIPANT_EXPENSE_CHAT = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, role: 'admin'}; +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"; OnyxUpdateManager();