diff --git a/common/mixins/history-mixin.js b/common/mixins/history-mixin.js index e34df32..9625aeb 100755 --- a/common/mixins/history-mixin.js +++ b/common/mixins/history-mixin.js @@ -32,7 +32,7 @@ var loopback = require('loopback'); var logger = require('oe-logger'); var log = logger('history-mixin'); log.info('history-mixin Loaded'); - +const oecloudutil = require('oe-cloud/lib/common/util'); module.exports = function HistoryMixin(Model) { // Skip this mixin where ever not applicable. @@ -231,6 +231,22 @@ function createHistoryData(ctx, next) { }); return next(); }); + } else if ( ctx.data || ctx.instance ) { + var instance = ctx.data || ctx.instance; + var id = oecloudutil.getIdValue(ctx.Model, instance); + ctx.Model.findById(id, {notify: false}, function (err, data) { + if (err) { + return next(err); + } + if (!data) { + var e = new Error('Model ID error ' + id); + e.message = 'Model ID error ' + id; + return next(e); + } + ctx.hookState.historyData = [data.toObject()]; + ctx.currentInstance = data; + return next(); + }); } else { return next(); } @@ -287,5 +303,3 @@ function createHistoryDataForDelete(ctx, next) { function insertIntoHistoryForDelete(ctx, next) { return insertIntoHistory(ctx, next); } - - diff --git a/package.json b/package.json index 065cbc9..a009586 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "oe-common-mixins", - "version": "2.1.0", + "version": "2.1.1", "description": "oe-cloud modularization project aka oecloud.io", "engines": { "node": ">=6" diff --git a/test/history-mixin-test.js b/test/history-mixin-test.js index 5cc84f8..1deeab5 100755 --- a/test/history-mixin-test.js +++ b/test/history-mixin-test.js @@ -1,77 +1,59 @@ -/** - * - * 2018-2019 EdgeVerve Systems Limited (a fully owned Infosys subsidiary), - * Bangalore, India. All Rights Reserved. - * - */ - -// Author : Atul -var oecloud = require('oe-cloud'); -var loopback = require('loopback'); -var bootstrap = require('./bootstrap'); -const uuidv4 = require('uuid/v4'); -var debug = require('debug')('history-mixin-test'); -/*var oecloud = require('oe-cloud'); -var loopback = require('loopback'); - -oecloud.observe('loaded', function (ctx, next) { - oecloud.attachMixinsToBaseEntity("VersionMixin"); - return next(); -}) - - -oecloud.boot(__dirname, function (err) { - if (err) { - console.log(err); - process.exit(1); - } - oecloud.start(); - oecloud.emit('test-start'); -}); -*/ - -var chalk = require('chalk'); -var chai = require('chai'); -var async = require('async'); -chai.use(require('chai-things')); - -var expect = chai.expect; - -var app = oecloud; -var defaults = require('superagent-defaults'); -var supertest = require('supertest'); -var Customer; -var api = defaults(supertest(app)); -var basePath = app.get('restApiRoot'); -var url = basePath + '/Employees'; - -var models = oecloud.models; - -function deleteAllUsers(done) { - var userModel = loopback.findModel("User"); - userModel.destroyAll({}, { notify: false }, function (err) { - if (err) { - return done(err); - } - userModel.find({}, {}, function (err2, r2) { - if (err2) { - return done(err2); - } - if (r2 && r2.length > 0) { - return done(new Error("Error : users were not deleted")); - } - }); - return done(err); - }); -} - -var globalCtx = { - ignoreAutoScope: true, - ctx: { tenantId: '/default' } -}; - -describe(chalk.blue('History Mixin Test Started'), function (done) { - +/** + * + * 2018-2019 EdgeVerve Systems Limited (a fully owned Infosys subsidiary), + * Bangalore, India. All Rights Reserved. + * + */ + +// Author : Atul +var oecloud = require('oe-cloud'); +var loopback = require('loopback'); +var bootstrap = require('./bootstrap'); +const uuidv4 = require('uuid/v4'); +var debug = require('debug')('history-mixin-test'); +/*var oecloud = require('oe-cloud'); +var loopback = require('loopback'); + +oecloud.observe('loaded', function (ctx, next) { + oecloud.attachMixinsToBaseEntity("VersionMixin"); + return next(); +}) + + +oecloud.boot(__dirname, function (err) { + if (err) { + console.log(err); + process.exit(1); + } + oecloud.start(); + oecloud.emit('test-start'); +}); +*/ + +var chalk = require('chalk'); +var chai = require('chai'); +var async = require('async'); +chai.use(require('chai-things')); + +var expect = chai.expect; + +var app = oecloud; +var defaults = require('superagent-defaults'); +var supertest = require('supertest'); +var api = defaults(supertest(app)); + +var basePath = app.get('restApiRoot'); + +var models = oecloud.models; + + +var globalCtx = { + ignoreAutoScope: true, + ctx: { tenantId: '/default' } +}; + +describe(chalk.blue('History Mixin Test Started'), function (done) { + var modelName = 'MixinTest'; var modelDetails = { name: modelName, @@ -85,13 +67,13 @@ describe(chalk.blue('History Mixin Test Started'), function (done) { }, strict: false, plural: modelName - }; - - - this.timeout(10000); - var model; - - before('Starting history mixin tests', function (done) { + }; + + + this.timeout(10000); + var model; + + before('Starting history mixin tests', function (done) { models.ModelDefinition.create(modelDetails, globalCtx, function (err, res) { if (err) { debug('unable to create historyMixinTest model'); @@ -100,14 +82,14 @@ describe(chalk.blue('History Mixin Test Started'), function (done) { model = loopback.getModel(modelName, globalCtx); done(); } - }); - }); - - afterEach('destroy context', function (done) { - done(); - }); - - + }); + }); + + afterEach('destroy context', function (done) { + done(); + }); + + it('t1 (oecloud 1.x tests) should create a history model for Test model', function (done) { var mainModel = loopback.getModel(modelName, globalCtx); @@ -115,9 +97,9 @@ describe(chalk.blue('History Mixin Test Started'), function (done) { expect(model).not.to.be.null; expect(model).not.to.be.undefined; done(); - }); - - + }); + + it('t2 (oecloud 1.x tests) should insert data to TestModel, check if version is set and history model is empty ---programmatically', function (done) { this.timeout(50000); @@ -281,11 +263,54 @@ describe(chalk.blue('History Mixin Test Started'), function (done) { }); }); - - -}); - - - - - + + it('t6 (oecloud 2.x test) create record and then update using replacebyid', function (done) { + this.timeout(50000); + var postData = { + id : 123, + name : "Atul", + age : 30 + }; + var customerModel = loopback.findModel('Customer'); + customerModel.create(postData, globalCtx, function (err, customer) { + if (err) { + return done(err); + } else { + customerModel.replaceById(customer.id, {name: "Atul111", age : 31, _version : customer._version}, globalCtx, function(err, customer2){ + if(err){ + return done(err); + } + var newData = customer2.toObject(); + newData.name = 'Atul222'; + customerModel.replaceOrCreate(newData, globalCtx, function(err, customer3){ + if(err){ + return done(err); + } + customer3.updateAttributes({name : "Atul3333", age : 35, id : customer3.id, _version : customer3._version}, globalCtx, function(err, customer4){ + if(err){ + return done(err); + } + if(customer4.name !== 'Atul3333' || customer4.age !== 35){ + return done(new Error("data not matching. expetcing name change")); + } + var url = basePath + '/Customers/history?filter={"where" : { "_modelId" : 123 } }'; + + api + .get(url) + .send() + .expect(200).end(function (err, historyRes) { + if (err) { + return done(err); + } else { + expect(historyRes.body).not.to.be.empty; + expect(historyRes.body).to.have.length(3); + return done(); + } + }); + }) + }) + }) + } + }); + }); +});