feat(blog): add material freshness and authorship

This commit is contained in:
devops committed 2026-10-20 05:36:03 +00:00
1 parent 3f5258a76c
commit 51ca8c7e34
24 files changed
+187 -52

No files matched your search

+25 -1
View File
@@ -274,6 +274,31 @@ describe("Admin BlogsService", () => {
});
});
describe("material freshness and scheduled publishing", () => {
it("sets contentUpdatedAt intentionally and never rewrites createdAt", async () => {
mockPrisma.blog.findUnique.mockResolvedValue({ id: "b1", slug: "post", createdAt: new Date("2025-01-01") });
mockPrisma.blog.update.mockResolvedValue({ id: "b1", slug: "post" });
await service.updateBlog("b1", { title: "Material edit", markContentUpdated: true });
const payload = mockPrisma.blog.update.mock.calls[0][0].data;
expect(payload.contentUpdatedAt).toEqual(expect.any(Date));
expect(payload).not.toHaveProperty("markContentUpdated");
expect(payload).not.toHaveProperty("createdAt");
});
it("allows explicitly clearing contentUpdatedAt", async () => {
mockPrisma.blog.findUnique.mockResolvedValue({ id: "b1", slug: "post" });
mockPrisma.blog.update.mockResolvedValue({ id: "b1", slug: "post" });
await service.updateBlog("b1", { contentUpdatedAt: null });
expect(mockPrisma.blog.update.mock.calls[0][0].data.contentUpdatedAt).toBeNull();
});
it("publishes due posts without rewriting createdAt", async () => {
const scheduledAt = new Date("2025-02-01T10:00:00Z");
mockPrisma.blog.findMany.mockResolvedValue([{ id: "b1", slug: "post", title: "Post", scheduledAt }]);
mockPrisma.blog.update.mockResolvedValue({});
await service.publishDueScheduledBlogs();
expect(mockPrisma.blog.update).toHaveBeenCalledWith({ where: { id: "b1" }, data: { status: "PUBLISHED", isPublished: true, publishedAt: scheduledAt } });
});
});
describe("deleteBlog", () => {
it("should throw NotFoundException if blog not found", async () => {
mockPrisma.blog.findUnique.mockResolvedValue(null);
@@ -468,7 +493,6 @@ describe("Admin BlogsService", () => {
status: "PUBLISHED",
isPublished: true,
publishedAt: scheduledDate,
createdAt: scheduledDate,
},
});
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith(
+10 -2
View File
@@ -148,6 +148,8 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy {
publishedAt,
scheduledAt,
readingTime,
markContentUpdated,
contentUpdatedAt,
...rest
} = data;
@@ -198,6 +200,7 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy {
readingTime: readingTime ? Number(readingTime) : 3,
publishedAt: finalPublishedAt,
scheduledAt: parsedScheduledAt,
contentUpdatedAt: markContentUpdated ? now : contentUpdatedAt ? new Date(contentUpdatedAt) : null,
author: { connect: { id: targetAuthorId } },
...(categoryId ? { category: { connect: { id: categoryId } } } : {}),
...(tagConnectOrCreate.length > 0
@@ -230,12 +233,19 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy {
publishedAt,
scheduledAt,
readingTime,
markContentUpdated,
contentUpdatedAt,
...rest
} = data;
const now = new Date();
const updatePayload: Prisma.BlogUpdateInput = {
...rest,
...(markContentUpdated
? { contentUpdatedAt: now }
: contentUpdatedAt !== undefined
? { contentUpdatedAt: contentUpdatedAt ? new Date(contentUpdatedAt) : null }
: {}),
...(readingTime !== undefined
? { readingTime: Number(readingTime) }
: {}),
@@ -259,7 +269,6 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy {
updatePayload.status = "PUBLISHED";
updatePayload.isPublished = true;
updatePayload.publishedAt = parsedScheduledAt;
updatePayload.createdAt = parsedScheduledAt;
}
} else if (publishedAt !== undefined) {
updatePayload.publishedAt = publishedAt ? new Date(publishedAt) : null;
@@ -472,7 +481,6 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy {
status: "PUBLISHED",
isPublished: true,
publishedAt: publishDate,
createdAt: publishDate,
},
});
+19 -1
View File
@@ -50,7 +50,7 @@ describe("BlogsService", () => {
expect(result.data).toEqual(blogs);
expect(result.meta.total).toBe(1);
const query = mockPrisma.blog.findMany.mock.calls[0][0];
expect(query.include.author).toBeUndefined();
expect(query.include.author).toEqual({ select: { id: true, firstName: true, lastName: true } });
});
it("should filter by categorySlug and search term", async () => {
@@ -69,7 +69,19 @@ describe("BlogsService", () => {
});
});
it("selects the minimal public author identity for list results", async () => {
mockPrisma.blog.findMany.mockResolvedValue([]); mockPrisma.blog.count.mockResolvedValue(0);
await service.findAll({});
expect(mockPrisma.blog.findMany.mock.calls[0][0].include.author).toEqual({ select: { id: true, firstName: true, lastName: true } });
});
describe("findHomepage", () => {
it("selects the minimal public author identity for homepage results", async () => {
mockPrisma.blog.findMany.mockResolvedValueOnce([]).mockResolvedValueOnce([]);
await service.findHomepage();
for (const call of mockPrisma.blog.findMany.mock.calls.slice(0, 2)) expect(call[0].include.author).toEqual({ select: { id: true, firstName: true, lastName: true } });
});
it("should return selected blogs if showOnHome posts exist", async () => {
const selected = [{ id: "b1", title: "Hero Blog", showOnHome: true }];
mockPrisma.blog.findMany.mockResolvedValueOnce(selected);
@@ -92,6 +104,12 @@ describe("BlogsService", () => {
});
describe("findOneBySlug", () => {
it("selects the minimal public author identity for detail results", async () => {
mockPrisma.blog.findFirst.mockResolvedValue(null);
await expect(service.findOneBySlug("missing")).rejects.toThrow(NotFoundException);
expect(mockPrisma.blog.findFirst.mock.calls[0][0].include.author).toEqual({ select: { id: true, firstName: true, lastName: true } });
});
it("should throw NotFoundException if slug is empty or blog not found", async () => {
mockPrisma.blog.findFirst.mockResolvedValue(null);
await expect(service.findOneBySlug("unknown-slug")).rejects.toThrow(
+10
View File
@@ -68,6 +68,9 @@ export class BlogsService {
[sortBy === "createdAt" ? "createdAt" : "publishedAt"]: sortOrder,
},
include: {
author: {
select: { id: true, firstName: true, lastName: true },
},
category: {
select: { id: true, name: true, slug: true },
},
@@ -107,6 +110,9 @@ export class BlogsService {
take: 4,
orderBy: { publishedAt: "desc" },
include: {
author: {
select: { id: true, firstName: true, lastName: true },
},
category: { select: { id: true, name: true, slug: true } },
tags: { select: { id: true, name: true, slug: true } },
_count: { select: { comments: { where: { status: "APPROVED" } } } },
@@ -123,6 +129,9 @@ export class BlogsService {
take: 4,
orderBy: { publishedAt: "desc" },
include: {
author: {
select: { id: true, firstName: true, lastName: true },
},
category: { select: { id: true, name: true, slug: true } },
tags: { select: { id: true, name: true, slug: true } },
_count: { select: { comments: { where: { status: "APPROVED" } } } },
@@ -158,6 +167,7 @@ export class BlogsService {
],
},
include: {
author: { select: { id: true, firstName: true, lastName: true } },
category: {
select: { id: true, name: true, slug: true },
},