Skip to content

Commit

Permalink
Relevant Reports Functionalities (#464)
Browse files Browse the repository at this point in the history
* Introduced SMTCTags to Schema

In addition there are now add, remove, and clear functions in the schema file for SMTCTag. Currently no function to remove SMTCTags when an SMTCTag is deleted, but will be finished once search functionality for SMTCtags is implemented for Incidents

* Added Escalation Field and Back-end API connection

* Quick Front-end Mockup and Application

Would like someone to test it before moving forward.

* Added Backend API to ADD REMOVE Single Tag to MULTIPLE Incidents

* Added Front-end API endpoints for Incident Tags

* Escalate Buttons Completed

* Inclusion of the Relevant Reports Page

* Updated the Correct Pages to Have Escalation

* Updates for Prototyping

* Tags for Incidents

Currently there is an issue with not seeing new Tags, but this is also because incidents don't update when they are changed, we should change this.

* Final Functioning Relevant Report Page

* Final Functioning Relevant Report Page

Co-authored-by: Matthew Lim <limmat@gatech.edu>
  • Loading branch information
Matthew Lim and Matthew Lim committed Apr 3, 2021
1 parent 02eecc5 commit b0d6638
Show file tree
Hide file tree
Showing 25 changed files with 2,710 additions and 838 deletions.
84 changes: 84 additions & 0 deletions lib/api/v1/incident-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,90 @@ module.exports = function(app, user) {
});
});

app.patch('/api/v1/incident/_tag', user.can('edit data'), function(req, res) {
if (!req.body.ids || !req.body.ids.length) return res.send(200);
Incident.find({ '_id': { $in: req.body.ids } }, function(err, incidents) {
if (err) return res.send(err.status, err.message);
if (incidents.length === 0) return res.send(200);
var remaining = incidents.length;
incidents.forEach(function(incident) {
incident.addSMTCTag(req.body.smtcTag, (err) => {
if (err && !res.headersSent) {
res.send(500, err.message);
return;
}
// Save incident
incident.save(function(err, numberAffected) {
if (err) {
res.send(err.status, err.message);
} else if (!numberAffected) {
res.send(404);
} else {
writelog.writeIncident(req, incident, 'addTagToIncident');
res.send(200);
}
});
});
});
});
});

app.patch('/api/v1/incident/_untag', user.can('edit data'), function(req, res) {
if (!req.body.ids || !req.body.ids.length) return res.send(200);
Incident.find({ '_id': { $in: req.body.ids } }, function(err, incidents) {
if (err) return res.send(err.status, err.message);
if (incidents.length === 0) return res.send(200);
var remaining = incidents.length;
incidents.forEach(function(incident) {
incident.removeSMTCTag(req.body.smtcTag, (err) => {
if (err && !res.headersSent) {
res.send(500, err.message);
return;
}
// Save incident
incident.save(function(err, numberAffected) {
if (err) {
res.send(err.status, err.message);
} else if (!numberAffected) {
res.send(404);
} else {
writelog.writeIncident(req, incident, 'removeTagFromIncident');
res.send(200);
}
});
});
});
});
});

app.patch('/api/v1/incident/_clearTags', user.can('edit data'), function(req, res) {
if (!req.body.ids || !req.body.ids.length) return res.send(200);
Incident.find({ '_id': { $in: req.body.ids } }, function(err, incidents) {
if (err) return res.send(err.status, err.message);
if (incidents.length === 0) return res.send(200);
var remaining = incidents.length;
incidents.forEach(function(incident) {
incident.clearSMTCTags(req.body.smtcTag, (err) => {
if (err && !res.headersSent) {
res.send(500, err.message);
return;
}
// Save incident
incident.save(function(err, numberAffected) {
if (err) {
res.send(err.status, err.message);
} else if (!numberAffected) {
res.send(404);
} else {
writelog.writeIncident(req, incident, 'ClearTagsFromIncident');
res.send(200);
}
});
});
});
});
});

// Delete an Incident
app.delete('/api/v1/incident/:_id', user.can('edit data'), function(req, res, next) {
if (req.params._id === '_all') return next();
Expand Down
42 changes: 42 additions & 0 deletions lib/api/v1/report-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,28 @@ module.exports = function(app, user) {
});
});

// Escalate selected reports
app.patch('/api/v1/report/_escalate', user.can('edit data'), function(req, res) {
if (!req.body.ids || !req.body.ids.length) return res.send(200);
Report.find({ _id: { $in: req.body.ids } }, function(err, reports) {
if (err) return res.send(err.status, err.message);
if (reports.length === 0) return res.send(200);
var remaining = reports.length;
reports.forEach(function(report) {
// Mark each report as escalated to catch it in model
report.toggleEscalated(req.body.escalated);
report.save(function(err) {
if (err) {
if (!res.headersSent) res.send(err.status, err.message)
return;
}
writelog.writeReport(req, report, 'escalatedReport');
if (--remaining === 0) return res.send(200);
});
});
});
});

// Link selected reports to one incident
app.patch('/api/v1/report/_link', user.can('edit data'), function(req, res) {
if (!req.body.ids || !req.body.ids.length) return res.send(200);
Expand Down Expand Up @@ -260,6 +282,26 @@ module.exports = function(app, user) {
});
});

app.patch('/api/v1/report/_updateVeracity', user.can('edit data'), function(req, res) {
if (!req.body.ids || !req.body.ids.length) return res.send(200);
Report.find({ _id: { $in: req.body.ids } }, function(err, reports) {
if (err) return res.send(err.status, err.message);
if (reports.length === 0) return res.send(200);
var remaining = reports.length;
reports.forEach(function(report) {
report.setVeracity(req.body.veracity);
report.save(function(err) {
if (err) {
if (!res.headersSent) res.send(err.status, err.message)
return;
}
writelog.writeReport(req, report, 'addToIncident');
if (--remaining === 0) return res.send(200);
});
});
});
});

app.patch('/api/v1/report/_tag', user.can('edit data'), function(req, res) {
if (!req.body.ids || !req.body.ids.length) return res.send(200);
Report.find({ _id: { $in: req.body.ids } }, function(err, reports) {
Expand Down
76 changes: 76 additions & 0 deletions models/incident.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@

var database = require('../lib/database');
var mongoose = database.mongoose;
var SchemaTypes = mongoose.SchemaTypes;
var validator = require('validator');
var _ = require('underscore');
var AutoIncrement = require('mongoose-sequence')(mongoose);
var Report = require('./report');
var logger = require('../lib/logger');
var SMTCTag = require('../models/tag');

require('../lib/error');


var lengthValidator = function(str) {
return validator.isLength(str, {min: 0, max: 42})
}
Expand All @@ -28,6 +31,7 @@ var schema = new mongoose.Schema({
storedAt: Date,
tags: { type: [String], default: [] },
assignedTo: { type: mongoose.Schema.ObjectId, ref: 'User' },
smtcTags: {type: [{type: SchemaTypes.ObjectId, ref: 'SMTCTag'}], default: []},
creator: { type: mongoose.Schema.ObjectId, ref: 'User' },
status: { type: String, default: 'new', required: true },
veracity: { type: String, default: 'Unconfirmed', enum: ['Unconfirmed', 'Confirmed True','Confirmed False']},
Expand Down Expand Up @@ -68,9 +72,81 @@ schema.post('remove', function() {

});

schema.methods.addSMTCTag = function(smtcTagId, callback) {
// TODO: Use Functional Programming
// ML This finds the smtcTag to add (if it doesn't exists) then add it.
let isRepeat = false;
this.smtcTags.forEach(function(tag) {
if(smtcTagId === tag.toString()) {
isRepeat = true;
}
});
if (isRepeat === false) {
this.smtcTags.push({_id: smtcTagId});
}
callback();
}

schema.methods.removeSMTCTag = function(smtcTagId, callback) {
// TODO: Use Functional Programming
// ML This finds the smtcTag to remove (if it exists) then remove it.
if (this.smtcTags) {
let fndIndex = -1;
this.smtcTags.forEach(function(tag, index) {
let string = tag.toString();
if (smtcTagId === tag.toString()) {
fndIndex = index;
}
})
if (fndIndex !== -1) {
this.smtcTags.splice(fndIndex, 1);
}
}
callback();
}

schema.methods.clearSMTCTags = function(callback) {

const cb = () => {
this.smtcTags = [];
callback();
}

if (!this.commentTo) {
var remaining = this.smtcTags.length;
this.smtcTags.forEach((tag) => {
const tagId = tag.toString();
this.removeSMTCTag(tagId, (err) => {
if (err) {
logger.error(err);
}
if (--remaining === 0) {
cb();
}
});
});
return;
}
cb();
}

schema.plugin(AutoIncrement, { inc_field: 'idnum' });
var Incident = mongoose.model('Incident', schema);

/* We need to be able to find Incidents by smtcTag Id
SMTCTag.schema.on('tag:removed', function(id) {
Incident.find({smtcTags: id}, function(err, reports) {
if (err) {
logger.error(err);
}
reports.forEach(function(report) {
report.removeSMTCTag(id, () => {
report.save();
})
});
});
})*/

Report.schema.on('change:incident', function(prevIncident, newIncident) {
if (prevIncident !== newIncident) {
if (prevIncident) {
Expand Down
8 changes: 6 additions & 2 deletions models/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ schema.post('save', function() {
}
});



schema.methods.toggleRead = function(read) {
this.read = read;
};

schema.methods.toggleVeracity = function(veracity) {
schema.methods.setVeracity = function(veracity) {
this.veracity = veracity;
};

Expand All @@ -91,7 +93,7 @@ schema.methods.addSMTCTag = function(smtcTagId, callback) {
});
if (isRepeat === false) {
this.smtcTags.push({_id: smtcTagId});

this.read = true;
// Only send a post to the acquisition API if it is a) not a comment b) a FB post and c) not a group post
if (!this.commentTo && this._media[0] === 'crowdtangle' && !this.url.match(/permalink/)) {
SMTCTag.findById(smtcTagId, (err, tag) => {
Expand Down Expand Up @@ -197,6 +199,8 @@ Report.queryReports = function(query, page, callback) {
// Re-set search timestamp
query.since = new Date();

if (query.escalated === 'escalated') filter.escalated = true;
if (query.escalated === 'unescalated') filter.escalated = false;
if (query.veracity === 'confirmed true') filter.veracity = true;
if (query.veracity === 'confirmed false') filter.veracity = false;
if (query.veracity === 'unconfirmed') filter.veracity = null;
Expand Down
3 changes: 2 additions & 1 deletion public/angular/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ require('./controllers/password_reset_modal');
require('./controllers/password_reset_admin');
require('./controllers/reports/index');
require('./controllers/reports/show');
require('./controllers/reports/relevant_reports');
require('./controllers/sources/form_modal');
require('./controllers/sources/index');
require('./controllers/sources/show');
Expand All @@ -125,7 +126,7 @@ require('./controllers/users/index');
require('./controllers/users/profile');
require('./controllers/tags/index');
require('./controllers/tags/tags_modal');
require('./controllers/tags/report_tags_modal');
require('./controllers/tags/report_and_incident_tags_modal');
require('./controllers/incidents/index');
require('./controllers/incidents/show');
require('./controllers/incidents/map');
Expand Down
Loading

0 comments on commit b0d6638

Please sign in to comment.