-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
80 lines (73 loc) · 3.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import DefaultFipeSDK from './default_fipe_sdk'
export class FipeSDK extends DefaultFipeSDK {
/**
* Verifies if the desired brand is available
* by FIPE's table
* @param {String} typeOfVehicle The vehicle's
* type: 'car', 'truck' or 'motor'
* @param {String} brandName The vehicle's
* brand name
*/
isBrandSupported(typeOfVehicle, brandName) {
return new Promise( (resolve, reject) => {
this.fetchAvailableBrands(typeOfVehicle)
.then( response => resolve(response.map( item => item.brand.toLowerCase() ).includes(brandName)) )
.catch( err => reject(err) )
})
}
/**
* Find all models by brand name supported
* by FIPE's table
* @param {String} typeOfVehicle The vehicle's
* type: 'car', 'truck' or 'motor'
* @param {String} brandName The vehicle's
* brand name
*/
findModelsByBrand(typeOfVehicle, brandName) {
return new Promise( async (resolve, reject) => {
const brand = await this.fetchAvailableBrands(typeOfVehicle)
.then( response => response.filter( item => item.brand.toLowerCase() === brandName.toLocaleLowerCase())[0] )
.catch( err => reject(err))
const models = await this.fetchAvailableModelsByCode(typeOfVehicle, brand.code)
.catch( err => reject(err))
resolve({ models, brandCode: brand.code })
})
}
/**
* Find all years by brand and model name
* @param {String} typeOfVehicle The vehicle's
* type: 'car', 'truck' or 'motor'
* @param {String} brandName The vehicle's
* brand name
* @param {*} modelName The vehicle's model
* name
*/
findYearsByBrandAndModel(typeOfVehicle, brandName, modelName) {
return new Promise( async (resolve, reject) => {
const model = await this.findModelsByBrand(typeOfVehicle, brandName)
.then( response => {
const selectedModel = response.models
.filter( item => item.model.toLowerCase() === modelName.toLocaleLowerCase())[0]
return { brandCode: response.brandCode, model: selectedModel.model, code: selectedModel.code }
})
.catch( err => reject(err))
const years = await this.fetchAvailableYearsByCode(typeOfVehicle, model.brandCode, model.code)
.then(years => resolve({ brandCode: model.brandCode, modelCode: model.code, years }))
.catch( err => reject(err))
})
}
estimatePrice(typeOfVehicle, brandName, modelName, yearModel, fuelCode) {
return new Promise( async (resolve, reject) => {
const model = await this.findModelsByBrand(typeOfVehicle, brandName)
.then( response => {
const selectedModel = response.models
.filter( item => item.model.toLowerCase() === modelName.toLocaleLowerCase())[0]
return { brandCode: response.brandCode, model: selectedModel.model, code: selectedModel.code }
})
.catch( err => reject(err))
const fipeEstimate = await this.fetchPriceByCode(typeOfVehicle, model.brandCode, model.code, yearModel, fuelCode)
.then( response => resolve(response) )
.catch( err => reject(err) )
})
}
}