project service
This commit is contained in:
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user