From 28485d51f7748afc3e612c1e64f7f62a1928f641 Mon Sep 17 00:00:00 2001 From: devops Date: Tue, 20 Oct 2026 08:41:40 +0000 Subject: [PATCH] fix(blog): improve attribution layout and freshness --- backend/src/admin/blogs.service.spec.ts | 12 +++++++++ backend/src/admin/blogs.service.ts | 13 +++++++++- .../app/blog/[slug]/blog-slug-page.test.tsx | 11 +++++--- frontend/application/components/BlogPage.tsx | 26 +++++++++---------- .../components/__tests__/BlogPage.test.tsx | 4 ++- frontend/application/lib/blogDates.ts | 3 +-- 6 files changed, 49 insertions(+), 20 deletions(-) diff --git a/backend/src/admin/blogs.service.spec.ts b/backend/src/admin/blogs.service.spec.ts index 04c56330..b75a52e9 100644 --- a/backend/src/admin/blogs.service.spec.ts +++ b/backend/src/admin/blogs.service.spec.ts @@ -328,6 +328,18 @@ describe("Admin BlogsService", () => { expect(payload).not.toHaveProperty("markContentUpdated"); expect(payload).not.toHaveProperty("createdAt"); }); + it("automatically marks freshness when material article content changes", async () => { + mockPrisma.blog.findUnique.mockResolvedValue({ id: "b1", slug: "post", content: "Old content" }); + mockPrisma.blog.update.mockResolvedValue({ id: "b1", slug: "post" }); + await service.updateBlog("b1", { content: "Meaningfully revised content" }); + expect(mockPrisma.blog.update.mock.calls[0][0].data.contentUpdatedAt).toEqual(expect.any(Date)); + }); + it("does not mark freshness for attribution-only changes", async () => { + mockPrisma.blog.findUnique.mockResolvedValue({ id: "b1", slug: "post", content: "Same content" }); + mockPrisma.blog.update.mockResolvedValue({ id: "b1", slug: "post" }); + await service.updateBlog("b1", { authorTeam: "Clinical team", authorRole: "Review" }); + expect(mockPrisma.blog.update.mock.calls[0][0].data).not.toHaveProperty("contentUpdatedAt"); + }); it("allows explicitly clearing contentUpdatedAt", async () => { mockPrisma.blog.findUnique.mockResolvedValue({ id: "b1", slug: "post" }); mockPrisma.blog.update.mockResolvedValue({ id: "b1", slug: "post" }); diff --git a/backend/src/admin/blogs.service.ts b/backend/src/admin/blogs.service.ts index aedd7dd2..9c3c7e8e 100644 --- a/backend/src/admin/blogs.service.ts +++ b/backend/src/admin/blogs.service.ts @@ -312,7 +312,18 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy { } const now = new Date(); - if (markContentUpdated) { + const materialContentFields = [ + "title", + "content", + "excerpt", + "imageUrl", + "imageAlt", + "imageCaption", + ] as const; + const hasMaterialContentChange = materialContentFields.some( + (field) => data[field] !== undefined && data[field] !== blog[field], + ); + if (markContentUpdated || (contentUpdatedAt === undefined && hasMaterialContentChange)) { (updatePayload as Record).contentUpdatedAt = now; } else if (contentUpdatedAt !== undefined) { if (contentUpdatedAt === null || contentUpdatedAt === "") { 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 3ae90e4a..5f80390e 100644 --- a/frontend/application/app/blog/[slug]/blog-slug-page.test.tsx +++ b/frontend/application/app/blog/[slug]/blog-slug-page.test.tsx @@ -77,9 +77,14 @@ describe('BlogPostPage FAQPage Schema injection (SEO-P1-07)', () => { expect(faqJson.mainEntity[0].name).toBe('آیا این مکمل برای سگ مسن مناسب است؟'); }); - it('omits dateModified for invalid technical updates and uses matching Organization fallback author', async () => { - vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ok:true,json:async()=>({slug:'fresh',title:'Fresh',content:'

x

',publishedAt:'2026-01-01T01:00:00Z',contentUpdatedAt:'2026-01-01T12:00:00Z'})})); - const jsx=await BlogPostPage({params:Promise.resolve({slug:'fresh'})}); const script:any=React.Children.toArray(jsx.props.children)[0]; const json=JSON.parse(script.props.dangerouslySetInnerHTML.__html); expect(json.dateModified).toBeUndefined(); expect(json.author['@type']).toBe('Organization'); expect(json.author.name).toBe('کنینا'); + it('uses explicit same-day material freshness for dateModified and ignores technical updatedAt', async () => { + vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ok:true,json:async()=>({slug:'fresh',title:'Fresh',content:'

x

',publishedAt:'2026-01-01T01:00:00Z',updatedAt:'2026-01-01T10:00:00Z',contentUpdatedAt:'2026-01-01T12:00:00Z'})})); + const jsx=await BlogPostPage({params:Promise.resolve({slug:'fresh'})}); const script:any=React.Children.toArray(jsx.props.children)[0]; const json=JSON.parse(script.props.dangerouslySetInnerHTML.__html); expect(json.dateModified).toBe('2026-01-01T12:00:00.000Z'); expect(json.author['@type']).toBe('Organization'); expect(json.author.name).toBe('کنینا'); + }); + + it('omits dateModified when only technical updatedAt exists', async () => { + vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ok:true,json:async()=>({slug:'technical',title:'Technical',content:'

x

',publishedAt:'2026-01-01T01:00:00Z',updatedAt:'2026-01-01T12:00:00Z',contentUpdatedAt:null})})); + 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 () => { diff --git a/frontend/application/components/BlogPage.tsx b/frontend/application/components/BlogPage.tsx index fa905fff..4dc70dfe 100644 --- a/frontend/application/components/BlogPage.tsx +++ b/frontend/application/components/BlogPage.tsx @@ -347,18 +347,18 @@ export default function BlogPage({

{featuredPost.excerpt}

-
-
-
+
+
+
-
-
نویسنده: {featuredPost.author}
-
تیم: {featuredPost.authorTeam || BLOG_TEAM_FALLBACK}
- {featuredPost.authorRole &&
{featuredPost.authorRole}
} +
+
نویسنده: {featuredPost.author}
+
تیم: {featuredPost.authorTeam || BLOG_TEAM_FALLBACK}
+ {featuredPost.authorRole &&
{featuredPost.authorRole}
}
-
+
مطالعه مقاله کامل
@@ -433,12 +433,12 @@ export default function BlogPage({
-
-
- - نویسنده: {post.author}تیم: {post.authorTeam || BLOG_TEAM_FALLBACK} +
+
+
نویسنده: {post.author}
+
تیم: {post.authorTeam || BLOG_TEAM_FALLBACK}
-
+
مطالعه
diff --git a/frontend/application/components/__tests__/BlogPage.test.tsx b/frontend/application/components/__tests__/BlogPage.test.tsx index 93d1a381..fe19e389 100644 --- a/frontend/application/components/__tests__/BlogPage.test.tsx +++ b/frontend/application/components/__tests__/BlogPage.test.tsx @@ -21,7 +21,9 @@ const categories=[{id:'joints',name:'Joints',slug:'joints'},{id:'skin',name:'Ski beforeEach(()=>vi.clearAllMocks()); describe('BlogPage',()=>{ it('renders featured and regular posts as semantic links',()=>{render();expect(screen.getAllByRole('heading',{level:2})[0]).toHaveTextContent('Skin article');expect(screen.getByRole('link',{name:/Skin article/})).toHaveAttribute('href','/blog/skin');expect(screen.getByRole('link',{name:/Joint article/})).toHaveAttribute('href','/blog/joint');}); - it('labels publication, valid material update, author and stable team on featured and regular cards',()=>{const fresh=[{...blogs[0],publishedAt:'2026-01-01T08:00:00Z',contentUpdatedAt:'2026-01-02T08:00:00Z',authorTeam:'Clinical'}, {...blogs[1],publishedAt:'2026-01-02T08:00:00Z',contentUpdatedAt:'2026-01-02T12:00:00Z'}];render();expect(screen.getAllByText(/تاریخ انتشار/)).toHaveLength(2);expect(screen.getByText(/آخرین به‌روزرسانی/)).toBeInTheDocument();expect(screen.getAllByText(/نویسنده:/)).toHaveLength(2);expect(screen.getByText(/تیم: Clinical/)).toBeInTheDocument();expect(screen.getByText(/تیم: پژوهش و محتوای علمی کنینا/)).toBeInTheDocument();expect(document.querySelectorAll('time[datetime]').length).toBeGreaterThanOrEqual(3);}); + it('labels publication, including a same-day material update, author and stable team on featured and regular cards',()=>{const fresh=[{...blogs[0],publishedAt:'2026-01-01T08:00:00Z',contentUpdatedAt:'2026-01-02T08:00:00Z',authorTeam:'Clinical'}, {...blogs[1],publishedAt:'2026-01-02T08:00:00Z',contentUpdatedAt:'2026-01-02T12:00:00Z'}];render();expect(screen.getAllByText(/تاریخ انتشار/)).toHaveLength(2);expect(screen.getAllByText(/آخرین به‌روزرسانی/)).toHaveLength(2);expect(screen.getAllByText(/نویسنده:/)).toHaveLength(2);expect(screen.getByText(/تیم: Clinical/)).toBeInTheDocument();expect(screen.getByText(/تیم: پژوهش و محتوای علمی کنینا/)).toBeInTheDocument();expect(document.querySelectorAll('time[datetime]').length).toBeGreaterThanOrEqual(4);}); + it('stacks author and team on separate rows and keeps featured action on one line',()=>{render();const featuredMeta=screen.getByTestId('featured-attribution');expect(featuredMeta).toHaveClass('flex-col');expect(screen.getByTestId('featured-read-more')).toHaveClass('whitespace-nowrap');const regularMeta=screen.getByTestId('regular-attribution-1');expect(regularMeta).toHaveClass('flex-col');expect(regularMeta.children).toHaveLength(2);}); + it('keeps the featured footer responsive without forcing attribution or action to wrap',()=>{render();expect(screen.getByTestId('featured-footer')).toHaveClass('flex-col','sm:flex-row');expect(screen.getByTestId('featured-author-name')).toHaveClass('whitespace-nowrap');expect(screen.getByTestId('featured-team-name')).toHaveClass('whitespace-nowrap');}); it('combines category, excerpt search and tags then resets empty intersection',()=>{render();fireEvent.click(screen.getByRole('button',{name:'Joints'}));expect(h.replace).toHaveBeenCalledWith('/blog?categoryId=joints',{scroll:false});expect(screen.queryByText('Skin article')).not.toBeInTheDocument();fireEvent.change(screen.getByPlaceholderText('جستجو در موضوعات، بیماری‌ها، ویتامین‌ها...'),{target:{value:'MOBILITY'}});expect(screen.getByText('Joint article')).toBeInTheDocument();fireEvent.click(screen.getByRole('button',{name:'#coat'}));expect(screen.getByText('هیچ مقاله‌ای با معیارهای جستجوی شما یافت نشد.')).toBeInTheDocument();fireEvent.click(screen.getByRole('button',{name:'پاکسازی همه فیلترها'}));expect(screen.getByText('Skin article')).toBeInTheDocument();expect(screen.getByText('Joint article')).toBeInTheDocument();}); it('syncs category with route props',()=>{const view=render();expect(screen.queryByText('Skin article')).not.toBeInTheDocument();view.rerender();expect(screen.queryByText('Joint article')).not.toBeInTheDocument();}); it('clears search and toggles tags off',()=>{render();fireEvent.click(screen.getByRole('button',{name:'#health'}));expect(screen.queryByText('Skin article')).not.toBeInTheDocument();fireEvent.click(screen.getByRole('button',{name:'#health'}));expect(screen.getByText('Skin article')).toBeInTheDocument();fireEvent.change(screen.getByPlaceholderText('جستجو در موضوعات، بیماری‌ها، ویتامین‌ها...'),{target:{value:'missing'}});fireEvent.click(screen.getByRole('button',{name:'پاک کردن'}));expect(screen.getByText('Skin article')).toBeInTheDocument();}); diff --git a/frontend/application/lib/blogDates.ts b/frontend/application/lib/blogDates.ts index 50f03e4d..49ec45ba 100644 --- a/frontend/application/lib/blogDates.ts +++ b/frontend/application/lib/blogDates.ts @@ -1,5 +1,4 @@ export const BLOG_TEAM_FALLBACK = "پژوهش و محتوای علمی کنینا"; export function publicationDate(blog:any): string | undefined { return blog?.publishedAt || blog?.createdAt; } -function persianDay(value:string): string { return new Intl.DateTimeFormat("fa-IR-u-ca-persian-nu-latn",{year:"numeric",month:"2-digit",day:"2-digit",timeZone:"Asia/Tehran"}).format(new Date(value)); } -export function validContentUpdatedAt(blog:any): string | undefined { const published=publicationDate(blog), updated=blog?.contentUpdatedAt; if(!published||!updated) return undefined; const p=new Date(published),u=new Date(updated); return Number.isFinite(p.getTime())&&Number.isFinite(u.getTime())&&u>p&&persianDay(updated)!==persianDay(published)?updated:undefined; } +export function validContentUpdatedAt(blog:any): string | undefined { const published=publicationDate(blog), updated=blog?.contentUpdatedAt; if(!published||!updated) return undefined; const p=new Date(published),u=new Date(updated); return Number.isFinite(p.getTime())&&Number.isFinite(u.getTime())&&u>p?updated:undefined; } export function persianDate(value?:string): string { return value ? new Intl.DateTimeFormat("fa-IR-u-ca-persian", { year: "numeric", month: "numeric", day: "numeric", timeZone: "Asia/Tehran" }).format(new Date(value)) : ""; }