create token decorator and consumer module
This commit is contained in:
@@ -1,20 +1,18 @@
|
||||
import { checkAndDecodeJwtToken } from '@/common/utils/jwt-user.util'
|
||||
import { AccountType } from '@/generated/prisma/enums'
|
||||
import { Injectable, NestMiddleware, UnauthorizedException } from '@nestjs/common'
|
||||
import { JwtService } from '@nestjs/jwt'
|
||||
import { NextFunction, Request, Response } from 'express'
|
||||
|
||||
@Injectable()
|
||||
export class AdminMiddleware implements NestMiddleware {
|
||||
constructor(private jwtService: JwtService) {}
|
||||
use(req: Request, _res: Response, next: NextFunction) {
|
||||
// const a = reqTokenPayload()
|
||||
// console.log(a)
|
||||
|
||||
// Middleware-based admin check (replace with real auth logic)
|
||||
const isAdminHeader = req.headers['x-admin']
|
||||
|
||||
if (isAdminHeader === 'true') {
|
||||
// mark request if needed downstream
|
||||
req['isAdminRequest'] = true
|
||||
const decodedToken = checkAndDecodeJwtToken(req, this.jwtService)
|
||||
if (decodedToken?.type === AccountType.ADMIN) {
|
||||
req.decodedToken = decodedToken
|
||||
return next()
|
||||
}
|
||||
return next()
|
||||
|
||||
throw new UnauthorizedException('Admin access required')
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { MiddlewareConsumer, Module, NestModule, RequestMethod } from '@nestjs/common'
|
||||
import { JwtService } from '@nestjs/jwt'
|
||||
import { AdminController } from './admin.controller'
|
||||
import { AdminMiddleware } from './admin.middleware'
|
||||
import { AdminConsumersModule } from './consumers/consumers.module'
|
||||
@@ -27,6 +28,7 @@ import { AdminUsersModule } from './users/users.module'
|
||||
AdminLicensesModule,
|
||||
AdminConsumersModule,
|
||||
],
|
||||
providers: [JwtService],
|
||||
})
|
||||
export class AdminModule implements NestModule {
|
||||
configure(consumer: MiddlewareConsumer) {
|
||||
|
||||
@@ -3,7 +3,7 @@ import { ApiTags } from '@nestjs/swagger'
|
||||
import { AccountsService } from './accounts.service'
|
||||
import { CreateConsumerAccountDto, UpdateAccountDto } from './dto/account.dto'
|
||||
|
||||
@ApiTags('ConsumerAccounts')
|
||||
@ApiTags('AdminConsumerAccounts')
|
||||
@Controller('admin/consumers/:consumerId/accounts')
|
||||
export class AccountsController {
|
||||
constructor(private readonly accountsService: AccountsService) {}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { Body, Controller, Get, Param, Post } from '@nestjs/common'
|
||||
import { Body, Controller, Get, Param, Post, Put } from '@nestjs/common'
|
||||
import { BusinessActivitiesService } from './business-activities.service'
|
||||
import { CreateBusinessActivitiesDto } from './dto/create-business-activities.dto'
|
||||
import {
|
||||
CreateBusinessActivitiesDto,
|
||||
UpdateBusinessActivityDto,
|
||||
} from './dto/create-business-activities.dto'
|
||||
|
||||
@Controller('admin/consumers/:consumerId/business_activities')
|
||||
export class BusinessActivitiesController {
|
||||
@@ -24,14 +27,14 @@ export class BusinessActivitiesController {
|
||||
return this.businessActivitiesService.create(consumerId, data)
|
||||
}
|
||||
|
||||
// @Put(':id')
|
||||
// async update(
|
||||
// @Param('consumerId') consumerId: string,
|
||||
// @Param('id') id: string,
|
||||
// @Body() data: UpdateBusinessActivityDto,
|
||||
// ) {
|
||||
// return this.businessActivitiesService.update(consumerId, id, data)
|
||||
// }
|
||||
@Put(':id')
|
||||
async update(
|
||||
@Param('consumerId') consumerId: string,
|
||||
@Param('id') id: string,
|
||||
@Body() data: UpdateBusinessActivityDto,
|
||||
) {
|
||||
return this.businessActivitiesService.update(consumerId, id, data)
|
||||
}
|
||||
|
||||
// @Delete(':id')
|
||||
// async delete(@Param('id') id: string) {
|
||||
|
||||
@@ -59,9 +59,9 @@ export class BusinessActivitiesService {
|
||||
return ResponseMapper.create(businessActivity)
|
||||
}
|
||||
|
||||
async update(id: string, data: any) {
|
||||
async update(user_id: string, id: string, data: any) {
|
||||
const businessActivity = await this.prisma.businessActivity.update({
|
||||
where: { id },
|
||||
where: { id, user_id },
|
||||
data,
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
+3
-1
@@ -1,4 +1,4 @@
|
||||
import { ApiProperty } from '@nestjs/swagger'
|
||||
import { ApiProperty, PartialType } from '@nestjs/swagger'
|
||||
import { IsString } from 'class-validator'
|
||||
|
||||
export class CreateBusinessActivitiesDto {
|
||||
@@ -10,3 +10,5 @@ export class CreateBusinessActivitiesDto {
|
||||
@ApiProperty({})
|
||||
guild_id: string
|
||||
}
|
||||
|
||||
export class UpdateBusinessActivityDto extends PartialType(CreateBusinessActivitiesDto) {}
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
import { PartialType } from '@nestjs/swagger'
|
||||
import { CreateBusinessActivitiesDto } from './create-business-activities.dto'
|
||||
|
||||
export class UpdateBusinessActivityDto extends PartialType(CreateBusinessActivitiesDto) {}
|
||||
@@ -8,8 +8,6 @@ export class AccountsService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async findAll(userId: string) {
|
||||
console.log('first')
|
||||
|
||||
// const accounts = await this.prisma.account.findMany({
|
||||
// where: { user_id: userId },
|
||||
// omit: { password: true, user_id: true },
|
||||
|
||||
Reference in New Issue
Block a user