Skip to content

Commit

Permalink
Feature #53 Tracing
Browse files Browse the repository at this point in the history
surveys, survey answers
  • Loading branch information
dtseytlin committed Mar 25, 2016
1 parent 086927a commit 2aa8221
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 91 deletions.
94 changes: 88 additions & 6 deletions backend/app/controllers/survey_answers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var
_ = require('underscore'),
BoLogger = require('app/bologger'),
bologger = new BoLogger(),
Survey = require('app/models/surveys'),
SurveyAnswer = require('app/models/survey_answers'),
AnswerAttachment = require('app/models/answer_attachments'),
Expand Down Expand Up @@ -159,6 +161,13 @@ module.exports = {
if (err) {
return next(err);
}
bologger.log({
user: req.user.id,
action: 'delete',
object: 'survey answers',
entity: req.params.id,
info: 'Delete survey answer'
});
res.status(204).end();
});
},
Expand Down Expand Up @@ -228,6 +237,13 @@ module.exports = {
);

}).then(function (data){
bologger.log({
user: req.user.id,
action: 'update',
object: 'survey answers',
entity: req.params.id,
info: 'Update survey answer'
});
res.status(202).end('updated');
}, function (err) {
next(err);
Expand Down Expand Up @@ -297,6 +313,13 @@ module.exports = {
}
yield thunkQuery(AnswerAttachment.delete().where(AnswerAttachment.id.equals(req.params.id)));
}).then(function(){
bologger.log({
user: req.user.id,
action: 'delete',
object: 'answerattachments',
entity: req.params.id,
info: 'Delete answerattachment'
});
res.status(204).end();
}, function(err){
next(err);
Expand All @@ -309,7 +332,7 @@ module.exports = {
var id = yield mc.get(req.mcClient, req.params.ticket);
}catch(e){
throw new HttpError(500, e);
};
}

if(!id){
throw new HttpError(400, 'Token is not valid');
Expand All @@ -333,7 +356,7 @@ module.exports = {
},
getTicket: function (req, res, next) {
co(function* (){

var attachment = yield thunkQuery(
AnswerAttachment.select().where(AnswerAttachment.id.equals(req.params.id))
);
Expand All @@ -345,11 +368,11 @@ module.exports = {
var ticket = crypto.randomBytes(10).toString('hex');

try{
var r = yield mc.set(req.mcClient, ticket, attachment[0].id);
var r = yield mc.set(req.mcClient, ticket, attachment[0].id);
return ticket;
}catch(e){
throw new HttpError(500, e);
};
}

}).then(function(data){
res.status(201).json({tiсket:data});
Expand Down Expand Up @@ -388,6 +411,13 @@ module.exports = {
);

}).then(function(data){
bologger.log({
user: req.user.id,
action: 'update',
object: 'answerattachments',
entity: data[0].id,
info: 'Update (link) answer attachment'
});
res.status(202).json(data);
}, function(err){
next(err);
Expand All @@ -405,7 +435,7 @@ module.exports = {
throw new HttpError(400, 'Answer with id = ' + req.body.answerId + ' does not exist');
}
}

if (req.files.file) {
var file = req.files.file;

Expand Down Expand Up @@ -438,7 +468,7 @@ module.exports = {
mimetype: file.mimetype,
body: filecontent,
owner: req.user.id
}
};

if (req.body.answerId) {
record.answerId = req.body.answerId;
Expand All @@ -447,6 +477,13 @@ module.exports = {
var inserted = yield thunkQuery(
AnswerAttachment.insert(record).returning(AnswerAttachment.id)
);
bologger.log({
user: req.user.id,
action: 'insert',
object: 'answerattachments',
entity: inserted[0].id,
info: 'Insert answer attachment'
});

return inserted[0];

Expand Down Expand Up @@ -625,13 +662,27 @@ function *addAnswer (req, dataObject) {
.update(_.pick(dataObject, editFields))
.where(SurveyAnswer.id.equals(existsNullVer[0].id))
);
bologger.log({
user: req.user.id,
action: 'update',
object: 'survey answers',
entity: existsNullVer[0].id,
info: 'Update survey answer'
});
} else {
var answer = yield thunkQuery(
SurveyAnswer
.insert(_.pick(dataObject, SurveyAnswer.table._initialConfig.columns))
.returning(SurveyAnswer.id)
);
answer = answer[0];
bologger.log({
user: req.user.id,
action: 'insert',
object: 'survey answers',
entity: answer.id,
info: 'Add new survey answer'
});
}

return answer;
Expand Down Expand Up @@ -728,13 +779,37 @@ function *moveWorkflow (req, productId, UOAid) {
.update({currentStepId: nextStep[0].id})
.where({productId: curStep.task.productId, UOAid: curStep.task.uoaId})
);
bologger.log({
user: req.user.id,
action: 'update',
object: 'ProductUOA',
entities: {
productId: curStep.task.productId,
uoaId: curStep.task.uoaId,
currentStepId: nextStep[0].id
},
quantity: 1,
info: 'Update currentStep to `'+nextStep[0].id+'` for subject `'+curStep.task.uoaId+'` for product `'+curStep.task.productId+'`'
});
}else{
// set productUOA status to complete
yield thunkQuery(
ProductUOA
.update({isComplete: true})
.where({productId: curStep.task.productId, UOAid: curStep.task.uoaId})
);
bologger.log({
user: req.user.id,
action: 'update',
object: 'ProductUOA',
entities: {
productId: curStep.task.productId,
uoaId: curStep.task.uoaId,
isComplete: true
},
quantity: 1,
info: 'Set productUOA status to complete for subject `'+curStep.task.uoaId+'` for product `'+curStep.task.productId+'`'
});
var uncompleted = yield thunkQuery( // check for uncompleted
ProductUOA
.select()
Expand All @@ -749,6 +824,13 @@ function *moveWorkflow (req, productId, UOAid) {
yield thunkQuery(
Product.update({status: 3}).where(Product.id.equals(curStep.task.productId))
);
bologger.log({
user: req.user.id,
action: 'update',
object: 'Product',
entity: curStep.task.productId,
info: 'Set product status to complete'
});
}
}
console.log(nextStep);
Expand Down
60 changes: 60 additions & 0 deletions backend/app/controllers/surveys.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var
_ = require('underscore'),
BoLogger = require('app/bologger'),
bologger = new BoLogger(),
Survey = require('app/models/surveys'),
Product = require('app/models/products'),
Project = require('app/models/projects'),
Expand Down Expand Up @@ -79,10 +81,25 @@ module.exports = {
yield thunkQuery(
SurveyQuestionOption.delete().where(SurveyQuestionOption.questionId.equals(questions[i].id))
); // delete options
bologger.log({
user: req.user.id,
action: 'delete',
object: 'survey question options',
entities: {questionId: questions[i].id},
quantity: 1,
info: 'Delete survey question options for question '+questions[i].id
});

yield thunkQuery(
SurveyQuestion.delete().where(SurveyQuestion.id.equals(questions[i].id))
); // delete question
bologger.log({
user: req.user.id,
action: 'delete',
object: 'survey questions',
entity: questions[i].id,
info: 'Delete survey question'
});
}
}
yield thunkQuery(Survey.delete().where(Survey.id.equals(req.params.id)));
Expand All @@ -104,6 +121,13 @@ module.exports = {
.update(updateObj)
.where(Survey.id.equals(req.params.id))
);
bologger.log({
user: req.user.id,
action: 'update',
object: 'surveys',
entity: req.params.id,
info: 'Update survey'
});
}
}).then(function (data) {
res.status(202).end();
Expand Down Expand Up @@ -133,6 +157,13 @@ module.exports = {

return survey;
}).then(function (data) {
bologger.log({
user: req.user.id,
action: 'insert',
object: 'surveys',
entity: data.id,
info: 'Add new survey'
});
res.status(201).json(data);
}, function (err) {
next(err);
Expand Down Expand Up @@ -186,6 +217,13 @@ module.exports = {
.where(SurveyQuestion.id.equals(req.params.id))
);
}).then(function (data) {
bologger.log({
user: req.user.id,
action: 'update',
object: 'survey questions',
entity: req.params.id,
info: 'Update survey question'
});
res.status(202).end();
}, function (err) {
next(err);
Expand All @@ -197,6 +235,13 @@ module.exports = {
if (err) {
return next(err);
}
bologger.log({
user: req.user.id,
action: 'delete',
object: 'survey questions',
entity: req.params.id,
info: 'Delete survey question'
});
res.status(204).end();
});
}
Expand All @@ -212,6 +257,13 @@ function* addQuestion (req, dataObj) {
.returning(SurveyQuestion.id)
);
result = result[0];
bologger.log({
user: req.user.id,
action: 'insert',
object: 'survey questions',
entity: result.id,
info: 'Add new survey question'
});

if (dataObj.options && dataObj.options.length) {
var insertArr = [];
Expand All @@ -224,6 +276,14 @@ function* addQuestion (req, dataObj) {
result.options = yield thunkQuery(
SurveyQuestionOption.insert(insertArr).returning(SurveyQuestionOption.id)
);
bologger.log({
user: req.user.id,
action: 'insert',
object: 'survey question options',
entities: result.options,
quantity: (result.options) ? result.options.length : 0,
info: 'Add new survey question options'
});
}

return result;
Expand Down
1 change: 1 addition & 0 deletions backend/db_dump/essences.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ WITH new_values ("tableName","name","fileName","nameField") as (
('SurveyQuestions', 'Survey Questions', 'survey_questions', 'label'),
('SurveyQuestionOptions', 'Survey Question Options', 'survey_question_options', 'label'),
('SurveyAnswers', 'Survey Answers', 'survey_answers', 'value'),
('AnswerAttachments', 'AnswerAttachments', 'answer_attachments', 'filename'),
('Groups', 'Groups', 'groups', 'title'),
('Organizations', 'Organizations', 'organizations', 'name'),
('Tasks', 'Tasks', 'tasks', 'title'),
Expand Down
38 changes: 0 additions & 38 deletions backend/db_dump/patches/20160319-03-essences.sql

This file was deleted.

Loading

0 comments on commit 2aa8221

Please sign in to comment.