fix: make sure authors are removed when no models assigned to them

This commit is contained in:
Tautvydas Derzinskas committed 2026-09-12 09:48:57 +02:00
1 parent 37ac318e40
commit 55f5aaa4a0
2 files changed
+15 -1

No files matched your search

+4 -1
View File
@@ -11,6 +11,7 @@ import { modelUpload } from "../uploadMiddleware";
import { createPrint, deletePlateFiles, resolvePlateFilePath, type NewPlateInput } from "../services/printCreation";
import { plateThumbPath, relocatePrint, uniqueModelName } from "../services/printService";
import { previewImagePath, deleteAllPreviewImages } from "../services/previewImageService";
import { deleteAuthorIfOrphaned } from "../services/authorService";
import { toPrintOut } from "../dto";
import { loadFullPrint, printOutById } from "../services/printLoader";
import { deleteAllPrintFiles } from "../services/printFileService";
@@ -466,7 +467,8 @@ router.post(
// Clears the imported author/creator and import-source linkage, so the print reads exactly like
// one this user uploaded themselves (a plain upload never sets authorId/creator/sourceProvider/
// sourceExternalId -- see createPrint in printCreation.ts). Used by the Edit modal's "reset
// author" action.
// author" action. If that was the last print anywhere referencing this Author, the row itself is
// deleted too (see deleteAuthorIfOrphaned) rather than left behind with nothing pointing to it.
router.post(
"/print/:id/author-reset",
asyncHandler(async (req, res) => {
@@ -476,6 +478,7 @@ router.post(
where: { id: print.id },
data: { authorId: null, creator: null, sourceProvider: null, sourceExternalId: null },
});
if (print.authorId) await deleteAuthorIfOrphaned(print.authorId);
res.json({ print: await printOutById(req.userId!, print.id) });
void createLog({ userId: req.userId!, action: "model_edited", targetId: print.id, details: { field: "author_reset", name: print.name } });
}),
+11
View File
@@ -43,3 +43,14 @@ export async function upsertAuthorFromImport(info: ImportedAuthorInfo | null): P
return null;
}
}
/** Deletes an Author row once nothing references it any more. Author rows aren't user-scoped --
* two different users importing the same MakerWorld/Thingiverse/Printables creator share one row
* -- so this checks Print.authorId across every user, not just the caller's. Used after clearing
* a print's authorId (the Edit modal's "reset author" action) to avoid leaving an orphaned Author
* behind once the last print referencing it has been detached. */
export async function deleteAuthorIfOrphaned(authorId: string): Promise<void> {
const remaining = await prisma.print.count({ where: { authorId } });
if (remaining > 0) return;
await prisma.author.delete({ where: { id: authorId } }).catch(() => undefined);
}