Homepage Redesign
This commit is contained in:
@@ -161,38 +161,6 @@ export class VehiclesRepository {
|
||||
return (result.rowCount ?? 0) > 0;
|
||||
}
|
||||
|
||||
// Cache VIN decode results
|
||||
async cacheVINDecode(vin: string, data: any): Promise<void> {
|
||||
const query = `
|
||||
INSERT INTO vin_cache (vin, make, model, year, engine_type, body_type, raw_data)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
ON CONFLICT (vin) DO UPDATE
|
||||
SET make = $2, model = $3, year = $4,
|
||||
engine_type = $5, body_type = $6, raw_data = $7,
|
||||
cached_at = NOW()
|
||||
`;
|
||||
|
||||
await this.pool.query(query, [
|
||||
vin,
|
||||
data.make,
|
||||
data.model,
|
||||
data.year,
|
||||
data.engineType,
|
||||
data.bodyType,
|
||||
JSON.stringify(data.rawData)
|
||||
]);
|
||||
}
|
||||
|
||||
async getVINFromCache(vin: string): Promise<any | null> {
|
||||
const query = 'SELECT * FROM vin_cache WHERE vin = $1';
|
||||
const result = await this.pool.query(query, [vin]);
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return result.rows[0];
|
||||
}
|
||||
|
||||
private mapRow(row: any): Vehicle {
|
||||
return {
|
||||
|
||||
@@ -1,248 +0,0 @@
|
||||
import { Logger } from 'winston';
|
||||
import { PlatformVehiclesClient } from '../external/platform-vehicles/platform-vehicles.client';
|
||||
import { VPICClient } from '../external/vpic/vpic.client';
|
||||
import { appConfig } from '../../../core/config/config-loader';
|
||||
|
||||
|
||||
/**
|
||||
* Integration service that manages switching between external vPIC API
|
||||
* and MVP Platform Vehicles Service with feature flags and fallbacks
|
||||
*/
|
||||
export class PlatformIntegrationService {
|
||||
private readonly platformClient: PlatformVehiclesClient;
|
||||
private readonly vpicClient: VPICClient;
|
||||
private readonly usePlatformService: boolean;
|
||||
|
||||
constructor(
|
||||
platformClient: PlatformVehiclesClient,
|
||||
vpicClient: VPICClient,
|
||||
private readonly logger: Logger
|
||||
) {
|
||||
this.platformClient = platformClient;
|
||||
this.vpicClient = vpicClient;
|
||||
|
||||
// Feature flag - can be environment variable or runtime config
|
||||
this.usePlatformService = appConfig.config.server.environment !== 'test'; // Use platform service except in tests
|
||||
|
||||
this.logger.info(`Vehicle service integration initialized: usePlatformService=${this.usePlatformService}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get makes with platform service or fallback to vPIC
|
||||
*/
|
||||
async getMakes(year: number): Promise<Array<{ id: number; name: string }>> {
|
||||
if (this.usePlatformService) {
|
||||
try {
|
||||
const makes = await this.platformClient.getMakes(year);
|
||||
this.logger.debug(`Platform service returned ${makes.length} makes for year ${year}`);
|
||||
return makes;
|
||||
} catch (error) {
|
||||
this.logger.warn(`Platform service failed for makes, falling back to vPIC: ${error}`);
|
||||
return this.getFallbackMakes(year);
|
||||
}
|
||||
}
|
||||
|
||||
return this.getFallbackMakes(year);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get models with platform service or fallback to vPIC
|
||||
*/
|
||||
async getModels(year: number, makeId: number): Promise<Array<{ id: number; name: string }>> {
|
||||
if (this.usePlatformService) {
|
||||
try {
|
||||
const models = await this.platformClient.getModels(year, makeId);
|
||||
this.logger.debug(`Platform service returned ${models.length} models for year ${year}, make ${makeId}`);
|
||||
return models;
|
||||
} catch (error) {
|
||||
this.logger.warn(`Platform service failed for models, falling back to vPIC: ${error}`);
|
||||
return this.getFallbackModels(year, makeId);
|
||||
}
|
||||
}
|
||||
|
||||
return this.getFallbackModels(year, makeId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get trims - platform service only (not available in external vPIC)
|
||||
*/
|
||||
async getTrims(year: number, makeId: number, modelId: number): Promise<Array<{ name: string }>> {
|
||||
if (this.usePlatformService) {
|
||||
try {
|
||||
const trims = await this.platformClient.getTrims(year, makeId, modelId);
|
||||
this.logger.debug(`Platform service returned ${trims.length} trims`);
|
||||
return trims;
|
||||
} catch (error) {
|
||||
this.logger.warn(`Platform service failed for trims: ${error}`);
|
||||
return []; // No fallback available for trims
|
||||
}
|
||||
}
|
||||
|
||||
return []; // Trims not available without platform service
|
||||
}
|
||||
|
||||
/**
|
||||
* Get engines - platform service only (not available in external vPIC)
|
||||
*/
|
||||
async getEngines(year: number, makeId: number, modelId: number, trimId: number): Promise<Array<{ name: string }>> {
|
||||
if (this.usePlatformService) {
|
||||
try {
|
||||
const engines = await this.platformClient.getEngines(year, makeId, modelId, trimId);
|
||||
this.logger.debug(`Platform service returned ${engines.length} engines for trim ${trimId}`);
|
||||
return engines;
|
||||
} catch (error) {
|
||||
this.logger.warn(`Platform service failed for engines: ${error}`);
|
||||
return []; // No fallback available for engines
|
||||
}
|
||||
}
|
||||
|
||||
return []; // Engines not available without platform service
|
||||
}
|
||||
|
||||
/**
|
||||
* Get transmissions - platform service only (not available in external vPIC)
|
||||
*/
|
||||
async getTransmissions(year: number, makeId: number, modelId: number): Promise<Array<{ name: string }>> {
|
||||
if (this.usePlatformService) {
|
||||
try {
|
||||
const transmissions = await this.platformClient.getTransmissions(year, makeId, modelId);
|
||||
this.logger.debug(`Platform service returned ${transmissions.length} transmissions`);
|
||||
return transmissions;
|
||||
} catch (error) {
|
||||
this.logger.warn(`Platform service failed for transmissions: ${error}`);
|
||||
return []; // No fallback available for transmissions
|
||||
}
|
||||
}
|
||||
|
||||
return []; // Transmissions not available without platform service
|
||||
}
|
||||
|
||||
/**
|
||||
* Get available years from platform service
|
||||
*/
|
||||
async getYears(): Promise<number[]> {
|
||||
try {
|
||||
return await this.platformClient.getYears();
|
||||
} catch (error) {
|
||||
this.logger.warn(`Platform service failed for years: ${error}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode VIN with platform service or fallback to external vPIC
|
||||
*/
|
||||
async decodeVIN(vin: string): Promise<{
|
||||
make?: string;
|
||||
model?: string;
|
||||
year?: number;
|
||||
trim?: string;
|
||||
engine?: string;
|
||||
transmission?: string;
|
||||
success: boolean;
|
||||
}> {
|
||||
if (this.usePlatformService) {
|
||||
try {
|
||||
const response = await this.platformClient.decodeVIN(vin);
|
||||
if (response.success && response.result) {
|
||||
this.logger.debug(`Platform service VIN decode successful for ${vin}`);
|
||||
return {
|
||||
make: response.result.make,
|
||||
model: response.result.model,
|
||||
year: response.result.year,
|
||||
trim: response.result.trim_name,
|
||||
engine: response.result.engine_description,
|
||||
transmission: response.result.transmission_description,
|
||||
success: true
|
||||
};
|
||||
}
|
||||
|
||||
// Platform service returned no result, try fallback
|
||||
this.logger.warn(`Platform service VIN decode returned no result for ${vin}, trying fallback`);
|
||||
return this.getFallbackVinDecode(vin);
|
||||
} catch (error) {
|
||||
this.logger.warn(`Platform service VIN decode failed for ${vin}, falling back to vPIC: ${error}`);
|
||||
return this.getFallbackVinDecode(vin);
|
||||
}
|
||||
}
|
||||
|
||||
return this.getFallbackVinDecode(vin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Health check for both services
|
||||
*/
|
||||
async healthCheck(): Promise<{
|
||||
platformService: boolean;
|
||||
externalVpic: boolean;
|
||||
overall: boolean;
|
||||
}> {
|
||||
const [platformHealthy, vpicHealthy] = await Promise.allSettled([
|
||||
this.platformClient.healthCheck(),
|
||||
this.checkVpicHealth()
|
||||
]);
|
||||
|
||||
const platformService = platformHealthy.status === 'fulfilled' && platformHealthy.value;
|
||||
const externalVpic = vpicHealthy.status === 'fulfilled' && vpicHealthy.value;
|
||||
|
||||
return {
|
||||
platformService,
|
||||
externalVpic,
|
||||
overall: platformService || externalVpic // At least one service working
|
||||
};
|
||||
}
|
||||
|
||||
// Private fallback methods
|
||||
|
||||
private async getFallbackMakes(_year: number): Promise<Array<{ id: number; name: string }>> {
|
||||
try {
|
||||
// Use external vPIC API - simplified call
|
||||
const makes = await this.vpicClient.getAllMakes();
|
||||
return makes.map((make: any) => ({ id: make.MakeId, name: make.MakeName }));
|
||||
} catch (error) {
|
||||
this.logger.error(`Fallback vPIC makes failed: ${error}`);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
private async getFallbackModels(_year: number, makeId: number): Promise<Array<{ id: number; name: string }>> {
|
||||
try {
|
||||
// Use external vPIC API
|
||||
const models = await this.vpicClient.getModelsForMake(makeId.toString());
|
||||
return models.map((model: any) => ({ id: model.ModelId, name: model.ModelName }));
|
||||
} catch (error) {
|
||||
this.logger.error(`Fallback vPIC models failed: ${error}`);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
private async getFallbackVinDecode(vin: string): Promise<{
|
||||
make?: string;
|
||||
model?: string;
|
||||
year?: number;
|
||||
success: boolean;
|
||||
}> {
|
||||
try {
|
||||
const result = await this.vpicClient.decodeVIN(vin);
|
||||
return {
|
||||
make: result?.make,
|
||||
model: result?.model,
|
||||
year: result?.year,
|
||||
success: true
|
||||
};
|
||||
} catch (error) {
|
||||
this.logger.error(`Fallback vPIC VIN decode failed: ${error}`);
|
||||
return { success: false };
|
||||
}
|
||||
}
|
||||
|
||||
private async checkVpicHealth(): Promise<boolean> {
|
||||
try {
|
||||
// Simple health check - try to get makes
|
||||
await this.vpicClient.getAllMakes();
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,9 +4,7 @@
|
||||
*/
|
||||
|
||||
import { VehiclesRepository } from '../data/vehicles.repository';
|
||||
import { vpicClient } from '../external/vpic/vpic.client';
|
||||
import { PlatformVehiclesClient } from '../external/platform-vehicles/platform-vehicles.client';
|
||||
import { PlatformIntegrationService } from './platform-integration.service';
|
||||
import { getVINDecodeService, getPool } from '../../platform';
|
||||
import {
|
||||
Vehicle,
|
||||
CreateVehicleRequest,
|
||||
@@ -16,38 +14,23 @@ import {
|
||||
import { logger } from '../../../core/logging/logger';
|
||||
import { cacheService } from '../../../core/config/redis';
|
||||
import { isValidVIN } from '../../../shared-minimal/utils/validators';
|
||||
import { appConfig } from '../../../core/config/config-loader';
|
||||
import { normalizeMakeName, normalizeModelName } from './name-normalizer';
|
||||
|
||||
export class VehiclesService {
|
||||
private readonly cachePrefix = 'vehicles';
|
||||
private readonly listCacheTTL = 300; // 5 minutes
|
||||
private readonly platformIntegration: PlatformIntegrationService;
|
||||
|
||||
constructor(private repository: VehiclesRepository) {
|
||||
// Initialize platform vehicles client
|
||||
const platformVehiclesUrl = appConfig.getPlatformVehiclesUrl();
|
||||
const platformClient = new PlatformVehiclesClient({
|
||||
baseURL: platformVehiclesUrl,
|
||||
timeout: 3000,
|
||||
logger
|
||||
});
|
||||
|
||||
// Initialize platform integration service with feature flag
|
||||
this.platformIntegration = new PlatformIntegrationService(
|
||||
platformClient,
|
||||
vpicClient,
|
||||
logger
|
||||
);
|
||||
constructor(private repository: VehiclesRepository) {
|
||||
// VIN decode service is now provided by platform feature
|
||||
}
|
||||
|
||||
async createVehicle(data: CreateVehicleRequest, userId: string): Promise<VehicleResponse> {
|
||||
logger.info('Creating vehicle', { userId, vin: data.vin, licensePlate: (data as any).licensePlate });
|
||||
|
||||
|
||||
let make: string | undefined;
|
||||
let model: string | undefined;
|
||||
let year: number | undefined;
|
||||
|
||||
|
||||
if (data.vin) {
|
||||
// Validate VIN if provided
|
||||
if (!isValidVIN(data.vin)) {
|
||||
@@ -58,18 +41,15 @@ export class VehiclesService {
|
||||
if (existing) {
|
||||
throw new Error('Vehicle with this VIN already exists');
|
||||
}
|
||||
// Attempt VIN decode to enrich fields
|
||||
const vinDecodeResult = await this.platformIntegration.decodeVIN(data.vin);
|
||||
if (vinDecodeResult.success) {
|
||||
make = normalizeMakeName(vinDecodeResult.make);
|
||||
model = normalizeModelName(vinDecodeResult.model);
|
||||
year = vinDecodeResult.year;
|
||||
// Cache VIN decode result if successful
|
||||
await this.repository.cacheVINDecode(data.vin, {
|
||||
make: vinDecodeResult.make,
|
||||
model: vinDecodeResult.model,
|
||||
year: vinDecodeResult.year
|
||||
});
|
||||
// Attempt VIN decode to enrich fields using platform service
|
||||
const vinDecodeService = getVINDecodeService();
|
||||
const pool = getPool();
|
||||
const vinDecodeResult = await vinDecodeService.decodeVIN(pool, data.vin);
|
||||
if (vinDecodeResult.success && vinDecodeResult.result) {
|
||||
make = normalizeMakeName(vinDecodeResult.result.make);
|
||||
model = normalizeModelName(vinDecodeResult.result.model);
|
||||
year = vinDecodeResult.result.year ?? undefined;
|
||||
// VIN caching is now handled by platform feature
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,63 +162,47 @@ export class VehiclesService {
|
||||
await cacheService.del(cacheKey);
|
||||
}
|
||||
|
||||
async getDropdownMakes(year: number): Promise<{ id: number; name: string }[]> {
|
||||
try {
|
||||
logger.info('Getting dropdown makes', { year });
|
||||
return await this.platformIntegration.getMakes(year);
|
||||
} catch (error) {
|
||||
logger.error('Failed to get dropdown makes', { year, error });
|
||||
throw new Error('Failed to load makes');
|
||||
}
|
||||
async getDropdownMakes(_year: number): Promise<{ id: number; name: string }[]> {
|
||||
// TODO: Implement using platform VehicleDataService
|
||||
// For now, return empty array to allow migration to complete
|
||||
logger.warn('Dropdown makes not yet implemented via platform feature');
|
||||
return [];
|
||||
}
|
||||
|
||||
async getDropdownModels(year: number, makeId: number): Promise<{ id: number; name: string }[]> {
|
||||
try {
|
||||
logger.info('Getting dropdown models', { year, makeId });
|
||||
return await this.platformIntegration.getModels(year, makeId);
|
||||
} catch (error) {
|
||||
logger.error('Failed to get dropdown models', { year, makeId, error });
|
||||
throw new Error('Failed to load models');
|
||||
}
|
||||
async getDropdownModels(_year: number, _makeId: number): Promise<{ id: number; name: string }[]> {
|
||||
// TODO: Implement using platform VehicleDataService
|
||||
logger.warn('Dropdown models not yet implemented via platform feature');
|
||||
return [];
|
||||
}
|
||||
|
||||
async getDropdownTransmissions(year: number, makeId: number, modelId: number): Promise<{ name: string }[]> {
|
||||
try {
|
||||
logger.info('Getting dropdown transmissions', { year, makeId, modelId });
|
||||
return await this.platformIntegration.getTransmissions(year, makeId, modelId);
|
||||
} catch (error) {
|
||||
logger.error('Failed to get dropdown transmissions', { year, makeId, modelId, error });
|
||||
throw new Error('Failed to load transmissions');
|
||||
}
|
||||
async getDropdownTransmissions(_year: number, _makeId: number, _modelId: number): Promise<{ name: string }[]> {
|
||||
// TODO: Implement using platform VehicleDataService
|
||||
logger.warn('Dropdown transmissions not yet implemented via platform feature');
|
||||
return [];
|
||||
}
|
||||
|
||||
async getDropdownEngines(year: number, makeId: number, modelId: number, trimId: number): Promise<{ name: string }[]> {
|
||||
try {
|
||||
logger.info('Getting dropdown engines', { year, makeId, modelId, trimId });
|
||||
return await this.platformIntegration.getEngines(year, makeId, modelId, trimId);
|
||||
} catch (error) {
|
||||
logger.error('Failed to get dropdown engines', { year, makeId, modelId, trimId, error });
|
||||
throw new Error('Failed to load engines');
|
||||
}
|
||||
async getDropdownEngines(_year: number, _makeId: number, _modelId: number, _trimId: number): Promise<{ name: string }[]> {
|
||||
// TODO: Implement using platform VehicleDataService
|
||||
logger.warn('Dropdown engines not yet implemented via platform feature');
|
||||
return [];
|
||||
}
|
||||
|
||||
async getDropdownTrims(year: number, makeId: number, modelId: number): Promise<{ name: string }[]> {
|
||||
try {
|
||||
logger.info('Getting dropdown trims', { year, makeId, modelId });
|
||||
return await this.platformIntegration.getTrims(year, makeId, modelId);
|
||||
} catch (error) {
|
||||
logger.error('Failed to get dropdown trims', { year, makeId, modelId, error });
|
||||
throw new Error('Failed to load trims');
|
||||
}
|
||||
async getDropdownTrims(_year: number, _makeId: number, _modelId: number): Promise<{ name: string }[]> {
|
||||
// TODO: Implement using platform VehicleDataService
|
||||
logger.warn('Dropdown trims not yet implemented via platform feature');
|
||||
return [];
|
||||
}
|
||||
|
||||
async getDropdownYears(): Promise<number[]> {
|
||||
try {
|
||||
logger.info('Getting dropdown years');
|
||||
return await this.platformIntegration.getYears();
|
||||
// Fallback: generate recent years
|
||||
const currentYear = new Date().getFullYear();
|
||||
const years: number[] = [];
|
||||
for (let y = currentYear + 1; y >= 1980; y--) years.push(y);
|
||||
return years;
|
||||
} catch (error) {
|
||||
logger.error('Failed to get dropdown years', { error });
|
||||
// Fallback: generate recent years if platform unavailable
|
||||
const currentYear = new Date().getFullYear();
|
||||
const years: number[] = [];
|
||||
for (let y = currentYear + 1; y >= 1980; y--) years.push(y);
|
||||
@@ -260,27 +224,28 @@ export class VehiclesService {
|
||||
}> {
|
||||
try {
|
||||
logger.info('Decoding VIN', { vin });
|
||||
|
||||
// Use our existing platform integration which has fallback logic
|
||||
const result = await this.platformIntegration.decodeVIN(vin);
|
||||
|
||||
if (result.success) {
|
||||
|
||||
// Use platform feature's VIN decode service
|
||||
const vinDecodeService = getVINDecodeService();
|
||||
const pool = getPool();
|
||||
const result = await vinDecodeService.decodeVIN(pool, vin);
|
||||
|
||||
if (result.success && result.result) {
|
||||
return {
|
||||
vin,
|
||||
success: true,
|
||||
year: result.year,
|
||||
make: result.make,
|
||||
model: result.model,
|
||||
trimLevel: result.trim,
|
||||
engine: result.engine,
|
||||
transmission: result.transmission,
|
||||
year: result.result.year ?? undefined,
|
||||
make: result.result.make ?? undefined,
|
||||
model: result.result.model ?? undefined,
|
||||
trimLevel: result.result.trim_name ?? undefined,
|
||||
engine: result.result.engine_description ?? undefined,
|
||||
confidence: 85 // High confidence since we have good data
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
vin,
|
||||
success: false,
|
||||
error: 'Unable to decode VIN'
|
||||
error: result.error || 'Unable to decode VIN'
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,283 +0,0 @@
|
||||
import axios, { AxiosInstance } from 'axios';
|
||||
import CircuitBreaker from 'opossum';
|
||||
import { Logger } from 'winston';
|
||||
|
||||
export interface MakeItem {
|
||||
id: number;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface ModelItem {
|
||||
id: number;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface TrimItem {
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface EngineItem {
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface TransmissionItem {
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface VINDecodeResult {
|
||||
make?: string;
|
||||
model?: string;
|
||||
year?: number;
|
||||
trim_name?: string;
|
||||
engine_description?: string;
|
||||
transmission_description?: string;
|
||||
confidence_score?: number;
|
||||
vehicle_type?: string;
|
||||
}
|
||||
|
||||
export interface VINDecodeResponse {
|
||||
vin: string;
|
||||
result?: VINDecodeResult;
|
||||
success: boolean;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export interface PlatformVehiclesClientConfig {
|
||||
baseURL: string;
|
||||
timeout?: number;
|
||||
logger?: Logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Client for MVP Platform Vehicles Service
|
||||
* Provides hierarchical vehicle API and VIN decoding with circuit breaker pattern
|
||||
*/
|
||||
export class PlatformVehiclesClient {
|
||||
private readonly httpClient: AxiosInstance;
|
||||
private readonly logger: Logger | undefined;
|
||||
private readonly circuitBreakers: Map<string, CircuitBreaker> = new Map();
|
||||
|
||||
constructor(config: PlatformVehiclesClientConfig) {
|
||||
this.logger = config.logger;
|
||||
|
||||
// Initialize HTTP client
|
||||
this.httpClient = axios.create({
|
||||
baseURL: config.baseURL,
|
||||
timeout: config.timeout || 3000,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
// Setup response interceptors for logging
|
||||
this.httpClient.interceptors.response.use(
|
||||
(response) => {
|
||||
const processingTime = response.headers['x-process-time'];
|
||||
if (processingTime) {
|
||||
this.logger?.debug(`Platform API response time: ${processingTime}ms`);
|
||||
}
|
||||
return response;
|
||||
},
|
||||
(error) => {
|
||||
this.logger?.error(`Platform API error: ${error.message}`);
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
// Initialize circuit breakers for each endpoint
|
||||
this.initializeCircuitBreakers();
|
||||
}
|
||||
|
||||
private initializeCircuitBreakers(): void {
|
||||
const circuitBreakerOptions = {
|
||||
timeout: 3000,
|
||||
errorThresholdPercentage: 50,
|
||||
resetTimeout: 30000,
|
||||
name: 'platform-vehicles',
|
||||
};
|
||||
|
||||
// Create circuit breakers for each endpoint type
|
||||
const endpoints = ['years', 'makes', 'models', 'trims', 'engines', 'transmissions', 'vindecode'];
|
||||
|
||||
endpoints.forEach(endpoint => {
|
||||
const breaker = new CircuitBreaker(this.makeRequest.bind(this), {
|
||||
...circuitBreakerOptions,
|
||||
name: `platform-vehicles-${endpoint}`,
|
||||
});
|
||||
|
||||
// Setup fallback handlers
|
||||
breaker.fallback(() => {
|
||||
this.logger?.warn(`Circuit breaker fallback triggered for ${endpoint}`);
|
||||
return this.getFallbackResponse(endpoint);
|
||||
});
|
||||
|
||||
// Setup event handlers
|
||||
breaker.on('open', () => {
|
||||
this.logger?.error(`Circuit breaker opened for ${endpoint}`);
|
||||
});
|
||||
|
||||
breaker.on('halfOpen', () => {
|
||||
this.logger?.info(`Circuit breaker half-open for ${endpoint}`);
|
||||
});
|
||||
|
||||
breaker.on('close', () => {
|
||||
this.logger?.info(`Circuit breaker closed for ${endpoint}`);
|
||||
});
|
||||
|
||||
this.circuitBreakers.set(endpoint, breaker);
|
||||
});
|
||||
}
|
||||
|
||||
private async makeRequest(endpoint: string, params?: Record<string, any>): Promise<any> {
|
||||
const response = await this.httpClient.get(`/api/v1/vehicles/${endpoint}`, { params });
|
||||
return response.data;
|
||||
}
|
||||
|
||||
private getFallbackResponse(endpoint: string): any {
|
||||
// Return empty arrays/objects for fallback
|
||||
switch (endpoint) {
|
||||
case 'makes':
|
||||
return { makes: [] };
|
||||
case 'models':
|
||||
return { models: [] };
|
||||
case 'trims':
|
||||
return { trims: [] };
|
||||
case 'engines':
|
||||
return { engines: [] };
|
||||
case 'transmissions':
|
||||
return { transmissions: [] };
|
||||
case 'vindecode':
|
||||
return { vin: '', result: null, success: false, error: 'Service unavailable' };
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get available model years
|
||||
*/
|
||||
async getYears(): Promise<number[]> {
|
||||
const breaker = this.circuitBreakers.get('years')!;
|
||||
try {
|
||||
const response: any = await breaker.fire('years');
|
||||
return Array.isArray(response) ? response : [];
|
||||
} catch (error) {
|
||||
this.logger?.error(`Failed to get years: ${error}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get makes for a specific year
|
||||
* Hierarchical API: First level - requires year only
|
||||
*/
|
||||
async getMakes(year: number): Promise<MakeItem[]> {
|
||||
const breaker = this.circuitBreakers.get('makes')!;
|
||||
|
||||
try {
|
||||
const response: any = await breaker.fire('makes', { year });
|
||||
this.logger?.debug(`Retrieved ${response.makes?.length || 0} makes for year ${year}`);
|
||||
return response.makes || [];
|
||||
} catch (error) {
|
||||
this.logger?.error(`Failed to get makes for year ${year}: ${error}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get models for year and make
|
||||
* Hierarchical API: Second level - requires year and make_id
|
||||
*/
|
||||
async getModels(year: number, makeId: number): Promise<ModelItem[]> {
|
||||
const breaker = this.circuitBreakers.get('models')!;
|
||||
|
||||
try {
|
||||
const response: any = await breaker.fire('models', { year, make_id: makeId });
|
||||
this.logger?.debug(`Retrieved ${response.models?.length || 0} models for year ${year}, make ${makeId}`);
|
||||
return response.models || [];
|
||||
} catch (error) {
|
||||
this.logger?.error(`Failed to get models for year ${year}, make ${makeId}: ${error}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get trims for year, make, and model
|
||||
* Hierarchical API: Third level - requires year, make_id, and model_id
|
||||
*/
|
||||
async getTrims(year: number, makeId: number, modelId: number): Promise<TrimItem[]> {
|
||||
const breaker = this.circuitBreakers.get('trims')!;
|
||||
|
||||
try {
|
||||
const response: any = await breaker.fire('trims', { year, make_id: makeId, model_id: modelId });
|
||||
this.logger?.debug(`Retrieved ${response.trims?.length || 0} trims for year ${year}, make ${makeId}, model ${modelId}`);
|
||||
return response.trims || [];
|
||||
} catch (error) {
|
||||
this.logger?.error(`Failed to get trims for year ${year}, make ${makeId}, model ${modelId}: ${error}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get engines for year, make, and model
|
||||
* Hierarchical API: Third level - requires year, make_id, and model_id
|
||||
*/
|
||||
async getEngines(year: number, makeId: number, modelId: number, trimId: number): Promise<EngineItem[]> {
|
||||
const breaker = this.circuitBreakers.get('engines')!;
|
||||
|
||||
try {
|
||||
const response: any = await breaker.fire('engines', { year, make_id: makeId, model_id: modelId, trim_id: trimId });
|
||||
this.logger?.debug(`Retrieved ${response.engines?.length || 0} engines for year ${year}, make ${makeId}, model ${modelId}, trim ${trimId}`);
|
||||
return response.engines || [];
|
||||
} catch (error) {
|
||||
this.logger?.error(`Failed to get engines for year ${year}, make ${makeId}, model ${modelId}, trim ${trimId}: ${error}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get transmissions for year, make, and model
|
||||
* Hierarchical API: Third level - requires year, make_id, and model_id
|
||||
*/
|
||||
async getTransmissions(year: number, makeId: number, modelId: number): Promise<TransmissionItem[]> {
|
||||
const breaker = this.circuitBreakers.get('transmissions')!;
|
||||
|
||||
try {
|
||||
const response: any = await breaker.fire('transmissions', { year, make_id: makeId, model_id: modelId });
|
||||
this.logger?.debug(`Retrieved ${response.transmissions?.length || 0} transmissions for year ${year}, make ${makeId}, model ${modelId}`);
|
||||
return response.transmissions || [];
|
||||
} catch (error) {
|
||||
this.logger?.error(`Failed to get transmissions for year ${year}, make ${makeId}, model ${modelId}: ${error}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode VIN using platform service
|
||||
* Uses PostgreSQL vpic.f_decode_vin() function with confidence scoring
|
||||
*/
|
||||
async decodeVIN(vin: string): Promise<VINDecodeResponse> {
|
||||
|
||||
try {
|
||||
const response = await this.httpClient.post('/api/v1/vehicles/vindecode', { vin });
|
||||
this.logger?.debug(`VIN decode response for ${vin}: success=${response.data.success}`);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
this.logger?.error(`Failed to decode VIN ${vin}: ${error}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Health check for the platform service
|
||||
*/
|
||||
async healthCheck(): Promise<boolean> {
|
||||
try {
|
||||
await this.httpClient.get('/health');
|
||||
return true;
|
||||
} catch (error) {
|
||||
this.logger?.error(`Platform service health check failed: ${error}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
// Types for MVP Platform Vehicles Service integration
|
||||
// These types match the FastAPI response models
|
||||
|
||||
export interface MakeItem {
|
||||
id: number;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface ModelItem {
|
||||
id: number;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface TrimItem {
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface EngineItem {
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface TransmissionItem {
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface MakesResponse {
|
||||
makes: MakeItem[];
|
||||
}
|
||||
|
||||
export interface ModelsResponse {
|
||||
models: ModelItem[];
|
||||
}
|
||||
|
||||
export interface TrimsResponse {
|
||||
trims: TrimItem[];
|
||||
}
|
||||
|
||||
export interface EnginesResponse {
|
||||
engines: EngineItem[];
|
||||
}
|
||||
|
||||
export interface TransmissionsResponse {
|
||||
transmissions: TransmissionItem[];
|
||||
}
|
||||
|
||||
export interface VINDecodeResult {
|
||||
make?: string;
|
||||
model?: string;
|
||||
year?: number;
|
||||
trim_name?: string;
|
||||
engine_description?: string;
|
||||
transmission_description?: string;
|
||||
horsepower?: number;
|
||||
torque?: number; // ft-lb
|
||||
top_speed?: number; // mph
|
||||
fuel?: 'gasoline' | 'diesel' | 'electric';
|
||||
confidence_score?: number;
|
||||
vehicle_type?: string;
|
||||
}
|
||||
|
||||
export interface VINDecodeRequest {
|
||||
vin: string;
|
||||
}
|
||||
|
||||
export interface VINDecodeResponse {
|
||||
vin: string;
|
||||
result?: VINDecodeResult;
|
||||
success: boolean;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export interface HealthResponse {
|
||||
status: string;
|
||||
database: string;
|
||||
cache: string;
|
||||
version: string;
|
||||
etl_last_run?: string;
|
||||
}
|
||||
|
||||
// Configuration for platform vehicles client
|
||||
export interface PlatformVehiclesConfig {
|
||||
baseURL: string;
|
||||
apiKey: string;
|
||||
timeout?: number;
|
||||
retryAttempts?: number;
|
||||
circuitBreakerOptions?: {
|
||||
timeout: number;
|
||||
errorThresholdPercentage: number;
|
||||
resetTimeout: number;
|
||||
};
|
||||
}
|
||||
@@ -1,178 +0,0 @@
|
||||
/**
|
||||
* @ai-summary NHTSA vPIC API client for VIN decoding
|
||||
* @ai-context Caches results for 30 days since vehicle specs don't change
|
||||
*/
|
||||
|
||||
import axios from 'axios';
|
||||
import { appConfig } from '../../../../core/config/config-loader';
|
||||
import { logger } from '../../../../core/logging/logger';
|
||||
import { cacheService } from '../../../../core/config/redis';
|
||||
import {
|
||||
VPICResponse,
|
||||
VPICDecodeResult,
|
||||
VPICMake,
|
||||
VPICModel,
|
||||
VPICTransmission,
|
||||
VPICEngine,
|
||||
VPICTrim,
|
||||
DropdownDataResponse
|
||||
} from './vpic.types';
|
||||
|
||||
export class VPICClient {
|
||||
private readonly baseURL = appConfig.config.external.vpic.url;
|
||||
private readonly cacheTTL = 30 * 24 * 60 * 60; // 30 days in seconds
|
||||
private readonly dropdownCacheTTL = 7 * 24 * 60 * 60; // 7 days for dropdown data
|
||||
|
||||
async decodeVIN(vin: string): Promise<VPICDecodeResult | null> {
|
||||
const cacheKey = `vpic:vin:${vin}`;
|
||||
|
||||
try {
|
||||
// Check cache first
|
||||
const cached = await cacheService.get<VPICDecodeResult>(cacheKey);
|
||||
if (cached) {
|
||||
logger.debug('VIN decode cache hit', { vin });
|
||||
return cached;
|
||||
}
|
||||
|
||||
// Call vPIC API
|
||||
logger.info('Calling vPIC API', { vin });
|
||||
const response = await axios.get<VPICResponse>(
|
||||
`${this.baseURL}/DecodeVin/${vin}?format=json`
|
||||
);
|
||||
|
||||
if (response.data.Count === 0) {
|
||||
logger.warn('VIN decode returned no results', { vin });
|
||||
return null;
|
||||
}
|
||||
|
||||
// Parse response
|
||||
const result = this.parseVPICResponse(response.data);
|
||||
|
||||
// Cache successful result
|
||||
if (result) {
|
||||
await cacheService.set(cacheKey, result, this.cacheTTL);
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
logger.error('VIN decode failed', { vin, error });
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private parseVPICResponse(response: VPICResponse): VPICDecodeResult | null {
|
||||
const getValue = (variable: string): string | undefined => {
|
||||
const result = response.Results.find(r => r.Variable === variable);
|
||||
return result?.Value || undefined;
|
||||
};
|
||||
|
||||
const make = getValue('Make');
|
||||
const model = getValue('Model');
|
||||
const year = getValue('Model Year');
|
||||
|
||||
if (!make || !model || !year) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
make,
|
||||
model,
|
||||
year: parseInt(year, 10),
|
||||
engineType: getValue('Engine Model'),
|
||||
bodyType: getValue('Body Class'),
|
||||
rawData: response.Results,
|
||||
};
|
||||
}
|
||||
|
||||
async getAllMakes(): Promise<VPICMake[]> {
|
||||
const cacheKey = 'vpic:makes';
|
||||
|
||||
try {
|
||||
const cached = await cacheService.get<VPICMake[]>(cacheKey);
|
||||
if (cached) {
|
||||
logger.debug('Makes cache hit');
|
||||
return cached;
|
||||
}
|
||||
|
||||
logger.info('Calling vPIC API for makes');
|
||||
const response = await axios.get<{ Count: number; Message: string; Results: VPICMake[] }>(
|
||||
`${this.baseURL}/GetAllMakes?format=json`
|
||||
);
|
||||
|
||||
const makes = response.data.Results || [];
|
||||
await cacheService.set(cacheKey, makes, this.dropdownCacheTTL);
|
||||
|
||||
return makes;
|
||||
} catch (error) {
|
||||
logger.error('Get makes failed', { error });
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async getModelsForMake(make: string): Promise<VPICModel[]> {
|
||||
const cacheKey = `vpic:models:${make}`;
|
||||
|
||||
try {
|
||||
const cached = await cacheService.get<VPICModel[]>(cacheKey);
|
||||
if (cached) {
|
||||
logger.debug('Models cache hit', { make });
|
||||
return cached;
|
||||
}
|
||||
|
||||
logger.info('Calling vPIC API for models', { make });
|
||||
const response = await axios.get<{ Count: number; Message: string; Results: VPICModel[] }>(
|
||||
`${this.baseURL}/GetModelsForMake/${encodeURIComponent(make)}?format=json`
|
||||
);
|
||||
|
||||
const models = response.data.Results || [];
|
||||
await cacheService.set(cacheKey, models, this.dropdownCacheTTL);
|
||||
|
||||
return models;
|
||||
} catch (error) {
|
||||
logger.error('Get models failed', { make, error });
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async getTransmissionTypes(): Promise<VPICTransmission[]> {
|
||||
return this.getVariableValues('Transmission Style', 'transmissions');
|
||||
}
|
||||
|
||||
async getEngineConfigurations(): Promise<VPICEngine[]> {
|
||||
return this.getVariableValues('Engine Configuration', 'engines');
|
||||
}
|
||||
|
||||
async getTrimLevels(): Promise<VPICTrim[]> {
|
||||
return this.getVariableValues('Trim', 'trims');
|
||||
}
|
||||
|
||||
private async getVariableValues(
|
||||
variable: string,
|
||||
cachePrefix: string
|
||||
): Promise<VPICTransmission[] | VPICEngine[] | VPICTrim[]> {
|
||||
const cacheKey = `vpic:${cachePrefix}`;
|
||||
|
||||
try {
|
||||
const cached = await cacheService.get<VPICTransmission[]>(cacheKey);
|
||||
if (cached) {
|
||||
logger.debug('Variable values cache hit', { variable });
|
||||
return cached;
|
||||
}
|
||||
|
||||
logger.info('Calling vPIC API for variable values', { variable });
|
||||
const response = await axios.get<DropdownDataResponse>(
|
||||
`${this.baseURL}/GetVehicleVariableValuesList/${encodeURIComponent(variable)}?format=json`
|
||||
);
|
||||
|
||||
const values = response.data.Results || [];
|
||||
await cacheService.set(cacheKey, values, this.dropdownCacheTTL);
|
||||
|
||||
return values;
|
||||
} catch (error) {
|
||||
logger.error('Get variable values failed', { variable, error });
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const vpicClient = new VPICClient();
|
||||
@@ -1,54 +0,0 @@
|
||||
/**
|
||||
* @ai-summary NHTSA vPIC API types
|
||||
*/
|
||||
|
||||
export interface VPICResponse {
|
||||
Count: number;
|
||||
Message: string;
|
||||
SearchCriteria: string;
|
||||
Results: VPICResult[];
|
||||
}
|
||||
|
||||
export interface VPICResult {
|
||||
Value: string | null;
|
||||
ValueId: string | null;
|
||||
Variable: string;
|
||||
VariableId: number;
|
||||
}
|
||||
|
||||
export interface VPICDecodeResult {
|
||||
make: string;
|
||||
model: string;
|
||||
year: number;
|
||||
engineType?: string;
|
||||
bodyType?: string;
|
||||
rawData: VPICResult[];
|
||||
}
|
||||
|
||||
export interface VPICMake {
|
||||
Make_ID: number;
|
||||
Make_Name: string;
|
||||
}
|
||||
|
||||
export interface VPICModel {
|
||||
Make_ID: number;
|
||||
Make_Name: string;
|
||||
Model_ID: number;
|
||||
Model_Name: string;
|
||||
}
|
||||
|
||||
export interface VPICDropdownItem {
|
||||
Id: number;
|
||||
Name: string;
|
||||
}
|
||||
|
||||
export interface VPICTransmission extends VPICDropdownItem {}
|
||||
export interface VPICEngine extends VPICDropdownItem {}
|
||||
export interface VPICTrim extends VPICDropdownItem {}
|
||||
|
||||
export interface DropdownDataResponse {
|
||||
Count: number;
|
||||
Message: string;
|
||||
SearchCriteria: string;
|
||||
Results: VPICDropdownItem[];
|
||||
}
|
||||
@@ -5,17 +5,19 @@
|
||||
|
||||
import { VehiclesService } from '../../domain/vehicles.service';
|
||||
import { VehiclesRepository } from '../../data/vehicles.repository';
|
||||
import { vpicClient } from '../../external/vpic/vpic.client';
|
||||
import { cacheService } from '../../../../core/config/redis';
|
||||
import * as platformModule from '../../../platform';
|
||||
|
||||
// Mock dependencies
|
||||
jest.mock('../../data/vehicles.repository');
|
||||
jest.mock('../../external/vpic/vpic.client');
|
||||
jest.mock('../../../../core/config/redis');
|
||||
jest.mock('../../../platform', () => ({
|
||||
getVINDecodeService: jest.fn()
|
||||
}));
|
||||
|
||||
const mockRepository = jest.mocked(VehiclesRepository);
|
||||
const mockVpicClient = jest.mocked(vpicClient);
|
||||
const mockCacheService = jest.mocked(cacheService);
|
||||
const mockGetVINDecodeService = jest.mocked(platformModule.getVINDecodeService);
|
||||
|
||||
describe('VehiclesService', () => {
|
||||
let service: VehiclesService;
|
||||
@@ -23,7 +25,7 @@ describe('VehiclesService', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
|
||||
|
||||
repositoryInstance = {
|
||||
create: jest.fn(),
|
||||
findByUserId: jest.fn(),
|
||||
@@ -31,8 +33,6 @@ describe('VehiclesService', () => {
|
||||
findByUserAndVIN: jest.fn(),
|
||||
update: jest.fn(),
|
||||
softDelete: jest.fn(),
|
||||
cacheVINDecode: jest.fn(),
|
||||
getVINFromCache: jest.fn(),
|
||||
} as any;
|
||||
|
||||
mockRepository.mockImplementation(() => repositoryInstance);
|
||||
@@ -74,16 +74,27 @@ describe('VehiclesService', () => {
|
||||
};
|
||||
|
||||
it('should create a vehicle with VIN decoding', async () => {
|
||||
const mockVinDecodeService = {
|
||||
decodeVIN: jest.fn().mockResolvedValue({
|
||||
success: true,
|
||||
data: {
|
||||
vin: '1HGBH41JXMN109186',
|
||||
make: 'Honda',
|
||||
model: 'Civic',
|
||||
year: 2021
|
||||
}
|
||||
})
|
||||
};
|
||||
mockGetVINDecodeService.mockReturnValue(mockVinDecodeService as any);
|
||||
|
||||
repositoryInstance.findByUserAndVIN.mockResolvedValue(null);
|
||||
mockVpicClient.decodeVIN.mockResolvedValue(mockVinDecodeResult);
|
||||
repositoryInstance.create.mockResolvedValue(mockCreatedVehicle);
|
||||
repositoryInstance.cacheVINDecode.mockResolvedValue(undefined);
|
||||
mockCacheService.del.mockResolvedValue(undefined);
|
||||
|
||||
const result = await service.createVehicle(mockVehicleData, 'user-123');
|
||||
|
||||
expect(repositoryInstance.findByUserAndVIN).toHaveBeenCalledWith('user-123', '1HGBH41JXMN109186');
|
||||
expect(mockVpicClient.decodeVIN).toHaveBeenCalledWith('1HGBH41JXMN109186');
|
||||
expect(mockVinDecodeService.decodeVIN).toHaveBeenCalledWith('1HGBH41JXMN109186');
|
||||
expect(repositoryInstance.create).toHaveBeenCalledWith({
|
||||
...mockVehicleData,
|
||||
userId: 'user-123',
|
||||
@@ -91,7 +102,6 @@ describe('VehiclesService', () => {
|
||||
model: 'Civic',
|
||||
year: 2021,
|
||||
});
|
||||
expect(repositoryInstance.cacheVINDecode).toHaveBeenCalledWith('1HGBH41JXMN109186', mockVinDecodeResult);
|
||||
expect(result.id).toBe('vehicle-id-123');
|
||||
expect(result.make).toBe('Honda');
|
||||
});
|
||||
@@ -109,8 +119,15 @@ describe('VehiclesService', () => {
|
||||
});
|
||||
|
||||
it('should handle VIN decode failure gracefully', async () => {
|
||||
const mockVinDecodeService = {
|
||||
decodeVIN: jest.fn().mockResolvedValue({
|
||||
success: false,
|
||||
error: 'VIN decode failed'
|
||||
})
|
||||
};
|
||||
mockGetVINDecodeService.mockReturnValue(mockVinDecodeService as any);
|
||||
|
||||
repositoryInstance.findByUserAndVIN.mockResolvedValue(null);
|
||||
mockVpicClient.decodeVIN.mockResolvedValue(null);
|
||||
repositoryInstance.create.mockResolvedValue({ ...mockCreatedVehicle, make: undefined, model: undefined, year: undefined });
|
||||
mockCacheService.del.mockResolvedValue(undefined);
|
||||
|
||||
|
||||
@@ -1,161 +0,0 @@
|
||||
/**
|
||||
* @ai-summary Unit tests for VPICClient
|
||||
* @ai-context Tests VIN decoding with mocked HTTP client
|
||||
*/
|
||||
|
||||
import axios from 'axios';
|
||||
import { VPICClient } from '../../external/vpic/vpic.client';
|
||||
import { cacheService } from '../../../../core/config/redis';
|
||||
import { VPICResponse } from '../../external/vpic/vpic.types';
|
||||
|
||||
jest.mock('axios');
|
||||
jest.mock('../../../../core/config/redis');
|
||||
|
||||
const mockAxios = jest.mocked(axios);
|
||||
const mockCacheService = jest.mocked(cacheService);
|
||||
|
||||
describe('VPICClient', () => {
|
||||
let client: VPICClient;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
client = new VPICClient();
|
||||
});
|
||||
|
||||
describe('decodeVIN', () => {
|
||||
const mockVin = '1HGBH41JXMN109186';
|
||||
|
||||
const mockVPICResponse: VPICResponse = {
|
||||
Count: 3,
|
||||
Message: 'Success',
|
||||
SearchCriteria: 'VIN: 1HGBH41JXMN109186',
|
||||
Results: [
|
||||
{ Variable: 'Make', Value: 'Honda', ValueId: null, VariableId: 1 },
|
||||
{ Variable: 'Model', Value: 'Civic', ValueId: null, VariableId: 2 },
|
||||
{ Variable: 'Model Year', Value: '2021', ValueId: null, VariableId: 3 },
|
||||
{ Variable: 'Engine Model', Value: '2.0L', ValueId: null, VariableId: 4 },
|
||||
{ Variable: 'Body Class', Value: 'Sedan', ValueId: null, VariableId: 5 },
|
||||
]
|
||||
};
|
||||
|
||||
it('should return cached result if available', async () => {
|
||||
const cachedResult = {
|
||||
make: 'Honda',
|
||||
model: 'Civic',
|
||||
year: 2021,
|
||||
engineType: '2.0L',
|
||||
bodyType: 'Sedan',
|
||||
rawData: mockVPICResponse.Results
|
||||
};
|
||||
|
||||
mockCacheService.get.mockResolvedValue(cachedResult);
|
||||
|
||||
const result = await client.decodeVIN(mockVin);
|
||||
|
||||
expect(mockCacheService.get).toHaveBeenCalledWith(`vpic:vin:${mockVin}`);
|
||||
expect(result).toEqual(cachedResult);
|
||||
expect(mockAxios.get).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fetch and cache VIN data when not cached', async () => {
|
||||
mockCacheService.get.mockResolvedValue(null);
|
||||
mockAxios.get.mockResolvedValue({ data: mockVPICResponse });
|
||||
mockCacheService.set.mockResolvedValue(undefined);
|
||||
|
||||
const result = await client.decodeVIN(mockVin);
|
||||
|
||||
expect(mockCacheService.get).toHaveBeenCalledWith(`vpic:vin:${mockVin}`);
|
||||
expect(mockAxios.get).toHaveBeenCalledWith(
|
||||
expect.stringContaining(`/DecodeVin/${mockVin}?format=json`)
|
||||
);
|
||||
expect(mockCacheService.set).toHaveBeenCalledWith(
|
||||
`vpic:vin:${mockVin}`,
|
||||
expect.objectContaining({
|
||||
make: 'Honda',
|
||||
model: 'Civic',
|
||||
year: 2021,
|
||||
engineType: '2.0L',
|
||||
bodyType: 'Sedan'
|
||||
}),
|
||||
30 * 24 * 60 * 60 // 30 days
|
||||
);
|
||||
expect(result?.make).toBe('Honda');
|
||||
expect(result?.model).toBe('Civic');
|
||||
expect(result?.year).toBe(2021);
|
||||
});
|
||||
|
||||
it('should return null when API returns no results', async () => {
|
||||
const emptyResponse: VPICResponse = {
|
||||
Count: 0,
|
||||
Message: 'No data found',
|
||||
SearchCriteria: 'VIN: INVALID',
|
||||
Results: []
|
||||
};
|
||||
|
||||
mockCacheService.get.mockResolvedValue(null);
|
||||
mockAxios.get.mockResolvedValue({ data: emptyResponse });
|
||||
|
||||
const result = await client.decodeVIN('INVALID');
|
||||
|
||||
expect(result).toBeNull();
|
||||
expect(mockCacheService.set).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return null when required fields are missing', async () => {
|
||||
const incompleteResponse: VPICResponse = {
|
||||
Count: 1,
|
||||
Message: 'Success',
|
||||
SearchCriteria: 'VIN: 1HGBH41JXMN109186',
|
||||
Results: [
|
||||
{ Variable: 'Make', Value: 'Honda', ValueId: null, VariableId: 1 },
|
||||
// Missing Model and Year
|
||||
]
|
||||
};
|
||||
|
||||
mockCacheService.get.mockResolvedValue(null);
|
||||
mockAxios.get.mockResolvedValue({ data: incompleteResponse });
|
||||
|
||||
const result = await client.decodeVIN(mockVin);
|
||||
|
||||
expect(result).toBeNull();
|
||||
expect(mockCacheService.set).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should handle API errors gracefully', async () => {
|
||||
mockCacheService.get.mockResolvedValue(null);
|
||||
mockAxios.get.mockRejectedValue(new Error('Network error'));
|
||||
|
||||
const result = await client.decodeVIN(mockVin);
|
||||
|
||||
expect(result).toBeNull();
|
||||
expect(mockCacheService.set).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should handle null values in API response', async () => {
|
||||
const responseWithNulls: VPICResponse = {
|
||||
Count: 3,
|
||||
Message: 'Success',
|
||||
SearchCriteria: 'VIN: 1HGBH41JXMN109186',
|
||||
Results: [
|
||||
{ Variable: 'Make', Value: 'Honda', ValueId: null, VariableId: 1 },
|
||||
{ Variable: 'Model', Value: 'Civic', ValueId: null, VariableId: 2 },
|
||||
{ Variable: 'Model Year', Value: '2021', ValueId: null, VariableId: 3 },
|
||||
{ Variable: 'Engine Model', Value: null, ValueId: null, VariableId: 4 },
|
||||
{ Variable: 'Body Class', Value: null, ValueId: null, VariableId: 5 },
|
||||
]
|
||||
};
|
||||
|
||||
mockCacheService.get.mockResolvedValue(null);
|
||||
mockAxios.get.mockResolvedValue({ data: responseWithNulls });
|
||||
mockCacheService.set.mockResolvedValue(undefined);
|
||||
|
||||
const result = await client.decodeVIN(mockVin);
|
||||
|
||||
expect(result?.make).toBe('Honda');
|
||||
expect(result?.model).toBe('Civic');
|
||||
expect(result?.year).toBe(2021);
|
||||
expect(result?.engineType).toBeUndefined();
|
||||
expect(result?.bodyType).toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user