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:
1 parent
7ec20199d3
commit
09407d4378
7 files changed
+297
-1
No files matched your search
@@ -18,6 +18,7 @@ import { HarvestsModule } from './harvests/harvests.module';
|
||||
import { ExtractionsModule } from './extractions/extractions.module';
|
||||
import { HoneyBatchesModule } from './honey-batches/honey-batches.module';
|
||||
import { StorageModule } from './storage/storage.module';
|
||||
import { DashboardModule } from './dashboard/dashboard.module';
|
||||
import { JwtAuthGuard } from './auth/guards/jwt-auth.guard';
|
||||
import { RolesGuard } from './auth/guards/roles.guard';
|
||||
|
||||
@@ -41,6 +42,7 @@ import { RolesGuard } from './auth/guards/roles.guard';
|
||||
ExtractionsModule,
|
||||
HoneyBatchesModule,
|
||||
StorageModule,
|
||||
DashboardModule,
|
||||
],
|
||||
controllers: [],
|
||||
providers: [
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
||||
import { DashboardService } from './dashboard.service';
|
||||
|
||||
@ApiTags('dashboard')
|
||||
@ApiBearerAuth()
|
||||
@Controller('dashboard')
|
||||
export class DashboardController {
|
||||
constructor(private readonly dashboardService: DashboardService) {}
|
||||
|
||||
@Get('summary')
|
||||
summary() {
|
||||
return this.dashboardService.summary();
|
||||
}
|
||||
|
||||
@Get('alerts')
|
||||
alerts() {
|
||||
return this.dashboardService.alerts();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { DashboardController } from './dashboard.controller';
|
||||
import { DashboardService } from './dashboard.service';
|
||||
|
||||
@Module({
|
||||
controllers: [DashboardController],
|
||||
providers: [DashboardService],
|
||||
})
|
||||
export class DashboardModule {}
|
||||
@@ -0,0 +1,66 @@
|
||||
import { DashboardService } from './dashboard.service';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
describe('DashboardService', () => {
|
||||
let service: DashboardService;
|
||||
let prisma: {
|
||||
apiary: { count: jest.Mock };
|
||||
colony: { count: jest.Mock; findMany: jest.Mock };
|
||||
queen: { count: jest.Mock };
|
||||
task: { count: jest.Mock; findMany: jest.Mock };
|
||||
honeyBatch: { count: jest.Mock };
|
||||
stockItem: { findMany: jest.Mock };
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
prisma = {
|
||||
apiary: { count: jest.fn().mockResolvedValue(2) },
|
||||
colony: {
|
||||
count: jest.fn().mockResolvedValue(5),
|
||||
findMany: jest.fn().mockResolvedValue([]),
|
||||
},
|
||||
queen: { count: jest.fn().mockResolvedValue(4) },
|
||||
task: {
|
||||
count: jest.fn().mockResolvedValue(3),
|
||||
findMany: jest.fn().mockResolvedValue([]),
|
||||
},
|
||||
honeyBatch: { count: jest.fn().mockResolvedValue(1) },
|
||||
stockItem: { findMany: jest.fn().mockResolvedValue([]) },
|
||||
};
|
||||
service = new DashboardService(prisma as unknown as PrismaService);
|
||||
});
|
||||
|
||||
it('summary counts low-stock items below their minimum threshold', async () => {
|
||||
prisma.stockItem.findMany.mockResolvedValue([
|
||||
{ id: 'item-1', quantity: 2, minThreshold: 5 },
|
||||
{ id: 'item-2', quantity: 10, minThreshold: 5 },
|
||||
]);
|
||||
|
||||
const summary = await service.summary();
|
||||
|
||||
expect(summary.stockItemsLowCount).toBe(1);
|
||||
expect(summary.apiariesActive).toBe(2);
|
||||
expect(summary.coloniesActive).toBe(5);
|
||||
});
|
||||
|
||||
it('alerts flags overdue tasks, attention colonies, and low stock', async () => {
|
||||
prisma.task.findMany.mockResolvedValue([
|
||||
{ id: 'task-1', title: 'Beute prüfen' },
|
||||
]);
|
||||
prisma.colony.findMany.mockResolvedValue([
|
||||
{ id: 'colony-1', code: 'V-001', status: 'WEAK' },
|
||||
]);
|
||||
prisma.stockItem.findMany.mockResolvedValue([
|
||||
{ id: 'item-1', name: 'Zuckerlösung', quantity: 2, minThreshold: 5 },
|
||||
]);
|
||||
|
||||
const alerts = await service.alerts();
|
||||
|
||||
expect(alerts).toHaveLength(3);
|
||||
expect(alerts.map((alert) => alert.type)).toEqual([
|
||||
'OVERDUE_TASK',
|
||||
'COLONY_ATTENTION',
|
||||
'LOW_STOCK',
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user