73 lines
1.4 KiB
TypeScript
73 lines
1.4 KiB
TypeScript
import 'dotenv/config'
|
|
import { prisma } from '../src/lib/prisma'
|
|
import { readSkuCadDataFromAttributes } from '../src/lib/sku-cad-data'
|
|
import { ensureSkuStructuralData } from '../src/server/services/sku-cad-import.helper'
|
|
|
|
async function main() {
|
|
const skus = await prisma.productSKU.findMany({
|
|
where: { deletedAt: null },
|
|
select: {
|
|
id: true,
|
|
productId: true,
|
|
skuCode: true,
|
|
attributes: true,
|
|
},
|
|
orderBy: { createdAt: 'asc' },
|
|
})
|
|
|
|
let scanned = 0
|
|
let filled = 0
|
|
let skipped = 0
|
|
|
|
for (const sku of skus) {
|
|
scanned += 1
|
|
const cadData = readSkuCadDataFromAttributes(
|
|
(sku.attributes ?? null) as Record<string, unknown> | null
|
|
)
|
|
|
|
if (!cadData) {
|
|
skipped += 1
|
|
continue
|
|
}
|
|
|
|
const existingPanelsCount = await prisma.panel.count({
|
|
where: { skuId: sku.id },
|
|
})
|
|
|
|
if (existingPanelsCount > 0) {
|
|
skipped += 1
|
|
continue
|
|
}
|
|
|
|
await ensureSkuStructuralData({
|
|
productId: sku.productId,
|
|
skuId: sku.id,
|
|
cadSourceData: cadData,
|
|
})
|
|
|
|
filled += 1
|
|
console.log(`[backfill] filled ${sku.skuCode}`)
|
|
}
|
|
|
|
console.log(
|
|
JSON.stringify(
|
|
{
|
|
scanned,
|
|
filled,
|
|
skipped,
|
|
},
|
|
null,
|
|
2
|
|
)
|
|
)
|
|
}
|
|
|
|
main()
|
|
.catch((error) => {
|
|
console.error(error)
|
|
process.exit(1)
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect()
|
|
})
|