createBulkMonitors -> CreateMonitors
This commit is contained in:
1 parent
83d56f1ff6
commit
da38f2ee76
3 files changed
+20
-10
No files matched your search
@@ -17,7 +17,7 @@ export interface SummaryConfig {
|
|||||||
export interface IMonitorsRepository {
|
export interface IMonitorsRepository {
|
||||||
// create
|
// create
|
||||||
create(monitor: Monitor, teamId: string, userId: string): Promise<Monitor | null>;
|
create(monitor: Monitor, teamId: string, userId: string): Promise<Monitor | null>;
|
||||||
createBulkMonitors(monitors: Monitor[]): Promise<Monitor[]>;
|
createMonitors(monitors: Monitor[]): Promise<Monitor[]>;
|
||||||
// single fetch
|
// single fetch
|
||||||
findById(monitorId: string, teamId: string): Promise<Monitor>;
|
findById(monitorId: string, teamId: string): Promise<Monitor>;
|
||||||
|
|
||||||
|
|||||||
@@ -12,13 +12,23 @@ class MongoMonitorsRepository implements IMonitorsRepository {
|
|||||||
return this.toEntity(saved);
|
return this.toEntity(saved);
|
||||||
};
|
};
|
||||||
|
|
||||||
createBulkMonitors = async (monitors: Monitor[]): Promise<Monitor[]> => {
|
createMonitors = async (monitors: Monitor[]): Promise<Monitor[]> => {
|
||||||
if (!monitors.length) {
|
if (!monitors.length) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
const payload = monitors.map((monitor) => ({ ...monitor, notifications: undefined }));
|
const payload = monitors.map((monitor) => ({ ...monitor, notifications: undefined }));
|
||||||
const inserted = await MonitorModel.insertMany(payload, { ordered: false });
|
try {
|
||||||
return this.mapDocuments(inserted);
|
const inserted = await MonitorModel.insertMany(payload, { ordered: false });
|
||||||
|
return this.mapDocuments(inserted);
|
||||||
|
} catch (error: any) {
|
||||||
|
if (error.name === "MongoBulkWriteError" || error.name === "BulkWriteError") {
|
||||||
|
const insertedDocs = error.insertedDocs || [];
|
||||||
|
if (insertedDocs.length > 0) {
|
||||||
|
return this.mapDocuments(insertedDocs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
findById = async (monitorId: string, teamId: string): Promise<Monitor> => {
|
findById = async (monitorId: string, teamId: string): Promise<Monitor> => {
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ export interface IMonitorService {
|
|||||||
|
|
||||||
// create
|
// create
|
||||||
createMonitor(teamId: string, userId: string, body: Monitor): Promise<void>;
|
createMonitor(teamId: string, userId: string, body: Monitor): Promise<void>;
|
||||||
|
createMonitors(monitors: Array<Monitor>, userId: string, teamId: string): Promise<Monitor[] | null>;
|
||||||
addDemoMonitors(args: { userId: string; teamId: string }): Promise<Monitor[]>;
|
addDemoMonitors(args: { userId: string; teamId: string }): Promise<Monitor[]>;
|
||||||
|
|
||||||
// read
|
// read
|
||||||
@@ -157,11 +158,10 @@ export class MonitorService implements IMonitorService {
|
|||||||
this.jobQueue.addJob(monitor.id, monitor);
|
this.jobQueue.addJob(monitor.id, monitor);
|
||||||
};
|
};
|
||||||
|
|
||||||
createBulkMonitors = async (monitors: Array<Monitor>, userId: string, teamId: string): Promise<Monitor[] | null> => {
|
createMonitors = async (monitors: Array<Monitor>, userId: string, teamId: string): Promise<Monitor[] | null> => {
|
||||||
// TDO rename createMonitors, change to accept JSON
|
const createdMonitors = await this.monitorsRepository.createMonitors(monitors);
|
||||||
const createdMonitors = await this.monitorsRepository.createBulkMonitors(monitors);
|
|
||||||
if (!monitors || monitors.length === 0) {
|
if (!monitors || monitors.length === 0) {
|
||||||
throw new AppError({ message: "Failed to create monitors", status: 500, service: SERVICE_NAME, method: "createBulkMonitors" });
|
throw new AppError({ message: "Failed to create monitors", status: 500, service: SERVICE_NAME, method: "createMonitors" });
|
||||||
}
|
}
|
||||||
|
|
||||||
await Promise.all(createdMonitors.map((monitor) => this.jobQueue.addJob(monitor.id, monitor)));
|
await Promise.all(createdMonitors.map((monitor) => this.jobQueue.addJob(monitor.id, monitor)));
|
||||||
@@ -185,7 +185,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
interval: 60000,
|
interval: 60000,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
const demoMonitors = await this.monitorsRepository.createBulkMonitors(monitors);
|
const demoMonitors = await this.monitorsRepository.createMonitors(monitors);
|
||||||
|
|
||||||
await Promise.all(demoMonitors.map((monitor) => this.jobQueue.addJob(monitor.id, monitor)));
|
await Promise.all(demoMonitors.map((monitor) => this.jobQueue.addJob(monitor.id, monitor)));
|
||||||
return demoMonitors;
|
return demoMonitors;
|
||||||
@@ -511,7 +511,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
return cleanData;
|
return cleanData;
|
||||||
});
|
});
|
||||||
|
|
||||||
const createdMonitors = await this.createBulkMonitors(cleanedMonitors, userId, teamId);
|
const createdMonitors = await this.createMonitors(cleanedMonitors, userId, teamId);
|
||||||
|
|
||||||
if (!createdMonitors || createdMonitors.length === 0) {
|
if (!createdMonitors || createdMonitors.length === 0) {
|
||||||
throw new AppError({
|
throw new AppError({
|
||||||
|
|||||||
Reference in new issue
Block a user