feat: Bearbeiten für alle Log-Einträge (Aufgaben, Durchsichten, Behandlungen, Fütterungen, Varroa-Messungen, Ernten, Schleuderungen, Abfüllungen)
Bisher liessen sich diese acht Objekttypen nur anlegen, nie nachträglich korrigieren – Aufgaben hatten zwar schon einen PATCH-Endpunkt, aber keine Bearbeiten-UI, die übrigen sieben hatten weder Backend- noch Frontend-Unterstützung. Jetzt bekommt jeder Typ einen PATCH :id Endpunkt (UpdateXDto per PartialType(PickType(...)) nach demselben Muster wie UpdateColonyDto/UpdateApiaryDto, mit Audit-Log-Eintrag inkl. Vorher-/Nachher-Stand) sowie eine Bearbeiten-UI: - Aufgaben: Dialog auf der Aufgabenliste - Durchsichten/Behandlungen/Fütterungen/Varroa-Messungen: Dialog pro Listeneintrag auf der Volk-Detailseite - Ernten: Dialog auf der eigenen Ernte-Detailseite - Schleuderungen: Dialog direkt in der Rückverfolgbarkeits-Karte der Honigcharge (keine eigene Detailseite nötig); Erntezeilen dort verlinken jetzt zur Ernte-Detailseite statt sie zu duplizieren - Abfüllungen: Dialog auf der Abfüllungs-Detailseite, bewusst nur Datum/MHD/Losnummer editierbar – Glasgröße/-anzahl bleiben unveränderlich, weil sie bereits unumkehrbar die Honigcharge-Menge reduziert haben Relationsstiftende Fremdschlüssel (colonyId, apiaryId/colonyIds, harvestIds, honeyBatchId) bleiben überall bewusst nicht editierbar, konsistent mit dem bestehenden apiaryId-Ausschluss bei UpdateColonyDto.
This commit is contained in:
1 parent
63ab0e842d
commit
68f459001a
58 files changed
+1909
-60
No files matched your search
@@ -0,0 +1,13 @@
|
||||
import { PartialType, PickType } from '@nestjs/swagger';
|
||||
import { CreateExtractionDto } from './create-extraction.dto';
|
||||
|
||||
export class UpdateExtractionDto extends PartialType(
|
||||
PickType(CreateExtractionDto, [
|
||||
'date',
|
||||
'amount',
|
||||
'room',
|
||||
'waterContent',
|
||||
'temperature',
|
||||
'notes',
|
||||
] as const),
|
||||
) {}
|
||||
@@ -1,10 +1,11 @@
|
||||
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
|
||||
import { Body, Controller, Get, Param, Patch, Post } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
||||
import { Role } from '../../generated/prisma';
|
||||
import { Roles } from '../auth/decorators/roles.decorator';
|
||||
import { CurrentUser } from '../auth/decorators/current-user.decorator';
|
||||
import type { AuthenticatedUser } from '../auth/types/authenticated-user';
|
||||
import { CreateExtractionDto } from './dto/create-extraction.dto';
|
||||
import { UpdateExtractionDto } from './dto/update-extraction.dto';
|
||||
import { ExtractionsService } from './extractions.service';
|
||||
|
||||
@ApiTags('extractions')
|
||||
@@ -31,4 +32,14 @@ export class ExtractionsController {
|
||||
) {
|
||||
return this.extractionsService.create(dto, actor.id);
|
||||
}
|
||||
|
||||
@Roles(Role.ADMIN, Role.BEEKEEPER)
|
||||
@Patch(':id')
|
||||
update(
|
||||
@Param('id') id: string,
|
||||
@Body() dto: UpdateExtractionDto,
|
||||
@CurrentUser() actor: AuthenticatedUser,
|
||||
) {
|
||||
return this.extractionsService.update(id, dto, actor.id);
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import { AuditLogService } from '../audit/audit-log.service';
|
||||
describe('ExtractionsService', () => {
|
||||
let service: ExtractionsService;
|
||||
let prisma: {
|
||||
extraction: { findUnique: jest.Mock };
|
||||
extraction: { findUnique: jest.Mock; update: jest.Mock };
|
||||
$transaction: jest.Mock;
|
||||
};
|
||||
let auditLog: { record: jest.Mock };
|
||||
@@ -23,7 +23,7 @@ describe('ExtractionsService', () => {
|
||||
extractionHarvest: { createMany: jest.fn() },
|
||||
};
|
||||
prisma = {
|
||||
extraction: { findUnique: jest.fn() },
|
||||
extraction: { findUnique: jest.fn(), update: jest.fn() },
|
||||
$transaction: jest.fn((callback: (txArg: typeof tx) => unknown) =>
|
||||
callback(tx),
|
||||
),
|
||||
@@ -58,4 +58,29 @@ describe('ExtractionsService', () => {
|
||||
{ extractionId: extraction.id, harvestId: 'harvest-2' },
|
||||
]);
|
||||
});
|
||||
|
||||
it('update writes an audit log entry with before and after data', async () => {
|
||||
const updated = { ...extraction, notes: 'geändert' };
|
||||
prisma.extraction.findUnique
|
||||
.mockResolvedValueOnce(extraction)
|
||||
.mockResolvedValueOnce(updated);
|
||||
prisma.extraction.update.mockResolvedValue(updated);
|
||||
|
||||
const result = await service.update(
|
||||
'extraction-1',
|
||||
{ notes: 'geändert' },
|
||||
'user-1',
|
||||
);
|
||||
|
||||
expect(result).toEqual(updated);
|
||||
expect(auditLog.record).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
entityType: 'Extraction',
|
||||
entityId: 'extraction-1',
|
||||
action: 'UPDATE',
|
||||
beforeData: extraction,
|
||||
afterData: updated,
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -2,6 +2,7 @@ import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { AuditLogService } from '../audit/audit-log.service';
|
||||
import { CreateExtractionDto } from './dto/create-extraction.dto';
|
||||
import { UpdateExtractionDto } from './dto/update-extraction.dto';
|
||||
|
||||
const extractionInclude = {
|
||||
harvests: {
|
||||
@@ -66,4 +67,22 @@ export class ExtractionsService {
|
||||
});
|
||||
return this.findById(extraction.id);
|
||||
}
|
||||
|
||||
async update(id: string, dto: UpdateExtractionDto, actingUserId: string) {
|
||||
const before = await this.findById(id);
|
||||
await this.prisma.extraction.update({
|
||||
where: { id },
|
||||
data: { ...dto, date: dto.date ? new Date(dto.date) : undefined },
|
||||
});
|
||||
const extraction = await this.findById(id);
|
||||
await this.auditLog.record({
|
||||
entityType: 'Extraction',
|
||||
entityId: id,
|
||||
action: 'UPDATE',
|
||||
userId: actingUserId,
|
||||
beforeData: before,
|
||||
afterData: extraction,
|
||||
});
|
||||
return extraction;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user