mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-27 00:52:50 +00:00
fix: correct fuel module TypeScript errors
- Use @InjectRepository decorators for proper dependency injection - Fix BaseRepository initialization with Repository instance - Remove unnecessary DataSource references - Add proper type annotations to reduce handlers Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,13 +1,19 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { BaseRepository } from '@edr/api-common';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { Repository, Between } from 'typeorm';
|
||||
import { FuelPurchase } from './entities/fuel-purchase.entity';
|
||||
import { FuelConsumption } from './entities/fuel-consumption.entity';
|
||||
|
||||
@Injectable()
|
||||
export class FuelRepository extends BaseRepository<FuelPurchase> {
|
||||
constructor(dataSource: DataSource) {
|
||||
super(FuelPurchase, dataSource.createEntityManager());
|
||||
constructor(
|
||||
@InjectRepository(FuelPurchase)
|
||||
private readonly purchaseRepository: Repository<FuelPurchase>,
|
||||
@InjectRepository(FuelConsumption)
|
||||
private readonly consumptionRepository: Repository<FuelConsumption>,
|
||||
) {
|
||||
super(purchaseRepository);
|
||||
}
|
||||
|
||||
async findByVehicleAndDateRange(
|
||||
@@ -15,13 +21,10 @@ export class FuelRepository extends BaseRepository<FuelPurchase> {
|
||||
startDate: Date,
|
||||
endDate: Date,
|
||||
): Promise<FuelPurchase[]> {
|
||||
return this.find({
|
||||
return this.purchaseRepository.find({
|
||||
where: {
|
||||
vehicleId,
|
||||
purchaseDate: {
|
||||
$gte: startDate,
|
||||
$lte: endDate,
|
||||
},
|
||||
purchaseDate: Between(startDate, endDate),
|
||||
},
|
||||
order: { purchaseDate: 'DESC' },
|
||||
});
|
||||
@@ -31,8 +34,7 @@ export class FuelRepository extends BaseRepository<FuelPurchase> {
|
||||
vehicleId: string,
|
||||
month: Date,
|
||||
): Promise<FuelConsumption | null> {
|
||||
const consumptionRepository = this.manager.getRepository(FuelConsumption);
|
||||
return consumptionRepository.findOne({
|
||||
return this.consumptionRepository.findOne({
|
||||
where: {
|
||||
vehicleId,
|
||||
month,
|
||||
@@ -45,8 +47,7 @@ export class FuelRepository extends BaseRepository<FuelPurchase> {
|
||||
month: Date,
|
||||
data: Partial<FuelConsumption>,
|
||||
): Promise<FuelConsumption> {
|
||||
const consumptionRepository = this.manager.getRepository(FuelConsumption);
|
||||
let consumption = await consumptionRepository.findOne({
|
||||
let consumption = await this.consumptionRepository.findOne({
|
||||
where: {
|
||||
vehicleId,
|
||||
month,
|
||||
@@ -54,7 +55,7 @@ export class FuelRepository extends BaseRepository<FuelPurchase> {
|
||||
});
|
||||
|
||||
if (!consumption) {
|
||||
consumption = consumptionRepository.create({
|
||||
consumption = this.consumptionRepository.create({
|
||||
vehicleId,
|
||||
month,
|
||||
...data,
|
||||
@@ -63,6 +64,13 @@ export class FuelRepository extends BaseRepository<FuelPurchase> {
|
||||
Object.assign(consumption, data);
|
||||
}
|
||||
|
||||
return consumptionRepository.save(consumption);
|
||||
return this.consumptionRepository.save(consumption);
|
||||
}
|
||||
|
||||
async findPurchasesByVehicle(vehicleId: string): Promise<FuelPurchase[]> {
|
||||
return this.purchaseRepository.find({
|
||||
where: { vehicleId },
|
||||
order: { purchaseDate: 'DESC' },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { FuelRepository } from './fuel.repository';
|
||||
import { FuelPurchase } from './entities/fuel-purchase.entity';
|
||||
import { FuelConsumption } from './entities/fuel-consumption.entity';
|
||||
@@ -6,17 +8,21 @@ import { CreateFuelPurchaseDto } from './dto/create-fuel-purchase.dto';
|
||||
|
||||
@Injectable()
|
||||
export class FuelService {
|
||||
constructor(private readonly fuelRepository: FuelRepository) {}
|
||||
constructor(
|
||||
private readonly fuelRepository: FuelRepository,
|
||||
@InjectRepository(FuelPurchase)
|
||||
private readonly purchaseRepository: Repository<FuelPurchase>,
|
||||
) {}
|
||||
|
||||
async recordFuelPurchase(dto: CreateFuelPurchaseDto): Promise<FuelPurchase> {
|
||||
const totalCost = dto.liters * dto.costPerLiter;
|
||||
|
||||
const purchase = this.fuelRepository.create({
|
||||
const purchase = this.purchaseRepository.create({
|
||||
...dto,
|
||||
totalCost,
|
||||
});
|
||||
|
||||
const saved = await this.fuelRepository.save(purchase);
|
||||
const saved = await this.purchaseRepository.save(purchase);
|
||||
|
||||
// Update monthly consumption
|
||||
await this.updateMonthlyConsumption(dto.vehicleId, new Date(dto.purchaseDate));
|
||||
@@ -45,8 +51,8 @@ export class FuelService {
|
||||
|
||||
const purchases = await this.getFuelPurchases(vehicleId, startDate, endDate);
|
||||
|
||||
const totalLiters = purchases.reduce((sum, p) => sum + Number(p.liters), 0);
|
||||
const totalCost = purchases.reduce((sum, p) => sum + Number(p.totalCost), 0);
|
||||
const totalLiters = purchases.reduce((sum: number, p: FuelPurchase) => sum + Number(p.liters), 0);
|
||||
const totalCost = purchases.reduce((sum: number, p: FuelPurchase) => sum + Number(p.totalCost), 0);
|
||||
const averagePrice = totalLiters > 0 ? totalCost / totalLiters : 0;
|
||||
|
||||
return {
|
||||
@@ -61,19 +67,16 @@ export class FuelService {
|
||||
|
||||
private async updateMonthlyConsumption(vehicleId: string, date: Date): Promise<void> {
|
||||
const monthStart = new Date(date.getFullYear(), date.getMonth(), 1);
|
||||
const monthEnd = new Date(monthStart.getFullYear(), monthStart.getMonth() + 1, 1);
|
||||
|
||||
const purchases = await this.fuelRepository.find({
|
||||
where: {
|
||||
vehicleId,
|
||||
purchaseDate: {
|
||||
$gte: monthStart,
|
||||
$lt: new Date(monthStart.getFullYear(), monthStart.getMonth() + 1, 1),
|
||||
},
|
||||
},
|
||||
});
|
||||
const purchases = await this.fuelRepository.findByVehicleAndDateRange(
|
||||
vehicleId,
|
||||
monthStart,
|
||||
monthEnd,
|
||||
);
|
||||
|
||||
const totalLiters = purchases.reduce((sum, p) => sum + Number(p.liters), 0);
|
||||
const totalCost = purchases.reduce((sum, p) => sum + Number(p.totalCost), 0);
|
||||
const totalLiters = purchases.reduce((sum: number, p: FuelPurchase) => sum + Number(p.liters), 0);
|
||||
const totalCost = purchases.reduce((sum: number, p: FuelPurchase) => sum + Number(p.totalCost), 0);
|
||||
const numberOfPurchases = purchases.length;
|
||||
const averageCostPerLiter = totalLiters > 0 ? totalCost / totalLiters : 0;
|
||||
|
||||
@@ -82,6 +85,6 @@ export class FuelService {
|
||||
totalCost,
|
||||
numberOfPurchases,
|
||||
averageCostPerLiter,
|
||||
} as Partial<FuelConsumption>);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user