diff --git a/backend/server.js b/backend/server.js index af1959b..c11e8fd 100644 --- a/backend/server.js +++ b/backend/server.js @@ -296,6 +296,32 @@ async function startServer() { } }) + // NEW: DELETE a tag + app.delete('/api/managers/tags/:id', authenticateJWT, async (req, res) => { + try { + const { id } = req.params + + // Optional: Check if the user is a manager before allowing deletion + if (req.user.role !== 'manager') { + return res.status(403).json({ message: 'Forbidden: Only managers can delete tags.' }) + } + + // Delete the tag from the 'tags' table. + // If 'worker_tags' table has ON DELETE CASCADE for tag_id, + // related entries in 'worker_tags' will automatically be removed. + const [result] = await db.execute('DELETE FROM tags WHERE id = ?', [id]) + + if (result.affectedRows === 0) { + return res.status(404).json({ message: 'Tag not found.' }) + } + + res.status(204).send() // 204 No Content for successful deletion + } catch (error) { + console.error('Delete tag error:', error) + res.status(500).json({ message: 'Database error deleting tag.' }) + } + }) + // POST to assign a tag to a worker app.post('/api/managers/workers/:workerId/tags', authenticateJWT, async (req, res) => { try { @@ -484,7 +510,6 @@ async function startServer() { return res.status(409).json({ message: `Worker is already clocked ${status}.` }) } // --- THIS IS THE FIX --- - // Sanitize the timestamp from "YYYY-MM-DDTHH:mm" to "YYYY-MM-DD HH:mm" const sanitizedTimestamp = timestamp.replace('T', ' ') await db.execute( diff --git a/src/components/PersonnelManagement.vue b/src/components/PersonnelManagement.vue index 4fe147a..50c0846 100644 --- a/src/components/PersonnelManagement.vue +++ b/src/components/PersonnelManagement.vue @@ -88,9 +88,23 @@ {{ tag.tag_name }} + @@ -273,15 +287,35 @@
{
}
}
+const deleteTag = async (tagId) => {
+ if (!confirm('Are you sure you want to delete this tag? This will remove it from all workers.'))
+ return
+ try {
+ await apiFetch(`/api/managers/tags/${tagId}`, { method: 'DELETE' })
+ allTags.value = allTags.value.filter((tag) => tag.id !== tagId)
+ // Also re-fetch workers to update their tag display if any had this tag
+ fetchWorkers(currentPage.value)
+ alert('Tag deleted successfully.')
+ } catch (err) {
+ alert(err.message || 'Failed to delete tag.')
+ console.error(err)
+ }
+}
+
const openTagEditor = (worker) => {
editingWorker.value = worker
isEditingTags.value = true
diff --git a/src/views/ManagerDashboardView.vue b/src/views/ManagerDashboardView.vue
index 71dfa87..b4c71b7 100644
--- a/src/views/ManagerDashboardView.vue
+++ b/src/views/ManagerDashboardView.vue
@@ -1,38 +1,41 @@