* create or update
(data: MonitorUpsertData)
| 30 | * create or update |
| 31 | */ |
| 32 | async upsert(data: MonitorUpsertData): Promise<MonitorWithNotification> { |
| 33 | let monitor: MonitorWithNotification; |
| 34 | const { id, workspaceId, notificationIds = [], ...others } = data; |
| 35 | if (id) { |
| 36 | // update |
| 37 | monitor = await prisma.monitor.update({ |
| 38 | where: { |
| 39 | id, |
| 40 | workspaceId, |
| 41 | }, |
| 42 | data: { |
| 43 | ...others, |
| 44 | notifications: { |
| 45 | set: notificationIds.map((id) => ({ id })), |
| 46 | }, |
| 47 | }, |
| 48 | include: { |
| 49 | notifications: true, |
| 50 | }, |
| 51 | }); |
| 52 | } else { |
| 53 | // create |
| 54 | monitor = await prisma.monitor.create({ |
| 55 | data: { |
| 56 | ...others, |
| 57 | workspaceId, |
| 58 | notifications: { |
| 59 | connect: notificationIds.map((id) => ({ id })), |
| 60 | }, |
| 61 | }, |
| 62 | include: { |
| 63 | notifications: true, |
| 64 | }, |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | if (this.monitorRunner[monitor.id]) { |
| 69 | // Stop and remove old |
| 70 | this.monitorRunner[monitor.id].stopMonitor(); |
| 71 | delete this.monitorRunner[monitor.id]; |
| 72 | } |
| 73 | |
| 74 | const runner = await this.createRunner(monitor); |
| 75 | runner.startMonitor(); |
| 76 | |
| 77 | return monitor; |
| 78 | } |
| 79 | |
| 80 | async delete(workspaceId: string, monitorId: string) { |
| 81 | const runner = this.getRunner(monitorId); |
no test coverage detected