From 0194f3472bbe37ec31475fbaaacd01e961203bbc Mon Sep 17 00:00:00 2001 From: Selim Mustafaev Date: Sat, 30 Jan 2021 21:56:46 +0300 Subject: [PATCH] Add warnings to debugInfo --- data_providers/avtocod.js | 9 ++------- data_providers/nomerogram.js | 3 ++- data_providers/vin01.js | 23 ++++++++++++----------- models/DebugInfo.js | 32 ++++++++++++++++++++++++++++++++ models/vehicle.js | 25 ++++--------------------- routes/vehicles.js | 19 ++++++++++++++----- 6 files changed, 66 insertions(+), 45 deletions(-) create mode 100644 models/DebugInfo.js diff --git a/data_providers/avtocod.js b/data_providers/avtocod.js index 02c2e43..7d3042a 100644 --- a/data_providers/avtocod.js +++ b/data_providers/avtocod.js @@ -2,7 +2,7 @@ const crypto = require('crypto'); const fetch = require('node-fetch'); const Vehicle = require('../models/vehicle'); const PubNub = require('pubnub'); -const { SSL_OP_SSLEAY_080_CLIENT_DH_BUG } = require('constants'); +const DebugInfo = require('../models/DebugInfo'); const baseUrl = 'https://avtocod.ru/api/v3'; let deviceToken = crypto.createHash('sha256').update(Date.now().toString()).digest().toString('hex'); @@ -99,12 +99,7 @@ class AvtocodProvider { console.log('Avtocod found vehicle: ', vehicle?.brand?.name?.original); return vehicle; } catch(ex) { - ex.debugInfo = { - autocod: { - fields: 0, - error: ex.message - } - } + ex.debugInfo = { autocod: DebugInfo.fromError(ex.message) }; throw ex; } } diff --git a/data_providers/nomerogram.js b/data_providers/nomerogram.js index adef3c1..2839a08 100644 --- a/data_providers/nomerogram.js +++ b/data_providers/nomerogram.js @@ -2,6 +2,7 @@ const crypto = require('crypto'); const fetch = require('node-fetch'); const Utils = require('../utils'); const utf8 = require('utf8'); +const DebugInfo = require('../models/DebugInfo'); const baseUrl = 'https://www.nomerogram.ru/api/v1.1'; @@ -51,7 +52,7 @@ class NomerogramProvider { }); } catch(ex) { console.log('Nomerogram error: ', ex.message); - ex.debugInfo = { nomerogram: { fields: 0, error: ex.message } }; + ex.debugInfo = { nomerogram: DebugInfo.fromError(ex.message) }; throw ex; } } diff --git a/data_providers/vin01.js b/data_providers/vin01.js index 8ebabd6..fca1540 100644 --- a/data_providers/vin01.js +++ b/data_providers/vin01.js @@ -1,4 +1,5 @@ const fetch = require('node-fetch'); +const DebugInfo = require('../models/DebugInfo'); const Vehicle = require('../models/vehicle'); const Utils = require('../utils'); @@ -67,9 +68,9 @@ class Vin01Provider { let vehicle = new Vehicle(); vehicle.vin1 = Utils.cyrillicToLatin(vin); vehicle.debugInfo = { - vin01vin: { fields: 0, error: null }, - vin01history: { fields: 0, error: history.reason.message }, - vin01base: { fields: 0, error: base.reason.message } + vin01vin: new DebugInfo(), + vin01history: DebugInfo.fromError(history.reason.message), + vin01base: DebugInfo.fromError(base.reason.message) }; return vehicle; } else if(base.status == 'rejected') { @@ -77,8 +78,8 @@ class Vin01Provider { let vehicle = Vehicle.fromVin01History(history.value); vehicle.number = number; Object.assign(vehicle.debugInfo, { - vin01vin: { fields: 0, error: null }, - vin01base: { fields: 0, error: base.reason.message } + vin01vin: new DebugInfo(), + vin01base: DebugInfo.fromError(base.reason.message) }); return vehicle; } else if(history.status == 'rejected') { @@ -86,8 +87,8 @@ class Vin01Provider { let vehicle = Vehicle.fromVin01Base(base.value); vehicle.number = number; Object.assign(vehicle.debugInfo, { - vin01vin: { fields: 0, error: null }, - vin01history: { fields: 0, error: history.reason.message } + vin01vin: new DebugInfo(), + vin01history: DebugInfo.fromError(history.reason.message) }); return vehicle; } else { @@ -123,7 +124,7 @@ class Vin01Provider { } } - Object.assign(historyVehicle.debugInfo, { vin01vin: { fields: 0, error: null } }); + Object.assign(historyVehicle.debugInfo, { vin01vin: new DebugInfo() }); Object.assign(historyVehicle.debugInfo, baseVehicle.debugInfo); historyVehicle.number = number; @@ -131,9 +132,9 @@ class Vin01Provider { } } catch(ex) { ex.debugInfo = { - vin01vin: { fields: 0, error: ex.message }, - vin01history: { fields: 0, error: 'Not applicable (VIN was not found)' }, - vin01base: { fields: 0, error: 'Not applicable (VIN was not found)' } + vin01vin: DebugInfo.fromError(ex.message), + vin01history: DebugInfo.fromError('Not applicable (VIN was not found)'), + vin01base: DebugInfo.fromError('Not applicable (VIN was not found)') }; throw ex; } diff --git a/models/DebugInfo.js b/models/DebugInfo.js new file mode 100644 index 0000000..e188fc8 --- /dev/null +++ b/models/DebugInfo.js @@ -0,0 +1,32 @@ +class DebugInfo { + status + fields + error + + constructor() { + this.status = 0; + this.fields = 0; + this.error = null; + } + + static fromError(error) { + let di = new DebugInfo(); + di.status = 1; + di.error = error; + return di; + } + + static fromWarning(warning) { + let di = new DebugInfo(); + di.status = 2; + di.error = warning; + return di; + } + + setWarning(warning) { + this.status = 2; + this.error = warning; + } +} + +module.exports = DebugInfo; \ No newline at end of file diff --git a/models/vehicle.js b/models/vehicle.js index 913f88e..8117b4f 100644 --- a/models/vehicle.js +++ b/models/vehicle.js @@ -1,5 +1,6 @@ const Constants = require('../data_providers/constants'); const Utils = require('../utils'); +const DebugInfo = require('./DebugInfo'); class Vehicle { brand @@ -61,13 +62,7 @@ class Vehicle { v.addedDate = Date.now(); v.updatedDate = v.addedDate; v.events = []; - - v.debugInfo = { - autocod: { - fields: 0, - error: null - } - }; + v.debugInfo = { autocod: new DebugInfo() }; return v; } @@ -105,13 +100,7 @@ class Vehicle { v.addedDate = Date.now(); v.updatedDate = v.addedDate; v.events = []; - - v.debugInfo = { - vin01history: { - fields: 0, - error: null - } - }; + v.debugInfo = { vin01history: new DebugInfo() }; return v; } @@ -167,13 +156,7 @@ class Vehicle { v.addedDate = Date.now(); v.updatedDate = v.addedDate; v.events = []; - - v.debugInfo = { - vin01base: { - fields: 0, - error: null - } - }; + v.debugInfo = { vin01base: new DebugInfo() }; return v; } diff --git a/routes/vehicles.js b/routes/vehicles.js index 1a7ac82..cb1967d 100644 --- a/routes/vehicles.js +++ b/routes/vehicles.js @@ -9,6 +9,7 @@ const RsaProvider = require('../data_providers/rsa'); const NomerogramProvider = require('../data_providers/nomerogram'); const Utils = require('../utils'); const ObjectId = require('mongodb').ObjectID; +const DebugInfo = require('../models/DebugInfo'); const makeError = error => ({ success: false, error }); @@ -39,9 +40,9 @@ router.post('/check', async (req, res) => { } else { all.push(Promise.reject({ debugInfo: { - vin01vin: { fields: 0, error: 'No auth token' }, - vin01history: { fields: 0, error: 'No auth token' }, - vin01base: { fields: 0, error: 'No auth token' } + vin01vin: DebugInfo.fromError('No auth token'), + vin01history: DebugInfo.fromError('No auth token'), + vin01base: DebugInfo.fromError('No auth token') } })); } @@ -64,18 +65,26 @@ router.post('/check', async (req, res) => { } } else { vehicle = autocod.value; - if(vehicle.vin1 && vin01.value.vin1.match(RegExp(vehicle.vin1.replace(/\*/g, '.')))) { + let vinMatch = vin01.value.vin1.match(RegExp(vehicle.vin1.replace(/\*/g, '.'))); + if(vehicle.vin1 && vinMatch) { vehicle.vin1 = vin01.value.vin1; vehicle.vin2 = vin01.value.vin2; vehicle.color = vin01.value.color; vehicle.ownershipPeriods = vin01.value.ownershipPeriods; + } else if(!vinMatch) { + let vin01data = `${vin01.value.vin1} - ${vin01.value.brand.name.original}`; + let autocodData = `${autocod.value.vin1} - ${autocod.value.brand.name.original}`; + let warning = `Vin01 VIN (${vin01data}) doesn't match with avtocod's one (${autocodData})`; + vin01.value.debugInfo.vin01vin.setWarning(warning); + vin01.value.debugInfo.vin01history.setWarning(warning); + vin01.value.debugInfo.vin01base.setWarning(warning); } Object.assign(vehicle.debugInfo, vin01.value.debugInfo); } if(nomerogram.status == 'fulfilled') { vehicle.ads = nomerogram.value; - Object.assign(vehicle.debugInfo, { nomerogram: { fields: 0, error: null } }); + Object.assign(vehicle.debugInfo, { nomerogram: new DebugInfo() }); } else { Object.assign(vehicle.debugInfo, nomerogram.reason.debugInfo); }