diff --git a/loopback/server/connectors/vn-mysql.js b/loopback/server/connectors/vn-mysql.js index aae58fe9a..4ad71c950 100644 --- a/loopback/server/connectors/vn-mysql.js +++ b/loopback/server/connectors/vn-mysql.js @@ -316,6 +316,8 @@ class VnMySQL extends MySQL { } async invokeMethodP(method, args, model, ctx, opts) { + const Model = this.getModelDefinition(model).model; + const settings = Model.definition.settings; let tx; if (!opts.transaction) { tx = await Transaction.begin(this, {}); @@ -325,8 +327,10 @@ class VnMySQL extends MySQL { try { // Fetch old values (update|delete) or login let where, id, data, idName, limit, op, oldInstances, newInstances; - const hasGrabUser = await this.grabUserLog(model, opts, 'login'); - if (!hasGrabUser) { + const hasGrabUser = settings.log && settings.log.grabUser; + if(hasGrabUser) + await this.grabUserLog(Model, opts, 'login'); + else { where = ctx.where; id = ctx.id; data = ctx.data; @@ -358,7 +362,9 @@ class VnMySQL extends MySQL { super[method].apply(this, fnArgs); }); - if (!hasGrabUser) { + if(hasGrabUser) + await this.grabUserLog(Model, opts, 'logout'); + else { // Fetch new values const ids = []; @@ -386,7 +392,6 @@ class VnMySQL extends MySQL { } } - await this.grabUserLog(model, opts, 'logout'); await this.createLogRecord(oldInstances, newInstances, model, opts); } if (tx) await tx.commit(); @@ -397,23 +402,15 @@ class VnMySQL extends MySQL { } } - async grabUserLog(model, opts, action) { - const Model = this.getModelDefinition(model).model; - const settings = Model.definition.settings; - - if (!(settings.log && settings.log.grabUser)) - return false; - + async grabUserLog(Model, opts, action) { if(action == 'login'){ const userId = opts.httpCtx && opts.httpCtx.active.accessToken.userId; const user = await Model.app.models.Account.findById(userId, {fields: ['name']}, opts); await this.executeP(`CALL account.myUser_loginWithName(?)`, [user.name], opts); } - else if(action == 'logout'){ + else if(action == 'logout') await this.executeP(`CALL account.myUser_logout()`, null, opts); - } - return true; } buildSelectStmt(op, data, idName, model, where, limit) {