add opt out flag for recent checks

This commit is contained in:
Alex Holliday committed 2026-08-04 15:30:34 -07:00
1 parent 5fa74bd883
commit 303f47a117
7 files changed
+22 -14

No files matched your search

@@ -106,7 +106,7 @@ export class MaintenanceWindowService implements IMaintenanceWindowService {
start: string;
end: string;
}) => {
const monitors = await this.monitorsRepository.findByIds(monitorIDs);
const monitors = await this.monitorsRepository.findByIds(monitorIDs, { includeRecentChecks: false });
const unauthorizedMonitors = monitors.filter((monitor) => monitor.teamId !== teamId);
@@ -193,7 +193,7 @@ export class MaintenanceWindowService implements IMaintenanceWindowService {
const update: Partial<MaintenanceWindow> = rest;
if (monitors !== undefined) {
const monitorDocs = await this.monitorsRepository.findByIds(monitors);
const monitorDocs = await this.monitorsRepository.findByIds(monitors, { includeRecentChecks: false });
const unauthorizedMonitors = monitorDocs.filter((monitor) => monitor.teamId !== teamId);
if (unauthorizedMonitors.length > 0) {
throw new AppError({
@@ -27,9 +27,9 @@ export interface IMonitorsRepository {
// collection fetch
findAllForScheduling(): Promise<MonitorScheduleFields[]>;
findByTeamId(teamId: string, config: TeamQueryConfig): Promise<Monitor[]>;
findByTeamId(teamId: string, config: TeamQueryConfig, options?: { includeRecentChecks?: boolean }): Promise<Monitor[]>;
findByTeamIdWithStats(teamId: string, config: TeamQueryConfig): Promise<Monitor[]>;
findByIds(monitorIds: string[]): Promise<Monitor[]>;
findByIds(monitorIds: string[], options?: { includeRecentChecks?: boolean }): Promise<Monitor[]>;
// update
updateById(monitorId: string, teamId: string, updates: Partial<Monitor>): Promise<Monitor>;
@@ -104,10 +104,15 @@ class MongoMonitorsRepository implements IMonitorsRepository {
return { sort: { [field]: order === "asc" ? 1 : -1 } as const, skip: Math.max(page, 0) * rowsPerPage, limit: rowsPerPage };
};
findByTeamId = async (teamId: string, config: TeamQueryConfig): Promise<Monitor[]> => {
findByTeamId = async (teamId: string, config: TeamQueryConfig, options?: { includeRecentChecks?: boolean }): Promise<Monitor[]> => {
const query = this.queryBuilder(config, teamId);
const { sort, skip, limit } = this.pageOptions(config);
const documents = await MonitorModel.find(query).sort(sort).skip(skip).limit(limit);
const cursor = MonitorModel.find(query).sort(sort).skip(skip).limit(limit);
if (options?.includeRecentChecks === false) {
cursor.select({ recentChecks: 0 });
}
const documents = await cursor;
return this.mapDocuments(documents);
};
@@ -126,15 +131,18 @@ class MongoMonitorsRepository implements IMonitorsRepository {
return documents.map((doc) => this.toEntity(doc));
};
findByIds = async (monitorIds: string[]): Promise<Monitor[]> => {
findByIds = async (monitorIds: string[], options?: { includeRecentChecks?: boolean }): Promise<Monitor[]> => {
if (!monitorIds.length) {
return [];
}
const objectIds = monitorIds.map((id) => new mongoose.Types.ObjectId(id));
const pipeline: PipelineStage[] = [{ $match: { _id: { $in: objectIds } } }, ...this.uptimeStatsLookupStages];
const pipeline: PipelineStage[] = [
{ $match: { _id: { $in: objectIds } } },
...(options?.includeRecentChecks === false ? [{ $project: { recentChecks: 0 } } as PipelineStage] : []),
...this.uptimeStatsLookupStages,
];
const documents = await MonitorModel.aggregate(pipeline);
return documents.map((doc) => this.toEntity(doc));
};
@@ -519,7 +519,7 @@ export class MonitorService implements IMonitorService {
};
exportMonitorsToJSON = async ({ teamId }: { teamId: string }): Promise<Monitor[]> => {
const monitors = await this.monitorsRepository.findByTeamId(teamId, {});
const monitors = await this.monitorsRepository.findByTeamId(teamId, {}, { includeRecentChecks: false });
if (monitors.length === 0) {
throw new AppError({ message: "No monitors found to export.", service: SERVICE_NAME, method: "exportMonitorsToJSON", status: 400 });
+1 -1
View File
@@ -324,7 +324,7 @@ export class UserService implements IUserService {
}
if (roles.includes("superadmin")) {
const monitors = await this.monitorsRepository.findByTeamId(teamId, {});
const monitors = await this.monitorsRepository.findByTeamId(teamId, {}, { includeRecentChecks: false });
if (monitors) {
await Promise.all(monitors.map((monitor) => this.scheduler.deleteJob(monitor)));
}
@@ -123,7 +123,7 @@ describe("MaintenanceWindowService", () => {
await service.createMaintenanceWindow(defaultCreateParams);
expect(monitorsRepository.findByIds).toHaveBeenCalledWith(["mon-1", "mon-2"]);
expect(monitorsRepository.findByIds).toHaveBeenCalledWith(["mon-1", "mon-2"], { includeRecentChecks: false });
});
it("throws 403 when a monitor belongs to a different team", async () => {
@@ -404,7 +404,7 @@ describe("MaintenanceWindowService", () => {
body: { monitors: ["mon-1", "mon-2"] },
});
expect(monitorsRepository.findByIds).toHaveBeenCalledWith(["mon-1", "mon-2"]);
expect(monitorsRepository.findByIds).toHaveBeenCalledWith(["mon-1", "mon-2"], { includeRecentChecks: false });
});
it("throws 403 when a new monitor belongs to a different team", async () => {
@@ -472,7 +472,7 @@ describe("UserService", () => {
await service.deleteUser({ userId: "user-1", teamId: "team-1", roles: ["superadmin"] });
expect(monitorsRepository.findByTeamId).toHaveBeenCalledWith("team-1", {});
expect(monitorsRepository.findByTeamId).toHaveBeenCalledWith("team-1", {}, { includeRecentChecks: false });
expect(scheduler.deleteJob).toHaveBeenCalledTimes(2);
expect(usersRepository.deleteById).toHaveBeenCalledWith("user-1");
});