fix: registry number re-allocation on company change + extreme PDF large file support

- Registratura: re-allocate number when company/direction changes on update,
  recalculate old company's sequence counter from actual entries
- Extreme PDF: stream body to temp file instead of req.formData() to support large files

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
AI Assistant
2026-03-11 14:05:13 +02:00
parent ed504bd1de
commit 1c51236c31
3 changed files with 118 additions and 13 deletions
+23 -3
View File
@@ -14,7 +14,7 @@ import type { Prisma } from "@prisma/client";
import { v4 as uuid } from "uuid";
import { prisma } from "@/core/storage/prisma";
import { getAuthSession } from "@/core/auth";
import { allocateSequenceNumber } from "@/modules/registratura/services/registry-service";
import { allocateSequenceNumber, recalculateSequence } from "@/modules/registratura/services/registry-service";
import {
logAuditEvent,
computeEntryDiff,
@@ -349,9 +349,21 @@ export async function PUT(req: NextRequest) {
delete updates.createdBy;
delete updates.createdByName;
// Re-allocate registry number when company or direction changes
const companyChanged = updates.company && updates.company !== existing.company;
const directionChanged = updates.direction && updates.direction !== existing.direction;
let newNumber: string | undefined;
if (companyChanged || directionChanged) {
const targetCompany = (updates.company ?? existing.company) as CompanyId;
const targetDirection = (updates.direction ?? existing.direction) as RegistryDirection;
const allocated = await allocateSequenceNumber(targetCompany, targetDirection);
newNumber = allocated.number;
}
const updated: RegistryEntry = {
...existing,
...updates,
...(newNumber ? { number: newNumber } : {}),
updatedAt: new Date().toISOString(),
};
@@ -364,6 +376,14 @@ export async function PUT(req: NextRequest) {
await saveEntryToDB(updated);
// Recalculate old company's counter so next entry gets the correct number
if (companyChanged || directionChanged) {
await recalculateSequence(
existing.company as CompanyId,
existing.direction as RegistryDirection,
);
}
if (diff) {
const action = updates.status === "inchis" && existing.status !== "inchis"
? "closed" as const
@@ -373,11 +393,11 @@ export async function PUT(req: NextRequest) {
await logAuditEvent({
entryId: id,
entryNumber: existing.number,
entryNumber: updated.number,
action,
actor: actor.id,
actorName: actor.name,
company: existing.company,
company: updated.company,
detail: { changes: diff },
});
}