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.
87 lines
2.7 KiB
TypeScript
87 lines
2.7 KiB
TypeScript
import { NotFoundException } from '@nestjs/common';
|
|
import { ExtractionsService } from './extractions.service';
|
|
import { PrismaService } from '../prisma/prisma.service';
|
|
import { AuditLogService } from '../audit/audit-log.service';
|
|
|
|
describe('ExtractionsService', () => {
|
|
let service: ExtractionsService;
|
|
let prisma: {
|
|
extraction: { findUnique: jest.Mock; update: jest.Mock };
|
|
$transaction: jest.Mock;
|
|
};
|
|
let auditLog: { record: jest.Mock };
|
|
let tx: {
|
|
extraction: { create: jest.Mock };
|
|
extractionHarvest: { createMany: jest.Mock };
|
|
};
|
|
|
|
const extraction = { id: 'extraction-1', date: new Date() };
|
|
|
|
beforeEach(() => {
|
|
tx = {
|
|
extraction: { create: jest.fn().mockResolvedValue(extraction) },
|
|
extractionHarvest: { createMany: jest.fn() },
|
|
};
|
|
prisma = {
|
|
extraction: { findUnique: jest.fn(), update: jest.fn() },
|
|
$transaction: jest.fn((callback: (txArg: typeof tx) => unknown) =>
|
|
callback(tx),
|
|
),
|
|
};
|
|
auditLog = { record: jest.fn() };
|
|
service = new ExtractionsService(
|
|
prisma as unknown as PrismaService,
|
|
auditLog as unknown as AuditLogService,
|
|
);
|
|
});
|
|
|
|
it('findById throws NotFoundException when missing', async () => {
|
|
prisma.extraction.findUnique.mockResolvedValue(null);
|
|
await expect(service.findById('missing')).rejects.toBeInstanceOf(
|
|
NotFoundException,
|
|
);
|
|
});
|
|
|
|
it('create links every harvest to the extraction inside a transaction', async () => {
|
|
prisma.extraction.findUnique.mockResolvedValue(extraction);
|
|
|
|
await service.create(
|
|
{ date: '2026-07-05', harvestIds: ['harvest-1', 'harvest-2'] },
|
|
'user-1',
|
|
);
|
|
|
|
const [[createArg]] = tx.extractionHarvest.createMany.mock.calls as [
|
|
[{ data: { extractionId: string; harvestId: string }[] }],
|
|
];
|
|
expect(createArg.data).toEqual([
|
|
{ extractionId: extraction.id, harvestId: 'harvest-1' },
|
|
{ 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,
|
|
}),
|
|
);
|
|
});
|
|
});
|