Skip to content

Commit

Permalink
Feature #53 Tracing
Browse files Browse the repository at this point in the history
products
  • Loading branch information
dtseytlin committed Mar 22, 2016
1 parent f7f8b11 commit 1ea0082
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 1 deletion.
176 changes: 175 additions & 1 deletion backend/app/controllers/products.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var
_ = require('underscore'),
config = require('config'),
BoLogger = require('app/bologger'),
bologger = new BoLogger(),
csv = require('express-csv'),
Product = require('app/models/products'),
Project = require('app/models/projects'),
Expand Down Expand Up @@ -154,13 +156,27 @@ module.exports = {
var update = yield thunkQuery(Task.update(updateObj).where(Task.id.equals(req.body[i].id)));
updateObj.id = req.body[i].id;
res.updated.push(req.body[i].id);
bologger.log({
user: req.user.id,
action: 'update',
object: 'tasks',
entity: req.body[i].id,
info: 'Update task for product `'+req.params.id+'`'
});
}
} else { // create
var id = yield thunkQuery(
Task.insert(_.pick(req.body[i], Task.table._initialConfig.columns)).returning(Task.id)
);
req.body[i].id = _.first(id).id;
res.inserted.push(req.body[i].id);
bologger.log({
user: req.user.id,
action: 'insert',
object: 'tasks',
entity: req.body[i].id,
info: 'Add new task for product `'+req.params.id+'`'
});
}

}
Expand Down Expand Up @@ -291,14 +307,45 @@ module.exports = {
yield thunkQuery(Index.update(indexObj).where(Index.id.equals(req.body[i].id)), {
'realm': req.param('realm')
});
bologger.log({
user: req.user.id,
action: 'update',
object: 'indexes',
entity: req.body[i].id,
info: 'Update index for product `'+req.params.id+'`'
});

// drop all existing weights
yield thunkQuery(IndexQuestionWeight.delete().where(IndexQuestionWeight.indexId.equals(req.body[i].id)), {
'realm': req.param('realm')
});
bologger.log({
user: req.user.id,
action: 'delete',
object: 'IndexQuestionWeights',
entity: null,
entities: JSON.stringify({
productId: req.params.id,
indexId: req.body[i].id
}),
quantity: 1,
info: 'Drop all existing question weights for index `'+req.body[i].id+'` for product `'+req.params.id+'`'
});
yield thunkQuery(IndexSubindexWeight.delete().where(IndexSubindexWeight.indexId.equals(req.body[i].id)), {
'realm': req.param('realm')
});
bologger.log({
user: req.user.id,
action: 'delete',
object: 'IndexSubindexWeights',
entity: null,
entities: JSON.stringify({
productId: req.params.id,
indexId: req.body[i].id
}),
quantity: 1,
info: 'Drop all existing subindex weights for index `'+req.body[i].id+'` for product `'+req.params.id+'`'
});

indexId = req.body[i].id;
res.updated.push(indexId);
Expand All @@ -310,6 +357,13 @@ module.exports = {

indexId = _.first(id).id;
res.inserted.push(indexId);
bologger.log({
user: req.user.id,
action: 'insert',
object: 'Indexes',
entity: indexId,
info: 'Add new index for product `'+req.params.id+'`'
});
}

// insert weights
Expand All @@ -321,6 +375,19 @@ module.exports = {
type: req.body[i].questionWeights[questionId].type
};
yield thunkQuery(IndexQuestionWeight.insert(weightObj));
bologger.log({
user: req.user.id,
action: 'insert',
object: 'IndexQuestionWeights',
entity: null,
entities: JSON.stringify({
productId: req.params.id,
indexId: indexId,
questionId: questionId
}),
quantity: 1,
info: 'Add new question weight for index `'+indexId+'` for question `'+questionId+'` for product `'+req.params.id+'`'
});
}
for (var subindexId in req.body[i].subindexWeights) {
var weightObj = {
Expand All @@ -330,6 +397,19 @@ module.exports = {
type: req.body[i].subindexWeights[subindexId].type
};
yield thunkQuery(IndexSubindexWeight.insert(weightObj));
bologger.log({
user: req.user.id,
action: 'insert',
object: 'IndexSubindexWeights',
entity: null,
entities: JSON.stringify({
productId: req.params.id,
indexId: indexId,
subindexId: subindexId
}),
quantity: 1,
info: 'Add new subindex weight for index `'+indexId+'` for subindex `'+subindexId+'` for product `'+req.params.id+'`'
});
}
}

Expand Down Expand Up @@ -387,6 +467,13 @@ module.exports = {
yield thunkQuery(Subindex.update(subindexObj).where(Subindex.id.equals(req.body[i].id)), {
'realm': req.param('realm')
});
bologger.log({
user: req.user.id,
action: 'update',
object: 'Subindexes',
entity: req.body[i].id,
info: 'Update subindex for product `'+req.params.id+'`'
});

// drop all existing weights
yield thunkQuery(SubindexWeight.delete().where(SubindexWeight.subindexId.equals(req.body[i].id)), {
Expand All @@ -395,6 +482,18 @@ module.exports = {

subindexId = req.body[i].id;
res.updated.push(subindexId);
bologger.log({
user: req.user.id,
action: 'delete',
object: 'SubindexWeights',
entity: null,
entities: JSON.stringify({
productId: req.params.id,
subindexId: subindexId
}),
quantity: 1,
info: 'Drop all existing weights for subindex `'+subindexId+'` for product `'+req.params.id+'`'
});
} else { // create
subindexObj.productId = req.params.id;
var id = yield thunkQuery(Subindex.insert(subindexObj).returning(Subindex.id), {
Expand All @@ -403,6 +502,14 @@ module.exports = {

subindexId = _.first(id).id;
res.inserted.push(subindexId);
bologger.log({
user: req.user.id,
action: 'insert',
object: 'Subindexes',
entity: subindexId,
entities: null,
info: 'Add new subindex for product `'+req.params.id+'`'
});
}

// insert weights
Expand All @@ -414,6 +521,19 @@ module.exports = {
type: req.body[i].weights[questionId].type
};
yield thunkQuery(SubindexWeight.insert(weightObj));
bologger.log({
user: req.user.id,
action: 'insert',
object: 'SubindexWeights',
entity: null,
entities: JSON.stringify({
productId: req.params.id,
subindexId: subindexId,
questionId: questionId
}),
quantity: 1,
info: 'Add new weight for subindex `'+subindexId+'` for question `'+questionId+'` for product `'+req.params.id+'`'
});
}
}

Expand Down Expand Up @@ -509,6 +629,13 @@ module.exports = {
if (err) {
return next(err);
}
bologger.log({
user: req.user.id,
action: 'delete',
object: 'products',
entity: req.params.id,
info: 'Delete product'
});
res.status(204).end();
});
},
Expand All @@ -521,6 +648,13 @@ module.exports = {
}
return yield thunkQuery(Product.update(_.pick(req.body, Product.editCols)).where(Product.id.equals(req.params.id)));
}).then(function (data) {
bologger.log({
user: req.user.id,
action: 'update',
object: 'products',
entity: req.params.id,
info: 'Update product'
});
res.status(202).end();
}, function (err) {
next(err);
Expand All @@ -535,6 +669,13 @@ module.exports = {
);
return result;
}).then(function (data) {
bologger.log({
user: req.user.id,
action: 'insert',
object: 'products',
entity: _.first(data).id,
info: 'Add new product'
});
res.status(201).json(_.first(data));
}, function (err) {
next(err);
Expand Down Expand Up @@ -565,6 +706,18 @@ module.exports = {
UOAid: req.params.uoaid
}), function (err, data) {
if (!err) {
bologger.log({
user: req.user.id,
action: 'insert',
object: 'ProductUOA',
entity: null,
entities: JSON.stringify({
productId: req.params.id,
uoaId: uoaId
}),
quantity: 1,
info: 'Add new subject `'+uoaId+'` for product `'+req.params.id+'`'
});
res.status(201).end();
} else {
next(err);
Expand Down Expand Up @@ -605,8 +758,17 @@ module.exports = {
});
}

return yield thunkQuery(ProductUOA.insert(insertArr));
return yield thunkQuery(ProductUOA.insert(insertArr).returning('*'));
}).then(function (data) {
bologger.log({
user: req.user.id,
action: 'insert',
object: 'ProductUOA',
entity: null,
entities: JSON.stringify(data),
quantity: data.length,
info: 'Add new subjects (uoas) for product `'+req.params.id+'`'
});
res.json(data);
}, function (err) {
next(err);
Expand All @@ -620,6 +782,18 @@ module.exports = {
UOAid: req.params.uoaid
}), function (err, data) {
if (!err) {
bologger.log({
user: req.user.id,
action: 'delete',
object: 'ProductUOA',
entity: null,
entities: JSON.stringify({
productId: req.params.id,
uoaId: req.params.uoaid
}),
quantity: 1,
info: 'Delete subject `'+req.params.uoaid+'` for product `'+req.params.id+'`'
});
res.status(204).end();
} else {
next(err);
Expand Down
43 changes: 43 additions & 0 deletions backend/db_dump/patches/20160322-01-essences.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
WITH new_values ("tableName","name","fileName","nameField") as (
values
('Surveys', 'Surveys', 'surveys', 'title'),
('SurveyQuestions', 'Survey Questions', 'survey_questions', 'label'),
('SurveyQuestionOptions', 'Survey Question Options', 'survey_question_options', 'label'),
('SurveyAnswers', 'Survey Answers', 'survey_answers', 'value'),
('Groups', 'Groups', 'groups', 'title'),
('Organizations', 'Organizations', 'organizations', 'name'),
('Tasks', 'Tasks', 'tasks', 'title'),
('WorflowSteps', 'WorflowSteps', 'worflowSteps', 'title'),
('Products', 'Products', 'products', 'title'),
('UnitOfAnalysis', 'UnitOfAnalysis', 'uoas', 'name'),
('UnitOfAnalysisType', 'UnitOfAnalysisType', 'uoatypes', 'name'),
('UnitOfAnalysisClassType', 'UnitOfAnalysisClassType', 'uoaclasstypes', 'name'),
('UnitOfAnalysisTag', 'UnitOfAnalysisTag', 'uoatags', 'name'),
('Projects', 'projects', 'projects', 'codeName'),
('Discussions', 'Discussions', 'discussions', 'name'),
('Users', 'Users', 'users', 'email'),
('Notifications', 'notifications', 'notifications', 'body'),
('ProductUOA', 'productUoa', 'product_uoa', 'productId'),
('Indexes', 'Indexes', 'indexes', 'title'),
('Subindexes', 'Subindexes', 'subindexes', 'title'),
('IndexQuestionWeights', 'IndexQuestionWeights', 'index_question_weights', 'type'),
('IndexSubindexWeights', 'IndexSubindexWeights', 'index_subindex_weights', 'type'),
('SubindexWeights', 'SubindexWeights', 'subindex_weights', 'type')
),
upsert as
(
update "Essences" m
set "tableName" = nv."tableName",
"fileName" = nv."fileName",
"nameField" = nv."nameField"
FROM new_values nv
WHERE upper(m."name") = upper(nv."name")
RETURNING m.*
)
-- select * from upsert
INSERT INTO "Essences" ("tableName","name","fileName","nameField")
SELECT "tableName","name","fileName","nameField"
FROM new_values
WHERE NOT EXISTS (SELECT 1
FROM upsert up
WHERE upper(up."name") = upper(new_values."name"))

0 comments on commit 1ea0082

Please sign in to comment.