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(); });