canina/backend/prisma/seed.ts

232 lines
9.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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('Wiping existing database records...');
await prisma.reminderCompletion.deleteMany();
await prisma.reminder.deleteMany();
await prisma.healthLog.deleteMany();
await prisma.petMedicalCondition.deleteMany();
await prisma.pet.deleteMany();
await prisma.orderItem.deleteMany();
await prisma.order.deleteMany();
await prisma.walletTransaction.deleteMany();
await prisma.userAddress.deleteMany();
await prisma.user.deleteMany();
await prisma.productIngredient.deleteMany();
await prisma.productSymptom.deleteMany();
await prisma.product.deleteMany();
await prisma.coupon.deleteMany();
await prisma.uiText.deleteMany();
await prisma.scientificTerm.deleteMany();
console.log('Database successfully wiped.');
console.log('Seeding database with products...');
const productsFilePath = path.join(__dirname, '..', '..', 'src', 'data', 'products.ts');
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 src/data/products.ts');
process.exit(1);
}
for (const productData of PRODUCTS) {
const p = await prisma.product.upsert({
where: { artNo: productData.artNo },
update: {},
create: {
artNo: productData.artNo,
name: productData.name,
scientificTagline: productData.scientificTagline,
description: productData.description,
shortDescription: productData.shortDescription || '',
category: productData.category,
categorySlug: productData.categorySlug || 'general',
priceValue: productData.priceValue,
priceDisplay: productData.price,
unit: productData.unit,
packageSize: productData.packageSize,
dosageLogic: productData.dosage_logic,
suitableFor: productData.suitableFor,
imageUrl: productData.image,
}
});
if (productData.main_ingredients) {
for (const ing of productData.main_ingredients) {
await prisma.productIngredient.upsert({
where: { productId_ingredient: { productId: p.id, ingredient: ing } },
update: {},
create: { productId: p.id, ingredient: ing }
});
}
}
if (productData.symptoms) {
for (const sym of productData.symptoms) {
await prisma.productSymptom.upsert({
where: { productId_symptom: { productId: p.id, symptom: sym } },
update: {},
create: { productId: p.id, symptom: sym }
});
}
}
console.log(`Seeded product: ${p.name}`);
}
// 1. Seed UI Texts
console.log('Seeding UI Texts...');
const uiTexts = {
hero_badge: "تخصص دارویی از آلمان",
hero_title: "تخصص آلمانی در خدمت\nسلامت پت‌های خانگی",
hero_desc: "بیش از ۴۰ سال تجربه نوآورانه در تولید مکمل‌های درمانی با بالاترین استاندارد کیفی «گرید دارویی اختصاصی». راهکار هوشمند برای هر نیاز بالینی.",
hero_btn_advisor: "دستیار سلامت پت",
hero_btn_products: "مشاهده محصولات",
hero_stat_founded: "۱۹۸۴ سال تأسیس",
hero_stat_agencies: "نمایندگی فعال",
hero_stat_german_formula: "فرمول آلمانی",
hero_image_badge: "سرآمد علمی در پزشکی پت‌ها",
hero_image_title: "مکمل‌های تایید شده دامپزشکی",
hero_quality_standard: "استاندارد کیفی آلمان",
cta_title: "می‌خواهید بدانید کدام محصول برای پت شما مناسب‌تر است؟",
cta_subtitle: "تیم متخصص دامپزشکی کانینا ایران آماده پاسخگویی به سوالات شماست.",
cta_btn_free: "دریافت رژیم مکمل رایگان",
cta_btn_products: "ورود به محصولات تخصصی",
about_teaser_badge: "میراث ما از آلمان",
about_teaser_title: "چرا برند آلمانی Canina مرجع دامپزشکان است؟",
about_teaser_feat1_title: "مواد اولیه نایاب",
about_teaser_feat1_desc: "استفاده از پودر صدف لب‌سبز اصل نیوزیلند و مواد ارگانیک با گرید دارویی.",
about_teaser_feat2_title: "فاقد مواد نگهدارنده",
about_teaser_feat2_desc: "تمامی محصولات ۱۰۰٪ طبیعی و فاقد رنگ‌های مصنوعی و طعم‌دهنده‌های شیمیایی هستند.",
about_teaser_feat3_title: "تاییدیه اروپا",
about_teaser_feat3_desc: "مطابق با سخت‌گیرانه‌ترین استانداردهای ایمنی مواد غذایی و دارویی در اتحادیه اروپا.",
about_teaser_quality_title: "کیفیت",
about_teaser_quality_desc: "استانداردهای فوق‌دارویی",
ingredients_wiki: JSON.stringify([
{
id: "green-mussel",
name: "صدف لب‌سبز (Perna Canaliculus)",
description: "این صدف بومی سواحل بکر نیوزیلند است و تنها منبع طبیعی حاوی گلیکوزآمین‌گلیکان‌ها (GAG) در غلظت بسیار بالا محسوب می‌شود.",
benefits: [
"بازسازی بافت غضروفی تخریب شده",
"خواص ضدالتهابی طبیعی برای رباط‌ها",
"تامین اسیدهای چرب امگا ۳ خاص"
]
},
{
id: "silver",
name: "نقره میکروسیلور (Microsilver)",
description: "ذرات نقره خالص با ساختار اسفنجی که به دلیل اندازه ذرات درشت، جذب خون نمی‌شوند اما اثر آنتی‌باکتریال پایداری بر سطح غشاها دارند.",
benefits: [
"مبارزه با باکتری‌های مقاوم در دهان و پوست",
"عدم آسیب به فلور طبیعی بدن",
"جایگزین ایمن برای آنتی‌بیوتیک‌های موضعی"
]
},
{
id: "colostrum",
name: "آغوز (Colostrum)",
description: "اولین شیر مادر که حاوی دوز بالایی از ایمونوگلوبولین‌ها، ویتامین‌ها و مواد معدنی برای فعال‌سازی فوری سیستم ایمنی است.",
benefits: [
"تقویت سد دفاعی روده",
"انتقال مستقیم آنتی‌بادی به نوزادان",
"تسریع نقاهت بعد از جرافی"
]
},
{
id: "peat-extract",
name: "عصاره پیت (Peat Extract)",
description: "ماده‌ای کاملا طبیعی حاصل از تجزیه گیاهان در طول هزاران سال که غنی از اسیدهای هومیک و مواد معدنی است.",
benefits: [
"جذب سموم در دستگاه گوارش",
"تنظیم فلور روده",
"بهبود اشتها و هضم"
]
}
]),
medical_options: JSON.stringify([
{ id: "joint_surgery", label: "جراحی مفاصل", condition: "جراحی مفاصل" },
{ id: "recent_birth", label: "زایمان اخیر", condition: "زایمان اخیر" },
{ id: "pregnancy", label: "بارداری", condition: "بارداری" },
{ id: "digestion", label: "مشکلات گوارشی", condition: "مشکلات گوارشی" },
{ id: "hair_loss", label: "ریزش موی شدید", condition: "ریزش موی شدید" },
{ id: "appetite", label: "بی‌اشتهایی", condition: "بی‌اشتهایی" }
])
};
for (const [key, value] of Object.entries(uiTexts)) {
await prisma.uiText.upsert({
where: { key },
update: { value },
create: { key, value }
});
}
// 2. Seed Scientific Terms
console.log('Seeding Scientific Terms...');
const termsFilePath = path.join(__dirname, '..', '..', 'src', 'data', 'scientificTerms.ts');
if (fs.existsSync(termsFilePath)) {
const termsContent = fs.readFileSync(termsFilePath, 'utf-8');
const termsJsCode = ts.transpile(termsContent, {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2018,
});
const termsContext = { exports: {} };
vm.createContext(termsContext);
vm.runInContext(termsJsCode, termsContext);
const SCIENTIFIC_TERMS = (termsContext.exports as any).SCIENTIFIC_TERMS;
if (SCIENTIFIC_TERMS) {
for (const [key, termData] of Object.entries(SCIENTIFIC_TERMS)) {
const td = termData as any;
await prisma.scientificTerm.upsert({
where: { key },
update: {
term: td.term,
definition: td.definition,
wikiId: td.wikiId || 'general'
},
create: {
key,
term: td.term,
definition: td.definition,
wikiId: td.wikiId || 'general'
}
});
}
}
}
console.log('Seeding completed successfully!');
}
main()
.catch((e) => {
console.error('Error seeding data:', e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});