canina/backend/prisma/seed-custom.ts

153 lines
3.9 KiB
TypeScript

import { PrismaClient } from '@prisma/client';
import * as fs from 'fs';
import * as path from 'path';
const prisma = new PrismaClient();
async function main() {
const filePath = path.join(
'C:',
'Users',
'parsa',
'.gemini',
'antigravity-ide',
'brain',
'951ff86f-dc4e-4670-9a62-78e1f9c8c223',
'scratch',
'products.json',
);
console.log(`Reading from: ${filePath}`);
const productsData = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
for (const item of productsData) {
const artNo =
item.article_number ||
item.article_number_100g ||
'UNKNOWN-' + Math.floor(Math.random() * 1000);
const name = item.product_name_fa || 'محصول نامشخص';
const scientificTagline = item.product_name_en || null;
const description = item.description || '';
const shortDescription = item.analytical_constituents || null;
let priceStr = item.price || item.price_100g || item.price_90tab || '0';
priceStr = priceStr
.replace(/,/g, '')
.replace(' T', '')
.replace('T', '')
.trim();
const priceValue = parseFloat(priceStr) || 0;
const priceDisplay =
item.price || item.price_100g || item.price_90tab || 'نامشخص';
let packageSize = 100;
let unit = 'g';
const vw = item.volume_weight || '';
if (vw.includes('ml')) unit = 'ml';
else if (vw.includes('g')) unit = 'g';
else if (vw.includes('tab')) unit = 'tablet';
const sizeMatch = vw.match(/(\d+)/);
if (sizeMatch) {
packageSize = parseFloat(sizeMatch[1]);
}
const dosageLogic =
item.feeding_recommendation || item.feeding_advice || '';
let suitableFor = 'سگ';
const textToCheck = (name + ' ' + description).toLowerCase();
const hasCat =
textToCheck.includes('گربه') || textToCheck.includes('katzen');
const hasDog =
textToCheck.includes('سگ') ||
textToCheck.includes('hund') ||
textToCheck.includes('توله');
if (hasCat && hasDog) {
suitableFor = 'هر دو';
} else if (hasCat) {
suitableFor = 'گربه';
} else if (hasDog) {
suitableFor = 'سگ';
} else {
suitableFor = 'هر دو';
}
const category = 'مکمل‌ تخصصی';
const categorySlug = 'specialized-supplements';
const imageUrl = '/products/product-placeholder.png';
const createdProduct = await prisma.product.upsert({
where: { artNo },
update: {
slug: artNo,
nameFa: name,
nameEn: name,
scientificTagline,
description,
shortDescription,
priceValue,
priceDisplay,
packageSize,
unit,
dosageLogic,
suitableFor,
},
create: {
artNo,
nameFa: name,
nameEn: name,
slug: artNo,
scientificTagline,
description,
shortDescription,
categorySlug,
priceValue,
priceDisplay,
unit,
packageSize,
dosageLogic,
suitableFor,
imageUrl,
category: {
connectOrCreate: {
where: { slug: categorySlug },
create: { name: category, slug: categorySlug },
},
},
},
});
if (item.ingredients) {
const ingredientsArr = item.ingredients
.split('،')
.map((i: string) => i.trim())
.filter(Boolean);
for (const ing of ingredientsArr) {
await prisma.productIngredient.upsert({
where: {
productId_ingredient: {
productId: createdProduct.id,
ingredient: ing.substring(0, 150),
},
},
update: {},
create: {
productId: createdProduct.id,
ingredient: ing.substring(0, 150),
},
});
}
}
console.log(`Upserted ${name}`);
}
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});