added an action for creating incident note

This commit is contained in:
courage173 2021-03-31 16:14:20 +01:00
parent ebc2a75d2b
commit 7e06424cad
8 changed files with 127 additions and 6 deletions

View File

@ -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 {

View File

@ -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', {

View File

@ -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');

View File

@ -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));

View File

@ -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',
},
},
};

View File

@ -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,
},
};

View File

@ -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 <hello@hackerbay.io>",

View File

@ -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);
});
});