update
This commit is contained in:
@@ -13,12 +13,10 @@ export const TokenAccount = createParamDecorator(
|
||||
const request = ctx.switchToHttp().getRequest<Request>()
|
||||
const account = request.decodedToken
|
||||
|
||||
// ✅ if middleware didn't populate req.account, return undefined or throw
|
||||
if (!account || !account.user?.id) {
|
||||
throw new UnauthorizedException('شما به این بخش دسترسی ندارید')
|
||||
}
|
||||
|
||||
// ✅ if decorator called with a key (e.g. @account('accountId'))
|
||||
if (data) {
|
||||
if (data === 'userId') {
|
||||
return account.user?.id
|
||||
@@ -26,7 +24,6 @@ export const TokenAccount = createParamDecorator(
|
||||
return account[data]
|
||||
}
|
||||
|
||||
// ✅ if called with no param — return full account object
|
||||
return account
|
||||
} catch (err) {
|
||||
throw new BadRequestException('مشکلی در ساختار درخواست شما وجود دارد.')
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import { randomBytes } from 'crypto'
|
||||
|
||||
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) {
|
||||
const prismaError = error as {
|
||||
code?: string
|
||||
meta?: { target?: string[] | string }
|
||||
}
|
||||
|
||||
if (prismaError?.code !== 'P2002') {
|
||||
return false
|
||||
}
|
||||
|
||||
const target = prismaError.meta?.target
|
||||
if (Array.isArray(target)) {
|
||||
return target.includes('tracking_code')
|
||||
}
|
||||
|
||||
if (typeof target === 'string') {
|
||||
return target.includes('tracking_code')
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user