From bf3334878ae43835adfe898ca355607b9b5c1b0f Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Sat, 24 Oct 2026 23:04:40 +0000 Subject: [PATCH] fix(cms): honor explicit null author without defaulting to publisher --- backend/src/admin/blogs.service.spec.ts | 36 +++++++++++++++++++ backend/src/admin/blogs.service.ts | 15 ++++++-- .../app/blog/[slug]/blog-slug-page.test.tsx | 6 ++-- 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/backend/src/admin/blogs.service.spec.ts b/backend/src/admin/blogs.service.spec.ts index b75a52e9..fff12520 100644 --- a/backend/src/admin/blogs.service.spec.ts +++ b/backend/src/admin/blogs.service.spec.ts @@ -274,6 +274,42 @@ describe("Admin BlogsService", () => { }); }); + describe("explicit author contract", () => { + beforeEach(() => { + mockPrisma.blog.create.mockResolvedValue({ id: "b1", slug: "post" }); + mockPrisma.blog.findUnique.mockResolvedValue({ id: "b1", slug: "post" }); + mockPrisma.blog.update.mockResolvedValue({ id: "b1", slug: "post" }); + }); + + it.each([ + ["omitted", {}, { connect: { id: "current-user" } }], + ["legacy empty", { authorId: "" }, { connect: { id: "current-user" } }], + ["selected", { authorId: "selected-user" }, { connect: { id: "selected-user" } }], + ["authorless", { authorId: null }, undefined], + ])("creates with %s author without inventing an identity", async (_label, input, expected) => { + await service.createBlog({ title: "Post", slug: "post", content: "Content", ...input }, "current-user"); + const payload = mockPrisma.blog.create.mock.calls[0][0].data; + expect(payload.author).toEqual(expected); + if (expected === undefined) expect(payload).not.toHaveProperty("author"); + expect(payload).not.toHaveProperty("authorId"); + expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("post"); + }); + + it.each([ + ["omitted", {}, undefined], + ["legacy empty", { authorId: "" }, undefined], + ["selected", { authorId: "selected-user" }, { connect: { id: "selected-user" } }], + ["authorless", { authorId: null }, { disconnect: true }], + ])("updates with %s author while preserving omitted values", async (_label, input, expected) => { + await service.updateBlog("b1", input); + const payload = mockPrisma.blog.update.mock.calls[0][0].data; + expect(payload.author).toEqual(expected); + if (expected === undefined) expect(payload).not.toHaveProperty("author"); + expect(payload).not.toHaveProperty("authorId"); + expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("post"); + }); + }); + describe("updateBlog", () => { it("should throw NotFoundException if blog does not exist", async () => { mockPrisma.blog.findUnique.mockResolvedValue(null); diff --git a/backend/src/admin/blogs.service.ts b/backend/src/admin/blogs.service.ts index 9c3c7e8e..14f0927a 100644 --- a/backend/src/admin/blogs.service.ts +++ b/backend/src/admin/blogs.service.ts @@ -185,7 +185,9 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy { } } - const targetAuthorId = authorId || currentUserId; + // Explicit null requests Organization attribution; omission/legacy empty + // retains the authenticated-author default. No synthetic User is created. + const targetAuthorId = authorId === null ? null : authorId || currentUserId; const now = new Date(); const isFutureSchedule = parsedScheduledAt && parsedScheduledAt > now; @@ -239,7 +241,9 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy { publishedAt: finalPublishedAt, scheduledAt: parsedScheduledAt, contentUpdatedAt: parsedContentUpdatedAt, - author: { connect: { id: targetAuthorId } }, + ...(targetAuthorId !== null + ? { author: { connect: { id: targetAuthorId } } } + : {}), ...(categoryId ? { category: { connect: { id: categoryId } } } : {}), ...(tagConnectOrCreate.length > 0 ? { tags: { connectOrCreate: tagConnectOrCreate } } @@ -344,7 +348,12 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy { ...(readingTime !== undefined ? { readingTime: Number(readingTime) } : {}), - ...(authorId ? { author: { connect: { id: authorId } } } : {}), + // null clears attribution; omitted/legacy empty leaves it unchanged. + ...(authorId === null + ? { author: { disconnect: true } } + : authorId + ? { author: { connect: { id: authorId } } } + : {}), ...(categoryId !== undefined ? categoryId ? { category: { connect: { id: categoryId } } } diff --git a/frontend/application/app/blog/[slug]/blog-slug-page.test.tsx b/frontend/application/app/blog/[slug]/blog-slug-page.test.tsx index 5f80390e..2718a50b 100644 --- a/frontend/application/app/blog/[slug]/blog-slug-page.test.tsx +++ b/frontend/application/app/blog/[slug]/blog-slug-page.test.tsx @@ -87,9 +87,9 @@ describe('BlogPostPage FAQPage Schema injection (SEO-P1-07)', () => { const jsx=await BlogPostPage({params:Promise.resolve({slug:'technical'})}); const script:any=React.Children.toArray(jsx.props.children)[0]; const json=JSON.parse(script.props.dangerouslySetInnerHTML.__html); expect(json.dateModified).toBeUndefined(); }); - it('uses the stable Organization fallback visibly and in metadata', async () => { + it.each([null, { firstName: '', lastName: '' }])('uses the stable Organization fallback for author %j visibly and in metadata', async (author) => { vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true, json: async () => ({ - slug: 'authorless', title: 'Authorless', content: '

x

', publishedAt: '2026-01-01T00:00:00Z', author: { firstName: '', lastName: '' }, + slug: 'authorless', title: 'Authorless', content: '

x

', publishedAt: '2026-01-01T00:00:00Z', author, authorTeam: null, }) })); const metadata = await generateMetadata({ params: Promise.resolve({ slug: 'authorless' }) }); @@ -106,7 +106,7 @@ describe('BlogPostPage FAQPage Schema injection (SEO-P1-07)', () => { }); }); - it('uses a custom authorTeam as the authorless Organization identity in metadata and rendering', async () => { + it('keeps custom team attribution separate from the Organization author identity', async () => { const organizationName = 'تیم دامپزشکی کنینا'; vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true, json: async () => ({ slug: 'custom-team',