175 lines
6.2 KiB
TypeScript
175 lines
6.2 KiB
TypeScript
import { PrismaClient } from '@prisma/client';
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
import * as ts from 'typescript';
|
|
import * as vm from 'vm';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
async function main() {
|
|
console.log('Seeding products, categories, and blogs...');
|
|
|
|
// 1. Create a default category
|
|
const category = await prisma.category.upsert({
|
|
where: { slug: 'supplements' },
|
|
update: {},
|
|
create: {
|
|
name: 'مکملهای غذایی و درمانی',
|
|
slug: 'supplements',
|
|
description: 'انواع مکملهای اورجینال کانینو آلمان',
|
|
metaTitle: 'خرید مکمل کانینو',
|
|
metaDescription: 'بهترین مکملهای کانینو'
|
|
}
|
|
});
|
|
|
|
// 2. Read products.ts
|
|
const productsFilePath = path.join(__dirname, '..', '..', 'frontend', 'application', 'lib', 'data', 'products.ts');
|
|
if (fs.existsSync(productsFilePath)) {
|
|
const fileContent = fs.readFileSync(productsFilePath, 'utf-8');
|
|
|
|
const jsCode = ts.transpile(fileContent, {
|
|
module: ts.ModuleKind.CommonJS,
|
|
target: ts.ScriptTarget.ES2018,
|
|
});
|
|
|
|
const context = { exports: {} };
|
|
vm.createContext(context);
|
|
vm.runInContext(jsCode, context);
|
|
|
|
const PRODUCTS = (context.exports as any).PRODUCTS;
|
|
|
|
if (!PRODUCTS || !Array.isArray(PRODUCTS)) {
|
|
console.error('Could not load PRODUCTS from frontend/application/lib/data/products.ts');
|
|
process.exit(1);
|
|
}
|
|
|
|
let count = 0;
|
|
for (const p of PRODUCTS) {
|
|
const artNo = p.artNo || `ART-${count}`;
|
|
|
|
// Replace brand names in string fields
|
|
const cleanName = (p.name || '').replace(/کانینا/g, 'کانینو').replace(/کانینا/g, 'کانینو').replace(/Canina/g, 'Canino').replace(/canina/g, 'canino');
|
|
const cleanScientificTagline = (p.scientificTagline || '').replace(/Canina/g, 'Canino').replace(/canina/g, 'canino');
|
|
const cleanDescription = (p.description || '').replace(/کانینا/g, 'کانینو').replace(/کانینا/g, 'کانینو').replace(/Canina/g, 'Canino').replace(/canina/g, 'canino');
|
|
const cleanShortDesc = (p.shortDescription || '').replace(/کانینا/g, 'کانینو').replace(/کانینا/g, 'کانینو').replace(/Canina/g, 'Canino').replace(/canina/g, 'canino');
|
|
const cleanPriceDisplay = (p.price || '0 T').replace(/کانینا/g, 'کانینو').replace(/کانینا/g, 'کانینو').replace(/Canina/g, 'Canino').replace(/canina/g, 'canino');
|
|
|
|
const slug = (p.slug || p.id || `product-${count}`).replace(/\s+/g, '-').toLowerCase().replace(/[^a-z0-9-]/g, '');
|
|
|
|
const product = await prisma.product.upsert({
|
|
where: { artNo: artNo },
|
|
update: {
|
|
categoryId: category.id,
|
|
categorySlug: category.slug,
|
|
slug: slug,
|
|
name: cleanName,
|
|
scientificTagline: cleanScientificTagline,
|
|
description: cleanDescription,
|
|
shortDescription: cleanShortDesc,
|
|
priceValue: p.priceValue || 0,
|
|
priceDisplay: cleanPriceDisplay,
|
|
unit: p.unit || 'g',
|
|
packageSize: p.packageSize || 100,
|
|
dosageLogic: p.dosage_logic || '',
|
|
suitableFor: p.suitableFor || 'هر دو',
|
|
imageUrl: p.image || '/placeholder.png',
|
|
},
|
|
create: {
|
|
artNo: artNo,
|
|
slug: slug,
|
|
name: cleanName,
|
|
scientificTagline: cleanScientificTagline,
|
|
description: cleanDescription,
|
|
shortDescription: cleanShortDesc,
|
|
categoryId: category.id,
|
|
categorySlug: category.slug,
|
|
priceValue: p.priceValue || 0,
|
|
priceDisplay: cleanPriceDisplay,
|
|
unit: p.unit || 'g',
|
|
packageSize: p.packageSize || 100,
|
|
dosageLogic: p.dosage_logic || '',
|
|
suitableFor: p.suitableFor || 'هر دو',
|
|
imageUrl: p.image || '/placeholder.png',
|
|
}
|
|
});
|
|
|
|
// Clear existing ingredients/symptoms and insert new ones
|
|
await prisma.productIngredient.deleteMany({ where: { productId: product.id } });
|
|
if (p.main_ingredients) {
|
|
for (const ing of p.main_ingredients) {
|
|
const cleanIng = ing.replace(/کانینا/g, 'کانینو').replace(/کانینا/g, 'کانینو').replace(/Canina/g, 'Canino').replace(/canina/g, 'canino');
|
|
await prisma.productIngredient.create({
|
|
data: { productId: product.id, ingredient: cleanIng }
|
|
});
|
|
}
|
|
}
|
|
|
|
await prisma.productSymptom.deleteMany({ where: { productId: product.id } });
|
|
if (p.symptoms) {
|
|
for (const sym of p.symptoms) {
|
|
const cleanSym = sym.replace(/کانینا/g, 'کانینو').replace(/کانینا/g, 'کانینو').replace(/Canina/g, 'Canino').replace(/canina/g, 'canino');
|
|
await prisma.productSymptom.create({
|
|
data: { productId: product.id, symptom: cleanSym }
|
|
});
|
|
}
|
|
}
|
|
|
|
count++;
|
|
}
|
|
console.log(`Seeded ${count} products.`);
|
|
} else {
|
|
console.log('products.ts not found at', productsFilePath);
|
|
}
|
|
|
|
// Create admin user for blogs
|
|
const adminId = '12345678-1234-1234-1234-123456789012';
|
|
await prisma.user.upsert({
|
|
where: { id: adminId },
|
|
update: { role: 'ADMIN' },
|
|
create: {
|
|
id: adminId,
|
|
mobile: '09120000000',
|
|
email: 'admin@canino-iran.com',
|
|
firstName: 'Admin',
|
|
lastName: 'User',
|
|
role: 'ADMIN'
|
|
}
|
|
});
|
|
|
|
// 3. Seed some Blogs
|
|
const blogs = [
|
|
{
|
|
title: 'راهنمای جامع تغذیه سگهای بالغ',
|
|
slug: 'adult-dog-nutrition-guide',
|
|
content: 'محتوای کامل مقاله در مورد تغذیه سگهای بالغ...',
|
|
authorId: adminId,
|
|
isPublished: true,
|
|
},
|
|
{
|
|
title: 'چرا مکملهای کانینو؟',
|
|
slug: 'why-canino-supplements',
|
|
content: 'محتوای کامل مقاله در مورد محصولات کانینو...',
|
|
authorId: adminId,
|
|
isPublished: true,
|
|
}
|
|
];
|
|
|
|
for (const b of blogs) {
|
|
await prisma.blog.upsert({
|
|
where: { slug: b.slug },
|
|
update: {},
|
|
create: b
|
|
});
|
|
}
|
|
console.log('Seeded blogs.');
|
|
}
|
|
|
|
main()
|
|
.catch(e => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect();
|
|
});
|