62 lines
2.3 KiB
JavaScript
62 lines
2.3 KiB
JavaScript
/*
|
|
Author : Enrique Blasco BLanquer
|
|
Date: 27 de mayo de 2019
|
|
*/
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('checkUuid', {
|
|
description: 'Check UUID from user',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'data',
|
|
type: 'object',
|
|
required: true,
|
|
description: 'uuid,model',
|
|
http: {source: 'body'}
|
|
}],
|
|
returns: [{
|
|
type: 'Object',
|
|
root: true
|
|
}],
|
|
http: {
|
|
path: `/checkUuid`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.checkUuid = async(ctx, data) => {
|
|
const myUserId = ctx.req.accessToken.userId;
|
|
// 1 Check is a registered user with a uuid
|
|
let deviceUser = await Self.findOne({where: {userFk: myUserId, sn: data.uuid}});
|
|
|
|
if (deviceUser != null)
|
|
return {'state': true, 'mng': ''};
|
|
else {
|
|
// 2 If it does not exist it can be for two reasons:
|
|
// 2.1 It is the first time that the application enters so we have to register a new user associated with the user
|
|
// 2.2 Has the user associated with a different uuid, so we deny access.
|
|
|
|
let device = await Self.findOne({where: {userFk: myUserId}});
|
|
if (device != null) {
|
|
// The device is already registered by another user, access is denied
|
|
return {'state': false, 'mng': 'Ya estas regisgtrado en otro dispositivo, contacta con los dioses de informática.'};
|
|
} else {
|
|
// Check that the device is free
|
|
let aDevice = await Self.findOne({where: {sn: data.uuid}});
|
|
if (aDevice != null)
|
|
return {'state': false, 'mng': 'El dispositivo esta siendo usado por otro usuario'};
|
|
else {
|
|
// It's the first time you access the application, insert
|
|
/* await Self.rawSql('INSERT INTO vn2008.device (sn, model) VALUES (?,?);', [data.uuid, data.model]);*/
|
|
await Self.create({
|
|
sn: data.uuid,
|
|
model: data.model,
|
|
userFk: myUserId
|
|
});
|
|
|
|
return {'state': true, 'mng': 'Nuevo dispositivo registrado'};
|
|
}
|
|
}
|
|
}
|
|
};
|
|
};
|