feat(monitors): add bulk pause/resume api endpoints and service logic
- Validates bulk monitor IDs using zod - Implements optimized Mongo $in/$ne queries for performance - Orchestrates background job pausing/resuming via BullMQ - Added comprehensive unit tests for monitor service logic Fixes #3602
This commit is contained in:
1 parent
28baeaf218
commit
094def117a
7 files changed
+166
-1
No files matched your search
@@ -39,6 +39,7 @@ export interface IMonitorsRepository {
|
||||
statusPatch?: Partial<Monitor>
|
||||
): Promise<Monitor>;
|
||||
togglePauseById(monitorId: string, teamId: string): Promise<Monitor>;
|
||||
bulkTogglePause(monitorIds: string[], teamId: string, pause: boolean): Promise<Monitor[]>;
|
||||
// delete
|
||||
deleteById(monitorId: string, teamId: string): Promise<Monitor>;
|
||||
deleteByTeamId(teamId: string): Promise<{ monitors: Monitor[]; deletedCount: number }>;
|
||||
|
||||
@@ -234,6 +234,31 @@ class MongoMonitorsRepository implements IMonitorsRepository {
|
||||
return this.toEntity(monitor);
|
||||
};
|
||||
|
||||
bulkTogglePause = async (monitorIds: string[], teamId: string, pause: boolean): Promise<Monitor[]> => {
|
||||
const objectIds = monitorIds.map((id) => new mongoose.Types.ObjectId(id));
|
||||
const filter = {
|
||||
_id: { $in: objectIds },
|
||||
teamId: new mongoose.Types.ObjectId(teamId),
|
||||
isActive: pause, // Only pause if active (true), only resume if inactive (false)
|
||||
};
|
||||
|
||||
const eligibleIds = (await MonitorModel.find(filter).select("_id").lean()).map((doc) => doc._id);
|
||||
if (eligibleIds.length === 0) return [];
|
||||
|
||||
await MonitorModel.updateMany(
|
||||
{ _id: { $in: eligibleIds } },
|
||||
{
|
||||
$set: {
|
||||
isActive: !pause,
|
||||
status: pause ? "paused" : "initializing",
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
const updatedMonitors = await MonitorModel.find({ _id: { $in: eligibleIds } });
|
||||
return this.mapDocuments(updatedMonitors);
|
||||
};
|
||||
|
||||
deleteById = async (monitorId: string, teamId: string) => {
|
||||
const deletedMonitor = await MonitorModel.findOneAndDelete({ _id: monitorId, teamId });
|
||||
|
||||
|
||||
Reference in new issue
Block a user