fix(blog): resolve 404 and 500 error on blog post slug navigation
Deploy Canina / deploy (push) Has been cancelled

This commit is contained in:
parsa aghaei committed 2026-08-25 10:21:50 +03:30
1 parent 1ddfb0a823
commit 088f8e20bb
19 files changed
+145201 -5346

No files matched your search

+99 -59
View File
@@ -137,10 +137,28 @@ export class BlogsService {
return { mode: 'newest', blogs: newestBlogs };
}
async findOneBySlug(slug: string) {
const now = new Date();
const blog = await this.prisma.blog.findUnique({
where: { slug },
async findOneBySlug(rawSlug: string) {
if (!rawSlug) {
throw new NotFoundException('مقاله یافت نشد');
}
let decodedSlug = rawSlug;
try {
decodedSlug = decodeURIComponent(rawSlug);
} catch {
decodedSlug = rawSlug;
}
const isUuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(decodedSlug);
const blog = await this.prisma.blog.findFirst({
where: {
OR: [
{ slug: decodedSlug },
{ slug: rawSlug },
...(isUuid ? [{ id: decodedSlug }] : []),
],
},
include: {
author: {
select: { firstName: true, lastName: true },
@@ -161,68 +179,90 @@ export class BlogsService {
throw new NotFoundException('مقاله یافت نشد');
}
// Fetch related products if any
// Fetch related products safely
let relatedProducts: any[] = [];
if (blog.relatedProductIds && blog.relatedProductIds.length > 0) {
relatedProducts = await this.prisma.product.findMany({
where: { id: { in: blog.relatedProductIds } },
select: {
id: true,
nameFa: true,
slug: true,
priceValue: true,
priceDisplay: true,
imageUrl: true,
categorySlug: true,
unit: true,
},
});
if (blog.relatedProductIds && Array.isArray(blog.relatedProductIds) && blog.relatedProductIds.length > 0) {
const validProductUuids = blog.relatedProductIds.filter(
(id) => typeof id === 'string' && /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(id)
);
if (validProductUuids.length > 0) {
try {
relatedProducts = await this.prisma.product.findMany({
where: { id: { in: validProductUuids } },
select: {
id: true,
nameFa: true,
slug: true,
priceValue: true,
priceDisplay: true,
imageUrl: true,
categorySlug: true,
unit: true,
},
});
} catch (err) {
console.warn('[BlogsService] Error fetching related products:', err);
}
}
}
// Fetch related blogs if any or fallback to same category
// Fetch related blogs safely
let relatedBlogs: any[] = [];
if (blog.relatedBlogIds && blog.relatedBlogIds.length > 0) {
relatedBlogs = await this.prisma.blog.findMany({
where: {
id: { in: blog.relatedBlogIds },
isPublished: true,
status: 'PUBLISHED',
},
select: {
id: true,
title: true,
slug: true,
imageUrl: true,
readingTime: true,
publishedAt: true,
createdAt: true,
category: { select: { name: true, slug: true } },
},
});
if (blog.relatedBlogIds && Array.isArray(blog.relatedBlogIds) && blog.relatedBlogIds.length > 0) {
const validBlogUuids = blog.relatedBlogIds.filter(
(id) => typeof id === 'string' && /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(id)
);
if (validBlogUuids.length > 0) {
try {
relatedBlogs = await this.prisma.blog.findMany({
where: {
id: { in: validBlogUuids },
isPublished: true,
status: 'PUBLISHED',
},
select: {
id: true,
title: true,
slug: true,
imageUrl: true,
readingTime: true,
publishedAt: true,
createdAt: true,
category: { select: { name: true, slug: true } },
},
});
} catch (err) {
console.warn('[BlogsService] Error fetching related blogs:', err);
}
}
} else if (blog.categoryId) {
relatedBlogs = await this.prisma.blog.findMany({
where: {
categoryId: blog.categoryId,
id: { not: blog.id },
isPublished: true,
status: 'PUBLISHED',
},
take: 3,
select: {
id: true,
title: true,
slug: true,
imageUrl: true,
readingTime: true,
publishedAt: true,
createdAt: true,
category: { select: { name: true, slug: true } },
},
});
try {
relatedBlogs = await this.prisma.blog.findMany({
where: {
categoryId: blog.categoryId,
id: { not: blog.id },
isPublished: true,
status: 'PUBLISHED',
},
take: 3,
select: {
id: true,
title: true,
slug: true,
imageUrl: true,
readingTime: true,
publishedAt: true,
createdAt: true,
category: { select: { name: true, slug: true } },
},
});
} catch (err) {
console.warn('[BlogsService] Error fetching fallback related blogs:', err);
}
}
// Increment view count asynchronously
await this.prisma.blog
this.prisma.blog
.update({
where: { id: blog.id },
data: { viewCount: { increment: 1 } },
@@ -231,7 +271,7 @@ export class BlogsService {
return {
...blog,
viewCount: blog.viewCount + 1,
viewCount: (blog.viewCount || 0) + 1,
relatedProducts,
relatedBlogs,
};