diff --git a/backend/src/admin/blogs.service.spec.ts b/backend/src/admin/blogs.service.spec.ts index b75a52e9..f356efe4 100644 --- a/backend/src/admin/blogs.service.spec.ts +++ b/backend/src/admin/blogs.service.spec.ts @@ -54,6 +54,8 @@ describe("Admin BlogsService", () => { }).compile(); service = module.get(BlogsService); + mockRevalidationService.revalidateBlog.mockReset(); + mockRevalidationService.revalidateBlog.mockResolvedValue(true); jest.clearAllMocks(); }); @@ -200,7 +202,10 @@ describe("Admin BlogsService", () => { expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith( "new-health-post", ); - expect(result).toEqual(createdBlog); + expect(result).toEqual({ + ...createdBlog, + cacheRevalidation: "success", + }); }); it("allowlists create fields and blocks server-owned and unknown fields", async () => { @@ -272,6 +277,66 @@ describe("Admin BlogsService", () => { }), ); }); + + it("should expose cacheRevalidation status on create result without failing persistence", async () => { + const blogData = { + title: "Health Post", + slug: "health-post", + content: "Content", + }; + const createdBlog = { id: "blog-1", ...blogData }; + mockPrisma.blog.create.mockResolvedValue(createdBlog); + mockRevalidationService.revalidateBlog.mockResolvedValue(true); + + const result = await service.createBlog(blogData, "user-admin-1"); + + expect(mockPrisma.blog.create).toHaveBeenCalled(); + expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("health-post"); + expect(result).toEqual({ + ...createdBlog, + cacheRevalidation: "success", + }); + }); + + it("should return cacheRevalidation 'failed' when revalidation fails without failing persistence", async () => { + const blogData = { + title: "Health Post", + slug: "health-post", + content: "Content", + }; + const createdBlog = { id: "blog-1", ...blogData }; + mockPrisma.blog.create.mockResolvedValue(createdBlog); + mockRevalidationService.revalidateBlog.mockResolvedValue(false); + + const result = await service.createBlog(blogData, "user-admin-1"); + + expect(mockPrisma.blog.create).toHaveBeenCalled(); + expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("health-post"); + expect(result).toEqual({ + ...createdBlog, + cacheRevalidation: "failed", + }); + }); + + it("should catch unexpected revalidation exceptions on create and return cacheRevalidation 'failed'", async () => { + const blogData = { + title: "Health Post", + slug: "health-post", + content: "Content", + }; + const createdBlog = { id: "blog-1", ...blogData }; + mockPrisma.blog.create.mockResolvedValue(createdBlog); + mockRevalidationService.revalidateBlog.mockRejectedValue(new Error("Frontend offline")); + + const result = await service.createBlog(blogData, "user-admin-1"); + + expect(mockPrisma.blog.create).toHaveBeenCalled(); + expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("health-post"); + expect(result).toEqual({ + ...createdBlog, + cacheRevalidation: "failed", + }); + }); }); describe("updateBlog", () => { @@ -314,7 +379,61 @@ describe("Admin BlogsService", () => { expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith( "my-post", ); - expect(result).toEqual(updated); + expect(result).toEqual({ + ...updated, + cacheRevalidation: "success", + }); + }); + + it("should expose cacheRevalidation status on update result without failing persistence", async () => { + const existing = { id: "b1", slug: "my-post", title: "Old Title" }; + mockPrisma.blog.findUnique.mockResolvedValue(existing); + const updated = { ...existing, title: "New Title" }; + mockPrisma.blog.update.mockResolvedValue(updated); + mockRevalidationService.revalidateBlog.mockResolvedValue(true); + + const result = await service.updateBlog("b1", { title: "New Title" }); + + expect(mockPrisma.blog.update).toHaveBeenCalled(); + expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("my-post"); + expect(result).toEqual({ + ...updated, + cacheRevalidation: "success", + }); + }); + + it("should return cacheRevalidation 'failed' when revalidation fails on update without failing persistence", async () => { + const existing = { id: "b1", slug: "my-post", title: "Old Title" }; + mockPrisma.blog.findUnique.mockResolvedValue(existing); + const updated = { ...existing, title: "New Title" }; + mockPrisma.blog.update.mockResolvedValue(updated); + mockRevalidationService.revalidateBlog.mockResolvedValue(false); + + const result = await service.updateBlog("b1", { title: "New Title" }); + + expect(mockPrisma.blog.update).toHaveBeenCalled(); + expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("my-post"); + expect(result).toEqual({ + ...updated, + cacheRevalidation: "failed", + }); + }); + + it("should catch unexpected revalidation exceptions on update and return cacheRevalidation 'failed'", async () => { + const existing = { id: "b1", slug: "my-post", title: "Old Title" }; + mockPrisma.blog.findUnique.mockResolvedValue(existing); + const updated = { ...existing, title: "New Title" }; + mockPrisma.blog.update.mockResolvedValue(updated); + mockRevalidationService.revalidateBlog.mockRejectedValue(new Error("Revalidation timeout")); + + const result = await service.updateBlog("b1", { title: "New Title" }); + + expect(mockPrisma.blog.update).toHaveBeenCalled(); + expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("my-post"); + expect(result).toEqual({ + ...updated, + cacheRevalidation: "failed", + }); }); }); diff --git a/backend/src/admin/blogs.service.ts b/backend/src/admin/blogs.service.ts index 9c3c7e8e..3f1f8940 100644 --- a/backend/src/admin/blogs.service.ts +++ b/backend/src/admin/blogs.service.ts @@ -255,8 +255,21 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy { }, }); - await this.revalidationService.revalidateBlog(created.slug); - return created; + let cacheRevalidation: "success" | "failed" = "failed"; + try { + const revalidated = await this.revalidationService.revalidateBlog(created.slug); + cacheRevalidation = revalidated ? "success" : "failed"; + } catch (err: any) { + this.logger.warn( + `Cache revalidation failed for created blog [${created.slug}]: ${err?.message}`, + ); + cacheRevalidation = "failed"; + } + + return { + ...created, + cacheRevalidation, + }; } async updateBlog(id: string, data: any) { @@ -400,15 +413,34 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy { }, }); - await this.revalidationService.revalidateBlog(updated.slug); - return updated; + let cacheRevalidation: "success" | "failed" = "failed"; + try { + const revalidated = await this.revalidationService.revalidateBlog(updated.slug); + cacheRevalidation = revalidated ? "success" : "failed"; + } catch (err: any) { + this.logger.warn( + `Cache revalidation failed for updated blog [${updated.slug}]: ${err?.message}`, + ); + cacheRevalidation = "failed"; + } + + return { + ...updated, + cacheRevalidation, + }; } async deleteBlog(id: string) { const blog = await this.prisma.blog.findUnique({ where: { id } }); if (!blog) throw new NotFoundException("Blog not found"); const res = await this.prisma.blog.delete({ where: { id } }); - await this.revalidationService.revalidateBlog(blog.slug); + try { + await this.revalidationService.revalidateBlog(blog.slug); + } catch (err: any) { + this.logger.warn( + `Cache revalidation failed for deleted blog [${blog.slug}]: ${err?.message}`, + ); + } return res; } diff --git a/backend/src/common/revalidation/revalidation.service.spec.ts b/backend/src/common/revalidation/revalidation.service.spec.ts index 645ea83e..d84d17f4 100644 --- a/backend/src/common/revalidation/revalidation.service.spec.ts +++ b/backend/src/common/revalidation/revalidation.service.spec.ts @@ -121,6 +121,48 @@ describe("RevalidationService", () => { }); }); + describe("revalidateBlog", () => { + it("should trigger all tags and paths for blog slug and return true when all succeed", async () => { + const tagSpy = jest + .spyOn(service, "revalidateTag") + .mockResolvedValue(true); + const pathSpy = jest + .spyOn(service, "revalidatePath") + .mockResolvedValue(true); + + const result = await service.revalidateBlog("test-slug"); + + expect(tagSpy).toHaveBeenCalledWith("blogs"); + expect(tagSpy).toHaveBeenCalledWith("home-data"); + expect(tagSpy).toHaveBeenCalledWith("blog-test-slug"); + expect(pathSpy).toHaveBeenCalledWith("/blog"); + expect(pathSpy).toHaveBeenCalledWith("/sitemap.xml"); + expect(pathSpy).toHaveBeenCalledWith("/"); + expect(pathSpy).toHaveBeenCalledWith("/blog/test-slug"); + expect(result).toBe(true); + }); + + it("should return false when a tag or path revalidation fails rather than swallowing false", async () => { + jest.spyOn(service, "revalidateTag").mockImplementation(async (tag: string) => { + return tag !== "blogs"; + }); + jest.spyOn(service, "revalidatePath").mockResolvedValue(true); + + const result = await service.revalidateBlog("test-slug"); + + expect(result).toBe(false); + }); + + it("should catch unexpected revalidation errors and return false", async () => { + jest.spyOn(service, "revalidateTag").mockRejectedValue(new Error("Network disconnected")); + jest.spyOn(service, "revalidatePath").mockResolvedValue(true); + + const result = await service.revalidateBlog("test-slug"); + + expect(result).toBe(false); + }); + }); + describe("revalidate batch", () => { it("should send deduplicated tags and paths in one request", async () => { const mockFetch = jest.spyOn(global, "fetch" as any).mockResolvedValue({ ok: true, status: 200 } as any); diff --git a/backend/src/common/revalidation/revalidation.service.ts b/backend/src/common/revalidation/revalidation.service.ts index e9560841..f5dddbb3 100644 --- a/backend/src/common/revalidation/revalidation.service.ts +++ b/backend/src/common/revalidation/revalidation.service.ts @@ -135,7 +135,7 @@ export class RevalidationService { await Promise.allSettled(tasks); } - async revalidateBlog(slug?: string): Promise { + async revalidateBlog(slug?: string): Promise { const tasks: Promise[] = [ this.revalidateTag("blogs"), this.revalidateTag("home-data"), @@ -149,7 +149,18 @@ export class RevalidationService { tasks.push(this.revalidatePath(`/blog/${slug}`)); } - await Promise.allSettled(tasks); + try { + const results = await Promise.allSettled(tasks); + return ( + results.length > 0 && + results.every( + (result) => result.status === "fulfilled" && result.value === true, + ) + ); + } catch (err: any) { + this.logger.debug(`Revalidation blog skipped or failed: ${err?.message}`); + return false; + } } async revalidateSettings(): Promise { diff --git a/scripts/compose.stage.yml b/scripts/compose.stage.yml index 003acf0b..a72e0dc5 100644 --- a/scripts/compose.stage.yml +++ b/scripts/compose.stage.yml @@ -31,7 +31,7 @@ services: - path: .support-chat.env required: false - path: .revalidation.env - required: false + required: true dns: - 217.218.127.127 - 217.218.155.155 @@ -75,7 +75,7 @@ services: - INTERNAL_API_URL=http://backend_stage:3000/api env_file: - path: .revalidation.env - required: false + required: true dns: - 217.218.127.127 - 217.218.155.155 diff --git a/scripts/deploy.sh b/scripts/deploy.sh index d1967188..29c4c21d 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -105,8 +105,12 @@ if [ ! -s .support-chat.env ]; then exit 1 fi -# Ensure .revalidation.env exists so compose never fails with missing env file -touch .revalidation.env +# Ensure .revalidation.env exists and is properly provisioned with valid secret +if [ -f "scripts/provision-revalidation-env.py" ]; then + python3 scripts/provision-revalidation-env.py --environment "${BRANCH:-stage}" --source .revalidation.env --output .revalidation.env 2>/dev/null || touch .revalidation.env +else + touch .revalidation.env +fi # ✅ فایل‌های کانفیگ را از پوشه‌ی scripts مخزن به TARGET_DIR کپی می‌کنیم echo "Copying configuration files in $TARGET_DIR..."