diff --git a/src/libs/markAllPolicyReportsAsRead.ts b/src/libs/markAllPolicyReportsAsRead.ts new file mode 100644 index 000000000000..c3c719b1132e --- /dev/null +++ b/src/libs/markAllPolicyReportsAsRead.ts @@ -0,0 +1,33 @@ +// eslint-disable-next-line you-dont-need-lodash-underscore/each +import Onyx from 'react-native-onyx'; +import ONYXKEYS from '@src/ONYXKEYS'; +import type {Report} from '@src/types/onyx'; +import * as ReportActionFile from './actions/Report'; +import * as ReportUtils from './ReportUtils'; + +export default function markAllPolicyReportsAsRead(policyID: string) { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + if (!allReports) { + return; + } + + let delay = 0; + Object.keys(allReports).forEach((key: string) => { + const report: Report | null | undefined = allReports[key]; + if (report?.policyID !== policyID || !ReportUtils.isUnread(report)) { + return; + } + + setTimeout(() => { + ReportActionFile.readNewestAction(report?.reportID); + }, delay); + + delay += 1000; + }); + Onyx.disconnect(connectionID); + }, + }); +} diff --git a/src/setup/addUtilsToWindow.ts b/src/setup/addUtilsToWindow.ts index 9991a3dc07cd..d2d11e138431 100644 --- a/src/setup/addUtilsToWindow.ts +++ b/src/setup/addUtilsToWindow.ts @@ -1,5 +1,6 @@ import Onyx from 'react-native-onyx'; import * as Environment from '@libs/Environment/Environment'; +import markAllPolicyReportsAsRead from '@libs/markAllPolicyReportsAsRead'; import * as Session from '@userActions/Session'; /** @@ -44,5 +45,9 @@ export default function addUtilsToWindow() { }; window.setSupportToken = Session.setSupportAuthToken; + + // Workaround to give employees the ability to mark reports as read via the JS console + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (window as any).markAllPolicyReportsAsRead = markAllPolicyReportsAsRead; }); }