Fix project service

This commit is contained in:
2026-02-03 02:05:10 -06:00
parent 35c44ed13c
commit 5f51d25bc1

View File

@@ -241,14 +241,22 @@ export async function update(id: string, data: UpdateProjectInput): Promise<Proj
* @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);
try {
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`);
if (userCount > 0) {
throw new Error(`Cannot delete project: ${userCount} user(s) are assigned to this project`);
}
} catch (error) {
if (error instanceof Error && error.message.includes('column') && error.message.includes('project_id')) {
console.warn('Warning: users.project_id column does not exist. Skipping user dependency check. Please run add_user_project_relation.sql migration.');
} else {
throw error;
}
}
// Check for dependent meters