feat: sase.tr v2 full application implementation
Complete rewrite of sase.tr VIN lookup platform with modern stack: Backend (NestJS 10 + Drizzle ORM + PostgreSQL + Redis + BullMQ): - 34 DB models (core + PL24 + EMEX schemas) - Auth via Better Auth (email/password + social) - Brands, Plans, Subscriptions, Payments (iyzico + EFT) - VIN decode orchestration (Corgi + PL24 + EMEX + NHTSA) - Interactive schema viewer backend (MinIO storage) - EMEX scraping integration (Puppeteer + BullMQ workers) - Translation module (EN→TR automotive dictionary) - Admin dashboard API (stats, user mgmt, payment approval) - Rate limiting, Helmet security, file upload validation Frontend (Next.js 15 + Tailwind v4 + shadcn/ui + TanStack Query + Zustand): - 20 routes: auth, dashboard, VIN search, schema viewer, admin - Interactive schema viewer with zoom/pan/hotspot highlighting - Subscription management with brand selector - Payment flow (iyzico 3D Secure + EFT with receipt upload) - i18n support (TR/EN) - Error boundaries, loading skeletons, 404 page Infrastructure: - 85 tests (52 backend + 33 frontend, Vitest) - CI/CD (GitHub Actions: lint, typecheck, test, build, deploy) - Zero-downtime deploy script (PM2) - Env validation script Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
189
apps/api/src/integrations/pl24/pl24.service.ts
Normal file
189
apps/api/src/integrations/pl24/pl24.service.ts
Normal file
@@ -0,0 +1,189 @@
|
||||
import { Injectable, Logger } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { PL24AuthService } from "./pl24-auth.service";
|
||||
import { RedisService } from "../../redis/redis.service";
|
||||
import { StorageService } from "../../storage/storage.service";
|
||||
import { createParser } from "./parsers/parser-factory";
|
||||
import { PL24_DEFAULTS } from "./pl24.constants";
|
||||
import type {
|
||||
PL24VehicleResponse,
|
||||
PL24CategoryResponse,
|
||||
PL24PartResponse,
|
||||
PL24SchemaPicResponse,
|
||||
ParsedVehicle,
|
||||
ParsedCategory,
|
||||
} from "./pl24.types";
|
||||
|
||||
@Injectable()
|
||||
export class PL24Service {
|
||||
private readonly logger = new Logger(PL24Service.name);
|
||||
private readonly apiUrl: string;
|
||||
|
||||
constructor(
|
||||
private configService: ConfigService,
|
||||
private authService: PL24AuthService,
|
||||
private redis: RedisService,
|
||||
private storage: StorageService,
|
||||
) {
|
||||
this.apiUrl = this.configService.get<string>("pl24.apiUrl") || "";
|
||||
}
|
||||
|
||||
async decodeVin(vin: string, brandName: string): Promise<ParsedVehicle | null> {
|
||||
const cacheKey = `${PL24_DEFAULTS.CACHE_PREFIX}vehicle:${vin}`;
|
||||
const cached = await this.redis.getJson<ParsedVehicle>(cacheKey);
|
||||
if (cached) return cached;
|
||||
|
||||
if (!this.apiUrl) {
|
||||
this.logger.warn("PL24 API URL not configured");
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const token = await this.authService.getToken();
|
||||
const response = await this.makeRequest<PL24VehicleResponse>(`/vehicles/decode/${vin}`, token);
|
||||
|
||||
if (!response) return null;
|
||||
|
||||
const parser = createParser(brandName);
|
||||
const parsed = parser.parseVehicle(response as unknown as Record<string, unknown>);
|
||||
|
||||
await this.redis.setJson(cacheKey, parsed, 86400); // 24h cache
|
||||
return parsed;
|
||||
} catch (error) {
|
||||
this.logger.error(`PL24 decode failed for ${vin}`, error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async getCategories(vehicleId: string, brandName: string): Promise<ParsedCategory[]> {
|
||||
const cacheKey = `${PL24_DEFAULTS.CACHE_PREFIX}categories:${vehicleId}`;
|
||||
const cached = await this.redis.getJson<ParsedCategory[]>(cacheKey);
|
||||
if (cached) return cached;
|
||||
|
||||
if (!this.apiUrl) return [];
|
||||
|
||||
try {
|
||||
const token = await this.authService.getToken();
|
||||
const response = await this.makeRequest<PL24CategoryResponse[]>(
|
||||
`/vehicles/${vehicleId}/groups`,
|
||||
token,
|
||||
);
|
||||
|
||||
if (!response) return [];
|
||||
|
||||
const parser = createParser(brandName);
|
||||
const parsed = parser.parseCategories(response as unknown as unknown[]);
|
||||
|
||||
await this.redis.setJson(cacheKey, parsed, 3600); // 1h cache
|
||||
return parsed;
|
||||
} catch (error) {
|
||||
this.logger.error(`PL24 get categories failed for ${vehicleId}`, error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async getParts(vehicleId: string, groupId: string, brandName: string): Promise<PL24PartResponse[]> {
|
||||
const cacheKey = `${PL24_DEFAULTS.CACHE_PREFIX}parts:${vehicleId}:${groupId}`;
|
||||
const cached = await this.redis.getJson<PL24PartResponse[]>(cacheKey);
|
||||
if (cached) return cached;
|
||||
|
||||
if (!this.apiUrl) return [];
|
||||
|
||||
try {
|
||||
const token = await this.authService.getToken();
|
||||
const response = await this.makeRequest<PL24PartResponse[]>(
|
||||
`/vehicles/${vehicleId}/groups/${groupId}/parts`,
|
||||
token,
|
||||
);
|
||||
|
||||
if (!response) return [];
|
||||
|
||||
const parser = createParser(brandName);
|
||||
const parsed = parser.parseParts(response as unknown as unknown[]);
|
||||
|
||||
await this.redis.setJson(cacheKey, parsed, 3600);
|
||||
return parsed;
|
||||
} catch (error) {
|
||||
this.logger.error(`PL24 get parts failed`, error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async getSchemaImage(vehicleId: string, groupId: string): Promise<PL24SchemaPicResponse | null> {
|
||||
// Check if already uploaded to MinIO
|
||||
const minioKey = `schemas/${vehicleId}/${groupId}.png`;
|
||||
const existingUrl = this.storage.getPublicUrl(minioKey);
|
||||
|
||||
const cacheKey = `${PL24_DEFAULTS.CACHE_PREFIX}schema:${vehicleId}:${groupId}`;
|
||||
const cached = await this.redis.getJson<PL24SchemaPicResponse>(cacheKey);
|
||||
if (cached) return cached;
|
||||
|
||||
if (!this.apiUrl) return null;
|
||||
|
||||
try {
|
||||
const token = await this.authService.getToken();
|
||||
const response = await this.makeRequest<PL24SchemaPicResponse>(
|
||||
`/vehicles/${vehicleId}/groups/${groupId}/schema`,
|
||||
token,
|
||||
);
|
||||
|
||||
if (!response) return null;
|
||||
|
||||
// Download image and upload to MinIO
|
||||
if (response.imageUrl) {
|
||||
try {
|
||||
const imageResponse = await fetch(response.imageUrl, {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
signal: AbortSignal.timeout(PL24_DEFAULTS.REQUEST_TIMEOUT),
|
||||
});
|
||||
|
||||
if (imageResponse.ok) {
|
||||
const buffer = Buffer.from(await imageResponse.arrayBuffer());
|
||||
const uploadedUrl = await this.storage.upload(minioKey, buffer, "image/png");
|
||||
response.imageUrl = uploadedUrl;
|
||||
}
|
||||
} catch (imgError) {
|
||||
this.logger.warn(`Failed to upload schema image to MinIO`, imgError);
|
||||
}
|
||||
}
|
||||
|
||||
await this.redis.setJson(cacheKey, response, 86400);
|
||||
return response;
|
||||
} catch (error) {
|
||||
this.logger.error(`PL24 get schema failed`, error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private async makeRequest<T>(path: string, token: string): Promise<T | null> {
|
||||
try {
|
||||
const response = await fetch(`${this.apiUrl}${path}`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
signal: AbortSignal.timeout(PL24_DEFAULTS.REQUEST_TIMEOUT),
|
||||
});
|
||||
|
||||
if (response.status === 401) {
|
||||
await this.authService.invalidateToken();
|
||||
const newToken = await this.authService.getToken();
|
||||
const retry = await fetch(`${this.apiUrl}${path}`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${newToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
signal: AbortSignal.timeout(PL24_DEFAULTS.REQUEST_TIMEOUT),
|
||||
});
|
||||
if (!retry.ok) return null;
|
||||
return (await retry.json()) as T;
|
||||
}
|
||||
|
||||
if (!response.ok) return null;
|
||||
return (await response.json()) as T;
|
||||
} catch (error) {
|
||||
this.logger.error(`PL24 request failed: ${path}`, error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user