6868_createLogin #2598

Merged
sergiodt merged 20 commits from 6868_createLogin into dev 2024-09-13 09:43:32 +00:00
3 changed files with 82 additions and 58 deletions
Showing only changes of commit 9d5d62afaf - Show all commits

View File

@ -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;
};
};

View File

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

View File

@ -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"
}
]
}
}