Repository navigation
Expand file tree
/
Copy pathtools.js
More file actions
145 lines (135 loc) · 5.19 KB
/
Copy pathtools.js
File metadata and controls
145 lines (135 loc) · 5.19 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* carAgent tools — 6 tools for used-car dealers and buyers.
* All data sources are free public APIs (NHTSA); no keys required.
*/
const NHTSA_VIN = 'https://vpic.nhtsa.dot.gov/api/vehicles/decodevin';
const NHTSA_RECALLS = 'https://api.nhtsa.gov/recalls/recallsByVin';
/** 1. vin_decode — decode VIN via NHTSA VPIC (free). */
export async function vinDecode(vin) {
const res = await fetch(`${NHTSA_VIN}/${encodeURIComponent(vin)}?format=json`);
const data = await res.json();
const results = (data.Results || []).filter((r) => r.Value && r.Value !== '0' && r.Value !== 'Not Applicable');
const pick = (name) => (results.find((r) => r.Variable === name) || {}).Value || '';
return {
vin,
year: pick('Model Year'),
make: pick('Make'),
model: pick('Model'),
trim: pick('Trim'),
bodyClass: pick('Body Class'),
driveType: pick('Drive Type'),
fuelType: pick('Fuel Type - Primary'),
};
}
/** 2. check_recalls — recall campaigns by VIN. */
export async function checkRecalls(vin) {
const res = await fetch(`${NHTSA_RECALLS}?vin=${encodeURIComponent(vin)}`);
const data = await res.json();
const campaigns = (data.results || []).map((c) => ({
campaignNumber: c.NHTSACampaignNumber || c.CampaignNumber || '',
component: c.Component || '',
summary: (c.Summary || '').slice(0, 200),
consequence: (c.Consequence || '').slice(0, 200),
}));
return { vin, count: campaigns.length, campaigns };
}
/** 3. inspect_listing — extract structured fields from a listing text. */
export function inspectListing(text) {
const s = String(text);
const year = s.match(/\b(19|20)\d{2}\b/);
// $ first; otherwise a 4-7 digit number that is not a year and not mileage
const price =
s.match(/\$\s?(\d{1,3}(?:,\d{3}){1,2})/) ||
s.match(/\b(\d{1,3}(?:,\d{3}){1,2})\b(?!\s?(?:mi|miles|km|kms))/);
const mileage = s.match(/([\d,]{2,7})\s?(mi|miles|km|kms)\b/i);
const cleanTitle = s.match(/\bclean title\b/i);
const accident = s.match(/\b(accident|salvage|rebuilt|flood)\b/i);
return {
year: year ? Number(year[0]) : null,
priceUsd: price ? Number(price[1].replace(/,/g, '')) : null,
mileage: mileage ? Number(mileage[1].replace(/,/g, '')) : null,
mileageUnit: mileage ? mileage[2] : null,
cleanTitle: Boolean(cleanTitle),
flags: accident ? [accident[1].toLowerCase()] : [],
};
}
/** 4. compare_quotes — quote comparison: min / avg / suggested offer. */
export function compareQuotes(quotes) {
if (!Array.isArray(quotes) || quotes.length === 0) {
return { ok: false, reason: 'no quotes' };
}
const prices = quotes.map((q) => q.price).filter((p) => Number.isFinite(p));
if (prices.length === 0) return { ok: false, reason: 'no valid prices' };
const min = Math.min(...prices);
const avg = prices.reduce((a, b) => a + b, 0) / prices.length;
return {
ok: true,
count: prices.length,
min,
avg: Math.round(avg),
suggestedOffer: Math.round(min * 0.97), // 3% below best comparable
spreadPct: Math.round(((Math.max(...prices) - min) / min) * 100),
};
}
/** Simplified state sales tax rates (percent). */
const STATE_TAX = {
TX: 6.25, CA: 7.25, NY: 4.0, FL: 6.0, WA: 6.5, CO: 2.9,
AZ: 5.6, NV: 6.85, OR: 0, IL: 6.25, GA: 4.0, OH: 5.75,
};
/** 5. state_cost_calculator — out-the-door estimate. */
export function stateCostCalculator(price, state, options = {}) {
const st = String(state || '').toUpperCase();
const taxRate = STATE_TAX[st];
if (taxRate === undefined) {
return { ok: false, reason: `unknown state: ${st}` };
}
const docFee = options.docFee ?? 150;
const titleFee = options.titleFee ?? 33;
const salesTax = Math.round(price * (taxRate / 100));
const total = price + salesTax + docFee + titleFee;
return {
ok: true,
state: st,
taxRate,
salesTax,
docFee,
titleFee,
total,
outTheDoor: total,
};
}
/** Common repair items with rough parts+labor estimates (USD). */
const REPAIR_BASE = {
brake_pads: 300, brake_rotors: 500, oil_change: 80,
battery: 200, alternator: 600, starter: 450, tires_set: 800,
spark_plugs: 250, ac_recharge: 220, wheel_alignment: 120,
timing_belt: 900, water_pump: 650,
};
/** 6. estimate_repairs — rough repair estimate from item list. */
export function estimateRepairs(items, options = {}) {
const list = Array.isArray(items) ? items : [];
const laborRate = options.laborRate ?? 120; // per hour
const breakdown = list.map((item) => {
const key = String(item.item || '').toLowerCase().replace(/\s+/g, '_');
const base = REPAIR_BASE[key];
if (base === undefined) {
return { item: item.item, known: false, estimate: null };
}
const laborHrs = options.laborHrs?.[key] ?? Math.max(1, Math.round((base * 0.4) / laborRate));
return {
item: item.item,
known: true,
parts: Math.round(base * 0.6),
labor: laborHrs * laborRate,
laborHrs,
estimate: Math.round(base * 0.6 + laborHrs * laborRate),
};
});
const known = breakdown.filter((b) => b.known);
const total = known.reduce((a, b) => a + b.estimate, 0);
return { ok: true, breakdown, knownItems: known.length, total };
}
export default {
vinDecode, checkRecalls, inspectListing,
compareQuotes, stateCostCalculator, estimateRepairs,
};