389 lines
15 KiB
JavaScript
389 lines
15 KiB
JavaScript
const { PrismaClient } = require('@prisma/client');
|
||
const bcrypt = require('bcryptjs');
|
||
|
||
const prisma = new PrismaClient();
|
||
|
||
async function main() {
|
||
console.log('🌱 Veritabanı seed işlemi başlatılıyor...');
|
||
|
||
// Admin kullanıcısı oluştur
|
||
const hashedPassword = await bcrypt.hash('admin123', 12);
|
||
|
||
const adminUser = await prisma.user.upsert({
|
||
where: { email: 'admin@hmarket.com' },
|
||
update: {},
|
||
create: {
|
||
email: 'admin@hmarket.com',
|
||
username: 'admin',
|
||
firstName: 'Admin',
|
||
lastName: 'User',
|
||
password: hashedPassword,
|
||
isAdmin: true,
|
||
},
|
||
});
|
||
|
||
console.log('✅ Admin kullanıcısı oluşturuldu:', adminUser.email);
|
||
|
||
// Test kullanıcıları oluştur
|
||
const testUsers = [
|
||
{
|
||
email: 'ahmet@test.com',
|
||
username: 'ahmet',
|
||
firstName: 'Ahmet',
|
||
lastName: 'Yılmaz',
|
||
password: await bcrypt.hash('test123', 12),
|
||
},
|
||
{
|
||
email: 'ayse@test.com',
|
||
username: 'ayse',
|
||
firstName: 'Ayşe',
|
||
lastName: 'Kaya',
|
||
password: await bcrypt.hash('test123', 12),
|
||
},
|
||
{
|
||
email: 'mehmet@test.com',
|
||
username: 'mehmet',
|
||
firstName: 'Mehmet',
|
||
lastName: 'Demir',
|
||
password: await bcrypt.hash('test123', 12),
|
||
},
|
||
];
|
||
|
||
for (const userData of testUsers) {
|
||
const user = await prisma.user.upsert({
|
||
where: { email: userData.email },
|
||
update: {},
|
||
create: userData,
|
||
});
|
||
console.log('✅ Test kullanıcısı oluşturuldu:', user.email);
|
||
}
|
||
|
||
// Kategoriler oluştur
|
||
const categories = [
|
||
{ name: 'Meyve & Sebze', description: 'Taze meyve ve sebzeler', icon: '🥕', color: '#4CAF50' },
|
||
{ name: 'Et & Tavuk', description: 'Kırmızı et, beyaz et ve şarküteri ürünleri', icon: '🥩', color: '#F44336' },
|
||
{ name: 'Süt Ürünleri', description: 'Süt, peynir, yoğurt ve diğer süt ürünleri', icon: '🥛', color: '#2196F3' },
|
||
{ name: 'Fırın & Pastane', description: 'Ekmek, pasta ve unlu mamuller', icon: '🍞', color: '#FF9800' },
|
||
{ name: 'Atıştırmalık', description: 'Çikolata, bisküvi ve atıştırmalık ürünler', icon: '🍿', color: '#9C27B0' },
|
||
{ name: 'İçecek', description: 'Alkolsüz içecekler ve sıcak içecekler', icon: '🥤', color: '#00BCD4' },
|
||
{ name: 'Temizlik', description: 'Ev temizlik ürünleri ve deterjanlar', icon: '🧽', color: '#607D8B' },
|
||
{ name: 'Kişisel Bakım', description: 'Kişisel hijyen ve bakım ürünleri', icon: '🧴', color: '#E91E63' },
|
||
{ name: 'Bebek', description: 'Bebek bakım ürünleri ve mama', icon: '🍼', color: '#FFEB3B' },
|
||
{ name: 'Dondurulmuş', description: 'Dondurulmuş gıda ürünleri', icon: '🧊', color: '#3F51B5' },
|
||
];
|
||
|
||
for (const categoryData of categories) {
|
||
const category = await prisma.category.upsert({
|
||
where: { name: categoryData.name },
|
||
update: {},
|
||
create: categoryData,
|
||
});
|
||
console.log('✅ Kategori oluşturuldu:', category.name);
|
||
}
|
||
|
||
// Örnek ürünler oluştur
|
||
const products = [
|
||
// Meyve & Sebze
|
||
{ name: 'Elma', categoryName: 'Meyve & Sebze', barcode: '1234567890123', averagePrice: 12.50 },
|
||
{ name: 'Muz', categoryName: 'Meyve & Sebze', barcode: '1234567890124', averagePrice: 18.00 },
|
||
{ name: 'Domates', categoryName: 'Meyve & Sebze', barcode: '1234567890125', averagePrice: 8.75 },
|
||
{ name: 'Salatalık', categoryName: 'Meyve & Sebze', barcode: '1234567890126', averagePrice: 6.50 },
|
||
{ name: 'Soğan', categoryName: 'Meyve & Sebze', barcode: '1234567890127', averagePrice: 4.25 },
|
||
{ name: 'Patates', categoryName: 'Meyve & Sebze', barcode: '1234567890137', averagePrice: 5.75 },
|
||
{ name: 'Havuç', categoryName: 'Meyve & Sebze', barcode: '1234567890138', averagePrice: 7.50 },
|
||
{ name: 'Biber', categoryName: 'Meyve & Sebze', barcode: '1234567890139', averagePrice: 15.00 },
|
||
{ name: 'Portakal', categoryName: 'Meyve & Sebze', barcode: '1234567890140', averagePrice: 10.00 },
|
||
{ name: 'Limon', categoryName: 'Meyve & Sebze', barcode: '1234567890141', averagePrice: 8.00 },
|
||
|
||
// Süt Ürünleri
|
||
{ name: 'Süt 1L', categoryName: 'Süt Ürünleri', barcode: '1234567890128', averagePrice: 8.75 },
|
||
{ name: 'Yoğurt 500g', categoryName: 'Süt Ürünleri', barcode: '1234567890129', averagePrice: 12.50 },
|
||
{ name: 'Beyaz Peynir', categoryName: 'Süt Ürünleri', barcode: '1234567890130', averagePrice: 45.00 },
|
||
{ name: 'Kaşar Peyniri', categoryName: 'Süt Ürünleri', barcode: '1234567890131', averagePrice: 65.00 },
|
||
{ name: 'Tereyağı', categoryName: 'Süt Ürünleri', barcode: '1234567890142', averagePrice: 35.00 },
|
||
{ name: 'Yumurta 30\'lu', categoryName: 'Süt Ürünleri', barcode: '1234567890143', averagePrice: 85.00 },
|
||
{ name: 'Krema', categoryName: 'Süt Ürünleri', barcode: '1234567890144', averagePrice: 15.50 },
|
||
|
||
// Fırın & Pastane
|
||
{ name: 'Ekmek', categoryName: 'Fırın & Pastane', barcode: '1234567890132', averagePrice: 4.50 },
|
||
{ name: 'Simit', categoryName: 'Fırın & Pastane', barcode: '1234567890133', averagePrice: 2.50 },
|
||
{ name: 'Pide', categoryName: 'Fırın & Pastane', barcode: '1234567890145', averagePrice: 8.00 },
|
||
{ name: 'Çörek', categoryName: 'Fırın & Pastane', barcode: '1234567890146', averagePrice: 6.50 },
|
||
{ name: 'Kek', categoryName: 'Fırın & Pastane', barcode: '1234567890147', averagePrice: 25.00 },
|
||
|
||
// İçecek
|
||
{ name: 'Su 1.5L', categoryName: 'İçecek', barcode: '1234567890134', averagePrice: 2.50 },
|
||
{ name: 'Çay', categoryName: 'İçecek', barcode: '1234567890135', averagePrice: 35.00 },
|
||
{ name: 'Kahve', categoryName: 'İçecek', barcode: '1234567890136', averagePrice: 85.00 },
|
||
{ name: 'Meyve Suyu', categoryName: 'İçecek', barcode: '1234567890148', averagePrice: 12.50 },
|
||
{ name: 'Kola', categoryName: 'İçecek', barcode: '1234567890149', averagePrice: 8.75 },
|
||
{ name: 'Ayran', categoryName: 'İçecek', barcode: '1234567890150', averagePrice: 4.50 },
|
||
|
||
// Et & Tavuk
|
||
{ name: 'Tavuk But', categoryName: 'Et & Tavuk', barcode: '1234567890151', averagePrice: 45.00 },
|
||
{ name: 'Dana Kıyma', categoryName: 'Et & Tavuk', barcode: '1234567890152', averagePrice: 120.00 },
|
||
{ name: 'Köfte', categoryName: 'Et & Tavuk', barcode: '1234567890153', averagePrice: 85.00 },
|
||
{ name: 'Sosis', categoryName: 'Et & Tavuk', barcode: '1234567890154', averagePrice: 25.00 },
|
||
|
||
// Temizlik
|
||
{ name: 'Deterjan', categoryName: 'Temizlik', barcode: '1234567890155', averagePrice: 45.00 },
|
||
{ name: 'Sabun', categoryName: 'Temizlik', barcode: '1234567890156', averagePrice: 8.50 },
|
||
{ name: 'Şampuan', categoryName: 'Temizlik', barcode: '1234567890157', averagePrice: 35.00 },
|
||
{ name: 'Diş Macunu', categoryName: 'Kişisel Bakım', barcode: '1234567890158', averagePrice: 18.50 },
|
||
|
||
// Atıştırmalık
|
||
{ name: 'Çikolata', categoryName: 'Atıştırmalık', barcode: '1234567890159', averagePrice: 12.50 },
|
||
{ name: 'Bisküvi', categoryName: 'Atıştırmalık', barcode: '1234567890160', averagePrice: 8.75 },
|
||
{ name: 'Cips', categoryName: 'Atıştırmalık', barcode: '1234567890161', averagePrice: 6.50 },
|
||
];
|
||
|
||
for (const productData of products) {
|
||
const category = await prisma.category.findUnique({
|
||
where: { name: productData.categoryName }
|
||
});
|
||
|
||
if (category) {
|
||
const product = await prisma.product.upsert({
|
||
where: { barcode: productData.barcode },
|
||
update: {},
|
||
create: {
|
||
name: productData.name,
|
||
barcode: productData.barcode,
|
||
categoryId: category.id,
|
||
},
|
||
});
|
||
console.log('✅ Ürün oluşturuldu:', product.name);
|
||
}
|
||
}
|
||
|
||
// Örnek alışveriş listeleri oluştur
|
||
const ahmetUser = await prisma.user.findUnique({
|
||
where: { email: 'ahmet@test.com' }
|
||
});
|
||
|
||
const ayseUser = await prisma.user.findUnique({
|
||
where: { email: 'ayse@test.com' }
|
||
});
|
||
|
||
const mehmetUser = await prisma.user.findUnique({
|
||
where: { email: 'mehmet@test.com' }
|
||
});
|
||
|
||
// Ahmet için alışveriş listeleri
|
||
if (ahmetUser) {
|
||
const shoppingLists = [
|
||
{
|
||
name: 'Haftalık Alışveriş',
|
||
description: 'Bu haftanın market alışverişi',
|
||
color: '#4CAF50',
|
||
items: [
|
||
{ productName: 'Elma', quantity: 2, unit: 'kg', price: 15.50 },
|
||
{ productName: 'Süt 1L', quantity: 1, unit: 'adet', price: 8.75 },
|
||
{ customName: 'Deterjan', quantity: 1, unit: 'adet', price: 25.00, note: 'Çamaşır deterjanı' },
|
||
{ productName: 'Ekmek', quantity: 2, unit: 'adet', price: 9.00 },
|
||
{ productName: 'Yumurta 30\'lu', quantity: 1, unit: 'adet', price: 85.00 },
|
||
]
|
||
},
|
||
{
|
||
name: 'Kahvaltı Malzemeleri',
|
||
description: 'Kahvaltı için gerekli ürünler',
|
||
color: '#FF9800',
|
||
items: [
|
||
{ productName: 'Ekmek', quantity: 2, unit: 'adet', price: 9.00 },
|
||
{ productName: 'Beyaz Peynir', quantity: 1, unit: 'kg', price: 45.00 },
|
||
{ productName: 'Tereyağı', quantity: 1, unit: 'adet', price: 35.00 },
|
||
{ productName: 'Domates', quantity: 1, unit: 'kg', price: 8.75 },
|
||
{ productName: 'Salatalık', quantity: 2, unit: 'adet', price: 13.00 },
|
||
{ productName: 'Çay', quantity: 1, unit: 'adet', price: 35.00 },
|
||
]
|
||
}
|
||
];
|
||
|
||
for (const listData of shoppingLists) {
|
||
const shoppingList = await prisma.shoppingList.create({
|
||
data: {
|
||
name: listData.name,
|
||
description: listData.description,
|
||
color: listData.color,
|
||
ownerId: ahmetUser.id,
|
||
},
|
||
});
|
||
|
||
for (const itemData of listData.items) {
|
||
let productId = null;
|
||
|
||
if (itemData.productName) {
|
||
const product = await prisma.product.findFirst({
|
||
where: { name: itemData.productName }
|
||
});
|
||
if (product) {
|
||
productId = product.id;
|
||
}
|
||
}
|
||
|
||
await prisma.listItem.create({
|
||
data: {
|
||
listId: shoppingList.id,
|
||
productId: productId,
|
||
customName: itemData.customName,
|
||
quantity: itemData.quantity,
|
||
unit: itemData.unit,
|
||
price: itemData.price,
|
||
note: itemData.note,
|
||
},
|
||
});
|
||
}
|
||
|
||
console.log('✅ Alışveriş listesi oluşturuldu:', shoppingList.name);
|
||
}
|
||
}
|
||
|
||
// Ayşe için alışveriş listeleri
|
||
if (ayseUser) {
|
||
const ayseShoppingLists = [
|
||
{
|
||
name: 'Parti Alışverişi',
|
||
description: 'Doğum günü partisi için',
|
||
color: '#E91E63',
|
||
items: [
|
||
{ productName: 'Kek', quantity: 1, unit: 'adet', price: 25.00 },
|
||
{ productName: 'Çikolata', quantity: 5, unit: 'adet', price: 62.50 },
|
||
{ productName: 'Bisküvi', quantity: 3, unit: 'paket', price: 26.25 },
|
||
{ productName: 'Kola', quantity: 6, unit: 'adet', price: 52.50 },
|
||
{ productName: 'Meyve Suyu', quantity: 4, unit: 'adet', price: 50.00 },
|
||
{ customName: 'Parti Süsleri', quantity: 1, unit: 'set', price: 45.00 },
|
||
]
|
||
},
|
||
{
|
||
name: 'Temizlik Malzemeleri',
|
||
description: 'Ev temizliği için gerekli ürünler',
|
||
color: '#607D8B',
|
||
items: [
|
||
{ productName: 'Deterjan', quantity: 2, unit: 'adet', price: 90.00 },
|
||
{ productName: 'Sabun', quantity: 3, unit: 'adet', price: 25.50 },
|
||
{ productName: 'Şampuan', quantity: 1, unit: 'adet', price: 35.00 },
|
||
{ customName: 'Cam Temizleyici', quantity: 1, unit: 'adet', price: 18.50 },
|
||
{ customName: 'Yer Bezi', quantity: 2, unit: 'adet', price: 15.00 },
|
||
]
|
||
}
|
||
];
|
||
|
||
for (const listData of ayseShoppingLists) {
|
||
const shoppingList = await prisma.shoppingList.create({
|
||
data: {
|
||
name: listData.name,
|
||
description: listData.description,
|
||
color: listData.color,
|
||
ownerId: ayseUser.id,
|
||
},
|
||
});
|
||
|
||
for (const itemData of listData.items) {
|
||
let productId = null;
|
||
|
||
if (itemData.productName) {
|
||
const product = await prisma.product.findFirst({
|
||
where: { name: itemData.productName }
|
||
});
|
||
if (product) {
|
||
productId = product.id;
|
||
}
|
||
}
|
||
|
||
await prisma.listItem.create({
|
||
data: {
|
||
listId: shoppingList.id,
|
||
productId: productId,
|
||
customName: itemData.customName,
|
||
quantity: itemData.quantity,
|
||
unit: itemData.unit,
|
||
price: itemData.price,
|
||
},
|
||
});
|
||
}
|
||
|
||
console.log('✅ Alışveriş listesi oluşturuldu:', shoppingList.name);
|
||
}
|
||
}
|
||
|
||
// Mehmet için alışveriş listesi
|
||
if (mehmetUser) {
|
||
const mehmetShoppingList = {
|
||
name: 'Et ve Protein',
|
||
description: 'Protein ihtiyacı için et ürünleri',
|
||
color: '#F44336',
|
||
items: [
|
||
{ productName: 'Tavuk But', quantity: 2, unit: 'kg', price: 90.00 },
|
||
{ productName: 'Dana Kıyma', quantity: 1, unit: 'kg', price: 120.00 },
|
||
{ productName: 'Köfte', quantity: 1, unit: 'kg', price: 85.00 },
|
||
{ productName: 'Sosis', quantity: 2, unit: 'paket', price: 50.00 },
|
||
{ productName: 'Yumurta 30\'lu', quantity: 1, unit: 'adet', price: 85.00 },
|
||
]
|
||
};
|
||
|
||
const shoppingList = await prisma.shoppingList.create({
|
||
data: {
|
||
name: mehmetShoppingList.name,
|
||
description: mehmetShoppingList.description,
|
||
color: mehmetShoppingList.color,
|
||
ownerId: mehmetUser.id,
|
||
},
|
||
});
|
||
|
||
for (const itemData of mehmetShoppingList.items) {
|
||
let productId = null;
|
||
|
||
if (itemData.productName) {
|
||
const product = await prisma.product.findFirst({
|
||
where: { name: itemData.productName }
|
||
});
|
||
if (product) {
|
||
productId = product.id;
|
||
}
|
||
}
|
||
|
||
await prisma.listItem.create({
|
||
data: {
|
||
listId: shoppingList.id,
|
||
productId: productId,
|
||
quantity: itemData.quantity,
|
||
unit: itemData.unit,
|
||
price: itemData.price,
|
||
},
|
||
});
|
||
}
|
||
|
||
console.log('✅ Alışveriş listesi oluşturuldu:', shoppingList.name);
|
||
}
|
||
|
||
console.log('✅ Tüm örnek alışveriş listeleri oluşturuldu');
|
||
|
||
// Sistem ayarları
|
||
const settings = [
|
||
{ key: 'app_name', value: 'HMarket', type: 'string' },
|
||
{ key: 'app_version', value: '1.0.0', type: 'string' },
|
||
{ key: 'max_list_members', value: '10', type: 'number' },
|
||
{ key: 'enable_notifications', value: 'true', type: 'boolean' },
|
||
{ key: 'default_currency', value: 'TL', type: 'string' },
|
||
];
|
||
|
||
for (const settingData of settings) {
|
||
await prisma.setting.upsert({
|
||
where: { key: settingData.key },
|
||
update: { value: settingData.value },
|
||
create: settingData,
|
||
});
|
||
console.log('✅ Ayar oluşturuldu:', settingData.key);
|
||
}
|
||
|
||
console.log('🎉 Seed işlemi tamamlandı!');
|
||
}
|
||
|
||
main()
|
||
.catch((e) => {
|
||
console.error('❌ Seed işlemi sırasında hata:', e);
|
||
process.exit(1);
|
||
})
|
||
.finally(async () => {
|
||
await prisma.$disconnect();
|
||
}); |