feat: Add user data import feature (Fixes #26) #27
@@ -246,23 +246,41 @@ export class UserImportService {
|
|||||||
try {
|
try {
|
||||||
logger.debug('Processing vehicle', {
|
logger.debug('Processing vehicle', {
|
||||||
userId,
|
userId,
|
||||||
|
id: vehicle.id,
|
||||||
vin: vehicle.vin,
|
vin: vehicle.vin,
|
||||||
make: vehicle.make,
|
make: vehicle.make,
|
||||||
model: vehicle.model,
|
model: vehicle.model,
|
||||||
year: vehicle.year,
|
year: vehicle.year,
|
||||||
|
licensePlate: vehicle.licensePlate,
|
||||||
});
|
});
|
||||||
|
|
||||||
let existing = null;
|
let existing = null;
|
||||||
|
|
||||||
// Try to find existing vehicle by VIN first
|
// Try to find existing vehicle by ID first (preserves identity across exports)
|
||||||
if (vehicle.vin && vehicle.vin.trim().length > 0) {
|
if (vehicle.id) {
|
||||||
|
existing = await this.vehiclesRepo.findById(vehicle.id);
|
||||||
|
// Verify it belongs to the same user
|
||||||
|
if (existing && existing.userId !== userId) {
|
||||||
|
logger.warn('Vehicle ID belongs to different user, ignoring', {
|
||||||
|
vehicleId: vehicle.id,
|
||||||
|
expectedUserId: userId,
|
||||||
|
actualUserId: existing.userId,
|
||||||
|
});
|
||||||
|
existing = null;
|
||||||
|
} else if (existing) {
|
||||||
|
logger.debug('Found existing vehicle by ID', { vehicleId: existing.id });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to find existing vehicle by VIN if not found by ID
|
||||||
|
if (!existing && vehicle.vin && vehicle.vin.trim().length > 0) {
|
||||||
existing = await this.vehiclesRepo.findByUserAndVIN(userId, vehicle.vin.trim());
|
existing = await this.vehiclesRepo.findByUserAndVIN(userId, vehicle.vin.trim());
|
||||||
if (existing) {
|
if (existing) {
|
||||||
logger.debug('Found existing vehicle by VIN', { vehicleId: existing.id, vin: vehicle.vin });
|
logger.debug('Found existing vehicle by VIN', { vehicleId: existing.id, vin: vehicle.vin });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If not found by VIN and license plate exists, try license plate
|
// If not found by ID or VIN, and license plate exists, try license plate
|
||||||
if (!existing && vehicle.licensePlate && vehicle.licensePlate.trim().length > 0) {
|
if (!existing && vehicle.licensePlate && vehicle.licensePlate.trim().length > 0) {
|
||||||
const allUserVehicles = await this.vehiclesRepo.findByUserId(userId);
|
const allUserVehicles = await this.vehiclesRepo.findByUserId(userId);
|
||||||
existing = allUserVehicles.find(
|
existing = allUserVehicles.find(
|
||||||
|
|||||||
Reference in New Issue
Block a user