create license management
This commit is contained in:
@@ -1,6 +1,15 @@
|
||||
import { Controller } from '@nestjs/common'
|
||||
import { ConsumerInfoInfo } from '@/common/decorators/consumerInfo.decorator'
|
||||
import { Controller, Get } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { ConsumerService } from './consumer.service'
|
||||
|
||||
@ApiTags('Consumer')
|
||||
@Controller('consumer')
|
||||
export class ConsumerController {}
|
||||
export class ConsumerController {
|
||||
constructor(private service: ConsumerService) {}
|
||||
|
||||
@Get('')
|
||||
async findOne(@ConsumerInfoInfo('user_id') userId: string) {
|
||||
return this.service.getInfo(userId)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
// import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common'
|
||||
// import { Observable } from 'rxjs'
|
||||
|
||||
// @Injectable()
|
||||
// export class AdminGuard implements CanActivate {
|
||||
// canActivate(
|
||||
// _context: ExecutionContext,
|
||||
// ): boolean | Promise<boolean> | Observable<boolean> {
|
||||
// // Replace with real auth/permission checks (e.g., check JWT claims)
|
||||
// const request = _context.switchToHttp().getRequest()
|
||||
// const isAdminHeader = request.headers['x-admin']
|
||||
// return isAdminHeader === 'true' || request.isAdminRequest === true
|
||||
// }
|
||||
// }
|
||||
@@ -1,20 +1,50 @@
|
||||
import { checkAndDecodeJwtToken } from '@/common/utils/jwt-user.util'
|
||||
import { AccountType } from '@/generated/prisma/enums'
|
||||
import { Injectable, NestMiddleware, UnauthorizedException } from '@nestjs/common'
|
||||
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 ConsumerMiddleware implements NestMiddleware {
|
||||
constructor(private jwtService: JwtService) {}
|
||||
use(req: Request, _res: Response, next: NextFunction) {
|
||||
const decodedToken = checkAndDecodeJwtToken(req, this.jwtService)
|
||||
if (decodedToken?.type === AccountType.CONSUMER) {
|
||||
req.decodedToken = decodedToken
|
||||
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)
|
||||
|
||||
if (tokenAccount?.type !== 'CONSUMER') {
|
||||
return doForbidden()
|
||||
}
|
||||
|
||||
const consumerAccount = await this.prisma.consumerAccount.findUnique({
|
||||
where: {
|
||||
id: tokenAccount.account_id,
|
||||
},
|
||||
select: {
|
||||
user_id: true,
|
||||
account_id: true,
|
||||
},
|
||||
})
|
||||
|
||||
if (!consumerAccount) {
|
||||
return doForbidden()
|
||||
}
|
||||
|
||||
req.consumerData = {
|
||||
account_id: consumerAccount.account_id,
|
||||
user_id: consumerAccount.user_id,
|
||||
}
|
||||
req.decodedToken = tokenAccount
|
||||
|
||||
return next()
|
||||
} catch (error) {
|
||||
return doForbidden()
|
||||
}
|
||||
|
||||
throw new UnauthorizedException('شما دسترسی لازم را ندارید.')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,11 +4,12 @@ import { ConsumerAccountsModule } from './accounts/accounts.module'
|
||||
import { ConsumerBusinessActivitiesModule } from './business-activities/business-activities.module'
|
||||
import { ConsumerController } from './consumer.controller'
|
||||
import { ConsumerMiddleware } from './consumer.middleware'
|
||||
import { ConsumerService } from './consumer.service'
|
||||
|
||||
@Module({
|
||||
controllers: [ConsumerController],
|
||||
imports: [ConsumerAccountsModule, ConsumerBusinessActivitiesModule],
|
||||
providers: [JwtService],
|
||||
providers: [JwtService, ConsumerService],
|
||||
})
|
||||
export class ConsumerModule implements NestModule {
|
||||
configure(consumer: MiddlewareConsumer) {
|
||||
@@ -17,5 +18,10 @@ export class ConsumerModule implements NestModule {
|
||||
method: RequestMethod.ALL,
|
||||
version: '1',
|
||||
})
|
||||
consumer.apply(ConsumerMiddleware).forRoutes({
|
||||
path: '/consumer',
|
||||
method: RequestMethod.ALL,
|
||||
version: '1',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import { ResponseMapper } from '@/common/response/response-mapper'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
|
||||
@Injectable()
|
||||
export class ConsumerService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async getInfo(consumer_id: string) {
|
||||
const consumer = await this.prisma.consumer.findUnique({
|
||||
where: {
|
||||
id: consumer_id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
first_name: true,
|
||||
last_name: true,
|
||||
mobile_number: true,
|
||||
status: true,
|
||||
license: {
|
||||
select: {
|
||||
starts_at: true,
|
||||
expires_at: true,
|
||||
status: true,
|
||||
partner: {
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
return ResponseMapper.single({
|
||||
...consumer,
|
||||
full_name: `${consumer?.first_name} ${consumer?.last_name}`,
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user