2026-04-22 21:55:40 +03:30
|
|
|
import { randomBytes } from 'crypto'
|
2026-05-05 22:42:09 +03:30
|
|
|
import { PrismaErrorUtil } from './prisma-error.util'
|
2026-04-22 21:55:40 +03:30
|
|
|
|
|
|
|
|
export function generateTrackingCode(prefix: string, suffixLength: number) {
|
|
|
|
|
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
|
|
|
|
|
const bytes = randomBytes(suffixLength)
|
|
|
|
|
|
|
|
|
|
let suffix = ''
|
|
|
|
|
for (let i = 0; i < suffixLength; i++) {
|
|
|
|
|
suffix += chars[bytes[i] % chars.length]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return `${prefix}-${suffix}`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isTrackingCodeUniqueViolation(error: unknown) {
|
2026-05-05 22:42:09 +03:30
|
|
|
if (!PrismaErrorUtil.isCode(error, 'P2002')) {
|
2026-04-22 21:55:40 +03:30
|
|
|
return false
|
|
|
|
|
}
|
2026-05-05 22:42:09 +03:30
|
|
|
return PrismaErrorUtil.hasTarget(error, 'tracking_code')
|
2026-04-22 21:55:40 +03:30
|
|
|
}
|