Project deleting
This commit is contained in:
@@ -225,3 +225,37 @@ export async function getStats(req: Request, res: Response): Promise<void> {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /projects/:id/deactivate
|
||||
* Deactivate a project and unassign users
|
||||
* Requires authentication
|
||||
*/
|
||||
export async function deactivateProject(req: AuthenticatedRequest, res: Response): Promise<void> {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
const project = await projectService.getById(id);
|
||||
|
||||
if (!project) {
|
||||
res.status(404).json({
|
||||
success: false,
|
||||
error: 'Project not found',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const deactivatedProject = await projectService.deactivateProjectAndUnassignUsers(id);
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: deactivatedProject,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error deactivating project:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: 'Failed to deactivate project',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,6 +54,14 @@ router.put('/:id', authenticateToken, validateUpdateProject, projectController.u
|
||||
*/
|
||||
router.patch('/:id', authenticateToken, validateUpdateProject, projectController.update);
|
||||
|
||||
/**
|
||||
* POST /projects/:id/deactivate
|
||||
* Protected endpoint - deactivate a project and unassign users
|
||||
* Headers: Authorization: Bearer <accessToken>
|
||||
* Response: { success: true, data: Project }
|
||||
*/
|
||||
router.post('/:id/deactivate', authenticateToken, projectController.deactivateProject);
|
||||
|
||||
/**
|
||||
* DELETE /projects/:id
|
||||
* Protected endpoint - delete a project (requires admin role)
|
||||
|
||||
@@ -323,3 +323,25 @@ export async function getStats(id: string): Promise<ProjectStats | null> {
|
||||
concentrator_count: parseInt(concentratorStats.rows[0]?.count || '0', 10),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Deactivate a project and unassign all users
|
||||
* @param id - Project UUID
|
||||
* @returns Updated project
|
||||
*/
|
||||
export async function deactivateProjectAndUnassignUsers(id: string): Promise<Project> {
|
||||
await query(
|
||||
'UPDATE users SET project_id = NULL WHERE project_id = $1',
|
||||
[id]
|
||||
);
|
||||
|
||||
const result = await query<Project>(
|
||||
`UPDATE projects
|
||||
SET status = 'INACTIVE', updated_at = NOW()
|
||||
WHERE id = $1
|
||||
RETURNING id, name, description, area_name, location, status, meter_type_id, created_by, created_at, updated_at`,
|
||||
[id]
|
||||
);
|
||||
|
||||
return result.rows[0];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user