addDemo
This commit is contained in:
1 parent
8a986d2e58
commit
77764c51ee
3 files changed
+43
-11
No files matched your search
@@ -23,6 +23,7 @@ export interface IMonitorsRepository {
|
|||||||
// update
|
// update
|
||||||
update(monitorId: string, updates: Partial<Monitor>): Promise<Monitor>;
|
update(monitorId: string, updates: Partial<Monitor>): Promise<Monitor>;
|
||||||
// delete
|
// delete
|
||||||
|
deleteByTeamId(teamId: string): Promise<{ monitors: Monitor[]; deletedCount: number }>;
|
||||||
|
|
||||||
// counts
|
// counts
|
||||||
findMonitorCountByTeamIdAndType(teamId: string, config: TeamQueryConfig): Promise<number>;
|
findMonitorCountByTeamIdAndType(teamId: string, config: TeamQueryConfig): Promise<number>;
|
||||||
|
|||||||
@@ -13,9 +13,12 @@ class MongoMonitorsRepository implements IMonitorsRepository {
|
|||||||
};
|
};
|
||||||
|
|
||||||
createBulkMonitors = async (monitors: Monitor[]): Promise<Monitor[]> => {
|
createBulkMonitors = async (monitors: Monitor[]): Promise<Monitor[]> => {
|
||||||
const newMonitors = monitors.map((monitor) => new MonitorModel({ ...monitor, notifications: undefined }));
|
if (!monitors.length) {
|
||||||
await MonitorModel.bulkSave(newMonitors);
|
return [];
|
||||||
return monitors;
|
}
|
||||||
|
const payload = monitors.map((monitor) => ({ ...monitor, notifications: undefined }));
|
||||||
|
const inserted = await MonitorModel.insertMany(payload, { ordered: false });
|
||||||
|
return this.mapDocuments(inserted);
|
||||||
};
|
};
|
||||||
|
|
||||||
findById = async (MonitorModelId: string): Promise<Monitor> => {
|
findById = async (MonitorModelId: string): Promise<Monitor> => {
|
||||||
@@ -106,6 +109,13 @@ class MongoMonitorsRepository implements IMonitorsRepository {
|
|||||||
return this.toEntity(updatedMonitor);
|
return this.toEntity(updatedMonitor);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
deleteByTeamId = async (teamId: string) => {
|
||||||
|
const monitors = await MonitorModel.find({ teamId });
|
||||||
|
const { deletedCount } = await MonitorModel.deleteMany({ teamId });
|
||||||
|
|
||||||
|
return { monitors: this.mapDocuments(monitors), deletedCount };
|
||||||
|
};
|
||||||
|
|
||||||
private mapDocuments = (documents: MonitorDocument[]): Monitor[] => {
|
private mapDocuments = (documents: MonitorDocument[]): Monitor[] => {
|
||||||
if (!documents?.length) {
|
if (!documents?.length) {
|
||||||
return [];
|
return [];
|
||||||
|
|||||||
@@ -3,6 +3,10 @@ import { NormalizeData } from "@/utils/dataUtils.js";
|
|||||||
import { type Monitor } from "@/types/index.js";
|
import { type Monitor } from "@/types/index.js";
|
||||||
import type { MonitorType } from "@/types/monitor.js";
|
import type { MonitorType } from "@/types/monitor.js";
|
||||||
import type { IMonitorsRepository } from "@/repositories/index.js";
|
import type { IMonitorsRepository } from "@/repositories/index.js";
|
||||||
|
import fs from "fs";
|
||||||
|
import { fileURLToPath } from "url";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
import { AppError } from "../infrastructure/errorService.js";
|
import { AppError } from "../infrastructure/errorService.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "MonitorService";
|
const SERVICE_NAME = "MonitorService";
|
||||||
@@ -82,6 +86,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
private games: any;
|
private games: any;
|
||||||
private monitorsRepository: IMonitorsRepository;
|
private monitorsRepository: IMonitorsRepository;
|
||||||
private checksRepository: any;
|
private checksRepository: any;
|
||||||
|
private fs: any;
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
db,
|
db,
|
||||||
@@ -198,9 +203,25 @@ export class MonitorService implements IMonitorService {
|
|||||||
};
|
};
|
||||||
|
|
||||||
addDemoMonitors = async ({ userId, teamId }: { userId: string; teamId: string }): Promise<any[]> => {
|
addDemoMonitors = async ({ userId, teamId }: { userId: string; teamId: string }): Promise<any[]> => {
|
||||||
const demoMonitors = await this.db.monitorModule.addDemoMonitors(userId, teamId);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
const __dirname = path.dirname(__filename);
|
||||||
|
const demoMonitorsPath = path.resolve(__dirname, "../../utils/demoMonitors.json");
|
||||||
|
|
||||||
await Promise.all(demoMonitors.map((monitor: any) => this.jobQueue.addJob(monitor._id, monitor)));
|
const demoData = JSON.parse(fs.readFileSync(demoMonitorsPath, "utf8"));
|
||||||
|
const monitors: Monitor[] = demoData.map((monitor: Monitor) => {
|
||||||
|
return {
|
||||||
|
userId,
|
||||||
|
teamId,
|
||||||
|
name: monitor.name,
|
||||||
|
description: monitor.name,
|
||||||
|
type: "http",
|
||||||
|
url: monitor.url,
|
||||||
|
interval: 60000,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
const demoMonitors = await this.monitorsRepository.createBulkMonitors(monitors);
|
||||||
|
|
||||||
|
await Promise.all(demoMonitors.map((monitor) => this.jobQueue.addJob(monitor.id, monitor)));
|
||||||
return demoMonitors;
|
return demoMonitors;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -399,17 +420,17 @@ export class MonitorService implements IMonitorService {
|
|||||||
};
|
};
|
||||||
|
|
||||||
deleteAllMonitors = async ({ teamId }: { teamId: string }): Promise<number> => {
|
deleteAllMonitors = async ({ teamId }: { teamId: string }): Promise<number> => {
|
||||||
const { monitors, deletedCount } = await this.db.monitorModule.deleteAllMonitors(teamId);
|
const { monitors, deletedCount } = await this.monitorsRepository.deleteByTeamId(teamId);
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
monitors.map(async (monitor: any) => {
|
monitors.map(async (monitor) => {
|
||||||
try {
|
try {
|
||||||
await this.jobQueue.deleteJob(monitor);
|
await this.jobQueue.deleteJob(monitor);
|
||||||
await this.db.checkModule.deleteChecks(monitor._id);
|
await this.db.checkModule.deleteChecks(monitor.id);
|
||||||
await this.db.pageSpeedCheckModule.deletePageSpeedChecksByMonitorId(monitor._id);
|
await this.db.pageSpeedCheckModule.deletePageSpeedChecksByMonitorId(monitor.id);
|
||||||
await this.db.notificationsModule.deleteNotificationsByMonitorId(monitor._id);
|
await this.db.notificationsModule.deleteNotificationsByMonitorId(monitor.id);
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
this.logger.warn({
|
this.logger.warn({
|
||||||
message: `Error deleting associated records for monitor ${monitor._id} with name ${monitor.name}`,
|
message: `Error deleting associated records for monitor ${monitor.id} with name ${monitor.name}`,
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
method: "deleteAllMonitors",
|
method: "deleteAllMonitors",
|
||||||
stack: error.stack,
|
stack: error.stack,
|
||||||
|
|||||||
Reference in new issue
Block a user