| 10 | } |
| 11 | |
| 12 | function create(data) { |
| 13 | return db.Alert.create(data) |
| 14 | .then((createdAlert) => { |
| 15 | return findById(createdAlert.id); |
| 16 | }) |
| 17 | .then((alert) => { |
| 18 | if (data.alertIntegrations) { |
| 19 | data.alertIntegrations.forEach((ai) => { |
| 20 | const foundIntegration = alert.AlertIntegrations.find((alertIntegration) => { |
| 21 | return alertIntegration.id === ai.id; |
| 22 | }); |
| 23 | |
| 24 | if (foundIntegration) { |
| 25 | db.AlertIntegration.update({ |
| 26 | enabled: ai.enabled, |
| 27 | }, { |
| 28 | where: { |
| 29 | id: foundIntegration.id, |
| 30 | }, |
| 31 | }); |
| 32 | } else { |
| 33 | db.AlertIntegration.create({ |
| 34 | alert_id: alert.id, |
| 35 | integration_id: ai.integration_id, |
| 36 | enabled: ai.enabled, |
| 37 | }); |
| 38 | } |
| 39 | }); |
| 40 | } |
| 41 | |
| 42 | return findById(alert.id); |
| 43 | }) |
| 44 | .catch((err) => { |
| 45 | return new Promise((resolve, reject) => { |
| 46 | reject(err); |
| 47 | }); |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | function update(id, data) { |
| 52 | return db.Alert.update(data, { |