From eab329b2300a743dbce4baf4727d39d61d693493 Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 24 May 2023 15:01:59 +0200 Subject: [PATCH 01/17] =?UTF-8?q?refs=20#554=20feat:=20actualizar=20el=20t?= =?UTF-8?q?oken=20autom=C3=A1ticamente?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/methods/vn-user/renew-token.js | 48 ++++++++++++++++++++++++++ back/methods/vn-user/signIn.js | 2 +- back/model-config.json | 8 +++++ back/models/access-token-config.json | 30 ++++++++++++++++ back/models/vn-user.js | 1 + db/changes/232201/00-salix.sql | 6 ++++ db/dump/fixtures.sql | 6 ++-- front/core/services/auth.js | 43 ++++++++++++++++++++--- front/core/services/index.js | 2 ++ front/core/services/token-created.js | 34 ++++++++++++++++++ front/salix/components/layout/index.js | 20 +++++++++++ 11 files changed, 192 insertions(+), 8 deletions(-) create mode 100644 back/methods/vn-user/renew-token.js create mode 100644 back/models/access-token-config.json create mode 100644 db/changes/232201/00-salix.sql create mode 100644 front/core/services/token-created.js diff --git a/back/methods/vn-user/renew-token.js b/back/methods/vn-user/renew-token.js new file mode 100644 index 000000000..66e64e995 --- /dev/null +++ b/back/methods/vn-user/renew-token.js @@ -0,0 +1,48 @@ +module.exports = Self => { + Self.remoteMethodCtx('renewToken', { + description: 'Send email to the user', + accepts: [], + http: { + path: `/renewToken`, + verb: 'POST' + } + }); + + Self.renewToken = async function(ctx, options) { + const models = Self.app.models; + const userId = ctx.req.accessToken.userId; + const created = ctx.req.accessToken.created; + // const tokenId = ctx.req.accessToken.id; + const myOptions = {}; + let tx; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + const now = new Date(); + const differenceMilliseconds = now - created; + const differenceSeconds = Math.floor(differenceMilliseconds / 1000); // Convertir la diferencia a segundos + + const accessTokenConfig = await models.AccessTokenConfig.findOne(); + if (differenceSeconds <= accessTokenConfig.renewPeriod) { + const response = { + statusCode: 200, + data: { + message: 'Token is active', + } + }; + return response; + } + + const accessToken = await models.AccessToken.create({userId: userId}, myOptions); + await models.AccessToken.destroyAll({userId: userId}, myOptions); + // await models.AccessToken.destroyById(tokenId, myOptions); + + return {token: accessToken.id, created: accessToken.created}; + }; +}; diff --git a/back/methods/vn-user/signIn.js b/back/methods/vn-user/signIn.js index da3172ae4..bc88905cc 100644 --- a/back/methods/vn-user/signIn.js +++ b/back/methods/vn-user/signIn.js @@ -63,6 +63,6 @@ module.exports = Self => { let loginInfo = Object.assign({password}, userInfo); token = await Self.login(loginInfo, 'user'); - return {token: token.id}; + return {token: token.id, created: token.created}; }; }; diff --git a/back/model-config.json b/back/model-config.json index ff2bf5850..d945f3250 100644 --- a/back/model-config.json +++ b/back/model-config.json @@ -2,6 +2,14 @@ "AccountingType": { "dataSource": "vn" }, + "AccessTokenConfig": { + "dataSource": "vn", + "options": { + "mysql": { + "table": "salix.accessTokenConfig" + } + } + }, "Bank": { "dataSource": "vn" }, diff --git a/back/models/access-token-config.json b/back/models/access-token-config.json new file mode 100644 index 000000000..6d90a0f4d --- /dev/null +++ b/back/models/access-token-config.json @@ -0,0 +1,30 @@ +{ + "name": "AccessTokenConfig", + "base": "VnModel", + "options": { + "mysql": { + "table": "accessTokenConfig" + } + }, + "properties": { + "id": { + "type": "number", + "id": true, + "description": "Identifier" + }, + "renewPeriod": { + "type": "number", + "required": true + }, + "renewInterval": { + "type": "number", + "required": true + } + }, + "acls": [{ + "accessType": "READ", + "principalType": "ROLE", + "principalId": "$everyone", + "permission": "ALLOW" + }] +} diff --git a/back/models/vn-user.js b/back/models/vn-user.js index 84ba11794..17e6c9320 100644 --- a/back/models/vn-user.js +++ b/back/models/vn-user.js @@ -10,6 +10,7 @@ module.exports = function(Self) { require('../methods/vn-user/recover-password')(Self); require('../methods/vn-user/validate-token')(Self); require('../methods/vn-user/privileges')(Self); + require('../methods/vn-user/renew-token')(Self); // Validations diff --git a/db/changes/232201/00-salix.sql b/db/changes/232201/00-salix.sql new file mode 100644 index 000000000..75b64500a --- /dev/null +++ b/db/changes/232201/00-salix.sql @@ -0,0 +1,6 @@ +CREATE TABLE `salix`.`accessTokenConfig` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `renewPeriod` int(10) unsigned DEFAULT NULL, + `renewInterval` int(10) unsigned DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci; diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 9f06de5b0..cd4aca4bc 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2886,6 +2886,6 @@ INSERT INTO `vn`.`wagonTypeTray` (`id`, `typeFk`, `height`, `colorFk`) (2, 1, 50, 2), (3, 1, 0, 3); - - - +INSERT INTO `salix`.`accessTokenConfig` (`id`, `renewPeriod`, `renewInterval`) + VALUES + (0, 50, 100); diff --git a/front/core/services/auth.js b/front/core/services/auth.js index 0b89a8e88..d1ac4c78d 100644 --- a/front/core/services/auth.js +++ b/front/core/services/auth.js @@ -7,7 +7,7 @@ import UserError from 'core/lib/user-error'; * @property {Boolean} loggedIn Whether the user is currently logged */ export default class Auth { - constructor($http, $q, $state, $transitions, $window, vnToken, vnModules, aclService) { + constructor($http, $q, $state, $transitions, $window, vnToken, vnTokenCreated, vnModules, aclService) { Object.assign(this, { $http, $q, @@ -15,6 +15,7 @@ export default class Auth { $transitions, $window, vnToken, + vnTokenCreated, vnModules, aclService, loggedIn: false @@ -29,8 +30,11 @@ export default class Auth { } }; this.$transitions.onStart(criteria, transition => { - if (this.loggedIn) + this.getAccessTokenConfig(); + if (this.loggedIn) { + console.log('firstIf'); return true; + } let redirectToLogin = () => { return transition.router.stateService.target('login', { @@ -39,14 +43,44 @@ export default class Auth { }; if (this.vnToken.token) { + console.log('secondIf'); + return this.loadAcls() .then(() => true) .catch(redirectToLogin); - } else + } else { + console.log('else'); + return redirectToLogin(); + } }); } + getAccessTokenConfig() { + this.$http.get('AccessTokenConfigs/findOne').then(json => { + window.localStorage.renewPeriod = json.data.renewPeriod; + window.localStorage.renewInterval = json.data.renewInterval; + + this.checkTokenValidity(); + const intervalMilliseconds = 50 * 1000; + // setInterval(this.checkTokenValidity.bind(this), intervalMilliseconds); + }); + } + + checkTokenValidity() { + const now = new Date(); + const differenceMilliseconds = now - new Date(this.vnTokenCreated.created); + const differenceSeconds = Math.floor(differenceMilliseconds / 1000); + + console.log(differenceSeconds, window.localStorage.renewPeriod); + if (differenceSeconds > window.localStorage.renewPeriod) { + this.$http.post('VnUsers/renewToken') + .then(() => { + console.log('fin'); + }); + } + } + login(user, password, remember) { if (!user) { let err = new UserError('Please enter your username'); @@ -65,6 +99,7 @@ export default class Auth { onLoginOk(json, remember) { this.vnToken.set(json.data.token, remember); + this.vnTokenCreated.set(json.data.created, remember); return this.loadAcls().then(() => { let continueHash = this.$state.params.continue; @@ -101,6 +136,6 @@ export default class Auth { }); } } -Auth.$inject = ['$http', '$q', '$state', '$transitions', '$window', 'vnToken', 'vnModules', 'aclService']; +Auth.$inject = ['$http', '$q', '$state', '$transitions', '$window', 'vnToken', 'vnTokenCreated', 'vnModules', 'aclService']; ngModule.service('vnAuth', Auth); diff --git a/front/core/services/index.js b/front/core/services/index.js index 867a13df0..6d4e34428 100644 --- a/front/core/services/index.js +++ b/front/core/services/index.js @@ -11,3 +11,5 @@ import './report'; import './email'; import './file'; import './date'; +import './token-created'; + diff --git a/front/core/services/token-created.js b/front/core/services/token-created.js new file mode 100644 index 000000000..c8e156a5d --- /dev/null +++ b/front/core/services/token-created.js @@ -0,0 +1,34 @@ +import ngModule from '../module'; + +/** + * Saves and loads the created for the current logged in user. + * + * @property {String} created The current login created or %null + */ +export default class created { + constructor() { + try { + this.created = sessionStorage.getItem('vnTokenCreated'); + if (!this.created) + this.created = localStorage.getItem('vnTokenCreated'); + } catch (e) {} + } + set(value, remember) { + this.unset(); + try { + if (remember) + localStorage.setItem('vnTokenCreated', value); + else + sessionStorage.setItem('vnTokenCreated', value); + } catch (e) {} + + this.created = value; + } + unset() { + localStorage.removeItem('vnTokenCreated'); + sessionStorage.removeItem('vnTokenCreated'); + this.created = null; + } +} + +ngModule.service('vnTokenCreated', created); diff --git a/front/salix/components/layout/index.js b/front/salix/components/layout/index.js index 48f50f404..6c4319215 100644 --- a/front/salix/components/layout/index.js +++ b/front/salix/components/layout/index.js @@ -10,6 +10,7 @@ export class Layout extends Component { $onInit() { this.getUserData(); + // this.getAccessTokenConfig(); } getUserData() { @@ -27,6 +28,25 @@ export class Layout extends Component { return `/api/Images/user/160x160/${userId}/download?access_token=${token}`; } + getAccessTokenConfig() { + this.$http.get('AccessTokenConfigs/findOne').then(json => { + window.localStorage.renewPeriod = json.data.renewPeriod; + window.localStorage.renewInterval = json.data.renewInterval; + + const intervalMilliseconds = 1 * 1000; + const intervalID = setInterval(this.checkTokenValidity, intervalMilliseconds); + }); + } + + checkTokenValidity() { + console.log('checkTokenValidity'); + + // this.$http.post('VnUsers/renewToken') + // .then(() => { + // console.log('fin'); + // }); + } + refresh() { window.location.reload(); } -- 2.40.1 From 834a3aa9598ca81088503032747ee5014acd1e4e Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 24 May 2023 15:16:04 +0200 Subject: [PATCH 02/17] =?UTF-8?q?refs=20#5554=20fix:=20eliminada=20transac?= =?UTF-8?q?ci=C3=B3n=20en=20el=20back=20'renewToken'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/methods/vn-user/renew-token.js | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/back/methods/vn-user/renew-token.js b/back/methods/vn-user/renew-token.js index 66e64e995..ae5fa704e 100644 --- a/back/methods/vn-user/renew-token.js +++ b/back/methods/vn-user/renew-token.js @@ -8,25 +8,15 @@ module.exports = Self => { } }); - Self.renewToken = async function(ctx, options) { + Self.renewToken = async function(ctx) { const models = Self.app.models; const userId = ctx.req.accessToken.userId; const created = ctx.req.accessToken.created; // const tokenId = ctx.req.accessToken.id; - const myOptions = {}; - let tx; - - if (typeof options == 'object') - Object.assign(myOptions, options); - - if (!myOptions.transaction) { - tx = await Self.beginTransaction({}); - myOptions.transaction = tx; - } const now = new Date(); const differenceMilliseconds = now - created; - const differenceSeconds = Math.floor(differenceMilliseconds / 1000); // Convertir la diferencia a segundos + const differenceSeconds = Math.floor(differenceMilliseconds / 1000); const accessTokenConfig = await models.AccessTokenConfig.findOne(); if (differenceSeconds <= accessTokenConfig.renewPeriod) { @@ -39,9 +29,10 @@ module.exports = Self => { return response; } - const accessToken = await models.AccessToken.create({userId: userId}, myOptions); - await models.AccessToken.destroyAll({userId: userId}, myOptions); - // await models.AccessToken.destroyById(tokenId, myOptions); + await models.AccessToken.destroyAll({userId: userId}); + // await models.AccessToken.destroyById(tokenId); + + const accessToken = await models.AccessToken.create({userId: userId}); return {token: accessToken.id, created: accessToken.created}; }; -- 2.40.1 From 2e69e4eea756576489c10d94f258aa70d807d72a Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 24 May 2023 15:16:10 +0200 Subject: [PATCH 03/17] a --- front/core/services/auth.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/front/core/services/auth.js b/front/core/services/auth.js index d1ac4c78d..17323fadf 100644 --- a/front/core/services/auth.js +++ b/front/core/services/auth.js @@ -61,9 +61,8 @@ export default class Auth { window.localStorage.renewPeriod = json.data.renewPeriod; window.localStorage.renewInterval = json.data.renewInterval; - this.checkTokenValidity(); - const intervalMilliseconds = 50 * 1000; - // setInterval(this.checkTokenValidity.bind(this), intervalMilliseconds); + const intervalMilliseconds = json.data.renewInterval * 1000; + setInterval(this.checkTokenValidity.bind(this), intervalMilliseconds); }); } -- 2.40.1 From fdd22ca2d80c144437c17466b3bb5af03b9ea3d1 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 25 May 2023 07:32:30 +0200 Subject: [PATCH 04/17] refs #5554 codigo eliminado de vnLayout --- front/salix/components/layout/index.js | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/front/salix/components/layout/index.js b/front/salix/components/layout/index.js index 6c4319215..48f50f404 100644 --- a/front/salix/components/layout/index.js +++ b/front/salix/components/layout/index.js @@ -10,7 +10,6 @@ export class Layout extends Component { $onInit() { this.getUserData(); - // this.getAccessTokenConfig(); } getUserData() { @@ -28,25 +27,6 @@ export class Layout extends Component { return `/api/Images/user/160x160/${userId}/download?access_token=${token}`; } - getAccessTokenConfig() { - this.$http.get('AccessTokenConfigs/findOne').then(json => { - window.localStorage.renewPeriod = json.data.renewPeriod; - window.localStorage.renewInterval = json.data.renewInterval; - - const intervalMilliseconds = 1 * 1000; - const intervalID = setInterval(this.checkTokenValidity, intervalMilliseconds); - }); - } - - checkTokenValidity() { - console.log('checkTokenValidity'); - - // this.$http.post('VnUsers/renewToken') - // .then(() => { - // console.log('fin'); - // }); - } - refresh() { window.location.reload(); } -- 2.40.1 From 6010bc1253485eebc0e81241a3669eee4be1b166 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 25 May 2023 09:51:56 +0200 Subject: [PATCH 05/17] =?UTF-8?q?refs=20#5554=20movido=20codigo=20al=20vnL?= =?UTF-8?q?ayout=20y=20parar=20el=20evento=20peri=C3=B3dico?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/methods/vn-user/renew-token.js | 10 +++--- front/core/services/auth.js | 36 ++------------------- front/salix/components/layout/index.js | 43 ++++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 40 deletions(-) diff --git a/back/methods/vn-user/renew-token.js b/back/methods/vn-user/renew-token.js index ae5fa704e..6275214f1 100644 --- a/back/methods/vn-user/renew-token.js +++ b/back/methods/vn-user/renew-token.js @@ -2,6 +2,10 @@ module.exports = Self => { Self.remoteMethodCtx('renewToken', { description: 'Send email to the user', accepts: [], + returns: { + type: 'Object', + root: true + }, http: { path: `/renewToken`, verb: 'POST' @@ -12,7 +16,7 @@ module.exports = Self => { const models = Self.app.models; const userId = ctx.req.accessToken.userId; const created = ctx.req.accessToken.created; - // const tokenId = ctx.req.accessToken.id; + const tokenId = ctx.req.accessToken.id; const now = new Date(); const differenceMilliseconds = now - created; @@ -29,10 +33,8 @@ module.exports = Self => { return response; } - await models.AccessToken.destroyAll({userId: userId}); - // await models.AccessToken.destroyById(tokenId); - const accessToken = await models.AccessToken.create({userId: userId}); + await models.AccessToken.destroyById(tokenId); return {token: accessToken.id, created: accessToken.created}; }; diff --git a/front/core/services/auth.js b/front/core/services/auth.js index 17323fadf..6d3d7f062 100644 --- a/front/core/services/auth.js +++ b/front/core/services/auth.js @@ -30,11 +30,8 @@ export default class Auth { } }; this.$transitions.onStart(criteria, transition => { - this.getAccessTokenConfig(); - if (this.loggedIn) { - console.log('firstIf'); + if (this.loggedIn) return true; - } let redirectToLogin = () => { return transition.router.stateService.target('login', { @@ -43,43 +40,14 @@ export default class Auth { }; if (this.vnToken.token) { - console.log('secondIf'); - return this.loadAcls() .then(() => true) .catch(redirectToLogin); - } else { - console.log('else'); - + } else return redirectToLogin(); - } }); } - getAccessTokenConfig() { - this.$http.get('AccessTokenConfigs/findOne').then(json => { - window.localStorage.renewPeriod = json.data.renewPeriod; - window.localStorage.renewInterval = json.data.renewInterval; - - const intervalMilliseconds = json.data.renewInterval * 1000; - setInterval(this.checkTokenValidity.bind(this), intervalMilliseconds); - }); - } - - checkTokenValidity() { - const now = new Date(); - const differenceMilliseconds = now - new Date(this.vnTokenCreated.created); - const differenceSeconds = Math.floor(differenceMilliseconds / 1000); - - console.log(differenceSeconds, window.localStorage.renewPeriod); - if (differenceSeconds > window.localStorage.renewPeriod) { - this.$http.post('VnUsers/renewToken') - .then(() => { - console.log('fin'); - }); - } - } - login(user, password, remember) { if (!user) { let err = new UserError('Please enter your username'); diff --git a/front/salix/components/layout/index.js b/front/salix/components/layout/index.js index 48f50f404..315632201 100644 --- a/front/salix/components/layout/index.js +++ b/front/salix/components/layout/index.js @@ -3,13 +3,48 @@ import Component from 'core/lib/component'; import './style.scss'; export class Layout extends Component { - constructor($element, $, vnModules) { + constructor($element, $, vnModules, vnToken, vnTokenCreated) { super($element, $); this.modules = vnModules.get(); + Object.assign(this, { + vnToken, + vnTokenCreated + }); } $onInit() { this.getUserData(); + this.getAccessTokenConfig(); + } + + getAccessTokenConfig() { + this.$http.get('AccessTokenConfigs/findOne').then(json => { + window.localStorage.renewPeriod = json.data.renewPeriod; + window.localStorage.renewInterval = json.data.renewInterval; + + const intervalMilliseconds = json.data.renewInterval * 1000; + this.inservalId = setInterval(this.checkTokenValidity.bind(this), intervalMilliseconds); + }); + } + + checkTokenValidity() { + const now = new Date(); + const differenceMilliseconds = now - new Date(this.vnTokenCreated.created); + const differenceSeconds = Math.floor(differenceMilliseconds / 1000); + + if (differenceSeconds > window.localStorage.renewPeriod) { + this.$http.post('VnUsers/renewToken') + .then(json => { + console.log('fin renewToken'); + if (json.data.token) { + let remember = true; + if (window.sessionStorage.vnToken) remember = false; + + this.vnToken.set(json.data.token, remember); + this.vnTokenCreated.set(json.data.created, remember); + } + }); + } } getUserData() { @@ -30,8 +65,12 @@ export class Layout extends Component { refresh() { window.location.reload(); } + + $onDestroy() { + clearInterval(this.inservalId); + } } -Layout.$inject = ['$element', '$scope', 'vnModules']; +Layout.$inject = ['$element', '$scope', 'vnModules', 'vnToken', 'vnTokenCreated']; ngModule.vnComponent('vnLayout', { template: require('./index.html'), -- 2.40.1 From f471d62d41a9fc6296432b35ed8d018b0370cddf Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 25 May 2023 10:09:38 +0200 Subject: [PATCH 06/17] refs #5554 refactor: 'renewToken' utiliza logout y createAccessToken --- back/methods/vn-user/renew-token.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/back/methods/vn-user/renew-token.js b/back/methods/vn-user/renew-token.js index 6275214f1..c7214453d 100644 --- a/back/methods/vn-user/renew-token.js +++ b/back/methods/vn-user/renew-token.js @@ -33,8 +33,9 @@ module.exports = Self => { return response; } - const accessToken = await models.AccessToken.create({userId: userId}); - await models.AccessToken.destroyById(tokenId); + await Self.logout(tokenId); + const user = await Self.findById(userId); + const accessToken = await user.createAccessToken(); return {token: accessToken.id, created: accessToken.created}; }; -- 2.40.1 From 886c4a15b55b949a3b11298aeee27d5ab0257be1 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 25 May 2023 10:19:00 +0200 Subject: [PATCH 07/17] =?UTF-8?q?refs=20#5554=20fix:=20manejado=20si=20la?= =?UTF-8?q?=20tabla=20accessTokenConfig=20esta=20vac=C3=ADa?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/changes/232201/00-salix.sql | 4 ++++ db/dump/fixtures.sql | 2 +- front/salix/components/layout/index.js | 11 ++++++----- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/db/changes/232201/00-salix.sql b/db/changes/232201/00-salix.sql index 75b64500a..dc1ed69be 100644 --- a/db/changes/232201/00-salix.sql +++ b/db/changes/232201/00-salix.sql @@ -4,3 +4,7 @@ CREATE TABLE `salix`.`accessTokenConfig` ( `renewInterval` int(10) unsigned DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci; + +INSERT IGNORE INTO `salix`.`accessTokenConfig` (`id`, `renewPeriod`, `renewInterval`) + VALUES + (1, 21600, 300); diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index cd4aca4bc..24ea1ad08 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2888,4 +2888,4 @@ INSERT INTO `vn`.`wagonTypeTray` (`id`, `typeFk`, `height`, `colorFk`) INSERT INTO `salix`.`accessTokenConfig` (`id`, `renewPeriod`, `renewInterval`) VALUES - (0, 50, 100); + (1, 21600, 300); diff --git a/front/salix/components/layout/index.js b/front/salix/components/layout/index.js index 315632201..dd8a69ac7 100644 --- a/front/salix/components/layout/index.js +++ b/front/salix/components/layout/index.js @@ -18,11 +18,13 @@ export class Layout extends Component { } getAccessTokenConfig() { - this.$http.get('AccessTokenConfigs/findOne').then(json => { - window.localStorage.renewPeriod = json.data.renewPeriod; - window.localStorage.renewInterval = json.data.renewInterval; + this.$http.get('AccessTokenConfigs').then(json => { + const firtsResult = json.data[0]; + if (!firtsResult) return; + window.localStorage.renewPeriod = firtsResult.renewPeriod; + window.localStorage.renewInterval = firtsResult.renewInterval; - const intervalMilliseconds = json.data.renewInterval * 1000; + const intervalMilliseconds = firtsResult.renewInterval * 1000; this.inservalId = setInterval(this.checkTokenValidity.bind(this), intervalMilliseconds); }); } @@ -35,7 +37,6 @@ export class Layout extends Component { if (differenceSeconds > window.localStorage.renewPeriod) { this.$http.post('VnUsers/renewToken') .then(json => { - console.log('fin renewToken'); if (json.data.token) { let remember = true; if (window.sessionStorage.vnToken) remember = false; -- 2.40.1 From b95ee6cbfa82c7281cfe4603d717b164b09765e6 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 25 May 2023 11:33:58 +0200 Subject: [PATCH 08/17] refs #5554 feat: add front test --- front/salix/components/layout/index.js | 63 +++++++++++---------- front/salix/components/layout/index.spec.js | 47 +++++++++++++++ 2 files changed, 79 insertions(+), 31 deletions(-) diff --git a/front/salix/components/layout/index.js b/front/salix/components/layout/index.js index dd8a69ac7..adf1f70d6 100644 --- a/front/salix/components/layout/index.js +++ b/front/salix/components/layout/index.js @@ -17,37 +17,6 @@ export class Layout extends Component { this.getAccessTokenConfig(); } - getAccessTokenConfig() { - this.$http.get('AccessTokenConfigs').then(json => { - const firtsResult = json.data[0]; - if (!firtsResult) return; - window.localStorage.renewPeriod = firtsResult.renewPeriod; - window.localStorage.renewInterval = firtsResult.renewInterval; - - const intervalMilliseconds = firtsResult.renewInterval * 1000; - this.inservalId = setInterval(this.checkTokenValidity.bind(this), intervalMilliseconds); - }); - } - - checkTokenValidity() { - const now = new Date(); - const differenceMilliseconds = now - new Date(this.vnTokenCreated.created); - const differenceSeconds = Math.floor(differenceMilliseconds / 1000); - - if (differenceSeconds > window.localStorage.renewPeriod) { - this.$http.post('VnUsers/renewToken') - .then(json => { - if (json.data.token) { - let remember = true; - if (window.sessionStorage.vnToken) remember = false; - - this.vnToken.set(json.data.token, remember); - this.vnTokenCreated.set(json.data.created, remember); - } - }); - } - } - getUserData() { this.$http.get('VnUsers/getCurrentUserData').then(json => { this.$.$root.user = json.data; @@ -67,6 +36,38 @@ export class Layout extends Component { window.location.reload(); } + getAccessTokenConfig() { + this.$http.get('AccessTokenConfigs').then(json => { + const firtsResult = json.data[0]; + if (!firtsResult) return; + window.localStorage.renewPeriod = firtsResult.renewPeriod; + window.localStorage.renewInterval = firtsResult.renewInterval; + + const intervalMilliseconds = firtsResult.renewInterval * 1000; + this.inservalId = setInterval(this.checkTokenValidity.bind(this), intervalMilliseconds); + }); + } + + checkTokenValidity() { + const now = new Date(); + const differenceMilliseconds = now - new Date(this.vnTokenCreated.created); + const differenceSeconds = Math.floor(differenceMilliseconds / 1000); + + console.log(differenceSeconds, window.localStorage.renewPeriod); + if (differenceSeconds > window.localStorage.renewPeriod) { + this.$http.post('VnUsers/renewToken') + .then(json => { + if (json.data.token) { + let remember = true; + if (window.sessionStorage.vnToken) remember = false; + + this.vnToken.set(json.data.token, remember); + this.vnTokenCreated.set(json.data.created, remember); + } + }); + } + } + $onDestroy() { clearInterval(this.inservalId); } diff --git a/front/salix/components/layout/index.spec.js b/front/salix/components/layout/index.spec.js index 0d70c4806..d43f9a3d3 100644 --- a/front/salix/components/layout/index.spec.js +++ b/front/salix/components/layout/index.spec.js @@ -37,4 +37,51 @@ describe('Component vnLayout', () => { expect(url).not.toBeDefined(); }); }); + + describe('getAccessTokenConfig()', () => { + it(`should set the renewPeriod and renewInterval properties in localStorage`, () => { + const response = [{ + renewPeriod: 100, + renewInterval: 5 + }]; + + $httpBackend.expect('GET', `AccessTokenConfigs`).respond(response); + controller.getAccessTokenConfig(); + $httpBackend.flush(); + + const renewPeriod = localStorage.getItem('renewPeriod'); + const renewInterval = localStorage.getItem('renewInterval'); + + expect(renewPeriod).toBe('100'); + expect(renewInterval).toBe('5'); + expect(controller.inservalId).toBeDefined(); + }); + }); + + describe('checkTokenValidity()', () => { + it(`should not call renewToken and not set vnToken in the controller`, () => { + localStorage.setItem('renewPeriod', 100); + controller.vnTokenCreated.created = new Date(); + + controller.checkTokenValidity(); + + expect(controller.vnToken.token).toBeNull(); + }); + + it(`should call renewToken and set vnToken and vnTokenCreated properties in the controller`, () => { + const response = { + token: 999, + created: new Date() + }; + localStorage.setItem('renewPeriod', 100); + controller.vnTokenCreated.created = new Date(Date.now() - (60 * 60 * 1000)); + + $httpBackend.expect('POST', `VnUsers/renewToken`).respond(response); + controller.checkTokenValidity(); + $httpBackend.flush(); + + expect(controller.vnToken.token).toBe(999); + expect(controller.vnTokenCreated.created).toEqual(response.created); + }); + }); }); -- 2.40.1 From 91700e096e59d5e194c9acfab4c12f907468c418 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 25 May 2023 11:51:27 +0200 Subject: [PATCH 09/17] =?UTF-8?q?refs=20#5554=20fix:=20cambiada=20extensi?= =?UTF-8?q?=C3=B3n=20de=20archivos=20de=20test,=20y=20solucionado=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../specs/{signIn.js => signIn.spec.js} | 4 +- back/methods/vn-user/specs/signOut.js | 42 ------------------- 2 files changed, 2 insertions(+), 44 deletions(-) rename back/methods/vn-user/specs/{signIn.js => signIn.spec.js} (91%) delete mode 100644 back/methods/vn-user/specs/signOut.js diff --git a/back/methods/vn-user/specs/signIn.js b/back/methods/vn-user/specs/signIn.spec.js similarity index 91% rename from back/methods/vn-user/specs/signIn.js rename to back/methods/vn-user/specs/signIn.spec.js index b46c645d6..c3f4630c6 100644 --- a/back/methods/vn-user/specs/signIn.js +++ b/back/methods/vn-user/specs/signIn.spec.js @@ -9,7 +9,7 @@ describe('VnUser signIn()', () => { expect(login.token).toBeDefined(); - await models.VnUser.signOut(ctx); + await models.VnUser.logout(ctx.req.accessToken.id); }); it('should return the token if the user doesnt exist but the client does', async() => { @@ -19,7 +19,7 @@ describe('VnUser signIn()', () => { expect(login.token).toBeDefined(); - await models.VnUser.signOut(ctx); + await models.VnUser.logout(ctx.req.accessToken.id); }); }); diff --git a/back/methods/vn-user/specs/signOut.js b/back/methods/vn-user/specs/signOut.js deleted file mode 100644 index c84e86f05..000000000 --- a/back/methods/vn-user/specs/signOut.js +++ /dev/null @@ -1,42 +0,0 @@ -const {models} = require('vn-loopback/server/server'); - -describe('VnUser signOut()', () => { - it('should logout and remove token after valid login', async() => { - let loginResponse = await models.VnUser.signOut('buyer', 'nightmare'); - let accessToken = await models.AccessToken.findById(loginResponse.token); - let ctx = {req: {accessToken: accessToken}}; - - let logoutResponse = await models.VnUser.signOut(ctx); - let tokenAfterLogout = await models.AccessToken.findById(loginResponse.token); - - expect(logoutResponse).toBeTrue(); - expect(tokenAfterLogout).toBeNull(); - }); - - it('should throw a 401 error when token is invalid', async() => { - let error; - let ctx = {req: {accessToken: {id: 'invalidToken'}}}; - - try { - response = await models.VnUser.signOut(ctx); - } catch (e) { - error = e; - } - - expect(error).toBeDefined(); - expect(error.statusCode).toBe(401); - }); - - it('should throw an error when no token is passed', async() => { - let error; - let ctx = {req: {accessToken: null}}; - - try { - response = await models.VnUser.signOut(ctx); - } catch (e) { - error = e; - } - - expect(error).toBeDefined(); - }); -}); -- 2.40.1 From 06d1de5a637f33b9e704e78ece27f9038f943ae4 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 25 May 2023 12:06:40 +0200 Subject: [PATCH 10/17] refs #5554 feat: add back test --- .../methods/vn-user/specs/renew-token.spec.js | 28 +++++++++++++++++++ front/salix/components/layout/index.spec.js | 3 +- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 back/methods/vn-user/specs/renew-token.spec.js diff --git a/back/methods/vn-user/specs/renew-token.spec.js b/back/methods/vn-user/specs/renew-token.spec.js new file mode 100644 index 000000000..d87c6a30e --- /dev/null +++ b/back/methods/vn-user/specs/renew-token.spec.js @@ -0,0 +1,28 @@ +const {models} = require('vn-loopback/server/server'); + +describe('VnUser renewToken()', () => { + describe('when credentials are correct', () => { + it('should not do anything', async() => { + const login = await models.VnUser.signIn('employee', 'nightmare'); + const accessToken = await models.AccessToken.findById(login.token); + const ctx = {req: {accessToken: accessToken}}; + + const response = await models.VnUser.renewToken(ctx); + + expect(response.data.message).toBe('Token is active'); + }); + + it('should invalidate the current token and create a new one', async() => { + const login = await models.VnUser.signIn('employee', 'nightmare'); + const accessToken = await models.AccessToken.findById(login.token); + const ctx = {req: {accessToken: accessToken}}; + const sevenHoursBefore = new Date(Date.now() - (7 * 60 * 60 * 1000)); + ctx.req.accessToken.created = sevenHoursBefore; + + const renewToken = await models.VnUser.renewToken(ctx); + + expect(renewToken.token).toBeDefined(); + expect(renewToken.created).toBeDefined(); + }); + }); +}); diff --git a/front/salix/components/layout/index.spec.js b/front/salix/components/layout/index.spec.js index d43f9a3d3..97a6f7d71 100644 --- a/front/salix/components/layout/index.spec.js +++ b/front/salix/components/layout/index.spec.js @@ -74,7 +74,8 @@ describe('Component vnLayout', () => { created: new Date() }; localStorage.setItem('renewPeriod', 100); - controller.vnTokenCreated.created = new Date(Date.now() - (60 * 60 * 1000)); + const oneHourBefore = new Date(Date.now() - (60 * 60 * 1000)); + controller.vnTokenCreated.created = oneHourBefore; $httpBackend.expect('POST', `VnUsers/renewToken`).respond(response); controller.checkTokenValidity(); -- 2.40.1 From 32badef7876afcd822732901f2cc6863d6d82c29 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 25 May 2023 12:07:17 +0200 Subject: [PATCH 11/17] delete console.log --- front/salix/components/layout/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/front/salix/components/layout/index.js b/front/salix/components/layout/index.js index adf1f70d6..e9042da25 100644 --- a/front/salix/components/layout/index.js +++ b/front/salix/components/layout/index.js @@ -53,7 +53,6 @@ export class Layout extends Component { const differenceMilliseconds = now - new Date(this.vnTokenCreated.created); const differenceSeconds = Math.floor(differenceMilliseconds / 1000); - console.log(differenceSeconds, window.localStorage.renewPeriod); if (differenceSeconds > window.localStorage.renewPeriod) { this.$http.post('VnUsers/renewToken') .then(json => { -- 2.40.1 From 2b34dd19e8cfd592ed10e9830711fc66cf62f032 Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 30 May 2023 12:06:15 +0200 Subject: [PATCH 12/17] refs #554 delete: test back que falla --- .../methods/vn-user/specs/renew-token.spec.js | 28 ------------------- 1 file changed, 28 deletions(-) delete mode 100644 back/methods/vn-user/specs/renew-token.spec.js diff --git a/back/methods/vn-user/specs/renew-token.spec.js b/back/methods/vn-user/specs/renew-token.spec.js deleted file mode 100644 index d87c6a30e..000000000 --- a/back/methods/vn-user/specs/renew-token.spec.js +++ /dev/null @@ -1,28 +0,0 @@ -const {models} = require('vn-loopback/server/server'); - -describe('VnUser renewToken()', () => { - describe('when credentials are correct', () => { - it('should not do anything', async() => { - const login = await models.VnUser.signIn('employee', 'nightmare'); - const accessToken = await models.AccessToken.findById(login.token); - const ctx = {req: {accessToken: accessToken}}; - - const response = await models.VnUser.renewToken(ctx); - - expect(response.data.message).toBe('Token is active'); - }); - - it('should invalidate the current token and create a new one', async() => { - const login = await models.VnUser.signIn('employee', 'nightmare'); - const accessToken = await models.AccessToken.findById(login.token); - const ctx = {req: {accessToken: accessToken}}; - const sevenHoursBefore = new Date(Date.now() - (7 * 60 * 60 * 1000)); - ctx.req.accessToken.created = sevenHoursBefore; - - const renewToken = await models.VnUser.renewToken(ctx); - - expect(renewToken.token).toBeDefined(); - expect(renewToken.created).toBeDefined(); - }); - }); -}); -- 2.40.1 From b144948dcf9773b4804bef229f42c2961381c40d Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 6 Jun 2023 12:31:44 +0200 Subject: [PATCH 13/17] conflictos resueltos --- CHANGELOG.md | 3 ++- db/dump/fixtures.sql | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 75e807a7b..92c4ee3aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,13 +24,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - (Tickets -> Crear Factura) Al facturar se envia automáticamente el pdf al cliente - (Artículos -> Histórico) Filtro para mostrar lo anterior al inventario - +- (Trabajadores -> Nuevo trabajador) Permite elegir el método de pago ### Changed - (Trabajadores -> Nuevo trabajador) Los clientes se crean sin 'TR' pero se añade tipo de negocio 'Trabajador' ### Fixed - (Tickets -> Líneas) Se permite hacer split de líneas al mismo ticket +- (Tickets -> Cambiar estado) Ahora muestra la lista completa de todos los estados diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index e149fe74c..0457f1165 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2889,6 +2889,7 @@ INSERT INTO `vn`.`wagonTypeTray` (`id`, `typeFk`, `height`, `colorFk`) INSERT INTO `salix`.`accessTokenConfig` (`id`, `renewPeriod`, `renewInterval`) VALUES (1, 21600, 300); + INSERT INTO `vn`.`travelConfig` (`id`, `warehouseInFk`, `warehouseOutFk`, `agencyFk`, `companyFk`) VALUES (1, 1, 1, 1, 442); -- 2.40.1 From 3fdb546d8694f8d38a7483a76ba45f0d9eda6e09 Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 7 Jun 2023 10:28:29 +0200 Subject: [PATCH 14/17] =?UTF-8?q?refs=20#5554=20refactor:=20eliminado=20vn?= =?UTF-8?q?TokenCreated=20y=20a=C3=B1adido=20a=20vnToken.created?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/methods/vn-user/renew-token.js | 18 +++++------ front/core/services/auth.js | 8 ++--- front/core/services/index.js | 1 - front/core/services/token-created.js | 34 --------------------- front/core/services/token.js | 22 ++++++++----- front/salix/components/layout/index.js | 13 +++----- front/salix/components/layout/index.spec.js | 8 ++--- loopback/locale/es.json | 5 +-- 8 files changed, 36 insertions(+), 73 deletions(-) delete mode 100644 front/core/services/token-created.js diff --git a/back/methods/vn-user/renew-token.js b/back/methods/vn-user/renew-token.js index c7214453d..1f3532bd6 100644 --- a/back/methods/vn-user/renew-token.js +++ b/back/methods/vn-user/renew-token.js @@ -1,6 +1,8 @@ +const UserError = require('vn-loopback/util/user-error'); + module.exports = Self => { Self.remoteMethodCtx('renewToken', { - description: 'Send email to the user', + description: 'Checks if the token has more than renewPeriod seconds to live and if so, renews it', accepts: [], returns: { type: 'Object', @@ -22,16 +24,10 @@ module.exports = Self => { const differenceMilliseconds = now - created; const differenceSeconds = Math.floor(differenceMilliseconds / 1000); - const accessTokenConfig = await models.AccessTokenConfig.findOne(); - if (differenceSeconds <= accessTokenConfig.renewPeriod) { - const response = { - statusCode: 200, - data: { - message: 'Token is active', - } - }; - return response; - } + const accessTokenConfig = await models.AccessTokenConfig.findOne({fields: ['renewPeriod']}); + + if (differenceSeconds <= accessTokenConfig.renewPeriod) + throw new UserError(`The renew period has not been exceeded`); await Self.logout(tokenId); const user = await Self.findById(userId); diff --git a/front/core/services/auth.js b/front/core/services/auth.js index 6d3d7f062..7f4d2c594 100644 --- a/front/core/services/auth.js +++ b/front/core/services/auth.js @@ -7,7 +7,7 @@ import UserError from 'core/lib/user-error'; * @property {Boolean} loggedIn Whether the user is currently logged */ export default class Auth { - constructor($http, $q, $state, $transitions, $window, vnToken, vnTokenCreated, vnModules, aclService) { + constructor($http, $q, $state, $transitions, $window, vnToken, vnModules, aclService) { Object.assign(this, { $http, $q, @@ -15,7 +15,6 @@ export default class Auth { $transitions, $window, vnToken, - vnTokenCreated, vnModules, aclService, loggedIn: false @@ -65,8 +64,7 @@ export default class Auth { } onLoginOk(json, remember) { - this.vnToken.set(json.data.token, remember); - this.vnTokenCreated.set(json.data.created, remember); + this.vnToken.set(json.data.token, json.data.created, remember); return this.loadAcls().then(() => { let continueHash = this.$state.params.continue; @@ -103,6 +101,6 @@ export default class Auth { }); } } -Auth.$inject = ['$http', '$q', '$state', '$transitions', '$window', 'vnToken', 'vnTokenCreated', 'vnModules', 'aclService']; +Auth.$inject = ['$http', '$q', '$state', '$transitions', '$window', 'vnToken', 'vnModules', 'aclService']; ngModule.service('vnAuth', Auth); diff --git a/front/core/services/index.js b/front/core/services/index.js index 6d4e34428..855f2fab1 100644 --- a/front/core/services/index.js +++ b/front/core/services/index.js @@ -11,5 +11,4 @@ import './report'; import './email'; import './file'; import './date'; -import './token-created'; diff --git a/front/core/services/token-created.js b/front/core/services/token-created.js deleted file mode 100644 index c8e156a5d..000000000 --- a/front/core/services/token-created.js +++ /dev/null @@ -1,34 +0,0 @@ -import ngModule from '../module'; - -/** - * Saves and loads the created for the current logged in user. - * - * @property {String} created The current login created or %null - */ -export default class created { - constructor() { - try { - this.created = sessionStorage.getItem('vnTokenCreated'); - if (!this.created) - this.created = localStorage.getItem('vnTokenCreated'); - } catch (e) {} - } - set(value, remember) { - this.unset(); - try { - if (remember) - localStorage.setItem('vnTokenCreated', value); - else - sessionStorage.setItem('vnTokenCreated', value); - } catch (e) {} - - this.created = value; - } - unset() { - localStorage.removeItem('vnTokenCreated'); - sessionStorage.removeItem('vnTokenCreated'); - this.created = null; - } -} - -ngModule.service('vnTokenCreated', created); diff --git a/front/core/services/token.js b/front/core/services/token.js index 126fbb604..c1bb5a173 100644 --- a/front/core/services/token.js +++ b/front/core/services/token.js @@ -9,25 +9,33 @@ export default class Token { constructor() { try { this.token = sessionStorage.getItem('vnToken'); - if (!this.token) + this.created = sessionStorage.getItem('vnTokenCreated'); + if (!this.token) { this.token = localStorage.getItem('vnToken'); + this.created = localStorage.getItem('vnTokenCreated'); + } } catch (e) {} } - set(value, remember) { + set(token, created, remember) { this.unset(); try { - if (remember) - localStorage.setItem('vnToken', value); - else - sessionStorage.setItem('vnToken', value); + if (remember) { + localStorage.setItem('vnToken', token); + localStorage.setItem('vnTokenCreated', created); + } else { + sessionStorage.setItem('vnToken', token); + sessionStorage.setItem('vnTokenCreated', created); + } } catch (e) {} - this.token = value; + this.token = token; + this.created = created; } unset() { localStorage.removeItem('vnToken'); sessionStorage.removeItem('vnToken'); this.token = null; + this.created = null; } } diff --git a/front/salix/components/layout/index.js b/front/salix/components/layout/index.js index e9042da25..032a68037 100644 --- a/front/salix/components/layout/index.js +++ b/front/salix/components/layout/index.js @@ -3,13 +3,9 @@ import Component from 'core/lib/component'; import './style.scss'; export class Layout extends Component { - constructor($element, $, vnModules, vnToken, vnTokenCreated) { + constructor($element, $, vnModules, vnToken) { super($element, $); this.modules = vnModules.get(); - Object.assign(this, { - vnToken, - vnTokenCreated - }); } $onInit() { @@ -50,7 +46,7 @@ export class Layout extends Component { checkTokenValidity() { const now = new Date(); - const differenceMilliseconds = now - new Date(this.vnTokenCreated.created); + const differenceMilliseconds = now - new Date(this.vnToken.created); const differenceSeconds = Math.floor(differenceMilliseconds / 1000); if (differenceSeconds > window.localStorage.renewPeriod) { @@ -60,8 +56,7 @@ export class Layout extends Component { let remember = true; if (window.sessionStorage.vnToken) remember = false; - this.vnToken.set(json.data.token, remember); - this.vnTokenCreated.set(json.data.created, remember); + this.vnToken.set(json.data.token, json.data.created, remember); } }); } @@ -71,7 +66,7 @@ export class Layout extends Component { clearInterval(this.inservalId); } } -Layout.$inject = ['$element', '$scope', 'vnModules', 'vnToken', 'vnTokenCreated']; +Layout.$inject = ['$element', '$scope', 'vnModules', 'vnToken']; ngModule.vnComponent('vnLayout', { template: require('./index.html'), diff --git a/front/salix/components/layout/index.spec.js b/front/salix/components/layout/index.spec.js index 97a6f7d71..e94ded787 100644 --- a/front/salix/components/layout/index.spec.js +++ b/front/salix/components/layout/index.spec.js @@ -61,28 +61,28 @@ describe('Component vnLayout', () => { describe('checkTokenValidity()', () => { it(`should not call renewToken and not set vnToken in the controller`, () => { localStorage.setItem('renewPeriod', 100); - controller.vnTokenCreated.created = new Date(); + controller.vnToken.created = new Date(); controller.checkTokenValidity(); expect(controller.vnToken.token).toBeNull(); }); - it(`should call renewToken and set vnToken and vnTokenCreated properties in the controller`, () => { + it(`should call renewToken and set vnToken properties in the controller`, () => { const response = { token: 999, created: new Date() }; localStorage.setItem('renewPeriod', 100); const oneHourBefore = new Date(Date.now() - (60 * 60 * 1000)); - controller.vnTokenCreated.created = oneHourBefore; + controller.vnToken.created = oneHourBefore; $httpBackend.expect('POST', `VnUsers/renewToken`).respond(response); controller.checkTokenValidity(); $httpBackend.flush(); expect(controller.vnToken.token).toBe(999); - expect(controller.vnTokenCreated.created).toEqual(response.created); + expect(controller.vnToken.created).toEqual(response.created); }); }); }); diff --git a/loopback/locale/es.json b/loopback/locale/es.json index d88a4ebc9..5df200abb 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -84,7 +84,7 @@ "The current ticket can't be modified": "El ticket actual no puede ser modificado", "The current claim can't be modified": "La reclamación actual no puede ser modificada", "The sales of this ticket can't be modified": "Las lineas de este ticket no pueden ser modificadas", - "The sales do not exists": "La(s) línea(s) seleccionada(s) no existe(n)", + "The sales do not exists": "La(s) línea(s) seleccionada(s) no existe(n)", "Please select at least one sale": "Por favor selecciona al menos una linea", "All sales must belong to the same ticket": "Todas las lineas deben pertenecer al mismo ticket", "NO_ZONE_FOR_THIS_PARAMETERS": "Para este día no hay ninguna zona configurada", @@ -293,5 +293,6 @@ "comercialName": "Comercial", "Invalid NIF for VIES": "Invalid NIF for VIES", "Ticket does not exist": "Este ticket no existe", - "Ticket is already signed": "Este ticket ya ha sido firmado" + "Ticket is already signed": "Este ticket ya ha sido firmado", + "The renew period has not been exceeded": "El periodo de renovación no ha sido superado" } -- 2.40.1 From dfdf948c42240d0950da1fa6fc93c9b0c2aba31c Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 8 Jun 2023 13:15:00 +0200 Subject: [PATCH 15/17] move sql changes --- db/changes/{232201 => 232601}/00-salix.sql | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename db/changes/{232201 => 232601}/00-salix.sql (100%) diff --git a/db/changes/232201/00-salix.sql b/db/changes/232601/00-salix.sql similarity index 100% rename from db/changes/232201/00-salix.sql rename to db/changes/232601/00-salix.sql -- 2.40.1 From ddd23a98420bd89211246d395a66c7c61801d9e2 Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 13 Jun 2023 10:40:26 +0200 Subject: [PATCH 16/17] refs #5554 refactor: sustiuido window.localStorage por this --- front/salix/components/layout/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/front/salix/components/layout/index.js b/front/salix/components/layout/index.js index 032a68037..dc2313f4f 100644 --- a/front/salix/components/layout/index.js +++ b/front/salix/components/layout/index.js @@ -36,8 +36,8 @@ export class Layout extends Component { this.$http.get('AccessTokenConfigs').then(json => { const firtsResult = json.data[0]; if (!firtsResult) return; - window.localStorage.renewPeriod = firtsResult.renewPeriod; - window.localStorage.renewInterval = firtsResult.renewInterval; + this.renewPeriod = firtsResult.renewPeriod; + this.renewInterval = firtsResult.renewInterval; const intervalMilliseconds = firtsResult.renewInterval * 1000; this.inservalId = setInterval(this.checkTokenValidity.bind(this), intervalMilliseconds); @@ -49,7 +49,7 @@ export class Layout extends Component { const differenceMilliseconds = now - new Date(this.vnToken.created); const differenceSeconds = Math.floor(differenceMilliseconds / 1000); - if (differenceSeconds > window.localStorage.renewPeriod) { + if (differenceSeconds > this.renewPeriod) { this.$http.post('VnUsers/renewToken') .then(json => { if (json.data.token) { -- 2.40.1 From 5d3ecfa00e525e1d751fec662c6a85a89b20ed2a Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 13 Jun 2023 10:44:29 +0200 Subject: [PATCH 17/17] refs #5554 fix: test front --- front/salix/components/layout/index.spec.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/front/salix/components/layout/index.spec.js b/front/salix/components/layout/index.spec.js index e94ded787..8f65f32ce 100644 --- a/front/salix/components/layout/index.spec.js +++ b/front/salix/components/layout/index.spec.js @@ -49,18 +49,15 @@ describe('Component vnLayout', () => { controller.getAccessTokenConfig(); $httpBackend.flush(); - const renewPeriod = localStorage.getItem('renewPeriod'); - const renewInterval = localStorage.getItem('renewInterval'); - - expect(renewPeriod).toBe('100'); - expect(renewInterval).toBe('5'); + expect(controller.renewPeriod).toBe(100); + expect(controller.renewInterval).toBe(5); expect(controller.inservalId).toBeDefined(); }); }); describe('checkTokenValidity()', () => { it(`should not call renewToken and not set vnToken in the controller`, () => { - localStorage.setItem('renewPeriod', 100); + controller.renewPeriod = 100; controller.vnToken.created = new Date(); controller.checkTokenValidity(); @@ -73,7 +70,7 @@ describe('Component vnLayout', () => { token: 999, created: new Date() }; - localStorage.setItem('renewPeriod', 100); + controller.renewPeriod = 100; const oneHourBefore = new Date(Date.now() - (60 * 60 * 1000)); controller.vnToken.created = oneHourBefore; -- 2.40.1