diff --git a/backend/backend/api/zapier.js b/backend/backend/api/zapier.js index b555152c83..c04c600e29 100755 --- a/backend/backend/api/zapier.js +++ b/backend/backend/api/zapier.js @@ -73,7 +73,7 @@ router.get('/incidents', isAuthorized, async function(req, res) { } }); -router.get('/incident-note', isAuthorized, async function(req, res){ +router.get('/incident-note', isAuthorized, async function(req, res) { try { const projectId = req.query.projectId; // We return all the incidents to zapier because it gives user an option to configure zapier properly with all the steps. @@ -83,7 +83,17 @@ router.get('/incident-note', isAuthorized, async function(req, res){ } catch (error) { return sendErrorResponse(req, res, error); } -}) +}); + +router.post('/incident/incident-note', isAuthorized, async function(req, res) { + try { + const { data } = req.body; + const incidentNote = await ZapierService.createIncidentNote(data); + return sendItemResponse(req, res, incidentNote); + } catch (error) { + return sendErrorResponse(req, res, error); + } +}); router.get('/incident/resolved', isAuthorized, async function(req, res) { try { diff --git a/backend/backend/models/incidentMessage.js b/backend/backend/models/incidentMessage.js index afec035d75..d98f861cb0 100644 --- a/backend/backend/models/incidentMessage.js +++ b/backend/backend/models/incidentMessage.js @@ -12,7 +12,6 @@ const incidentMessageSchema = new Schema({ type: { type: String, enum: ['investigation', 'internal'], - required: true, }, incident_state: String, createdById: { type: String, ref: 'User', index: true }, //userId. @@ -29,6 +28,7 @@ const incidentMessageSchema = new Schema({ deletedById: { type: String, ref: 'User', index: true }, postOnStatusPage: { type: Boolean, default: false }, + createdByZapier: { type: Boolean, default: false }, }); incidentMessageSchema.virtual('incident', { diff --git a/backend/backend/services/zapierService.js b/backend/backend/services/zapierService.js index 91f59d78ac..595a541fe8 100755 --- a/backend/backend/services/zapierService.js +++ b/backend/backend/services/zapierService.js @@ -123,11 +123,33 @@ module.exports = { return []; } } catch (error) { - ErrorService.log('ZapierService.getIncidents', error); + ErrorService.log('ZapierService.getIncidentNote', error); throw error; } }, + createIncidentNote: async function(data) { + try { + const zapierResponse = {}; + const incidentNoteArr = []; + await Promise.all( + data.incidents.map(async incidentId => { + let incidentMessage = new IncidentMessageModel(); + incidentMessage.incidentId = incidentId; + incidentMessage.createdByZapier = true; + incidentMessage = await incidentMessage.save(); + IncidentService.refreshInterval(incidentId); + await RealTimeService.addIncidentNote(incidentMessage); + incidentNoteArr.push(incidentMessage); + }) + ); + zapierResponse.incidentMessage = incidentNoteArr; + return zapierResponse; + } catch (error) { + ErrorService.log('ZapierService.createIncidentNote', error); + throw error; + } + }, getAcknowledgedIncidents: async function(projectId) { try { const zapierResponseArray = []; @@ -626,3 +648,4 @@ const IncidentModel = require('../models/incident'); const NotificationService = require('./notificationService'); const RealTimeService = require('./realTimeService'); const IncidentMessageService = require('../services/incidentMessageService'); +const IncidentMessageModel = require('../models/incidentMessage'); diff --git a/zapier/actions/createIncident.js b/zapier/actions/createIncident.js index eb86904b1a..aa26a9d46c 100755 --- a/zapier/actions/createIncident.js +++ b/zapier/actions/createIncident.js @@ -5,7 +5,7 @@ const createIncident = (z, bundle) => { }; const responsePromise = z.request({ method: 'POST', - url: `${bundle.authData.serverUrl}/zapier/incident/createIncident1`, + url: `${bundle.authData.serverUrl}/zapier/incident/createIncident`, body: data, }); return responsePromise.then(response => JSON.parse(response.content)); diff --git a/zapier/actions/createIncidentNote.js b/zapier/actions/createIncidentNote.js new file mode 100644 index 0000000000..f001aea913 --- /dev/null +++ b/zapier/actions/createIncidentNote.js @@ -0,0 +1,49 @@ +const createIncidentNote = (z, bundle) => { + if (bundle.cleanedRequest) return bundle.cleanedRequest; + const data = { + data: bundle.inputData, + }; + const responsePromise = z.request({ + method: 'POST', + url: `${bundle.authData.serverUrl}/zapier/incident/incident-note`, + body: data, + }); + return responsePromise.then(response => JSON.parse(response.content)); +}; + +module.exports = { + key: 'incident_note', + noun: 'Incident Note', + + display: { + label: 'Create Incident Note', + description: 'Creates an incident Note.', + important: false, + }, + + operation: { + inputFields: [ + { + key: 'incidents', + type: 'string', + placeholder: 'list of incidents', + dynamic: 'incidents.id', + altersDynamicFields: true, + list: true, + required: true, + }, + ], + perform: createIncidentNote, + sample: { + projectName: 'New Project', + projectId: '1', + incidentId: '1', + id: '1', + content: 'new incidentNote', + incident_state: 'update', + type: 'investigation', + createdAt: new Date().toISOString(), + createdBy: 'Nawaz', + }, + }, +}; diff --git a/zapier/index.js b/zapier/index.js index 1b4fe0df97..be78e4af63 100755 --- a/zapier/index.js +++ b/zapier/index.js @@ -14,6 +14,7 @@ const acknowledgeAllIncidentsAction = require('./actions/acknowledgeAllIncidents const resolveAllIncidentsAction = require('./actions/resolveAllIncidents'); const acknowledgeIncidentAction = require('./actions/acknowledgeIncident'); const resolveIncidentAction = require('./actions/resolveIncident'); +const createIncidentNoteAction = require('./actions/createIncidentNote'); // To include the API key on all outbound requests, simply define a function here. // It runs runs before each request is sent out, allowing you to make tweaks to the request in a centralized spot. @@ -62,6 +63,7 @@ const App = { [resolveAllIncidentsAction.key]: resolveAllIncidentsAction, [acknowledgeIncidentAction.key]: acknowledgeIncidentAction, [resolveIncidentAction.key]: resolveIncidentAction, + [createIncidentNoteAction.key]: createIncidentNoteAction, }, }; diff --git a/zapier/package.json b/zapier/package.json index af4dcb36bb..ce01b0463f 100755 --- a/zapier/package.json +++ b/zapier/package.json @@ -1,6 +1,6 @@ { "name": "zapier", - "version": "3.0.12", + "version": "3.0.17", "description": "Fyipe is an automation tool that helps you monitor the performance and downtime of your APIs and Web apps.", "homepage": "https://fyipe.com", "author": "HackerBay ", diff --git a/zapier/test/actions/createIncidentNote.js b/zapier/test/actions/createIncidentNote.js new file mode 100644 index 0000000000..129c9a345a --- /dev/null +++ b/zapier/test/actions/createIncidentNote.js @@ -0,0 +1,37 @@ +require('should'); + +const zapier = require('zapier-platform-core'); + +const App = require('../../index'); + +const appTester = zapier.createAppTester(App); + +describe('Create Incident Note Action', () => { + it('passes authentication and create new note', done => { + zapier.tools.env.inject(); + const bundle = { + authData: { + apiKey: process.env.DEV_API_KEY, + projectId: process.env.DEV_PROJECT_ID, + }, + cleanedRequest: { + projectName: 'New Project', + projectId: '1', + incidentId: '1', + id: '1', + content: 'new incidentNote', + incident_state: 'update', + type: 'investigation', + createdAt: new Date().toISOString(), + createdBy: 'Nawaz', + }, + }; + appTester(App.creates.incident_note.operation.perform, bundle) + .then(response => { + response.should.be.an.instanceOf(Object); + response.should.have.property('projectName'); + done(); + }) + .catch(done); + }); +});