Files
timetracker/backend/src/routes/timer.routes.ts
simon.franken 06596dcee9 Add cancel (discard) timer feature
Allows users to discard a running timer without creating a time entry.
A trash icon in the timer widget reveals a confirmation step ('Discard / Keep')
to prevent accidental data loss. Backend exposes a new DELETE /api/timer
endpoint that simply deletes the ongoingTimer row.
2026-02-23 10:41:50 +01:00

76 lines
1.9 KiB
TypeScript

import { Router } from 'express';
import { requireAuth } from '../middleware/auth';
import { validateBody } from '../middleware/validation';
import { TimerService } from '../services/timer.service';
import { StartTimerSchema, UpdateTimerSchema, StopTimerSchema } from '../schemas';
import type { AuthenticatedRequest } from '../types';
const router = Router();
const timerService = new TimerService();
// GET /api/timer - Get current ongoing timer
router.get('/', requireAuth, async (req: AuthenticatedRequest, res, next) => {
try {
const timer = await timerService.getOngoingTimer(req.user!.id);
res.json(timer);
} catch (error) {
next(error);
}
});
// POST /api/timer/start - Start timer
router.post(
'/start',
requireAuth,
validateBody(StartTimerSchema),
async (req: AuthenticatedRequest, res, next) => {
try {
const timer = await timerService.start(req.user!.id, req.body);
res.status(201).json(timer);
} catch (error) {
next(error);
}
}
);
// PUT /api/timer - Update ongoing timer
router.put(
'/',
requireAuth,
validateBody(UpdateTimerSchema),
async (req: AuthenticatedRequest, res, next) => {
try {
const timer = await timerService.update(req.user!.id, req.body);
res.json(timer);
} catch (error) {
next(error);
}
}
);
// DELETE /api/timer - Cancel (discard) the ongoing timer without creating a time entry
router.delete('/', requireAuth, async (req: AuthenticatedRequest, res, next) => {
try {
await timerService.cancel(req.user!.id);
res.status(204).send();
} catch (error) {
next(error);
}
});
// POST /api/timer/stop - Stop timer
router.post(
'/stop',
requireAuth,
validateBody(StopTimerSchema),
async (req: AuthenticatedRequest, res, next) => {
try {
const entry = await timerService.stop(req.user!.id, req.body);
res.json(entry);
} catch (error) {
next(error);
}
}
);
export default router;