diff --git a/db/access_tokens.js b/db/access_tokens.js index 23321f2..dacc2c4 100644 --- a/db/access_tokens.js +++ b/db/access_tokens.js @@ -1,5 +1,6 @@ 'use strict'; +const {logger, authl} = global; const loki = require('lokijs'); global.dbl = new loki('./loki.json', { @@ -7,46 +8,50 @@ global.dbl = new loki('./loki.json', { autosave: true, autosaveInterval: 5000, autoloadCallback() { - global.authl = global.dbl.getCollection('tokens'); - if (global.authl === null) { - global.authl = global.dbl.addCollection('tokens'); + authl = global.dbl.getCollection('tokens'); + if (authl === null) { + authl = global.dbl.addCollection('tokens'); } } }); module.exports.find = (key, done) => { - const ltoken = global.authl.findOne({'token': key}); + const ltoken = authl.findOne({'token': key}); if (ltoken){ const {userId, clientId} = ltoken; return done(null, {userId, clientId}) } else { - global.logger.log('error', new Error('Token Not Found')); + logger.log('error', new Error('Token Not Found')); return done(); } }; module.exports.findByUserIdAndClientId = (userId, clientId, done) => { - const ltoken = global.authl.findOne({'userId': userId}); + const ltoken = authl.findOne({'userId': userId}); if (ltoken){ - global.logger.log('info', {message: `Load token by userId (${userId}): User found`}); + logger.log('info', {message: `Load token by userId (${userId}): User found`}); const {token, userId: uid, clientId: cid} = ltoken; - if (uid === userId && cid === clientId) return done(null, token); - else return done(new Error('Token Not Found')); + if (uid === userId && cid === clientId) { + return done(null, token); + } else { + logger.log('error', new Error('Token Not Found')); + return done(); + } } else { - console.log('User not found'); - return done(new Error('User Not Found')); + logger.log('error', new Error('User Not Found')); + return done(); } }; module.exports.save = (token, userId, clientId, done) => { - global.logger.log('info', {message: `Start saving token`}); - const ltoken = global.authl.findOne({'userId': userId}); + logger.log('info', {message: `Start saving token`}); + const ltoken = authl.findOne({'userId': userId}); if (ltoken){ - global.logger.log('info', {message: `User Updated`}); - global.authl.update(Object.assign({}, ltoken, {token, userId, clientId})); + logger.log('info', {message: `User Updated`}); + authl.update(Object.assign({}, ltoken, {token, userId, clientId})); } else { - global.logger.log('info', {message: `User not Found. Create new...`}); - global.authl.insert({'type': 'token', token, userId, clientId}); + logger.log('info', {message: `User not Found. Create new...`}); + authl.insert({'type': 'token', token, userId, clientId}); } done(); }; diff --git a/db/authorization_codes.js b/db/authorization_codes.js index ae37649..c716cf2 100644 --- a/db/authorization_codes.js +++ b/db/authorization_codes.js @@ -1,10 +1,15 @@ 'use strict'; +const {logger} = global; const codes = {}; module.exports.find = (key, done) => { - if (codes[key]) return done(null, codes[key]); - return done(new Error('Code Not Found')); + if (codes[key]) { + return done(null, codes[key]); + } else { + logger.log('error', new Error('Code Not Found')); + return done(); + } }; module.exports.save = (code, clientId, redirectUri, userId, userName, done) => { diff --git a/db/clients.js b/db/clients.js index e49069f..4308a4b 100644 --- a/db/clients.js +++ b/db/clients.js @@ -1,17 +1,20 @@ 'use strict'; +const {logger} = global; const {clients} = require('../config'); module.exports.findById = (id, done) => { for (const client of clients) { if (client.id === id) return done(null, client); } - return done(new Error('Client Not Found')); + logger.log('error', new Error('Client Not Found')); + return done(); }; module.exports.findByClientId = (clientId, done) => { for (const client of clients) { if (client.clientId === clientId) return done(null, client); } - return done(new Error('Client Not Found')); + logger.log('error', new Error('Client Not Found')); + return done(); }; diff --git a/db/users.js b/db/users.js index 906954d..e2bf6a6 100644 --- a/db/users.js +++ b/db/users.js @@ -1,17 +1,20 @@ 'use strict'; +const {logger} = global; const {users} = require('../config'); module.exports.findById = (id, done) => { for (const user of users) { if (user.id === id) return done(null, user); } - return done(new Error('User Not Found')); + logger.log('error', new Error('User Not Found')); + return done(); }; module.exports.findByUsername = (username, done) => { for (const user of users) { if (user.username === username) return done(null, user); } - return done(new Error('User Not Found')); + logger.log('error', new Error('User Not Found')); + return done(); }; diff --git a/device.js b/device.js index 5b0168f..0f3c069 100644 --- a/device.js +++ b/device.js @@ -1,3 +1,5 @@ +const {logger} = global; + /* function for convert system values to Yandex (depends of capability or property type) */ function convertToYandexValue(val, actType) { switch(actType) { @@ -8,7 +10,7 @@ function convertToYandexValue(val, actType) { const value = parseFloat(val); return isNaN(value) ? 0.0 : value; } catch(e) { - console.error(`Can't parse to float: ${val}`); + logger.log('error', {message: `Can't parse to float: ${val}`}); return 0.0; } } @@ -96,7 +98,7 @@ class Device { } } default: { - console.error(`Unsupported capability type: ${type}`) + logger.log('error', {message: `Unsupported capability type: ${type}`}); return undefined; } } @@ -195,7 +197,7 @@ class Device { message = `${value}`; } catch(e) { topic = false; - global.logger.log('error', {message: `${e}`}); + logger.log('error', {message: `${e}`}); } if (topic) { @@ -225,7 +227,7 @@ class Device { const value = this.getMappedValue(val, actType, false); cp.state = {instance, value: convertToYandexValue(value, actType)}; } catch(e) { - global.logger.log('error', {message: `${e}`}); + logger.log('error', {message: `${e}`}); } } }