39 lines
2.3 KiB
TypeScript
39 lines
2.3 KiB
TypeScript
import { loadEnvConfig } from '@next/env'
|
|
|
|
loadEnvConfig(process.cwd())
|
|
|
|
function argument(name: string) {
|
|
return process.argv.find((value) => value.startsWith(`--${name}=`))?.slice(name.length + 3)
|
|
}
|
|
|
|
async function main() {
|
|
const url = new URL(process.env.DATABASE_URL || '')
|
|
const identity = `${url.hostname}:${url.port || '5432'}/${url.pathname.slice(1)}`
|
|
const sourceIdentity = `${process.env.JST_REPORT_DB_HOST}:${process.env.JST_REPORT_DB_PORT || '1433'}/${process.env.JST_REPORT_DB_NAME}`
|
|
if (!process.argv.includes('--apply')) throw new Error('Explicit --apply required')
|
|
if (argument('confirm') !== 'sync-distribution-performance') throw new Error('Explicit --confirm token mismatch')
|
|
if (argument('expected-database') !== identity) throw new Error('Explicit --expected-database identity mismatch')
|
|
if (argument('expected-source') !== sourceIdentity) throw new Error('Explicit --expected-source identity mismatch')
|
|
const { DistributionPerformanceSourceRepository } = await import('../src/server/repositories/distribution-performance-source.repository')
|
|
const { distributionPerformanceRepository } = await import('../src/server/repositories/distribution-performance.repository')
|
|
const { DistributionPerformanceSyncService } = await import('../src/server/services/distribution-performance-sync.service')
|
|
const { prisma } = await import('../src/lib/prisma')
|
|
const source = new DistributionPerformanceSourceRepository()
|
|
const started = Date.now()
|
|
try {
|
|
const result = await new DistributionPerformanceSyncService(source, distributionPerformanceRepository).sync()
|
|
console.log(JSON.stringify({ ...result, elapsedSeconds: Math.round((Date.now() - started) / 1000) }))
|
|
} finally {
|
|
await source.close()
|
|
await prisma.$disconnect()
|
|
}
|
|
}
|
|
|
|
main().catch((error) => {
|
|
// 原始驱动错误可能包含连接上下文,终端只输出通用失败信息。
|
|
const message = error instanceof Error && /^(Explicit |当月尚无完整统计日|当月源库尚无可用业绩|铺货或产品档案为空|没有可匹配的当月业绩|Performance staging count mismatch|Another distribution performance sync)/.test(error.message)
|
|
? error.message : (error as { code?: string })?.code || (error instanceof Error ? error.name : 'UnknownError')
|
|
console.error('[performance-sync] failed:', message)
|
|
process.exitCode = 1
|
|
})
|