35 lines
1.1 KiB
TypeScript
35 lines
1.1 KiB
TypeScript
import { and, eq } from "drizzle-orm";
|
|
import type { AppDatabase } from "../database-context.js";
|
|
import { projectDevices } from "../schema/project-devices.js";
|
|
import { calculateRowTotalPower } from "../../domain/calculations/circuit-power-calculation.js";
|
|
|
|
function withTotalPower(row: typeof projectDevices.$inferSelect) {
|
|
return {
|
|
...row,
|
|
totalPower: calculateRowTotalPower(row.quantity, row.powerPerUnit, row.simultaneityFactor),
|
|
};
|
|
}
|
|
|
|
export class ProjectDeviceRepository {
|
|
constructor(private readonly database: AppDatabase) {}
|
|
|
|
async listByProject(projectId: string) {
|
|
const db = this.database;
|
|
const rows = await db.select().from(projectDevices).where(eq(projectDevices.projectId, projectId));
|
|
return rows.map(withTotalPower);
|
|
}
|
|
|
|
async findById(projectId: string, projectDeviceId: string) {
|
|
const db = this.database;
|
|
const [row] = await db
|
|
.select()
|
|
.from(projectDevices)
|
|
.where(
|
|
and(eq(projectDevices.id, projectDeviceId), eq(projectDevices.projectId, projectId))
|
|
)
|
|
.limit(1);
|
|
return row ? withTotalPower(row) : null;
|
|
}
|
|
|
|
}
|