AutoCatBackend/data_providers/vin01.js

52 lines
1.4 KiB
JavaScript

const fetch = require('node-fetch');
const Vehicle = require('../models/vehicle');
const baseUrl = 'https://vin01.ru/v2';
const reportBaseUrl = 'https://vin01.ru/api/gibddApp.php';
class Vin01Provider {
static async getVin(number, token) {
let url = `${baseUrl}/getVin.php?key=${token}&gosNumber=${encodeURIComponent(number)}`;
let result = await fetch(url);
let json = await result.json();
if(json.success && json.code == 200) {
return json.data.vin;
} else {
throw Error('Vin01 provider failed to get VIN');
}
}
static async getReport(number, token) {
let vin = await Vin01Provider.getVin(number, token);
console.log('vin01 found VIN: ', vin);
let result = await fetch(reportBaseUrl, {
method: 'POST',
body: new URLSearchParams({
typeCheckValue: 'history',
vinValue: vin,
key: token
})
});
let json = await result.json();
if(json.status == 200) {
if(json.data.status != 200) {
console.log('Vin01 failed to get GIBDD report with status: ', json.data.status, ', and error: ', json.data.message);
let vehicle = new Vehicle();
vehicle.vin1 = vin;
return vehicle;
}
console.log('vin01 found history');
let vehicle = Vehicle.fromVin01(json.data);
vehicle.number = number;
return vehicle;
} else {
let vehicle = new Vehicle();
vehicle.vin1 = vin;
return vehicle;
//throw Error('Vin01 provider failed to get gibdd info');
}
}
}
module.exports = Vin01Provider;