salix/modules/worker/back/methods/device/handle-user.js

119 lines
3.6 KiB
JavaScript
Raw Permalink Normal View History

2024-07-15 13:51:52 +00:00
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'
2024-07-16 15:08:38 +00:00
},
{
arg: 'deviceId',
type: 'String',
description: 'Device id'
},
{
2024-07-15 13:51:52 +00:00
arg: 'versionApp',
type: 'String',
description: 'Version app'
}, {
arg: 'nameApp',
type: 'String',
description: 'Version app'
},
],
returns: {
type: 'object',
root: true
},
http: {
path: `/handleUser`,
verb: 'POST'
}
});
2024-07-16 15:08:38 +00:00
Self.handleUser = async(ctx, androidId, deviceId, versionApp, nameApp, options) => {
2024-07-15 13:51:52 +00:00
const models = Self.app.models;
const myOptions = {};
2024-07-16 15:08:38 +00:00
2024-07-15 13:51:52 +00:00
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(?, ?);',
2024-07-16 15:08:38 +00:00
[user.id, androidId], myOptions);
2024-07-15 13:51:52 +00:00
if (!vIsAuthorized)
throw new UserError('Not authorized');
const isUserInOperator = await models.Operator.findOne({
where: {
workerFk: user.id
}
}, myOptions);
2024-09-12 15:09:27 +00:00
if (!isUserInOperator) {
await models.Operator.create({
'workerFk': user.id,
'isOnReservationMode': false
});
}
2024-07-15 13:51:52 +00:00
2024-07-16 15:08:38 +00:00
const whereCondition = deviceId ? {id: deviceId} : {android_id: androidId};
const serialNumber =
(await models.DeviceProduction.findOne({where: whereCondition}, myOptions))?.serialNumber ?? '';
2024-07-15 13:51:52 +00:00
await models.DeviceLog.create({
2024-07-16 15:08:38 +00:00
'android_id': androidId,
2024-07-15 13:51:52 +00:00
'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', 'isOnReservationMode'],
2024-07-15 13:51:52 +00:00
include: [
{
relation: 'sector',
scope: {
fields: ['warehouseFk', 'description', 'isOnReservationMode'],
2024-07-15 13:51:52 +00:00
}
}, {
relation: 'printer',
scope: {
fields: ['name'],
}
}, {
relation: 'train',
scope: {
fields: ['name'],
}
}
]
}, myOptions);
const getVersion = await models.MobileAppVersionControl.getVersion(ctx, nameApp);
const combinedResult = {
2024-09-12 15:09:27 +00:00
...getDataOperator.toObject(),
...getDataUser.toObject(),
...getVersion,
message: vMessage,
serialNumber,
};
2024-07-15 13:51:52 +00:00
return combinedResult;
};
};