Files
psp_api/src/modules/uploaders/uploader.middleware.ts
T

51 lines
1.4 KiB
TypeScript
Raw Normal View History

2026-04-14 15:56:49 +03:30
import { checkAndDecodeJwtToken } from '@/common/utils/jwt-user.util'
import { PrismaService } from '@/prisma/prisma.service'
import { ForbiddenException, Injectable, NestMiddleware } from '@nestjs/common'
import { JwtService } from '@nestjs/jwt'
import { NextFunction, Request, Response } from 'express'
@Injectable()
export class PosMiddleware implements NestMiddleware {
constructor(
private prisma: PrismaService,
private jwtService: JwtService,
) {}
async use(req: Request, _res: Response, next: NextFunction) {
const doForbidden = () => {
throw new ForbiddenException('شما دسترسی لازم را ندارید')
}
try {
const tokenAccount = checkAndDecodeJwtToken(req, this.jwtService)
const { type, account_id } = tokenAccount!
if (type === 'ADMIN' || type === 'CONSUMER') {
const account = await this.prisma.account.findFirst({
where: {
OR: [
{
admin_account: {
id: account_id,
},
},
{
consumer_account: {
id: account_id,
},
},
],
},
})
if (account) return next()
}
throw doForbidden()
} catch (error) {
if (error && typeof error === 'object') {
throw error
}
return doForbidden()
}
}
}