fix(admin-panel): fix blog bugs and add ingredients editor, draft autosave, and category seeding
This commit is contained in:
1 parent
ebc61d5be2
commit
913e6acd3d
11 files changed
+375
-20
No files matched your search
@@ -445,7 +445,7 @@ export class AdminService {
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: { [sortField]: sortDirection },
|
||||
include: { category: true, symptoms: true },
|
||||
include: { category: true, symptoms: true, ingredientList: true },
|
||||
}),
|
||||
this.prisma.product.count({ where }),
|
||||
]);
|
||||
@@ -516,6 +516,7 @@ export class AdminService {
|
||||
scientificTagline: data.scientificTagline || '',
|
||||
description: data.description || '',
|
||||
shortDescription: data.shortDescription || '',
|
||||
ingredients: data.ingredients || null,
|
||||
categoryId: data.categoryId || '',
|
||||
categorySlug: data.categorySlug || 'general',
|
||||
buyPrice: data.buyPrice !== undefined ? data.buyPrice : 0,
|
||||
@@ -587,6 +588,18 @@ export class AdminService {
|
||||
});
|
||||
}
|
||||
|
||||
if (data.ingredientList && Array.isArray(data.ingredientList)) {
|
||||
const uniqueIngs = Array.from(new Set(data.ingredientList.map((i: string) => i.trim()).filter(Boolean)));
|
||||
if (uniqueIngs.length > 0) {
|
||||
await this.prisma.productIngredient.createMany({
|
||||
data: uniqueIngs.map((ingredient: string) => ({
|
||||
productId: product.id,
|
||||
ingredient,
|
||||
})),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await this.revalidationService.revalidateProduct(
|
||||
product.slug,
|
||||
product.artNo,
|
||||
@@ -658,6 +671,7 @@ export class AdminService {
|
||||
scientificTagline: data.scientificTagline,
|
||||
description: data.description,
|
||||
shortDescription: data.shortDescription,
|
||||
ingredients: data.ingredients !== undefined ? data.ingredients : undefined,
|
||||
categoryId: data.categoryId,
|
||||
categorySlug: data.categorySlug,
|
||||
buyPrice: data.buyPrice !== undefined ? data.buyPrice : undefined,
|
||||
@@ -742,11 +756,27 @@ export class AdminService {
|
||||
}
|
||||
}
|
||||
|
||||
if (data.ingredientList !== undefined && Array.isArray(data.ingredientList)) {
|
||||
await this.prisma.productIngredient.deleteMany({ where: { productId: id } });
|
||||
if (data.ingredientList.length > 0) {
|
||||
const uniqueIngs = Array.from(new Set(data.ingredientList.map((i: string) => i.trim()).filter(Boolean)));
|
||||
await this.prisma.productIngredient.createMany({
|
||||
data: uniqueIngs.map((ingredient: string) => ({
|
||||
productId: id,
|
||||
ingredient,
|
||||
})),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await this.revalidationService.revalidateProduct(
|
||||
product.slug,
|
||||
product.artNo,
|
||||
);
|
||||
return product;
|
||||
return this.prisma.product.findUnique({
|
||||
where: { id },
|
||||
include: { category: true, symptoms: true, ingredientList: true },
|
||||
});
|
||||
}
|
||||
|
||||
async deleteProduct(id: string) {
|
||||
|
||||
@@ -1,16 +1,42 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { Injectable, NotFoundException, OnModuleInit } from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { PaginationDto } from '../common/dto/pagination.dto';
|
||||
import { RevalidationService } from '../common/revalidation/revalidation.service';
|
||||
|
||||
@Injectable()
|
||||
export class BlogsService {
|
||||
export class BlogsService implements OnModuleInit {
|
||||
constructor(
|
||||
private prisma: PrismaService,
|
||||
private revalidationService: RevalidationService,
|
||||
) {}
|
||||
|
||||
async onModuleInit() {
|
||||
await this.seedDefaultCategories();
|
||||
}
|
||||
|
||||
private async seedDefaultCategories() {
|
||||
try {
|
||||
const defaultCategories = [
|
||||
{ name: 'تغذیه و رژیم غذایی', slug: 'nutrition', order: 1 },
|
||||
{ name: 'سلامت گوارش و متابولیسم', slug: 'digestion', order: 2 },
|
||||
{ name: 'مفاصل، استخوان و اسکلت', slug: 'joints-bones', order: 3 },
|
||||
{ name: 'پوست، مو و آلرژی', slug: 'skin-coat', order: 4 },
|
||||
{ name: 'ایمنی، پیشگیری و مراقبت بالینی', slug: 'health-prevention', order: 5 },
|
||||
];
|
||||
|
||||
for (const cat of defaultCategories) {
|
||||
await this.prisma.blogCategory.upsert({
|
||||
where: { slug: cat.slug },
|
||||
update: { name: cat.name, order: cat.order },
|
||||
create: { name: cat.name, slug: cat.slug, order: cat.order },
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('[BlogsService] Failed to seed default blog categories:', err);
|
||||
}
|
||||
}
|
||||
|
||||
async getBlogs(
|
||||
query: PaginationDto & {
|
||||
status?: string;
|
||||
|
||||
@@ -38,6 +38,16 @@ export class ProductDto {
|
||||
@IsString()
|
||||
shortDescription?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'متن ترکیبات و مواد تشکیلدهنده محصول' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
ingredients?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'لیست ترکیبات کلیدی و مواد مؤثره محصول' })
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
ingredientList?: string[];
|
||||
|
||||
@ApiPropertyOptional({ description: 'شناسه دستهبندی' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
|
||||
@@ -12,7 +12,18 @@ export class MediaService {
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
|
||||
return items.map((item) => {
|
||||
const seenUrls = new Set<string>();
|
||||
const deduplicated = [];
|
||||
|
||||
for (const item of items) {
|
||||
const normalizedUrl = (item.url || '').trim().toLowerCase();
|
||||
if (normalizedUrl && seenUrls.has(normalizedUrl)) {
|
||||
continue;
|
||||
}
|
||||
if (normalizedUrl) {
|
||||
seenUrls.add(normalizedUrl);
|
||||
}
|
||||
|
||||
let cleanFilename = item.filename;
|
||||
// Check if filename contains common mojibake patterns from latin1 decoding
|
||||
if (cleanFilename && /[ØÙÚÛ]/.test(cleanFilename)) {
|
||||
@@ -25,11 +36,14 @@ export class MediaService {
|
||||
// Keep original if decoding fails
|
||||
}
|
||||
}
|
||||
return {
|
||||
|
||||
deduplicated.push({
|
||||
...item,
|
||||
filename: cleanFilename,
|
||||
};
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return deduplicated;
|
||||
}
|
||||
|
||||
async uploadFile(file: Express.Multer.File) {
|
||||
|
||||
Reference in new issue
Block a user