create token decorator and consumer module
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
import { ApiProperty, PartialType } from '@nestjs/swagger'
|
||||
import { IsEnum, IsOptional, IsString } from 'class-validator'
|
||||
import { AccountStatus, AccountType } from 'generated/prisma/enums'
|
||||
|
||||
export class CreateAccountDto {
|
||||
@IsString()
|
||||
@ApiProperty()
|
||||
username: string
|
||||
|
||||
@IsString()
|
||||
@ApiProperty()
|
||||
password: string
|
||||
|
||||
@IsEnum(AccountType)
|
||||
@ApiProperty({ enum: AccountType })
|
||||
type: AccountType
|
||||
}
|
||||
|
||||
export class UpdateAccountDto extends PartialType(CreateAccountDto) {
|
||||
@IsOptional()
|
||||
@IsEnum(AccountStatus)
|
||||
@ApiProperty({ enum: AccountStatus })
|
||||
status?: AccountStatus
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { Body, Controller, Get, Param, Post } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { CreateAccountDto } from './dto/permission.dto'
|
||||
import { AccountsService } from './permissions.service'
|
||||
|
||||
@ApiTags('AdminAccounts')
|
||||
@Controller('admin/users/:userId/accounts/:accountId/permissions')
|
||||
export class AccountPermissionsController {
|
||||
constructor(private readonly service: AccountsService) {}
|
||||
|
||||
@Get()
|
||||
async findAll(@Param('userId') userId: string, @Param('accountId') accountId: string) {
|
||||
return this.service.findAll(userId, accountId)
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(@Param('userId') userId: string, @Body() data: CreateAccountDto) {
|
||||
return this.service.create(userId, data)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { PrismaModule } from '@/prisma/prisma.module'
|
||||
import { Module } from '@nestjs/common'
|
||||
import { AccountPermissionsController } from './permissions.controller'
|
||||
import { AccountsService } from './permissions.service'
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [AccountPermissionsController],
|
||||
providers: [AccountsService],
|
||||
exports: [AccountsService],
|
||||
})
|
||||
export class AdminAccountPermissionsModule {}
|
||||
@@ -0,0 +1,116 @@
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreateAccountDto } from './dto/permission.dto'
|
||||
|
||||
@Injectable()
|
||||
export class AccountsService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async findAll(userId: string, account_id: string) {
|
||||
const businessActivities = await this.prisma.businessActivity.findMany({
|
||||
where: {
|
||||
user_id: userId,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
complexes: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
pos_list: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const permissions = await this.prisma.permissionConsumer.findMany({
|
||||
where: {
|
||||
account_id,
|
||||
},
|
||||
select: {
|
||||
posPermissions: {
|
||||
select: {
|
||||
pos_id: true,
|
||||
role: true,
|
||||
},
|
||||
},
|
||||
businessPermissions: {
|
||||
select: {
|
||||
business_id: true,
|
||||
role: true,
|
||||
},
|
||||
},
|
||||
complexPermissions: {
|
||||
select: {
|
||||
complex_id: true,
|
||||
role: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const businesses = [] as any
|
||||
const complexes = [] as any
|
||||
const poses = [] as any
|
||||
|
||||
businessActivities.forEach(businessActivity => {
|
||||
businesses.push({
|
||||
id: businessActivity.id,
|
||||
name: businessActivity.name,
|
||||
hasAccess: false,
|
||||
role: null,
|
||||
})
|
||||
|
||||
businessActivity.complexes.forEach(complex => {
|
||||
complexes.push({
|
||||
id: complex.id,
|
||||
name: complex.name,
|
||||
hasAccess: false,
|
||||
role: null,
|
||||
})
|
||||
|
||||
complex.pos_list.forEach(pos => {
|
||||
poses.push({
|
||||
id: pos.id,
|
||||
name: pos.name,
|
||||
hasAccess: false,
|
||||
role: null,
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
permissions.forEach(permission => {
|
||||
permission.posPermissions.forEach(posPermission => {
|
||||
poses.map(pos => {
|
||||
if (pos.id === posPermission.pos_id) {
|
||||
return {
|
||||
...pos,
|
||||
role: posPermission.role,
|
||||
hasAccess: true,
|
||||
}
|
||||
}
|
||||
return pos
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const account = await this.prisma.account.findUnique({
|
||||
where: { id },
|
||||
})
|
||||
return ResponseMapper.single(account)
|
||||
}
|
||||
|
||||
async create(userId: string, data: CreateAccountDto) {
|
||||
return ResponseMapper.create({})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user