consumer pos
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user