45 lines
1.4 KiB
TypeScript
45 lines
1.4 KiB
TypeScript
import crypto from "node:crypto";
|
|
import { eq } from "drizzle-orm";
|
|
import type { AppDatabase } from "../database-context.js";
|
|
import { projects } from "../schema/projects.js";
|
|
import type {
|
|
CreateProjectInput,
|
|
} from "../../shared/validation/project-structure.schemas.js";
|
|
import { defaultDistributionBoardSupplyTypes } from "../../shared/constants/distribution-board.js";
|
|
|
|
export class ProjectRepository {
|
|
constructor(private readonly database: AppDatabase) {}
|
|
|
|
async list() {
|
|
const db = this.database;
|
|
return db.select().from(projects);
|
|
}
|
|
|
|
async create(input: CreateProjectInput) {
|
|
const db = this.database;
|
|
const id = crypto.randomUUID();
|
|
const project = {
|
|
id,
|
|
name: input.name,
|
|
internalProjectNumber: input.internalProjectNumber ?? null,
|
|
externalProjectNumber: input.externalProjectNumber ?? null,
|
|
buildingOwner: input.buildingOwner ?? null,
|
|
description: input.description ?? null,
|
|
singlePhaseVoltageV: input.singlePhaseVoltageV ?? 230,
|
|
threePhaseVoltageV: input.threePhaseVoltageV ?? 400,
|
|
enabledDistributionBoardSupplyTypes: [
|
|
...defaultDistributionBoardSupplyTypes,
|
|
],
|
|
currentRevision: 0,
|
|
};
|
|
await db.insert(projects).values(project);
|
|
return project;
|
|
}
|
|
|
|
async findById(projectId: string) {
|
|
const db = this.database;
|
|
const [row] = await db.select().from(projects).where(eq(projects.id, projectId)).limit(1);
|
|
return row ?? null;
|
|
}
|
|
}
|