From d1214a998abd7636d862f0813634412f944ef184 Mon Sep 17 00:00:00 2001 From: sergiodt Date: Thu, 6 Jun 2024 07:01:55 +0200 Subject: [PATCH 01/16] feat: login refs #6868 --- .../account/back/methods/account/login-app.js | 88 +++++++++++++++++++ modules/account/back/models/account.js | 1 + 2 files changed, 89 insertions(+) create mode 100644 modules/account/back/methods/account/login-app.js diff --git a/modules/account/back/methods/account/login-app.js b/modules/account/back/methods/account/login-app.js new file mode 100644 index 000000000..18543f90e --- /dev/null +++ b/modules/account/back/methods/account/login-app.js @@ -0,0 +1,88 @@ +const {setDefaultHighWaterMark} = require('form-data'); +const {setLocale} = require('i18n'); + +module.exports = Self => { + Self.remoteMethodCtx('loginApp', { + description: 'Login a user with username/email and password', + accepts: [ + { + arg: 'user', + type: 'String', + description: 'The user name or email', + required: true + }, { + arg: 'password', + type: 'String', + description: 'The password' + }, { + arg: 'deviceId', + type: 'String', + description: 'Device id' + }, { + arg: 'android_id', + type: 'String', + description: 'Android id' + }, { + arg: 'versionApp', + type: 'String', + description: 'Version app' + }, { + arg: 'nameApp', + type: 'String', + description: 'Version app' + }, { + arg: 'serialNumber', + type: 'String', + description: 'Serial number' + } + + ], + returns: { + type: 'object', + root: true + }, + http: { + path: `/loginApp`, + verb: 'POST' + } + }); + + Self.loginApp = async(ctx, user, password, deviceId, android_id, versionApp, nameApp, options) => { + const models = Self.app.models; + + const login = await Self.app.models.VnUser.signIn(ctx, user, password, options); + + const userId = login.user; + + const query = + `INSERT IGNORE INTO operator (workerFk) + VALUES (?);`; + + const insertOperator = await Self.rawSql(query, [userId], options); + + const [serialNumber] = await models.DeviceProductionUser.findOne({ + where: {id: '100'} + }); + + const insertDeviceLog = await models.DeviceLog.create( + { + 'android_id': android_id, + 'userFk': userId, + 'versionApp': versionApp, + 'nameApp': nameApp, + 'serialNumber': [serialNumber] + }, + options + ); + + const queryDeviceCheck = + `CALL device_checkLogin(?, ?)`; + + const [queryDeviceCheckLogin] = await Self.rawSql(queryDeviceCheck, [userId, android_id], options); + + if (!queryDeviceCheckLogin.vIsAuthorized) + throw new UserError('User not authorized'); + + return insertDeviceLog; + }; +}; diff --git a/modules/account/back/models/account.js b/modules/account/back/models/account.js index ceb26053c..f89d3079e 100644 --- a/modules/account/back/models/account.js +++ b/modules/account/back/models/account.js @@ -10,6 +10,7 @@ module.exports = Self => { require('../methods/account/logout')(Self); require('../methods/account/change-password')(Self); require('../methods/account/set-password')(Self); + require('../methods/account/login-app')(Self); Self.setUnverifiedPassword = async(id, pass, options) => { const {emailVerified} = await models.VnUser.findById(id, {fields: ['emailVerified']}, options); From 5c3ce40bbdfba57fa06f7b86f0cf4eebf23484bb Mon Sep 17 00:00:00 2001 From: sergiodt Date: Tue, 11 Jun 2024 07:12:47 +0200 Subject: [PATCH 02/16] feat login-app refs #6868 --- .../account/back/methods/account/login-app.js | 54 ++++++++++--------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/modules/account/back/methods/account/login-app.js b/modules/account/back/methods/account/login-app.js index 18543f90e..3ade88fb8 100644 --- a/modules/account/back/methods/account/login-app.js +++ b/modules/account/back/methods/account/login-app.js @@ -49,40 +49,44 @@ module.exports = Self => { Self.loginApp = async(ctx, user, password, deviceId, android_id, versionApp, nameApp, options) => { const models = Self.app.models; + const myOptions = {}; + if (typeof options == 'object') + Object.assign(myOptions, options); - const login = await Self.app.models.VnUser.signIn(ctx, user, password, options); + await models.Account.login(ctx, user, password, myOptions); - const userId = login.user; + const userId = ctx.req.accessToken.userId; - const query = - `INSERT IGNORE INTO operator (workerFk) - VALUES (?);`; + const isUserInOperator = await models.Operator.findById(userId); + if (!isUserInOperator) + await models.Operator.create({'workerFk': userId}); - const insertOperator = await Self.rawSql(query, [userId], options); + const device = await models.DeviceProduction.findById(deviceId); - const [serialNumber] = await models.DeviceProductionUser.findOne({ - where: {id: '100'} - }); + // const [serialNumber] = await models.DeviceProductionUser.findOne({ + // where: {id: '100'} + // }); - const insertDeviceLog = await models.DeviceLog.create( - { - 'android_id': android_id, - 'userFk': userId, - 'versionApp': versionApp, - 'nameApp': nameApp, - 'serialNumber': [serialNumber] - }, - options - ); + // const insertDeviceLog = await models.DeviceLog.create( + // { + // 'android_id': android_id, + // 'userFk': userId, + // 'versionApp': versionApp, + // 'nameApp': nameApp, + // 'serialNumber': [serialNumber] + // }, + // options + // ); - const queryDeviceCheck = - `CALL device_checkLogin(?, ?)`; + // const queryDeviceCheck = + // `CALL device_checkLogin(?, ?)`; - const [queryDeviceCheckLogin] = await Self.rawSql(queryDeviceCheck, [userId, android_id], options); + // const [queryDeviceCheckLogin] = await Self.rawSql(queryDeviceCheck, [userId, android_id], options); - if (!queryDeviceCheckLogin.vIsAuthorized) - throw new UserError('User not authorized'); + // if (!queryDeviceCheckLogin.vIsAuthorized) + // throw new UserError('User not authorized'); - return insertDeviceLog; + // return insertDeviceLog; + return []; }; }; From 52b82603d22d4214282eaa216945423d6274ca86 Mon Sep 17 00:00:00 2001 From: sergiodt Date: Thu, 13 Jun 2024 10:40:37 +0200 Subject: [PATCH 03/16] feat login-app refs #6868 --- .../account/back/methods/account/login-app.js | 54 +++++++++++++++---- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/modules/account/back/methods/account/login-app.js b/modules/account/back/methods/account/login-app.js index 3ade88fb8..5392f8eb2 100644 --- a/modules/account/back/methods/account/login-app.js +++ b/modules/account/back/methods/account/login-app.js @@ -1,5 +1,4 @@ -const {setDefaultHighWaterMark} = require('form-data'); -const {setLocale} = require('i18n'); +const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('loginApp', { @@ -53,20 +52,55 @@ module.exports = Self => { if (typeof options == 'object') Object.assign(myOptions, options); - await models.Account.login(ctx, user, password, myOptions); + const login = await models.Account.login(ctx, user, password, myOptions); const userId = ctx.req.accessToken.userId; - const isUserInOperator = await models.Operator.findById(userId); - if (!isUserInOperator) - await models.Operator.create({'workerFk': userId}); + const resultCheckLogin = + await Self.rawSql('CALL vn.device_checkLogin(?, ?);', + [userId, android_id], myOptions); - const device = await models.DeviceProduction.findById(deviceId); + const [{vIsAuthorized, vMessage}] = resultCheckLogin[0]; - // const [serialNumber] = await models.DeviceProductionUser.findOne({ - // where: {id: '100'} - // }); + if (!vIsAuthorized) + throw new UserError('Not authorized'); + const isUserInOperator = await models.Operator.findOne({ + where: { + workerFk: userId + } + }); + + if (!isUserInOperator){ + await models.Operator.create({ 'workerFk': userId }); + + const getDataUser = await models.Operator.findOne({ + where: { + workerFk: userId + } + }); + + const resultDevice = await models.DeviceProduction.findOne({ + where: { + android_id: android_id + } + }); + + if (resultDevice) + serialNumber = resultDevice.serialNumber ?? ''; + + await models.DeviceLog.create({ + 'android_id': android_id, + 'userFk': userId, + 'nameApp': nameApp, + 'versionApp': versionApp, + 'serialNumber': serialNumber + }); + + + + console.log(vIsAuthorized); + console.log(vMessage); // const insertDeviceLog = await models.DeviceLog.create( // { // 'android_id': android_id, From 9d5d62afafd846b2f64989a24fb10f1d594964ed Mon Sep 17 00:00:00 2001 From: sergiodt Date: Mon, 17 Jun 2024 13:56:58 +0200 Subject: [PATCH 04/16] feat newLogin refs #6868 --- .../account/back/methods/account/login-app.js | 111 +++++++++--------- .../methods/account/specs/login-app.spec.js | 20 ++++ modules/account/back/models/account.json | 9 +- 3 files changed, 82 insertions(+), 58 deletions(-) create mode 100644 modules/account/back/methods/account/specs/login-app.spec.js diff --git a/modules/account/back/methods/account/login-app.js b/modules/account/back/methods/account/login-app.js index 5392f8eb2..b18226bd2 100644 --- a/modules/account/back/methods/account/login-app.js +++ b/modules/account/back/methods/account/login-app.js @@ -2,23 +2,20 @@ const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('loginApp', { - description: 'Login a user with username/email and password', + description: 'Login a user with username and password for app', accepts: [ { arg: 'user', type: 'String', - description: 'The user name or email', + description: 'The user name', required: true }, { arg: 'password', type: 'String', + required: true, description: 'The password' }, { - arg: 'deviceId', - type: 'String', - description: 'Device id' - }, { - arg: 'android_id', + arg: 'androidId', type: 'String', description: 'Android id' }, { @@ -29,11 +26,7 @@ module.exports = Self => { arg: 'nameApp', type: 'String', description: 'Version app' - }, { - arg: 'serialNumber', - type: 'String', - description: 'Serial number' - } + }, ], returns: { @@ -46,7 +39,7 @@ module.exports = Self => { } }); - Self.loginApp = async(ctx, user, password, deviceId, android_id, versionApp, nameApp, options) => { + Self.loginApp = async(ctx, user, password, android_id, versionApp, nameApp, options) => { const models = Self.app.models; const myOptions = {}; if (typeof options == 'object') @@ -54,14 +47,16 @@ module.exports = Self => { const login = await models.Account.login(ctx, user, password, myOptions); - const userId = ctx.req.accessToken.userId; + const {id: userId} = await models.VnUser.findOne({ + where: { + name: user + } + }); - const resultCheckLogin = + const [[{vIsAuthorized, vMessage}]] = await Self.rawSql('CALL vn.device_checkLogin(?, ?);', [userId, android_id], myOptions); - const [{vIsAuthorized, vMessage}] = resultCheckLogin[0]; - if (!vIsAuthorized) throw new UserError('Not authorized'); @@ -69,25 +64,14 @@ module.exports = Self => { where: { workerFk: userId } - }); + }, myOptions); - if (!isUserInOperator){ - await models.Operator.create({ 'workerFk': userId }); - - const getDataUser = await models.Operator.findOne({ - where: { - workerFk: userId - } - }); + if (!isUserInOperator) + await models.Operator.create({'workerFk': userId}); - const resultDevice = await models.DeviceProduction.findOne({ - where: { - android_id: android_id - } - }); - - if (resultDevice) - serialNumber = resultDevice.serialNumber ?? ''; + const serialNumber = (await models.DeviceProduction.findOne({ + where: {android_id: android_id} + }, myOptions))?.serialNumber ?? ''; await models.DeviceLog.create({ 'android_id': android_id, @@ -95,32 +79,45 @@ module.exports = Self => { 'nameApp': nameApp, 'versionApp': versionApp, 'serialNumber': serialNumber - }); + }, myOptions); + ctx.req.accessToken = {userId}; + const getDataUser = await models.VnUser.getCurrentUserData(ctx); - + const getDataOperator = await models.Operator.findOne({ + where: {workerFk: userId}, + fields: ['numberOfWagons', 'warehouseFk', 'itemPackingTypeFk', 'sectorFk', 'sector', + 'trainFk', 'train', 'labelerFk', 'printer'], + include: [ + { + relation: 'sector', + scope: { + fields: ['warehouseFk', 'description'], + } + }, { + relation: 'printer', + scope: { + fields: ['name'], + } + }, { + relation: 'train', + scope: { + fields: ['name'], + } + } - console.log(vIsAuthorized); - console.log(vMessage); - // const insertDeviceLog = await models.DeviceLog.create( - // { - // 'android_id': android_id, - // 'userFk': userId, - // 'versionApp': versionApp, - // 'nameApp': nameApp, - // 'serialNumber': [serialNumber] - // }, - // options - // ); + ] + }, myOptions); - // const queryDeviceCheck = - // `CALL device_checkLogin(?, ?)`; + const getVersion = await models.MobileAppVersionControl.getVersion(ctx, nameApp); - // const [queryDeviceCheckLogin] = await Self.rawSql(queryDeviceCheck, [userId, android_id], options); - - // if (!queryDeviceCheckLogin.vIsAuthorized) - // throw new UserError('User not authorized'); - - // return insertDeviceLog; - return []; + const combinedResult = { + ...login, + ...getDataOperator.__data, + ...getDataUser.__data, + ...getVersion, + message: vMessage, + serialNumber, + }; + return combinedResult; }; }; diff --git a/modules/account/back/methods/account/specs/login-app.spec.js b/modules/account/back/methods/account/specs/login-app.spec.js new file mode 100644 index 000000000..527429435 --- /dev/null +++ b/modules/account/back/methods/account/specs/login-app.spec.js @@ -0,0 +1,20 @@ +const {models} = require('vn-loopback/server/server'); + +describe('Account loginApp()', () => { + fit('should throw an error when user/password is wrong', async() => { + let req = models.Account.loginApp('user', 'pass'); + + await expectAsync(req).toBeRejected(); + }); + + fit('should return data from user', async() => { + let user = 'employee'; + let password = 'nightmare'; + let androidId = 'androidid11234567890'; + let nameApp = 'warehouse'; + let versionApp = '10'; + + let req = models.Account.loginApp(user, password, androidId, nameApp, versionApp); + await expectAsync(req).toBeResolved(); + }); +}); diff --git a/modules/account/back/models/account.json b/modules/account/back/models/account.json index 6c2784696..466b2bc04 100644 --- a/modules/account/back/models/account.json +++ b/modules/account/back/models/account.json @@ -31,6 +31,13 @@ "principalId": "$everyone", "permission": "ALLOW" }, + { + "property": "loginApp", + "accessType": "EXECUTE", + "principalType": "ROLE", + "principalId": "$everyone", + "permission": "ALLOW" + }, { "property": "logout", "accessType": "EXECUTE", @@ -46,4 +53,4 @@ "permission": "ALLOW" } ] -} +} \ No newline at end of file From 0ab620b4854006a36882dcbc683fd759a94a2860 Mon Sep 17 00:00:00 2001 From: sergiodt Date: Mon, 17 Jun 2024 16:50:00 +0200 Subject: [PATCH 05/16] feat newLogin refs #6868 --- modules/account/back/methods/account/login-app.js | 1 + modules/account/back/methods/account/specs/login-app.spec.js | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/account/back/methods/account/login-app.js b/modules/account/back/methods/account/login-app.js index b18226bd2..dc2708cbf 100644 --- a/modules/account/back/methods/account/login-app.js +++ b/modules/account/back/methods/account/login-app.js @@ -1,3 +1,4 @@ +const {ConsoleMessage} = require('puppeteer'); const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { diff --git a/modules/account/back/methods/account/specs/login-app.spec.js b/modules/account/back/methods/account/specs/login-app.spec.js index 527429435..90ce39f68 100644 --- a/modules/account/back/methods/account/specs/login-app.spec.js +++ b/modules/account/back/methods/account/specs/login-app.spec.js @@ -1,8 +1,9 @@ const {models} = require('vn-loopback/server/server'); describe('Account loginApp()', () => { + const ctx = {req: {accessToken: {}}}; fit('should throw an error when user/password is wrong', async() => { - let req = models.Account.loginApp('user', 'pass'); + let req = models.Account.loginApp(ctx, 'user', 'pass', 'androidid11234567890', 'warehouse', '10'); await expectAsync(req).toBeRejected(); }); @@ -14,7 +15,7 @@ describe('Account loginApp()', () => { let nameApp = 'warehouse'; let versionApp = '10'; - let req = models.Account.loginApp(user, password, androidId, nameApp, versionApp); + let req = models.Account.loginApp(ctx, user, password, androidId, nameApp, versionApp); await expectAsync(req).toBeResolved(); }); }); From c6da9e72e022c4101247b019887414b44c902693 Mon Sep 17 00:00:00 2001 From: sergiodt Date: Mon, 17 Jun 2024 16:53:06 +0200 Subject: [PATCH 06/16] feat newLogin refs #6868 --- modules/account/back/methods/account/login-app.js | 2 +- .../back/methods/account/specs/login-app.spec.js | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/modules/account/back/methods/account/login-app.js b/modules/account/back/methods/account/login-app.js index dc2708cbf..83c727f1a 100644 --- a/modules/account/back/methods/account/login-app.js +++ b/modules/account/back/methods/account/login-app.js @@ -1,4 +1,4 @@ -const {ConsoleMessage} = require('puppeteer'); + const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { diff --git a/modules/account/back/methods/account/specs/login-app.spec.js b/modules/account/back/methods/account/specs/login-app.spec.js index 90ce39f68..fc05cd726 100644 --- a/modules/account/back/methods/account/specs/login-app.spec.js +++ b/modules/account/back/methods/account/specs/login-app.spec.js @@ -3,19 +3,19 @@ const {models} = require('vn-loopback/server/server'); describe('Account loginApp()', () => { const ctx = {req: {accessToken: {}}}; fit('should throw an error when user/password is wrong', async() => { - let req = models.Account.loginApp(ctx, 'user', 'pass', 'androidid11234567890', 'warehouse', '10'); + const req = models.Account.loginApp(ctx, 'user', 'pass', 'androidid11234567890', 'warehouse', '10'); await expectAsync(req).toBeRejected(); }); fit('should return data from user', async() => { - let user = 'employee'; - let password = 'nightmare'; - let androidId = 'androidid11234567890'; - let nameApp = 'warehouse'; - let versionApp = '10'; + const user = 'employee'; + const password = 'nightmare'; + const androidId = 'androidid11234567890'; + const nameApp = 'warehouse'; + const versionApp = '10'; - let req = models.Account.loginApp(ctx, user, password, androidId, nameApp, versionApp); + const req = models.Account.loginApp(ctx, user, password, androidId, nameApp, versionApp); await expectAsync(req).toBeResolved(); }); }); From ac0c3b79b4647a51c576cd88b288a03f9975f11e Mon Sep 17 00:00:00 2001 From: sergiodt Date: Mon, 15 Jul 2024 15:51:52 +0200 Subject: [PATCH 07/16] feat createLogin refs #6868 --- .../back/methods/account/handle-user.js | 111 ++++++++++++++++++ modules/account/back/models/account.js | 1 + 2 files changed, 112 insertions(+) create mode 100644 modules/account/back/methods/account/handle-user.js diff --git a/modules/account/back/methods/account/handle-user.js b/modules/account/back/methods/account/handle-user.js new file mode 100644 index 000000000..18beb1796 --- /dev/null +++ b/modules/account/back/methods/account/handle-user.js @@ -0,0 +1,111 @@ + +const UserError = require('vn-loopback/util/user-error'); + +module.exports = Self => { + Self.remoteMethodCtx('handleUser', { + description: 'Manage various aspects related to a user with the app', + accepts: [ + { + arg: 'androidId', + type: 'String', + description: 'Android id' + }, { + arg: 'versionApp', + type: 'String', + description: 'Version app' + }, { + arg: 'nameApp', + type: 'String', + description: 'Version app' + }, + + ], + returns: { + type: 'object', + root: true + }, + http: { + path: `/handleUser`, + verb: 'POST' + } + }); + + Self.loginApp = async(ctx, android_id, versionApp, nameApp, options) => { + const models = Self.app.models; + const myOptions = {}; + if (typeof options == 'object') + Object.assign(myOptions, options); + + // const login = await models.Account.login(ctx, user, password, myOptions); + + const accessToken = ctx.req.accessToken; + let user = await models.VnUser.findById(accessToken.userId); + console.log(user.id); + + const [[{vIsAuthorized, vMessage}]] = + await Self.rawSql('CALL vn.device_checkLogin(?, ?);', + [user.id, android_id], myOptions); + + if (!vIsAuthorized) + throw new UserError('Not authorized'); + + const isUserInOperator = await models.Operator.findOne({ + where: { + workerFk: user.id + } + }, myOptions); + + if (!isUserInOperator) + await models.Operator.create({'workerFk': user.id}); + + const serialNumber = (await models.DeviceProduction.findOne({ + where: {android_id: android_id} + }, myOptions))?.serialNumber ?? ''; + + await models.DeviceLog.create({ + 'android_id': android_id, + 'userFk': user.id, + 'nameApp': nameApp, + 'versionApp': versionApp, + 'serialNumber': serialNumber + }, myOptions); + // ctx.req.accessToken = {userId}; + const getDataUser = await models.VnUser.getCurrentUserData(ctx); + + const getDataOperator = await models.Operator.findOne({ + where: {workerFk: user.id}, + fields: ['numberOfWagons', 'warehouseFk', 'itemPackingTypeFk', 'sectorFk', 'sector', + 'trainFk', 'train', 'labelerFk', 'printer'], + include: [ + { + relation: 'sector', + scope: { + fields: ['warehouseFk', 'description'], + } + }, { + relation: 'printer', + scope: { + fields: ['name'], + } + }, { + relation: 'train', + scope: { + fields: ['name'], + } + } + + ] + }, myOptions); + + const getVersion = await models.MobileAppVersionControl.getVersion(ctx, nameApp); + + const combinedResult = { + ...getDataOperator.__data, + ...getDataUser.__data, + ...getVersion, + message: vMessage, + serialNumber, + }; + return combinedResult; + }; +}; diff --git a/modules/account/back/models/account.js b/modules/account/back/models/account.js index f89d3079e..4dd1987d5 100644 --- a/modules/account/back/models/account.js +++ b/modules/account/back/models/account.js @@ -11,6 +11,7 @@ module.exports = Self => { require('../methods/account/change-password')(Self); require('../methods/account/set-password')(Self); require('../methods/account/login-app')(Self); + require('../methods/account/handle-user')(Self); Self.setUnverifiedPassword = async(id, pass, options) => { const {emailVerified} = await models.VnUser.findById(id, {fields: ['emailVerified']}, options); From ae6963c26d9f0d3a5ef4e15661f942b304d34e79 Mon Sep 17 00:00:00 2001 From: sergiodt Date: Tue, 16 Jul 2024 08:19:13 +0200 Subject: [PATCH 08/16] feat handleUser refs #6868 --- modules/account/back/methods/account/handle-user.js | 5 +---- modules/account/back/models/account.js | 1 - 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/modules/account/back/methods/account/handle-user.js b/modules/account/back/methods/account/handle-user.js index 18beb1796..136ce4800 100644 --- a/modules/account/back/methods/account/handle-user.js +++ b/modules/account/back/methods/account/handle-user.js @@ -30,17 +30,14 @@ module.exports = Self => { } }); - Self.loginApp = async(ctx, android_id, versionApp, nameApp, options) => { + Self.handleUser = async(ctx, android_id, versionApp, nameApp, options) => { const models = Self.app.models; const myOptions = {}; if (typeof options == 'object') Object.assign(myOptions, options); - // const login = await models.Account.login(ctx, user, password, myOptions); - const accessToken = ctx.req.accessToken; let user = await models.VnUser.findById(accessToken.userId); - console.log(user.id); const [[{vIsAuthorized, vMessage}]] = await Self.rawSql('CALL vn.device_checkLogin(?, ?);', diff --git a/modules/account/back/models/account.js b/modules/account/back/models/account.js index 4dd1987d5..08a5e0811 100644 --- a/modules/account/back/models/account.js +++ b/modules/account/back/models/account.js @@ -10,7 +10,6 @@ module.exports = Self => { require('../methods/account/logout')(Self); require('../methods/account/change-password')(Self); require('../methods/account/set-password')(Self); - require('../methods/account/login-app')(Self); require('../methods/account/handle-user')(Self); Self.setUnverifiedPassword = async(id, pass, options) => { From 144cf20e7148d67d6b3c40da6b6f7ad0615d2e42 Mon Sep 17 00:00:00 2001 From: sergiodt Date: Tue, 16 Jul 2024 10:14:19 +0200 Subject: [PATCH 09/16] feat handleUser refs #6868 --- .../account/back/methods/account/login-app.js | 124 ------------------ ...{login-app.spec.js => handle-user.spec.js} | 4 +- 2 files changed, 2 insertions(+), 126 deletions(-) delete mode 100644 modules/account/back/methods/account/login-app.js rename modules/account/back/methods/account/specs/{login-app.spec.js => handle-user.spec.js} (80%) diff --git a/modules/account/back/methods/account/login-app.js b/modules/account/back/methods/account/login-app.js deleted file mode 100644 index 83c727f1a..000000000 --- a/modules/account/back/methods/account/login-app.js +++ /dev/null @@ -1,124 +0,0 @@ - -const UserError = require('vn-loopback/util/user-error'); - -module.exports = Self => { - Self.remoteMethodCtx('loginApp', { - description: 'Login a user with username and password for app', - accepts: [ - { - arg: 'user', - type: 'String', - description: 'The user name', - required: true - }, { - arg: 'password', - type: 'String', - required: true, - description: 'The password' - }, { - arg: 'androidId', - type: 'String', - description: 'Android id' - }, { - arg: 'versionApp', - type: 'String', - description: 'Version app' - }, { - arg: 'nameApp', - type: 'String', - description: 'Version app' - }, - - ], - returns: { - type: 'object', - root: true - }, - http: { - path: `/loginApp`, - verb: 'POST' - } - }); - - Self.loginApp = async(ctx, user, password, android_id, versionApp, nameApp, options) => { - const models = Self.app.models; - const myOptions = {}; - if (typeof options == 'object') - Object.assign(myOptions, options); - - const login = await models.Account.login(ctx, user, password, myOptions); - - const {id: userId} = await models.VnUser.findOne({ - where: { - name: user - } - }); - - const [[{vIsAuthorized, vMessage}]] = - await Self.rawSql('CALL vn.device_checkLogin(?, ?);', - [userId, android_id], myOptions); - - if (!vIsAuthorized) - throw new UserError('Not authorized'); - - const isUserInOperator = await models.Operator.findOne({ - where: { - workerFk: userId - } - }, myOptions); - - if (!isUserInOperator) - await models.Operator.create({'workerFk': userId}); - - const serialNumber = (await models.DeviceProduction.findOne({ - where: {android_id: android_id} - }, myOptions))?.serialNumber ?? ''; - - await models.DeviceLog.create({ - 'android_id': android_id, - 'userFk': userId, - 'nameApp': nameApp, - 'versionApp': versionApp, - 'serialNumber': serialNumber - }, myOptions); - ctx.req.accessToken = {userId}; - const getDataUser = await models.VnUser.getCurrentUserData(ctx); - - const getDataOperator = await models.Operator.findOne({ - where: {workerFk: userId}, - fields: ['numberOfWagons', 'warehouseFk', 'itemPackingTypeFk', 'sectorFk', 'sector', - 'trainFk', 'train', 'labelerFk', 'printer'], - include: [ - { - relation: 'sector', - scope: { - fields: ['warehouseFk', 'description'], - } - }, { - relation: 'printer', - scope: { - fields: ['name'], - } - }, { - relation: 'train', - scope: { - fields: ['name'], - } - } - - ] - }, myOptions); - - const getVersion = await models.MobileAppVersionControl.getVersion(ctx, nameApp); - - const combinedResult = { - ...login, - ...getDataOperator.__data, - ...getDataUser.__data, - ...getVersion, - message: vMessage, - serialNumber, - }; - return combinedResult; - }; -}; diff --git a/modules/account/back/methods/account/specs/login-app.spec.js b/modules/account/back/methods/account/specs/handle-user.spec.js similarity index 80% rename from modules/account/back/methods/account/specs/login-app.spec.js rename to modules/account/back/methods/account/specs/handle-user.spec.js index fc05cd726..621ff28ae 100644 --- a/modules/account/back/methods/account/specs/login-app.spec.js +++ b/modules/account/back/methods/account/specs/handle-user.spec.js @@ -1,9 +1,9 @@ const {models} = require('vn-loopback/server/server'); -describe('Account loginApp()', () => { +describe('Account handleUser()', () => { const ctx = {req: {accessToken: {}}}; fit('should throw an error when user/password is wrong', async() => { - const req = models.Account.loginApp(ctx, 'user', 'pass', 'androidid11234567890', 'warehouse', '10'); + const req = models.Account.handleUser(ctx, 'user', 'pass', 'androidid11234567890', 'warehouse', '10'); await expectAsync(req).toBeRejected(); }); From 7870416860504662b3a332adf90676ac69e257fb Mon Sep 17 00:00:00 2001 From: sergiodt Date: Tue, 16 Jul 2024 17:08:38 +0200 Subject: [PATCH 10/16] feat handleUser refs #6868 --- .../back/methods/account/handle-user.js | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/modules/account/back/methods/account/handle-user.js b/modules/account/back/methods/account/handle-user.js index 136ce4800..02b505e39 100644 --- a/modules/account/back/methods/account/handle-user.js +++ b/modules/account/back/methods/account/handle-user.js @@ -9,7 +9,13 @@ module.exports = Self => { arg: 'androidId', type: 'String', description: 'Android id' - }, { + }, + { + arg: 'deviceId', + type: 'String', + description: 'Device id' + }, + { arg: 'versionApp', type: 'String', description: 'Version app' @@ -30,9 +36,10 @@ module.exports = Self => { } }); - Self.handleUser = async(ctx, android_id, versionApp, nameApp, options) => { + Self.handleUser = async(ctx, androidId, deviceId, versionApp, nameApp, options) => { const models = Self.app.models; const myOptions = {}; + if (typeof options == 'object') Object.assign(myOptions, options); @@ -41,7 +48,7 @@ module.exports = Self => { const [[{vIsAuthorized, vMessage}]] = await Self.rawSql('CALL vn.device_checkLogin(?, ?);', - [user.id, android_id], myOptions); + [user.id, androidId], myOptions); if (!vIsAuthorized) throw new UserError('Not authorized'); @@ -55,18 +62,17 @@ module.exports = Self => { if (!isUserInOperator) await models.Operator.create({'workerFk': user.id}); - const serialNumber = (await models.DeviceProduction.findOne({ - where: {android_id: android_id} - }, myOptions))?.serialNumber ?? ''; + const whereCondition = deviceId ? {id: deviceId} : {android_id: androidId}; + const serialNumber = + (await models.DeviceProduction.findOne({where: whereCondition}, myOptions))?.serialNumber ?? ''; await models.DeviceLog.create({ - 'android_id': android_id, + 'android_id': androidId, 'userFk': user.id, 'nameApp': nameApp, 'versionApp': versionApp, 'serialNumber': serialNumber }, myOptions); - // ctx.req.accessToken = {userId}; const getDataUser = await models.VnUser.getCurrentUserData(ctx); const getDataOperator = await models.Operator.findOne({ From a82df8e2a278711b563f87be024d700d5c232149 Mon Sep 17 00:00:00 2001 From: sergiodt Date: Tue, 16 Jul 2024 17:20:35 +0200 Subject: [PATCH 11/16] feat handleUser refs #6868 --- .../methods/account/specs/handle-user.spec.js | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/modules/account/back/methods/account/specs/handle-user.spec.js b/modules/account/back/methods/account/specs/handle-user.spec.js index 621ff28ae..486d9dc7a 100644 --- a/modules/account/back/methods/account/specs/handle-user.spec.js +++ b/modules/account/back/methods/account/specs/handle-user.spec.js @@ -1,21 +1,16 @@ const {models} = require('vn-loopback/server/server'); describe('Account handleUser()', () => { - const ctx = {req: {accessToken: {}}}; - fit('should throw an error when user/password is wrong', async() => { - const req = models.Account.handleUser(ctx, 'user', 'pass', 'androidid11234567890', 'warehouse', '10'); + const ctx = {req: {accessToken: {userId: 9}}}; - await expectAsync(req).toBeRejected(); - }); - - fit('should return data from user', async() => { - const user = 'employee'; - const password = 'nightmare'; + it('should return data from user', async() => { const androidId = 'androidid11234567890'; + const deviceId = 1; const nameApp = 'warehouse'; const versionApp = '10'; - const req = models.Account.loginApp(ctx, user, password, androidId, nameApp, versionApp); - await expectAsync(req).toBeResolved(); + const data = await models.Account.handleUser(ctx, androidId, deviceId, versionApp, nameApp); + + expect(data.numberOfWagons).toBe(2); }); }); From 36cf6b1aeee927320e58e19917405d88d26f533a Mon Sep 17 00:00:00 2001 From: sergiodt Date: Tue, 3 Sep 2024 16:50:55 +0200 Subject: [PATCH 12/16] feat: refs #6868 refs# 6868 handleUser --- modules/account/back/models/account.js | 1 - .../account => worker/back/methods/device}/handle-user.js | 0 .../back/methods/device}/specs/handle-user.spec.js | 4 ++-- modules/worker/back/models/device.js | 1 + 4 files changed, 3 insertions(+), 3 deletions(-) rename modules/{account/back/methods/account => worker/back/methods/device}/handle-user.js (100%) rename modules/{account/back/methods/account => worker/back/methods/device}/specs/handle-user.spec.js (72%) diff --git a/modules/account/back/models/account.js b/modules/account/back/models/account.js index 08a5e0811..ceb26053c 100644 --- a/modules/account/back/models/account.js +++ b/modules/account/back/models/account.js @@ -10,7 +10,6 @@ module.exports = Self => { require('../methods/account/logout')(Self); require('../methods/account/change-password')(Self); require('../methods/account/set-password')(Self); - require('../methods/account/handle-user')(Self); Self.setUnverifiedPassword = async(id, pass, options) => { const {emailVerified} = await models.VnUser.findById(id, {fields: ['emailVerified']}, options); diff --git a/modules/account/back/methods/account/handle-user.js b/modules/worker/back/methods/device/handle-user.js similarity index 100% rename from modules/account/back/methods/account/handle-user.js rename to modules/worker/back/methods/device/handle-user.js diff --git a/modules/account/back/methods/account/specs/handle-user.spec.js b/modules/worker/back/methods/device/specs/handle-user.spec.js similarity index 72% rename from modules/account/back/methods/account/specs/handle-user.spec.js rename to modules/worker/back/methods/device/specs/handle-user.spec.js index 486d9dc7a..c4cd37e33 100644 --- a/modules/account/back/methods/account/specs/handle-user.spec.js +++ b/modules/worker/back/methods/device/specs/handle-user.spec.js @@ -1,6 +1,6 @@ const {models} = require('vn-loopback/server/server'); -describe('Account handleUser()', () => { +describe('Device handleUser()', () => { const ctx = {req: {accessToken: {userId: 9}}}; it('should return data from user', async() => { @@ -9,7 +9,7 @@ describe('Account handleUser()', () => { const nameApp = 'warehouse'; const versionApp = '10'; - const data = await models.Account.handleUser(ctx, androidId, deviceId, versionApp, nameApp); + const data = await models.Device.handleUser(ctx, androidId, deviceId, versionApp, nameApp); expect(data.numberOfWagons).toBe(2); }); diff --git a/modules/worker/back/models/device.js b/modules/worker/back/models/device.js index cda7a958f..6a7d5dba5 100644 --- a/modules/worker/back/models/device.js +++ b/modules/worker/back/models/device.js @@ -1,6 +1,7 @@ const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { + require('../methods/device/handle-user')(Self); Self.rewriteDbError(function(err) { if (err.code === 'ER_DUP_ENTRY') return new UserError(``); From b492a1e6a8858f39f118bc0a356b14ff5f1b7860 Mon Sep 17 00:00:00 2001 From: sergiodt Date: Wed, 11 Sep 2024 08:05:26 +0200 Subject: [PATCH 13/16] feat: refs #6868 refs# 6868 handleUser --- db/versions/11223-turquoiseLilium/00-firstScript.sql | 2 ++ modules/account/back/models/account.json | 7 ------- 2 files changed, 2 insertions(+), 7 deletions(-) create mode 100644 db/versions/11223-turquoiseLilium/00-firstScript.sql diff --git a/db/versions/11223-turquoiseLilium/00-firstScript.sql b/db/versions/11223-turquoiseLilium/00-firstScript.sql new file mode 100644 index 000000000..9003451c4 --- /dev/null +++ b/db/versions/11223-turquoiseLilium/00-firstScript.sql @@ -0,0 +1,2 @@ +INSERT INTO salix.ACL (model, property, accessType, permission, principalType, principalId) +VALUES( 'Device', 'handleUser', '*', 'ALLOW', 'ROLE', 'employee'); \ No newline at end of file diff --git a/modules/account/back/models/account.json b/modules/account/back/models/account.json index 466b2bc04..fc1bbeb3d 100644 --- a/modules/account/back/models/account.json +++ b/modules/account/back/models/account.json @@ -31,13 +31,6 @@ "principalId": "$everyone", "permission": "ALLOW" }, - { - "property": "loginApp", - "accessType": "EXECUTE", - "principalType": "ROLE", - "principalId": "$everyone", - "permission": "ALLOW" - }, { "property": "logout", "accessType": "EXECUTE", From 93e71638581a6d9067f09b36057e551d7b621b87 Mon Sep 17 00:00:00 2001 From: sergiodt Date: Thu, 12 Sep 2024 17:09:27 +0200 Subject: [PATCH 14/16] feat: refs #6868 refs# 6868 handleUser --- .../worker/back/methods/device/handle-user.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/modules/worker/back/methods/device/handle-user.js b/modules/worker/back/methods/device/handle-user.js index 02b505e39..87aec378e 100644 --- a/modules/worker/back/methods/device/handle-user.js +++ b/modules/worker/back/methods/device/handle-user.js @@ -59,8 +59,12 @@ module.exports = Self => { } }, myOptions); - if (!isUserInOperator) - await models.Operator.create({'workerFk': user.id}); + if (!isUserInOperator) { + await models.Operator.create({ + 'workerFk': user.id, + 'isOnReservationMode': false + }); + } const whereCondition = deviceId ? {id: deviceId} : {android_id: androidId}; const serialNumber = @@ -103,12 +107,21 @@ module.exports = Self => { const getVersion = await models.MobileAppVersionControl.getVersion(ctx, nameApp); const combinedResult = { + ...getDataOperator.toObject(), + ...getDataUser.toObject(), + ...getVersion, + message: vMessage, + serialNumber, + }; + + const combinedResult2 = { ...getDataOperator.__data, ...getDataUser.__data, ...getVersion, message: vMessage, serialNumber, }; + console.log('conbinedResult2', combinedResult2); return combinedResult; }; }; From 2d4e6670099ef4f2ab8a38dd09923edf9e22385b Mon Sep 17 00:00:00 2001 From: sergiodt Date: Fri, 13 Sep 2024 07:02:30 +0200 Subject: [PATCH 15/16] feat: refs #6868 refs# 6868 handleUser --- modules/worker/back/methods/device/handle-user.js | 9 --------- .../worker/back/methods/device/specs/handle-user.spec.js | 2 +- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/modules/worker/back/methods/device/handle-user.js b/modules/worker/back/methods/device/handle-user.js index 87aec378e..ac4ff2704 100644 --- a/modules/worker/back/methods/device/handle-user.js +++ b/modules/worker/back/methods/device/handle-user.js @@ -113,15 +113,6 @@ module.exports = Self => { message: vMessage, serialNumber, }; - - const combinedResult2 = { - ...getDataOperator.__data, - ...getDataUser.__data, - ...getVersion, - message: vMessage, - serialNumber, - }; - console.log('conbinedResult2', combinedResult2); return combinedResult; }; }; diff --git a/modules/worker/back/methods/device/specs/handle-user.spec.js b/modules/worker/back/methods/device/specs/handle-user.spec.js index c4cd37e33..37f76b765 100644 --- a/modules/worker/back/methods/device/specs/handle-user.spec.js +++ b/modules/worker/back/methods/device/specs/handle-user.spec.js @@ -3,7 +3,7 @@ const {models} = require('vn-loopback/server/server'); describe('Device handleUser()', () => { const ctx = {req: {accessToken: {userId: 9}}}; - it('should return data from user', async() => { + fit('should return data from user', async() => { const androidId = 'androidid11234567890'; const deviceId = 1; const nameApp = 'warehouse'; From e80fe1d0dc8a5189652a1c44ec0985e0c1afd2da Mon Sep 17 00:00:00 2001 From: sergiodt Date: Fri, 13 Sep 2024 07:10:47 +0200 Subject: [PATCH 16/16] feat: refs #6868 refs# 6868 handleUser --- modules/worker/back/methods/device/specs/handle-user.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/worker/back/methods/device/specs/handle-user.spec.js b/modules/worker/back/methods/device/specs/handle-user.spec.js index 37f76b765..c4cd37e33 100644 --- a/modules/worker/back/methods/device/specs/handle-user.spec.js +++ b/modules/worker/back/methods/device/specs/handle-user.spec.js @@ -3,7 +3,7 @@ const {models} = require('vn-loopback/server/server'); describe('Device handleUser()', () => { const ctx = {req: {accessToken: {userId: 9}}}; - fit('should return data from user', async() => { + it('should return data from user', async() => { const androidId = 'androidid11234567890'; const deviceId = 1; const nameApp = 'warehouse';