From 4c41990a93ec26fd840c2974e2e7af3fec615652 Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Fri, 16 Aug 2024 22:41:19 +0530 Subject: [PATCH] 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); + }); }); }); });