From 1ea008217c8fccc5d194da2a4d2fda62ecf3aba8 Mon Sep 17 00:00:00 2001 From: "d.tseytlin" Date: Tue, 22 Mar 2016 21:17:22 +0300 Subject: [PATCH] Feature #53 Tracing products --- backend/app/controllers/products.js | 176 +++++++++++++++++- .../db_dump/patches/20160322-01-essences.sql | 43 +++++ 2 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 backend/db_dump/patches/20160322-01-essences.sql diff --git a/backend/app/controllers/products.js b/backend/app/controllers/products.js index cdbf69791..b6caa0009 100644 --- a/backend/app/controllers/products.js +++ b/backend/app/controllers/products.js @@ -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'), @@ -154,6 +156,13 @@ 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( @@ -161,6 +170,13 @@ module.exports = { ); 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+'`' + }); } } @@ -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); @@ -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 @@ -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 = { @@ -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+'`' + }); } } @@ -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)), { @@ -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), { @@ -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 @@ -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+'`' + }); } } @@ -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(); }); }, @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/backend/db_dump/patches/20160322-01-essences.sql b/backend/db_dump/patches/20160322-01-essences.sql new file mode 100644 index 000000000..67f1c7b87 --- /dev/null +++ b/backend/db_dump/patches/20160322-01-essences.sql @@ -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"))