Add some filters to search API

This commit is contained in:
Selim Mustafaev 2020-06-18 17:42:51 +03:00
parent 2875f5a66c
commit 5bae041381
3 changed files with 55 additions and 15 deletions

View File

@ -77,7 +77,6 @@ class AvtocodProvider {
let url = `${baseUrl}/auto/generate?number=${encodeURIComponent(number)}&device_token=${deviceToken}`;
let resp = await getJson(url);
console.log('URL: ', resp.report_uri);
let html = await getPage(resp.report_uri);
let result = html.match(/<meta name="app-version-hash" content="(.*?)" \/>/);
if(result == null) {

View File

@ -18,6 +18,7 @@ class Vin01Provider {
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({
@ -29,6 +30,7 @@ class Vin01Provider {
let json = await result.json();
if(json.status == 200) {
console.log('vin01 found history');
let vehicle = Vehicle.fromVin01(json.data);
vehicle.number = number;
return vehicle;

View File

@ -13,9 +13,12 @@ router.post('/check', async (req, res) => {
const forceUpdate = req.body.forceUpdate.toLowerCase() == 'true';
const { login } = req.user;
console.log(`=== checking number: ${number} ====================================`);
let collection = req.db.collection('vehicles');
let vehicles = await collection.find({ number }).toArray();
if(vehicles.length > 0 && !forceUpdate) {
console.log('vehicle found in database');
res.send({ success: true, data: vehicles[0] });
} else {
try {
@ -29,12 +32,15 @@ router.post('/check', async (req, res) => {
let [autocod, vin01] = await Promise.allSettled(all);
if(autocod.status == 'fulfilled') {
let vehicle = autocod.value;
console.log('autocod found vehicle: ', vehicle?.brand?.name?.original);
vehicle.addedBy = login;
if(vin01?.status == 'fulfilled' && vin01.value?.brand?.name?.original == vehicle?.brand?.name?.original) {
if(vin01?.status == 'fulfilled') {
vehicle.vin1 = vin01.value.vin1;
vehicle.vin2 = vin01.value.vin2;
vehicle.color = vin01.value.color;
vehicle.ownershipPeriods = vin01.value.ownershipPeriods;
if(vin01.value?.brand?.name?.original == vehicle?.brand?.name?.original) {
vehicle.vin2 = vin01.value.vin2;
vehicle.color = vin01.value.color;
vehicle.ownershipPeriods = vin01.value.ownershipPeriods;
}
}
await collection.replaceOne({ number }, vehicle, { upsert: true });
res.status(201).send({ success: true, data: vehicle });
@ -49,20 +55,19 @@ router.post('/check', async (req, res) => {
});
router.get('/', async (req, res) => {
const { limit, query } = req.query;
const { limit, query, brand, model, color } = req.query;
console.log('limit:', limit);
console.log('query:', query);
let findQuery = {};
if(query) {
findQuery.number = RegExp(query);
}
let findQuery = {
number: query,
'brand.name.normalized': brand,
'model.name.normalized': model,
color
};
let findQueryFiltered = Object.fromEntries(Object.entries(findQuery).filter(([,val]) => val ));
try {
let collection = req.db.collection('vehicles');
let vehicles = await collection.find(findQuery).sort({ addedDate: -1 }).limit(parseInt(limit)).toArray();
let vehicles = await collection.find(findQueryFiltered).sort({ addedDate: -1 }).limit(parseInt(limit ?? 0)).toArray();
res.send({ success: true, data: vehicles });
} catch(ex) {
res.send(makeError('Error reading vehicles from DB'));
@ -70,6 +75,40 @@ router.get('/', async (req, res) => {
}
});
router.get('/brands', async (req, res) => {
try {
let collection = req.db.collection('vehicles');
let brands = await collection.distinct('brand.name.normalized');
res.send({ success: true, data: brands });
} catch(ex) {
res.send(makeError('Error reading vehicle brands from DB'));
console.error(ex);
}
});
router.get('/models', async (req, res) => {
try {
const { brand } = req.query;
let collection = req.db.collection('vehicles');
let models = await collection.distinct('model.name.normalized', { 'brand.name.normalized': brand });
res.send({ success: true, data: models });
} catch(ex) {
res.send(makeError('Error reading vehicle models from DB'));
console.error(ex);
}
});
router.get('/colors', async (req, res) => {
try {
let collection = req.db.collection('vehicles');
let colors = await collection.distinct('color');
res.send({ success: true, data: colors });
} catch(ex) {
res.send(makeError('Error reading vehicle colors from DB'));
console.error(ex);
}
});
router.get('/shared_report', cors({ origin: 'https://auto.aliencat.pro' }), async (req, res) => {
try {
let { plateNumber } = jwt.verify(req.query.token, '#TheTruthIsOutThere');