123 lines
3.7 KiB
TypeScript
123 lines
3.7 KiB
TypeScript
import { PrismaClient } from '@prisma/client';
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
async function main() {
|
|
console.log('Seeding products 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.json
|
|
const productsPath = 'C:/Users/parsa/.gemini/antigravity-ide/brain/951ff86f-dc4e-4670-9a62-78e1f9c8c223/scratch/products.json';
|
|
if (fs.existsSync(productsPath)) {
|
|
const productsRaw = fs.readFileSync(productsPath, 'utf8');
|
|
const products = JSON.parse(productsRaw);
|
|
|
|
let count = 0;
|
|
for (const p of products) {
|
|
// Parse price "1,550,000 T"
|
|
let price = 0;
|
|
if (p.price) {
|
|
price = parseInt(p.price.replace(/,/g, '').replace(' T', ''));
|
|
}
|
|
|
|
const artNo = p.article_number || `ART-${count}`;
|
|
const slug = (p.product_name_en || p.product_name_fa || `product-${count}`).replace(/\s+/g, '-').toLowerCase().replace(/[^a-z0-9-]/g, '');
|
|
const priceVal = price || 0;
|
|
const imageUrl = '/placeholder.png'; // Fallback
|
|
|
|
await prisma.product.upsert({
|
|
where: { artNo: artNo },
|
|
update: {
|
|
categoryId: category.id,
|
|
categorySlug: category.slug,
|
|
slug: slug,
|
|
},
|
|
create: {
|
|
artNo: artNo,
|
|
slug: slug,
|
|
name: p.product_name_fa || p.product_name_en || 'بدون نام',
|
|
scientificTagline: p.product_name_en || null,
|
|
description: p.description || '',
|
|
categoryId: category.id,
|
|
categorySlug: category.slug,
|
|
priceValue: priceVal,
|
|
priceDisplay: p.price || '0 T',
|
|
unit: p.volume_weight?.includes('ml') ? 'ml' : 'g',
|
|
packageSize: parseFloat(p.volume_weight || '100') || 100,
|
|
suitableFor: 'هر دو', // Default
|
|
imageUrl: imageUrl,
|
|
}
|
|
});
|
|
count++;
|
|
}
|
|
console.log(`Seeded ${count} products.`);
|
|
} else {
|
|
console.log('products.json not found at', productsPath);
|
|
}
|
|
|
|
// Create admin user for blogs
|
|
const adminId = '12345678-1234-1234-1234-123456789012';
|
|
await prisma.user.upsert({
|
|
where: { mobile: '09120000000' },
|
|
update: { id: adminId, role: 'admin' },
|
|
create: {
|
|
id: adminId,
|
|
mobile: '09120000000',
|
|
email: 'admin@canina-iran.com',
|
|
firstName: 'Admin',
|
|
lastName: 'User',
|
|
role: 'admin'
|
|
}
|
|
});
|
|
|
|
// 3. Seed some Blogs
|
|
const blogs = [
|
|
{
|
|
title: 'راهنمای جامع تغذیه سگهای بالغ',
|
|
slug: 'adult-dog-nutrition-guide',
|
|
content: 'محتوای کامل مقاله در مورد تغذیه سگهای بالغ...',
|
|
authorId: '12345678-1234-1234-1234-123456789012', // Admin UUID
|
|
isPublished: true,
|
|
},
|
|
{
|
|
title: 'چرا مکملهای کانینا؟',
|
|
slug: 'why-canina-supplements',
|
|
content: 'محتوای کامل مقاله در مورد محصولات کانینا...',
|
|
authorId: '12345678-1234-1234-1234-123456789012',
|
|
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();
|
|
});
|