From ac0c3b79b4647a51c576cd88b288a03f9975f11e Mon Sep 17 00:00:00 2001 From: sergiodt Date: Mon, 15 Jul 2024 15:51:52 +0200 Subject: [PATCH] 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);