feat(backend): Dashboard-Modul (Modul 13)

DashboardModule mit Kennzahlen-Übersicht sowie Alerts fuer
überfällige Aufgaben, auffällige Völker und Lagerartikel unter
Mindestbestand.
This commit is contained in:
Nick Szaunig committed 2026-08-22 05:15:25 +02:00
1 parent 7ec20199d3
commit 09407d4378
7 files changed
+297 -1

No files matched your search

@@ -0,0 +1,107 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
export interface DashboardSummary {
apiariesActive: number;
coloniesActive: number;
queensActive: number;
openTasks: number;
honeyBatchesTotal: number;
stockItemsLowCount: number;
}
export interface DashboardAlert {
type: 'OVERDUE_TASK' | 'COLONY_ATTENTION' | 'LOW_STOCK';
message: string;
relatedType: string;
relatedId: string;
}
const ATTENTION_COLONY_STATUSES = ['WEAK', 'QUEENLESS', 'CONSPICUOUS'] as const;
@Injectable()
export class DashboardService {
constructor(private readonly prisma: PrismaService) {}
async summary(): Promise<DashboardSummary> {
const [
apiariesActive,
coloniesActive,
queensActive,
openTasks,
honeyBatchesTotal,
stockItems,
] = await Promise.all([
this.prisma.apiary.count({ where: { status: 'ACTIVE' } }),
this.prisma.colony.count({ where: { status: 'ACTIVE' } }),
this.prisma.queen.count({
where: { status: { in: ['ACTIVE', 'RESERVE'] } },
}),
this.prisma.task.count({
where: { status: { in: ['OPEN', 'IN_PROGRESS'] } },
}),
this.prisma.honeyBatch.count(),
this.prisma.stockItem.findMany({
where: { minThreshold: { not: null } },
}),
]);
const stockItemsLowCount = stockItems.filter(
(item) => item.minThreshold !== null && item.quantity < item.minThreshold,
).length;
return {
apiariesActive,
coloniesActive,
queensActive,
openTasks,
honeyBatchesTotal,
stockItemsLowCount,
};
}
async alerts(): Promise<DashboardAlert[]> {
const [overdueTasks, attentionColonies, stockItems] = await Promise.all([
this.prisma.task.findMany({
where: {
status: { in: ['OPEN', 'IN_PROGRESS'] },
dueDate: { lt: new Date() },
},
}),
this.prisma.colony.findMany({
where: { status: { in: [...ATTENTION_COLONY_STATUSES] } },
}),
this.prisma.stockItem.findMany({
where: { minThreshold: { not: null } },
}),
]);
const alerts: DashboardAlert[] = [];
for (const task of overdueTasks) {
alerts.push({
type: 'OVERDUE_TASK',
message: `Aufgabe "${task.title}" ist überfällig`,
relatedType: 'Task',
relatedId: task.id,
});
}
for (const colony of attentionColonies) {
alerts.push({
type: 'COLONY_ATTENTION',
message: `Volk ${colony.code} benötigt Aufmerksamkeit (${colony.status})`,
relatedType: 'Colony',
relatedId: colony.id,
});
}
for (const item of stockItems) {
if (item.minThreshold !== null && item.quantity < item.minThreshold) {
alerts.push({
type: 'LOW_STOCK',
message: `Lagerartikel "${item.name}" unter Mindestbestand`,
relatedType: 'StockItem',
relatedId: item.id,
});
}
}
return alerts;
}
}