adds statistics
This commit is contained in:
@@ -1,16 +1,44 @@
|
||||
import { Router } from 'express';
|
||||
import { requireAuth } from '../middleware/auth';
|
||||
import { validateBody, validateParams, validateQuery } from '../middleware/validation';
|
||||
import { TimeEntryService } from '../services/timeEntry.service';
|
||||
import { CreateTimeEntrySchema, UpdateTimeEntrySchema, IdSchema, TimeEntryFiltersSchema } from '../schemas';
|
||||
import type { AuthenticatedRequest } from '../types';
|
||||
import { Router } from "express";
|
||||
import { requireAuth } from "../middleware/auth";
|
||||
import {
|
||||
validateBody,
|
||||
validateParams,
|
||||
validateQuery,
|
||||
} from "../middleware/validation";
|
||||
import { TimeEntryService } from "../services/timeEntry.service";
|
||||
import {
|
||||
CreateTimeEntrySchema,
|
||||
UpdateTimeEntrySchema,
|
||||
IdSchema,
|
||||
TimeEntryFiltersSchema,
|
||||
StatisticsFiltersSchema,
|
||||
} from "../schemas";
|
||||
import type { AuthenticatedRequest } from "../types";
|
||||
|
||||
const router = Router();
|
||||
const timeEntryService = new TimeEntryService();
|
||||
|
||||
// GET /api/time-entries/statistics - Get aggregated statistics
|
||||
router.get(
|
||||
"/statistics",
|
||||
requireAuth,
|
||||
validateQuery(StatisticsFiltersSchema),
|
||||
async (req: AuthenticatedRequest, res, next) => {
|
||||
try {
|
||||
const stats = await timeEntryService.getStatistics(
|
||||
req.user!.id,
|
||||
req.query,
|
||||
);
|
||||
res.json(stats);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// GET /api/time-entries - List user's entries
|
||||
router.get(
|
||||
'/',
|
||||
"/",
|
||||
requireAuth,
|
||||
validateQuery(TimeEntryFiltersSchema),
|
||||
async (req: AuthenticatedRequest, res, next) => {
|
||||
@@ -20,12 +48,12 @@ router.get(
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// POST /api/time-entries - Create entry manually
|
||||
router.post(
|
||||
'/',
|
||||
"/",
|
||||
requireAuth,
|
||||
validateBody(CreateTimeEntrySchema),
|
||||
async (req: AuthenticatedRequest, res, next) => {
|
||||
@@ -35,28 +63,32 @@ router.post(
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// PUT /api/time-entries/:id - Update entry
|
||||
router.put(
|
||||
'/:id',
|
||||
"/:id",
|
||||
requireAuth,
|
||||
validateParams(IdSchema),
|
||||
validateBody(UpdateTimeEntrySchema),
|
||||
async (req: AuthenticatedRequest, res, next) => {
|
||||
try {
|
||||
const entry = await timeEntryService.update(req.params.id, req.user!.id, req.body);
|
||||
const entry = await timeEntryService.update(
|
||||
req.params.id,
|
||||
req.user!.id,
|
||||
req.body,
|
||||
);
|
||||
res.json(entry);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// DELETE /api/time-entries/:id - Delete entry
|
||||
router.delete(
|
||||
'/:id',
|
||||
"/:id",
|
||||
requireAuth,
|
||||
validateParams(IdSchema),
|
||||
async (req: AuthenticatedRequest, res, next) => {
|
||||
@@ -66,7 +98,7 @@ router.delete(
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
export default router;
|
||||
export default router;
|
||||
|
||||
@@ -51,6 +51,13 @@ export const TimeEntryFiltersSchema = z.object({
|
||||
limit: z.coerce.number().int().min(1).max(100).default(50),
|
||||
});
|
||||
|
||||
export const StatisticsFiltersSchema = z.object({
|
||||
startDate: z.string().datetime().optional(),
|
||||
endDate: z.string().datetime().optional(),
|
||||
projectId: z.string().uuid().optional(),
|
||||
clientId: z.string().uuid().optional(),
|
||||
});
|
||||
|
||||
export const StartTimerSchema = z.object({
|
||||
projectId: z.string().uuid().optional(),
|
||||
});
|
||||
@@ -61,4 +68,4 @@ export const UpdateTimerSchema = z.object({
|
||||
|
||||
export const StopTimerSchema = z.object({
|
||||
projectId: z.string().uuid().optional(),
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,9 +1,157 @@
|
||||
import { prisma } from '../prisma/client';
|
||||
import type { CreateTimeEntryInput, UpdateTimeEntryInput, TimeEntryFilters } from '../types';
|
||||
import { prisma } from "../prisma/client";
|
||||
import type {
|
||||
CreateTimeEntryInput,
|
||||
UpdateTimeEntryInput,
|
||||
TimeEntryFilters,
|
||||
StatisticsFilters,
|
||||
} from "../types";
|
||||
|
||||
export class TimeEntryService {
|
||||
async getStatistics(userId: string, filters: StatisticsFilters = {}) {
|
||||
const { startDate, endDate, projectId, clientId } = filters;
|
||||
const where: {
|
||||
userId: string;
|
||||
startTime?: { gte?: Date; lte?: Date };
|
||||
projectId?: string;
|
||||
project?: { clientId?: string };
|
||||
} = { userId };
|
||||
|
||||
if (startDate || endDate) {
|
||||
where.startTime = {};
|
||||
if (startDate) where.startTime.gte = new Date(startDate);
|
||||
if (endDate) where.startTime.lte = new Date(endDate);
|
||||
}
|
||||
|
||||
if (projectId) {
|
||||
where.projectId = projectId;
|
||||
}
|
||||
|
||||
if (clientId) {
|
||||
where.project = { clientId };
|
||||
}
|
||||
|
||||
const entries = await prisma.timeEntry.findMany({
|
||||
where,
|
||||
include: {
|
||||
project: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
color: true,
|
||||
client: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Calculate total duration in seconds
|
||||
const totalSeconds = entries.reduce((total, entry) => {
|
||||
const startTime = new Date(entry.startTime);
|
||||
const endTime = new Date(entry.endTime);
|
||||
return (
|
||||
total + Math.floor((endTime.getTime() - startTime.getTime()) / 1000)
|
||||
);
|
||||
}, 0);
|
||||
|
||||
// Calculate by project
|
||||
const byProject = entries.reduce(
|
||||
(acc, entry) => {
|
||||
const projectId = entry.project.id;
|
||||
const projectName = entry.project.name;
|
||||
const projectColor = entry.project.color;
|
||||
const startTime = new Date(entry.startTime);
|
||||
const endTime = new Date(entry.endTime);
|
||||
const duration = Math.floor(
|
||||
(endTime.getTime() - startTime.getTime()) / 1000,
|
||||
);
|
||||
|
||||
if (!acc[projectId]) {
|
||||
acc[projectId] = {
|
||||
projectId,
|
||||
projectName,
|
||||
projectColor,
|
||||
totalSeconds: 0,
|
||||
entryCount: 0,
|
||||
};
|
||||
}
|
||||
acc[projectId].totalSeconds += duration;
|
||||
acc[projectId].entryCount += 1;
|
||||
return acc;
|
||||
},
|
||||
{} as Record<
|
||||
string,
|
||||
{
|
||||
projectId: string;
|
||||
projectName: string;
|
||||
projectColor: string | null;
|
||||
totalSeconds: number;
|
||||
entryCount: number;
|
||||
}
|
||||
>,
|
||||
);
|
||||
|
||||
// Calculate by client
|
||||
const byClient = entries.reduce(
|
||||
(acc, entry) => {
|
||||
const clientId = entry.project.client.id;
|
||||
const clientName = entry.project.client.name;
|
||||
const startTime = new Date(entry.startTime);
|
||||
const endTime = new Date(entry.endTime);
|
||||
const duration = Math.floor(
|
||||
(endTime.getTime() - startTime.getTime()) / 1000,
|
||||
);
|
||||
|
||||
if (!acc[clientId]) {
|
||||
acc[clientId] = {
|
||||
clientId,
|
||||
clientName,
|
||||
totalSeconds: 0,
|
||||
entryCount: 0,
|
||||
};
|
||||
}
|
||||
acc[clientId].totalSeconds += duration;
|
||||
acc[clientId].entryCount += 1;
|
||||
return acc;
|
||||
},
|
||||
{} as Record<
|
||||
string,
|
||||
{
|
||||
clientId: string;
|
||||
clientName: string;
|
||||
totalSeconds: number;
|
||||
entryCount: number;
|
||||
}
|
||||
>,
|
||||
);
|
||||
|
||||
return {
|
||||
totalSeconds,
|
||||
entryCount: entries.length,
|
||||
byProject: Object.values(byProject),
|
||||
byClient: Object.values(byClient),
|
||||
filters: {
|
||||
startDate: startDate || null,
|
||||
endDate: endDate || null,
|
||||
projectId: projectId || null,
|
||||
clientId: clientId || null,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async findAll(userId: string, filters: TimeEntryFilters = {}) {
|
||||
const { startDate, endDate, projectId, clientId, page = 1, limit = 50 } = filters;
|
||||
const {
|
||||
startDate,
|
||||
endDate,
|
||||
projectId,
|
||||
clientId,
|
||||
page = 1,
|
||||
limit = 50,
|
||||
} = filters;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const where: {
|
||||
@@ -30,7 +178,7 @@ export class TimeEntryService {
|
||||
const [entries, total] = await Promise.all([
|
||||
prisma.timeEntry.findMany({
|
||||
where,
|
||||
orderBy: { startTime: 'desc' },
|
||||
orderBy: { startTime: "desc" },
|
||||
skip,
|
||||
take: limit,
|
||||
include: {
|
||||
@@ -90,7 +238,9 @@ export class TimeEntryService {
|
||||
|
||||
// Validate end time is after start time
|
||||
if (endTime <= startTime) {
|
||||
const error = new Error('End time must be after start time') as Error & { statusCode: number };
|
||||
const error = new Error("End time must be after start time") as Error & {
|
||||
statusCode: number;
|
||||
};
|
||||
error.statusCode = 400;
|
||||
throw error;
|
||||
}
|
||||
@@ -101,15 +251,23 @@ export class TimeEntryService {
|
||||
});
|
||||
|
||||
if (!project) {
|
||||
const error = new Error('Project not found') as Error & { statusCode: number };
|
||||
const error = new Error("Project not found") as Error & {
|
||||
statusCode: number;
|
||||
};
|
||||
error.statusCode = 404;
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Check for overlapping entries
|
||||
const hasOverlap = await this.hasOverlappingEntries(userId, startTime, endTime);
|
||||
const hasOverlap = await this.hasOverlappingEntries(
|
||||
userId,
|
||||
startTime,
|
||||
endTime,
|
||||
);
|
||||
if (hasOverlap) {
|
||||
const error = new Error('This time entry overlaps with an existing entry') as Error & { statusCode: number };
|
||||
const error = new Error(
|
||||
"This time entry overlaps with an existing entry",
|
||||
) as Error & { statusCode: number };
|
||||
error.statusCode = 400;
|
||||
throw error;
|
||||
}
|
||||
@@ -143,17 +301,23 @@ export class TimeEntryService {
|
||||
async update(id: string, userId: string, data: UpdateTimeEntryInput) {
|
||||
const entry = await this.findById(id, userId);
|
||||
if (!entry) {
|
||||
const error = new Error('Time entry not found') as Error & { statusCode: number };
|
||||
const error = new Error("Time entry not found") as Error & {
|
||||
statusCode: number;
|
||||
};
|
||||
error.statusCode = 404;
|
||||
throw error;
|
||||
}
|
||||
|
||||
const startTime = data.startTime ? new Date(data.startTime) : entry.startTime;
|
||||
const startTime = data.startTime
|
||||
? new Date(data.startTime)
|
||||
: entry.startTime;
|
||||
const endTime = data.endTime ? new Date(data.endTime) : entry.endTime;
|
||||
|
||||
// Validate end time is after start time
|
||||
if (endTime <= startTime) {
|
||||
const error = new Error('End time must be after start time') as Error & { statusCode: number };
|
||||
const error = new Error("End time must be after start time") as Error & {
|
||||
statusCode: number;
|
||||
};
|
||||
error.statusCode = 400;
|
||||
throw error;
|
||||
}
|
||||
@@ -165,16 +329,25 @@ export class TimeEntryService {
|
||||
});
|
||||
|
||||
if (!project) {
|
||||
const error = new Error('Project not found') as Error & { statusCode: number };
|
||||
const error = new Error("Project not found") as Error & {
|
||||
statusCode: number;
|
||||
};
|
||||
error.statusCode = 404;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Check for overlapping entries (excluding this entry)
|
||||
const hasOverlap = await this.hasOverlappingEntries(userId, startTime, endTime, id);
|
||||
const hasOverlap = await this.hasOverlappingEntries(
|
||||
userId,
|
||||
startTime,
|
||||
endTime,
|
||||
id,
|
||||
);
|
||||
if (hasOverlap) {
|
||||
const error = new Error('This time entry overlaps with an existing entry') as Error & { statusCode: number };
|
||||
const error = new Error(
|
||||
"This time entry overlaps with an existing entry",
|
||||
) as Error & { statusCode: number };
|
||||
error.statusCode = 400;
|
||||
throw error;
|
||||
}
|
||||
@@ -208,7 +381,9 @@ export class TimeEntryService {
|
||||
async delete(id: string, userId: string) {
|
||||
const entry = await this.findById(id, userId);
|
||||
if (!entry) {
|
||||
const error = new Error('Time entry not found') as Error & { statusCode: number };
|
||||
const error = new Error("Time entry not found") as Error & {
|
||||
statusCode: number;
|
||||
};
|
||||
error.statusCode = 404;
|
||||
throw error;
|
||||
}
|
||||
@@ -222,7 +397,7 @@ export class TimeEntryService {
|
||||
userId: string,
|
||||
startTime: Date,
|
||||
endTime: Date,
|
||||
excludeId?: string
|
||||
excludeId?: string,
|
||||
): Promise<boolean> {
|
||||
const where: {
|
||||
userId: string;
|
||||
@@ -246,4 +421,4 @@ export class TimeEntryService {
|
||||
const count = await prisma.timeEntry.count({ where });
|
||||
return count > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,6 +57,13 @@ export interface TimeEntryFilters {
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
export interface StatisticsFilters {
|
||||
startDate?: string;
|
||||
endDate?: string;
|
||||
projectId?: string;
|
||||
clientId?: string;
|
||||
}
|
||||
|
||||
export interface StartTimerInput {
|
||||
projectId?: string;
|
||||
}
|
||||
@@ -67,4 +74,4 @@ export interface UpdateTimerInput {
|
||||
|
||||
export interface StopTimerInput {
|
||||
projectId?: string;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user