From f42fbb80ecc1a3af2c29fd81ea0453217d936775 Mon Sep 17 00:00:00 2001 From: alexandre Date: Thu, 16 Feb 2023 08:54:45 +0100 Subject: [PATCH] refs #5036 changes --- loopback/server/connectors/vn-mysql.js | 28 +++++++++++++++++--------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/loopback/server/connectors/vn-mysql.js b/loopback/server/connectors/vn-mysql.js index b1c002363..61cd9a782 100644 --- a/loopback/server/connectors/vn-mysql.js +++ b/loopback/server/connectors/vn-mysql.js @@ -386,8 +386,9 @@ class VnMySQL extends MySQL { } } - await this.createLogRecord(oldInstances, newInstances, model, opts); - await this.grabUserLog(model, opts, 'logout'); + const hasGrabUser = await this.grabUserLog(model, opts, 'logout'); + if(!hasGrabUser) + await this.createLogRecord(oldInstances, newInstances, model, opts); if (tx) await tx.commit(); return res; @@ -397,31 +398,34 @@ class VnMySQL extends MySQL { } } - async grabUserLog(model, opts, logAction) { + async grabUserLog(model, opts, action) { const Model = this.getModelDefinition(model).model; const settings = Model.definition.settings; if (!(settings.log && settings.log.grabUser)) - return; + return false; - if (logAction === 'login') { + 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 + } + else if(action == 'logout'){ await this.executeP(`CALL account.myUser_logout()`, null, opts); + } + + return true; } buildSelectStmt(op, data, idName, model, where, limit) { const Model = this.getModelDefinition(model).model; - const settings = Model.definition.settings; const properties = Object.keys(Model.definition.properties); - const log = settings.log; const fields = data ? Object.keys(data) : []; if (op == 'delete') properties.forEach(property => fields.push(property)); else { + const log = Model.definition.settings.log; fields.push(idName); if (log.relation) fields.push(Model.relations[log.relation].keyFrom); if (log.showField) fields.push(log.showField); @@ -512,9 +516,13 @@ class VnMySQL extends MySQL { // Delete unchanged properties if (oldI) { Object.keys(oldI).forEach(prop => { - if (newI[prop] === oldI[prop]) { - delete newI[prop]; + const hasChanges = oldI[prop] instanceof Date ? + oldI[prop]?.getTime() != newI[prop]?.getTime() : + oldI[prop] != newI[prop]; + + if (!hasChanges) { delete oldI[prop]; + delete newI[prop]; } }); }