Fix Auth Errors

This commit is contained in:
Eric Gullickson
2025-09-22 10:27:10 -05:00
parent 3588372cef
commit 8fd7973656
19 changed files with 1342 additions and 174 deletions

View File

@@ -4,7 +4,7 @@
*/
import { FuelLogsRepository } from '../data/fuel-logs.repository';
import { EnhancedCreateFuelLogRequest, EnhancedFuelLogResponse, FuelType } from './fuel-logs.types';
import { EnhancedCreateFuelLogRequest, EnhancedUpdateFuelLogRequest, EnhancedFuelLogResponse, FuelType } from './fuel-logs.types';
import { logger } from '../../../core/logging/logger';
import { cacheService } from '../../../core/config/redis';
import pool from '../../../core/config/database';
@@ -109,7 +109,81 @@ export class FuelLogsService {
return this.toEnhancedResponse(row, undefined, unitSystem);
}
async updateFuelLog(): Promise<any> { throw new Error('Not Implemented'); }
async updateFuelLog(id: string, data: EnhancedUpdateFuelLogRequest, userId: string): Promise<EnhancedFuelLogResponse> {
logger.info('Updating enhanced fuel log', { id, userId });
// Verify the fuel log exists and belongs to the user
const existing = await this.repository.findByIdEnhanced(id);
if (!existing) throw new Error('Fuel log not found');
if (existing.user_id !== userId) throw new Error('Unauthorized');
// Get user settings for unit conversion
const userSettings = await UserSettingsService.getUserSettings(userId);
// Validate the update data
if (Object.keys(data).length === 0) {
throw new Error('No fields provided for update');
}
// Prepare update data with proper type conversion
const updateData: any = {};
if (data.dateTime !== undefined) {
updateData.dateTime = new Date(data.dateTime);
}
if (data.odometerReading !== undefined) {
updateData.odometerReading = data.odometerReading;
}
if (data.tripDistance !== undefined) {
updateData.tripDistance = data.tripDistance;
}
if (data.fuelType !== undefined) {
updateData.fuelType = data.fuelType;
}
if (data.fuelGrade !== undefined) {
updateData.fuelGrade = data.fuelGrade;
}
if (data.fuelUnits !== undefined) {
updateData.fuelUnits = data.fuelUnits;
}
if (data.costPerUnit !== undefined) {
updateData.costPerUnit = data.costPerUnit;
}
if (data.locationData !== undefined) {
updateData.locationData = data.locationData;
}
if (data.notes !== undefined) {
updateData.notes = data.notes;
}
// Update the fuel log
const updated = await this.repository.updateEnhanced(id, updateData);
if (!updated) throw new Error('Failed to update fuel log');
// Update vehicle odometer if changed
if (data.odometerReading !== undefined) {
await pool.query(
'UPDATE vehicles SET odometer_reading = $1 WHERE id = $2 AND user_id = $3 AND (odometer_reading IS NULL OR odometer_reading < $1)',
[data.odometerReading, existing.vehicle_id, userId]
);
}
// Invalidate caches
await this.invalidateCaches(userId, existing.vehicle_id, userSettings.unitSystem);
// Calculate efficiency for response
const efficiency = EfficiencyCalculationService.calculateEfficiency(
{
odometerReading: updated.odometer ?? undefined,
tripDistance: updated.trip_distance ?? undefined,
fuelUnits: updated.fuel_units ?? undefined
},
null, // Previous log efficiency calculation would require more complex logic for updates
userSettings.unitSystem
);
return this.toEnhancedResponse(updated, efficiency?.value ?? undefined, userSettings.unitSystem);
}
async deleteFuelLog(id: string, userId: string): Promise<void> {
const existing = await this.repository.findByIdEnhanced(id);