Initial Commit
This commit is contained in:
@@ -13,9 +13,9 @@ export class FuelLogsRepository {
|
||||
const query = `
|
||||
INSERT INTO fuel_logs (
|
||||
user_id, vehicle_id, date, odometer, gallons,
|
||||
price_per_gallon, total_cost, station, location, notes, mpg
|
||||
price_per_gallon, total_cost, station, location, notes
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||
RETURNING *
|
||||
`;
|
||||
|
||||
@@ -29,8 +29,7 @@ export class FuelLogsRepository {
|
||||
data.totalCost,
|
||||
data.station,
|
||||
data.location,
|
||||
data.notes,
|
||||
data.mpg
|
||||
data.notes
|
||||
];
|
||||
|
||||
const result = await this.pool.query(query, values);
|
||||
@@ -126,10 +125,7 @@ export class FuelLogsRepository {
|
||||
fields.push(`notes = $${paramCount++}`);
|
||||
values.push(data.notes);
|
||||
}
|
||||
if (data.mpg !== undefined) {
|
||||
fields.push(`mpg = $${paramCount++}`);
|
||||
values.push(data.mpg);
|
||||
}
|
||||
// mpg column removed; efficiency is computed dynamically
|
||||
|
||||
if (fields.length === 0) {
|
||||
return this.findById(id);
|
||||
@@ -165,7 +161,6 @@ export class FuelLogsRepository {
|
||||
SUM(gallons) as total_gallons,
|
||||
SUM(total_cost) as total_cost,
|
||||
AVG(price_per_gallon) as avg_price_per_gallon,
|
||||
AVG(mpg) as avg_mpg,
|
||||
MAX(odometer) - MIN(odometer) as total_miles
|
||||
FROM fuel_logs
|
||||
WHERE vehicle_id = $1
|
||||
@@ -183,7 +178,7 @@ export class FuelLogsRepository {
|
||||
totalGallons: parseFloat(row.total_gallons) || 0,
|
||||
totalCost: parseFloat(row.total_cost) || 0,
|
||||
averagePricePerGallon: parseFloat(row.avg_price_per_gallon) || 0,
|
||||
averageMPG: parseFloat(row.avg_mpg) || 0,
|
||||
averageMPG: 0,
|
||||
totalMiles: parseInt(row.total_miles) || 0,
|
||||
};
|
||||
}
|
||||
@@ -201,9 +196,94 @@ export class FuelLogsRepository {
|
||||
station: row.station,
|
||||
location: row.location,
|
||||
notes: row.notes,
|
||||
mpg: row.mpg ? parseFloat(row.mpg) : undefined,
|
||||
createdAt: row.created_at,
|
||||
updatedAt: row.updated_at,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Enhanced API support (new schema)
|
||||
async createEnhanced(data: {
|
||||
userId: string;
|
||||
vehicleId: string;
|
||||
dateTime: Date;
|
||||
odometerReading?: number;
|
||||
tripDistance?: number;
|
||||
fuelType: string;
|
||||
fuelGrade?: string | null;
|
||||
fuelUnits: number;
|
||||
costPerUnit: number;
|
||||
totalCost: number;
|
||||
locationData?: any;
|
||||
notes?: string;
|
||||
}): Promise<any> {
|
||||
const query = `
|
||||
INSERT INTO fuel_logs (
|
||||
user_id, vehicle_id, date, date_time, odometer, trip_distance,
|
||||
fuel_type, fuel_grade, fuel_units, cost_per_unit,
|
||||
gallons, price_per_gallon, total_cost, location_data, notes
|
||||
)
|
||||
VALUES (
|
||||
$1, $2, $3, $4, $5, $6,
|
||||
$7, $8, $9, $10,
|
||||
$11, $12, $13, $14, $15
|
||||
)
|
||||
RETURNING *
|
||||
`;
|
||||
const values = [
|
||||
data.userId,
|
||||
data.vehicleId,
|
||||
data.dateTime.toISOString().slice(0, 10),
|
||||
data.dateTime,
|
||||
data.odometerReading ?? null,
|
||||
data.tripDistance ?? null,
|
||||
data.fuelType,
|
||||
data.fuelGrade ?? null,
|
||||
data.fuelUnits,
|
||||
data.costPerUnit,
|
||||
data.fuelUnits, // legacy support
|
||||
data.costPerUnit, // legacy support
|
||||
data.totalCost,
|
||||
data.locationData ?? null,
|
||||
data.notes ?? null
|
||||
];
|
||||
const res = await this.pool.query(query, values);
|
||||
return res.rows[0];
|
||||
}
|
||||
|
||||
async findByVehicleIdEnhanced(vehicleId: string): Promise<any[]> {
|
||||
const res = await this.pool.query(
|
||||
`SELECT * FROM fuel_logs WHERE vehicle_id = $1 ORDER BY date_time DESC NULLS LAST, date DESC NULLS LAST, created_at DESC`,
|
||||
[vehicleId]
|
||||
);
|
||||
return res.rows;
|
||||
}
|
||||
|
||||
async findByUserIdEnhanced(userId: string): Promise<any[]> {
|
||||
const res = await this.pool.query(
|
||||
`SELECT * FROM fuel_logs WHERE user_id = $1 ORDER BY date_time DESC NULLS LAST, date DESC NULLS LAST, created_at DESC`,
|
||||
[userId]
|
||||
);
|
||||
return res.rows;
|
||||
}
|
||||
|
||||
async findByIdEnhanced(id: string): Promise<any | null> {
|
||||
const res = await this.pool.query(`SELECT * FROM fuel_logs WHERE id = $1`, [id]);
|
||||
return res.rows[0] || null;
|
||||
}
|
||||
|
||||
async getPreviousLogByOdometer(vehicleId: string, odometerReading: number): Promise<any | null> {
|
||||
const res = await this.pool.query(
|
||||
`SELECT * FROM fuel_logs WHERE vehicle_id = $1 AND odometer IS NOT NULL AND odometer < $2 ORDER BY odometer DESC LIMIT 1`,
|
||||
[vehicleId, odometerReading]
|
||||
);
|
||||
return res.rows[0] || null;
|
||||
}
|
||||
|
||||
async getLatestLogForVehicle(vehicleId: string): Promise<any | null> {
|
||||
const res = await this.pool.query(
|
||||
`SELECT * FROM fuel_logs WHERE vehicle_id = $1 ORDER BY date_time DESC NULLS LAST, date DESC NULLS LAST, created_at DESC LIMIT 1`,
|
||||
[vehicleId]
|
||||
);
|
||||
return res.rows[0] || null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user