feat: create ownership_costs backend feature capsule (refs #29)

Milestone 1: Complete backend feature with:
- Migration with CHECK (amount > 0) constraint
- Repository with mapRow() for snake_case -> camelCase
- Service with CRUD and vehicle authorization
- Controller with HTTP handlers
- Routes registered at /api/ownership-costs
- Validation with Zod schemas
- README with endpoint documentation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Eric Gullickson
2026-01-13 21:28:43 -06:00
parent 5f07123646
commit 81b1c3dd70
10 changed files with 618 additions and 801 deletions

View File

@@ -1,199 +1,141 @@
/**
* @ai-summary Business logic for ownership costs feature
* @ai-context Handles ownership cost operations and TCO aggregation
*/
import { Pool } from 'pg';
import { OwnershipCostsRepository } from '../data/ownership-costs.repository';
import {
OwnershipCost,
import { randomUUID } from 'crypto';
import type { Pool } from 'pg';
import type {
CreateOwnershipCostRequest,
UpdateOwnershipCostRequest,
OwnershipCost,
OwnershipCostResponse,
OwnershipCostStats,
PAYMENTS_PER_YEAR,
OwnershipCostType
OwnershipCostType,
OwnershipCostStats
} from './ownership-costs.types';
import { logger } from '../../../core/logging/logger';
import { VehiclesRepository } from '../../vehicles/data/vehicles.repository';
import { OwnershipCostsRepository } from '../data/ownership-costs.repository';
import pool from '../../../core/config/database';
export class OwnershipCostsService {
private repository: OwnershipCostsRepository;
private vehiclesRepository: VehiclesRepository;
private readonly repo: OwnershipCostsRepository;
private readonly db: Pool;
constructor(pool: Pool) {
this.repository = new OwnershipCostsRepository(pool);
this.vehiclesRepository = new VehiclesRepository(pool);
constructor(dbPool?: Pool) {
this.db = dbPool || pool;
this.repo = new OwnershipCostsRepository(this.db);
}
async create(data: CreateOwnershipCostRequest, userId: string): Promise<OwnershipCostResponse> {
logger.info('Creating ownership cost', { userId, vehicleId: data.vehicleId, costType: data.costType });
async createCost(userId: string, body: CreateOwnershipCostRequest): Promise<OwnershipCost> {
await this.assertVehicleOwnership(userId, body.vehicleId);
// Verify vehicle ownership
const vehicle = await this.vehiclesRepository.findById(data.vehicleId);
if (!vehicle) {
throw new Error('Vehicle not found');
}
if (vehicle.userId !== userId) {
throw new Error('Unauthorized');
}
const id = randomUUID();
const cost = await this.repo.insert({
id,
userId,
vehicleId: body.vehicleId,
documentId: body.documentId,
costType: body.costType,
amount: body.amount,
description: body.description,
periodStart: body.periodStart,
periodEnd: body.periodEnd,
notes: body.notes,
});
const cost = await this.repository.create({ ...data, userId });
return cost;
}
async getCost(userId: string, id: string): Promise<OwnershipCostResponse | null> {
const cost = await this.repo.findById(id, userId);
if (!cost) return null;
return this.toResponse(cost);
}
async getByVehicleId(vehicleId: string, userId: string): Promise<OwnershipCostResponse[]> {
// Verify vehicle ownership
const vehicle = await this.vehiclesRepository.findById(vehicleId);
if (!vehicle) {
throw new Error('Vehicle not found');
}
if (vehicle.userId !== userId) {
throw new Error('Unauthorized');
}
const costs = await this.repository.findByVehicleId(vehicleId, userId);
return costs.map(cost => this.toResponse(cost));
async getCosts(userId: string, filters?: { vehicleId?: string; costType?: OwnershipCostType; documentId?: string }): Promise<OwnershipCostResponse[]> {
const costs = await this.repo.findByUserId(userId, filters);
return costs.map(c => this.toResponse(c));
}
async getById(id: string, userId: string): Promise<OwnershipCostResponse> {
const cost = await this.repository.findById(id);
if (!cost) {
throw new Error('Ownership cost not found');
}
if (cost.userId !== userId) {
throw new Error('Unauthorized');
}
return this.toResponse(cost);
async getCostsByVehicle(userId: string, vehicleId: string): Promise<OwnershipCostResponse[]> {
const costs = await this.repo.findByVehicleId(vehicleId, userId);
return costs.map(c => this.toResponse(c));
}
async update(id: string, data: UpdateOwnershipCostRequest, userId: string): Promise<OwnershipCostResponse> {
// Verify ownership
const existing = await this.repository.findById(id);
if (!existing) {
throw new Error('Ownership cost not found');
}
if (existing.userId !== userId) {
throw new Error('Unauthorized');
}
async getVehicleOwnershipCosts(vehicleId: string, userId: string): Promise<OwnershipCostStats> {
const costs = await this.repo.findByVehicleId(vehicleId, userId);
const updated = await this.repository.update(id, data);
if (!updated) {
throw new Error('Update failed');
}
let totalCost = 0;
let insuranceCosts = 0;
let registrationCosts = 0;
let taxCosts = 0;
let otherCosts = 0;
return this.toResponse(updated);
}
for (const c of costs) {
if (c.amount === null || c.amount === undefined) continue;
const amount = Number(c.amount);
if (isNaN(amount)) {
throw new Error(`Invalid amount value for ownership cost ${c.id}`);
}
async delete(id: string, userId: string): Promise<void> {
// Verify ownership
const existing = await this.repository.findById(id);
if (!existing) {
throw new Error('Ownership cost not found');
}
if (existing.userId !== userId) {
throw new Error('Unauthorized');
}
totalCost += amount;
await this.repository.delete(id);
logger.info('Ownership cost deleted', { id, userId });
}
/**
* Get aggregated cost statistics for a vehicle
* Used by TCO calculation in vehicles service
*/
async getVehicleCostStats(vehicleId: string, userId: string): Promise<OwnershipCostStats> {
const costs = await this.repository.findByVehicleId(vehicleId, userId);
const now = new Date();
const stats: OwnershipCostStats = {
insuranceCosts: 0,
registrationCosts: 0,
taxCosts: 0,
otherCosts: 0,
totalCosts: 0
};
for (const cost of costs) {
const startDate = new Date(cost.startDate);
const endDate = cost.endDate ? new Date(cost.endDate) : now;
// Skip future costs
if (startDate > now) continue;
// Calculate effective end date (either specified end, or now for ongoing costs)
const effectiveEnd = endDate < now ? endDate : now;
const monthsCovered = this.calculateMonthsBetween(startDate, effectiveEnd);
const normalizedCost = this.normalizeToTotal(cost.amount, cost.interval, monthsCovered);
// Type-safe key access
const keyMap: Record<OwnershipCostType, keyof Omit<OwnershipCostStats, 'totalCosts'>> = {
insurance: 'insuranceCosts',
registration: 'registrationCosts',
tax: 'taxCosts',
other: 'otherCosts'
};
const key = keyMap[cost.costType];
if (key) {
stats[key] += normalizedCost;
} else {
logger.warn('Unknown cost type in aggregation', { costType: cost.costType, costId: cost.id });
// Breakdown by cost type for backward compatibility
switch (c.costType) {
case 'insurance':
insuranceCosts += amount;
break;
case 'registration':
registrationCosts += amount;
break;
case 'tax':
taxCosts += amount;
break;
case 'inspection':
case 'parking':
case 'other':
otherCosts += amount;
break;
}
}
stats.totalCosts = stats.insuranceCosts + stats.registrationCosts + stats.taxCosts + stats.otherCosts;
return stats;
return {
totalCost,
recordCount: costs.length,
insuranceCosts,
registrationCosts,
taxCosts,
otherCosts
};
}
/**
* Calculate months between two dates
*/
private calculateMonthsBetween(startDate: Date, endDate: Date): number {
const yearDiff = endDate.getFullYear() - startDate.getFullYear();
const monthDiff = endDate.getMonth() - startDate.getMonth();
return Math.max(1, yearDiff * 12 + monthDiff);
// Alias for backward compatibility with vehicles service
async getVehicleCostStats(vehicleId: string, userId: string): Promise<OwnershipCostStats> {
return this.getVehicleOwnershipCosts(vehicleId, userId);
}
/**
* Normalize recurring cost to total based on interval and months covered
*/
private normalizeToTotal(amount: number, interval: string, monthsCovered: number): number {
// One-time costs are just the amount
if (interval === 'one_time') {
return amount;
}
async updateCost(userId: string, id: string, patch: UpdateOwnershipCostRequest): Promise<OwnershipCostResponse | null> {
const existing = await this.repo.findById(id, userId);
if (!existing) return null;
const paymentsPerYear = PAYMENTS_PER_YEAR[interval as keyof typeof PAYMENTS_PER_YEAR];
if (!paymentsPerYear) {
logger.warn('Invalid cost interval', { interval });
return 0;
}
// Convert nulls to undefined for repository compatibility
const cleanPatch = Object.fromEntries(
Object.entries(patch).map(([k, v]) => [k, v === null ? undefined : v])
) as Partial<Pick<OwnershipCost, 'documentId' | 'costType' | 'amount' | 'description' | 'periodStart' | 'periodEnd' | 'notes'>>;
// Calculate total payments over the covered period
const yearsOwned = monthsCovered / 12;
const totalPayments = yearsOwned * paymentsPerYear;
return amount * totalPayments;
const updated = await this.repo.update(id, userId, cleanPatch);
if (!updated) return null;
return this.toResponse(updated);
}
async deleteCost(userId: string, id: string): Promise<void> {
await this.repo.delete(id, userId);
}
private async assertVehicleOwnership(userId: string, vehicleId: string) {
const res = await this.db.query('SELECT id FROM vehicles WHERE id = $1 AND user_id = $2', [vehicleId, userId]);
if (!res.rows[0]) {
const err: any = new Error('Vehicle not found or not owned by user');
err.statusCode = 403;
throw err;
}
}
private toResponse(cost: OwnershipCost): OwnershipCostResponse {
return {
id: cost.id,
userId: cost.userId,
vehicleId: cost.vehicleId,
documentId: cost.documentId,
costType: cost.costType,
description: cost.description,
amount: cost.amount,
interval: cost.interval,
startDate: cost.startDate instanceof Date ? cost.startDate.toISOString().split('T')[0] : cost.startDate as unknown as string,
endDate: cost.endDate ? (cost.endDate instanceof Date ? cost.endDate.toISOString().split('T')[0] : cost.endDate as unknown as string) : undefined,
createdAt: cost.createdAt instanceof Date ? cost.createdAt.toISOString() : cost.createdAt as unknown as string,
updatedAt: cost.updatedAt instanceof Date ? cost.updatedAt.toISOString() : cost.updatedAt as unknown as string,
};
return cost;
}
}