project service

This commit is contained in:
2026-02-03 01:39:48 -06:00
parent 31ab977f97
commit 5529739749
2 changed files with 38 additions and 5 deletions

View File

@@ -236,11 +236,21 @@ export async function update(id: string, data: UpdateProjectInput): Promise<Proj
/**
* Delete a project by ID
* Checks for dependent meters/concentrators before deletion
* Checks for dependent meters/concentrators/users before deletion
* @param id - Project UUID
* @returns True if deleted, throws error if has dependencies
*/
export async function deleteProject(id: string): Promise<boolean> {
const userCheck = await query<{ count: string }>(
'SELECT COUNT(*) as count FROM users WHERE project_id = $1',
[id]
);
const userCount = parseInt(userCheck.rows[0]?.count || '0', 10);
if (userCount > 0) {
throw new Error(`Cannot delete project: ${userCount} user(s) are assigned to this project`);
}
// Check for dependent meters
const meterCheck = await query<{ count: string }>(
'SELECT COUNT(*) as count FROM meters WHERE project_id = $1',