8093-devToTest_2442_3 #3100
|
@ -0,0 +1,2 @@
|
|||
INSERT INTO salix.ACL (model, property, accessType, permission, principalType, principalId)
|
||||
VALUES( 'Device', 'handleUser', '*', 'ALLOW', 'ROLE', 'employee');
|
|
@ -0,0 +1,118 @@
|
|||
|
||||
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: 'deviceId',
|
||||
type: 'String',
|
||||
description: 'Device 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.handleUser = async(ctx, androidId, deviceId, versionApp, nameApp, options) => {
|
||||
const models = Self.app.models;
|
||||
const myOptions = {};
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
const accessToken = ctx.req.accessToken;
|
||||
let user = await models.VnUser.findById(accessToken.userId);
|
||||
|
||||
const [[{vIsAuthorized, vMessage}]] =
|
||||
await Self.rawSql('CALL vn.device_checkLogin(?, ?);',
|
||||
[user.id, androidId], 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,
|
||||
'isOnReservationMode': false
|
||||
});
|
||||
}
|
||||
|
||||
const whereCondition = deviceId ? {id: deviceId} : {android_id: androidId};
|
||||
const serialNumber =
|
||||
(await models.DeviceProduction.findOne({where: whereCondition}, myOptions))?.serialNumber ?? '';
|
||||
|
||||
await models.DeviceLog.create({
|
||||
'android_id': androidId,
|
||||
'userFk': user.id,
|
||||
'nameApp': nameApp,
|
||||
'versionApp': versionApp,
|
||||
'serialNumber': serialNumber
|
||||
}, myOptions);
|
||||
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.toObject(),
|
||||
...getDataUser.toObject(),
|
||||
...getVersion,
|
||||
message: vMessage,
|
||||
serialNumber,
|
||||
};
|
||||
return combinedResult;
|
||||
};
|
||||
};
|
|
@ -0,0 +1,16 @@
|
|||
const {models} = require('vn-loopback/server/server');
|
||||
|
||||
describe('Device handleUser()', () => {
|
||||
const ctx = {req: {accessToken: {userId: 9}}};
|
||||
|
||||
it('should return data from user', async() => {
|
||||
const androidId = 'androidid11234567890';
|
||||
const deviceId = 1;
|
||||
const nameApp = 'warehouse';
|
||||
const versionApp = '10';
|
||||
|
||||
const data = await models.Device.handleUser(ctx, androidId, deviceId, versionApp, nameApp);
|
||||
|
||||
expect(data.numberOfWagons).toBe(2);
|
||||
});
|
||||
});
|
|
@ -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(``);
|
||||
|
|
Loading…
Reference in New Issue