fix: edit vehicle null fix
All checks were successful
Deploy to Staging / Build Images (push) Successful in 4m32s
Deploy to Staging / Deploy to Staging (push) Successful in 27s
Deploy to Staging / Verify Staging (push) Successful in 6s
Deploy to Staging / Notify Staging Ready (push) Successful in 5s
Deploy to Staging / Notify Staging Failure (push) Has been skipped

This commit is contained in:
Eric Gullickson
2025-12-31 15:33:27 -06:00
parent d9cc604137
commit 52f9414fd4
5 changed files with 53 additions and 34 deletions

View File

@@ -39,16 +39,22 @@ export class VehiclesController {
async createVehicle(request: FastifyRequest<{ Body: CreateVehicleBody }>, reply: FastifyReply) {
try {
// Require either a valid 17-char VIN or a non-empty license plate
const vin = request.body?.vin?.trim();
const plate = request.body?.licensePlate?.trim();
const hasValidVin = !!vin && vin.length === 17;
const hasPlate = !!plate && plate.length > 0;
if (!hasValidVin && !hasPlate) {
return reply.code(400).send({
error: 'Bad Request',
message: 'Either a valid 17-character VIN or a license plate is required'
});
// Pre-1981 vehicles have no VIN/plate requirement
const year = request.body?.year;
const isPreModern = year && year < 1981;
if (!isPreModern) {
// Require either a valid 17-char VIN or a non-empty license plate
const vin = request.body?.vin?.trim();
const plate = request.body?.licensePlate?.trim();
const hasValidVin = !!vin && vin.length === 17;
const hasPlate = !!plate && plate.length > 0;
if (!hasValidVin && !hasPlate) {
return reply.code(400).send({
error: 'Bad Request',
message: 'Either a valid 17-character VIN or a license plate is required'
});
}
}
const userId = (request as any).user.sub;

View File

@@ -29,11 +29,14 @@ export class VehiclesService {
}
async createVehicle(data: CreateVehicleRequest, userId: string): Promise<VehicleResponse> {
logger.info('Creating vehicle', { userId, vin: data.vin, licensePlate: (data as any).licensePlate });
logger.info('Creating vehicle', { userId, vin: data.vin, licensePlate: data.licensePlate });
// Pre-1981 vehicles have no VIN format requirement
const isPreModern = data.year && data.year < 1981;
if (data.vin) {
// Validate VIN if provided
if (!isValidVIN(data.vin)) {
// Validate VIN format only for modern vehicles (1981+)
if (!isPreModern && !isValidVIN(data.vin)) {
throw new Error('Invalid VIN format');
}
// Duplicate check only when VIN is present

View File

@@ -32,6 +32,7 @@ export interface Vehicle {
export interface CreateVehicleRequest {
vin?: string;
year?: number;
make?: string;
model?: string;
engine?: string;
@@ -101,6 +102,14 @@ export interface VINDecodeResult {
// Fastify-specific types for HTTP handling
export interface CreateVehicleBody {
vin?: string;
year?: number;
make?: string;
model?: string;
engine?: string;
transmission?: string;
trimLevel?: string;
driveType?: string;
fuelType?: string;
nickname?: string;
color?: string;
licensePlate?: string;