feat: add new category synchronization script and expand default categories with detailed keywords for various sectors
This commit is contained in:
82
scripts/sync-categories.ts
Normal file
82
scripts/sync-categories.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import { PrismaClient } from "@prisma/client"
|
||||
import { defaultCategories } from "../lib/defaults"
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
async function main() {
|
||||
console.log("🏷️ Synchronisation des catégories...")
|
||||
console.log(` ${defaultCategories.length} catégories à synchroniser\n`)
|
||||
|
||||
let created = 0
|
||||
let updated = 0
|
||||
let unchanged = 0
|
||||
|
||||
for (const category of defaultCategories) {
|
||||
const existing = await prisma.category.findFirst({
|
||||
where: { name: category.name },
|
||||
})
|
||||
|
||||
if (existing) {
|
||||
// Comparer pour voir si mise à jour nécessaire
|
||||
const existingKeywords = JSON.parse(existing.keywords) as string[]
|
||||
const keywordsChanged =
|
||||
JSON.stringify(existingKeywords.sort()) !== JSON.stringify([...category.keywords].sort())
|
||||
const colorChanged = existing.color !== category.color
|
||||
const iconChanged = existing.icon !== category.icon
|
||||
|
||||
if (keywordsChanged || colorChanged || iconChanged) {
|
||||
await prisma.category.update({
|
||||
where: { id: existing.id },
|
||||
data: {
|
||||
color: category.color,
|
||||
icon: category.icon,
|
||||
keywords: JSON.stringify(category.keywords),
|
||||
},
|
||||
})
|
||||
console.log(`✏️ Mise à jour: ${category.name}`)
|
||||
if (keywordsChanged) {
|
||||
console.log(` └─ Keywords: ${existingKeywords.length} → ${category.keywords.length}`)
|
||||
}
|
||||
updated++
|
||||
} else {
|
||||
unchanged++
|
||||
}
|
||||
} else {
|
||||
await prisma.category.create({
|
||||
data: {
|
||||
name: category.name,
|
||||
color: category.color,
|
||||
icon: category.icon,
|
||||
keywords: JSON.stringify(category.keywords),
|
||||
parentId: category.parentId,
|
||||
},
|
||||
})
|
||||
console.log(`✅ Créée: ${category.name} (${category.keywords.length} keywords)`)
|
||||
created++
|
||||
}
|
||||
}
|
||||
|
||||
console.log("\n" + "═".repeat(50))
|
||||
console.log("📊 Résumé:")
|
||||
console.log(` ✅ Créées: ${created}`)
|
||||
console.log(` ✏️ Mises à jour: ${updated}`)
|
||||
console.log(` ⏭️ Inchangées: ${unchanged}`)
|
||||
console.log("═".repeat(50))
|
||||
|
||||
// Stats finales
|
||||
const totalCategories = await prisma.category.count()
|
||||
const totalKeywords = defaultCategories.reduce((sum, c) => sum + c.keywords.length, 0)
|
||||
console.log(`\n📈 Base de données:`)
|
||||
console.log(` Total catégories: ${totalCategories}`)
|
||||
console.log(` Total keywords: ${totalKeywords}`)
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((e) => {
|
||||
console.error("❌ Erreur:", e)
|
||||
process.exit(1)
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user