39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
import prisma from '../src/lib/prisma'
|
|
import { decrypt } from '../src/lib/crypto'
|
|
|
|
function extractBirthDate(idCard: string): Date | null {
|
|
if (idCard.length === 18) {
|
|
const y = parseInt(idCard.substring(6, 10))
|
|
const m = parseInt(idCard.substring(10, 12))
|
|
const d = parseInt(idCard.substring(12, 14))
|
|
if (y && m && d) return new Date(y, m - 1, d)
|
|
}
|
|
if (idCard.length === 15) {
|
|
const y = parseInt('19' + idCard.substring(6, 8))
|
|
const m = parseInt(idCard.substring(8, 10))
|
|
const d = parseInt(idCard.substring(10, 12))
|
|
if (y && m && d) return new Date(y, m - 1, d)
|
|
}
|
|
return null
|
|
}
|
|
|
|
async function main() {
|
|
const employees = await prisma.employee.findMany({ select: { id: true, idCardNumber: true, birthDate: true } })
|
|
let updated = 0
|
|
for (const emp of employees) {
|
|
if (emp.birthDate) continue
|
|
if (!emp.idCardNumber) continue
|
|
let idCard: string
|
|
try { idCard = decrypt(emp.idCardNumber).toString() } catch { continue }
|
|
const birthDate = extractBirthDate(idCard)
|
|
if (birthDate) {
|
|
await prisma.employee.update({ where: { id: emp.id }, data: { birthDate } })
|
|
updated++
|
|
}
|
|
}
|
|
console.log(`Updated ${updated} employees with birthDate`)
|
|
await prisma.$disconnect()
|
|
}
|
|
|
|
main()
|