const { PrismaClient } = require('@prisma/client') const prisma = new PrismaClient() async function fixSupplyOrderTypes() { try { console.log('🔧 ИСПРАВЛЯЕМ ТИПЫ ЗАКАЗОВ ПОСТАВОК...') // Найдем все заказы поставок с проблемами const problemOrders = await prisma.supplyOrder.findMany({ include: { organization: { select: { id: true, name: true, type: true } } } }) console.log(`📋 Найдено заказов для анализа: ${problemOrders.length}`) let fixedCount = 0 for (const order of problemOrders) { const organizationType = order.organization?.type const currentConsumableType = order.consumableType // Определяем правильный тип на основе заказчика let correctConsumableType if (organizationType === 'SELLER') { correctConsumableType = 'SELLER_CONSUMABLES' } else if (organizationType === 'FULFILLMENT') { correctConsumableType = 'FULFILLMENT_CONSUMABLES' } else { continue // Пропускаем другие типы } // Проверяем, нужно ли исправление if (currentConsumableType !== correctConsumableType) { console.log(`\n🔧 ИСПРАВЛЯЕМ ЗАКАЗ ${order.id}:`) console.log(` Заказчик: ${order.organization?.name} (${organizationType})`) console.log(` БЫЛО: ${currentConsumableType}`) console.log(` СТАЛО: ${correctConsumableType}`) // Обновляем заказ await prisma.supplyOrder.update({ where: { id: order.id }, data: { consumableType: correctConsumableType } }) fixedCount++ } else { console.log(`✅ Заказ ${order.id} уже корректен (${organizationType} → ${currentConsumableType})`) } } console.log(`\n📊 ИТОГИ:`) console.log(` ✅ Исправлено заказов: ${fixedCount}`) console.log(` 📋 Всего проверено: ${problemOrders.length}`) if (fixedCount > 0) { console.log(`\n⚠️ ВАЖНО: Нужно также пересоздать Supply записи с правильными типами!`) console.log(` Проблемные Supply записи все еще имеют тип FULFILLMENT_CONSUMABLES`) console.log(` но должны быть SELLER_CONSUMABLES для заказов от селлеров`) } } catch (error) { console.error('❌ Ошибка при исправлении:', error.message) } finally { await prisma.$disconnect() } } fixSupplyOrderTypes()