update models and create consumer domain accounts permissions
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { TokenAccount } from '@/common/decorators/tokenInfo.decorator'
|
||||
import { Body, Controller, Get, Param, Patch, Post } from '@nestjs/common'
|
||||
import { Body, Controller, Get, Param, Patch } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { AccountsService } from './accounts.service'
|
||||
import { CreateConsumerAccountDto, UpdateAccountDto } from './dto/account.dto'
|
||||
import { UpdateAccountDto } from './dto/account.dto'
|
||||
|
||||
@ApiTags('ConsumerAccounts')
|
||||
@Controller('consumer/accounts')
|
||||
@@ -19,13 +19,13 @@ export class AccountsController {
|
||||
return this.accountsService.findOne(id)
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(
|
||||
@TokenAccount('userId') consumerId: string,
|
||||
@Body() data: CreateConsumerAccountDto,
|
||||
) {
|
||||
return this.accountsService.create(consumerId, data)
|
||||
}
|
||||
// @Post()
|
||||
// async create(
|
||||
// @TokenAccount('userId') consumerId: string,
|
||||
// @Body() data: CreateConsumerAccountDto,
|
||||
// ) {
|
||||
// return this.accountsService.create(consumerId, data)
|
||||
// }
|
||||
|
||||
@Patch(':id')
|
||||
async update(@Param('id') id: string, @Body() data: UpdateAccountDto) {
|
||||
|
||||
@@ -2,9 +2,10 @@ import { PrismaModule } from '@/prisma/prisma.module'
|
||||
import { Module } from '@nestjs/common'
|
||||
import { AccountsController } from './accounts.controller'
|
||||
import { AccountsService } from './accounts.service'
|
||||
import { AdminAccountPermissionsModule } from './permissions/permissions.module'
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
imports: [PrismaModule, AdminAccountPermissionsModule],
|
||||
controllers: [AccountsController],
|
||||
providers: [AccountsService],
|
||||
exports: [AccountsService],
|
||||
|
||||
@@ -9,9 +9,9 @@ import { CreateConsumerAccountDto, UpdateAccountDto } from './dto/account.dto'
|
||||
export class AccountsService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async findAll(consumerId: string) {
|
||||
async findAll(consumer_id: string) {
|
||||
const accounts = await this.prisma.consumerAccount.findMany({
|
||||
where: { user_id: consumerId },
|
||||
where: { consumer_id },
|
||||
select: {
|
||||
id: true,
|
||||
role: true,
|
||||
@@ -28,7 +28,7 @@ export class AccountsService {
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const account = await this.prisma.consumerAccount.findMany({
|
||||
const account = await this.prisma.consumerAccount.findUniqueOrThrow({
|
||||
where: { id },
|
||||
select: {
|
||||
id: true,
|
||||
@@ -57,11 +57,14 @@ export class AccountsService {
|
||||
username: data.username,
|
||||
},
|
||||
},
|
||||
user: {
|
||||
consumer: {
|
||||
connect: {
|
||||
id: consumerId,
|
||||
},
|
||||
},
|
||||
permission: {
|
||||
create: {},
|
||||
},
|
||||
},
|
||||
})
|
||||
return ResponseMapper.create(account)
|
||||
|
||||
@@ -1,24 +1,5 @@
|
||||
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
|
||||
export class UpdatePermissionDto {
|
||||
poses?: string[]
|
||||
complexes?: string[]
|
||||
business_activities?: string[]
|
||||
}
|
||||
|
||||
@@ -1,20 +1,28 @@
|
||||
import { Body, Controller, Get, Param, Post } from '@nestjs/common'
|
||||
import { TokenAccount } from '@/common/decorators/tokenInfo.decorator'
|
||||
import { Body, Controller, Get, Param, Put } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { CreateAccountDto } from './dto/permission.dto'
|
||||
import { UpdatePermissionDto } from './dto/permission.dto'
|
||||
import { AccountsService } from './permissions.service'
|
||||
|
||||
@ApiTags('AdminAccounts')
|
||||
@Controller('admin/users/:userId/accounts/:accountId/permissions')
|
||||
@ApiTags('ConsumerAccountPermissions')
|
||||
@Controller('consumer/accounts/:accountId/permissions')
|
||||
export class AccountPermissionsController {
|
||||
constructor(private readonly service: AccountsService) {}
|
||||
|
||||
@Get()
|
||||
async findAll(@Param('userId') userId: string, @Param('accountId') accountId: string) {
|
||||
async findAll(
|
||||
@TokenAccount('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)
|
||||
@Put()
|
||||
async create(
|
||||
@Param('userId') userId: string,
|
||||
@Param('accountId') accountId: string,
|
||||
@Body() data: UpdatePermissionDto,
|
||||
) {
|
||||
return this.service.create(userId, accountId, data)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +1,92 @@
|
||||
import { BusinessRole, ComplexRole, POSRole } from '@/generated/prisma/enums'
|
||||
import { PrismaPromise } from '@/generated/prisma/internal/prismaNamespace'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ForbiddenException, Injectable, NotFoundException } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreateAccountDto } from './dto/permission.dto'
|
||||
import { UpdatePermissionDto } 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({
|
||||
async findAll(consumer_id: string, account_id: string) {
|
||||
let data: any = []
|
||||
|
||||
const businesses = [] as any
|
||||
const complexes = [] as any
|
||||
let poses = [] as any
|
||||
|
||||
const consumerAccount = await this.prisma.consumerAccount.findUniqueOrThrow({
|
||||
where: {
|
||||
user_id: userId,
|
||||
id: account_id,
|
||||
consumer_id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
role: true,
|
||||
},
|
||||
})
|
||||
|
||||
const role = consumerAccount?.role
|
||||
|
||||
if (!role) {
|
||||
return new NotFoundException('کاربر مورد نظر شما یافت نشد.')
|
||||
}
|
||||
|
||||
if (role === 'MANAGER') {
|
||||
return ResponseMapper.list(data)
|
||||
}
|
||||
|
||||
const permissions = await this.prisma.permissionConsumer.findMany({
|
||||
where: {
|
||||
consumer_account: {
|
||||
id: account_id,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
pos_permissions: {
|
||||
select: {
|
||||
pos_id: true,
|
||||
role: true,
|
||||
},
|
||||
},
|
||||
business_permissions: {
|
||||
select: {
|
||||
business_id: true,
|
||||
role: true,
|
||||
},
|
||||
},
|
||||
complex_permissions: {
|
||||
select: {
|
||||
complex_id: true,
|
||||
role: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const posList = await this.prisma.pos.findMany({
|
||||
where: {
|
||||
complex: {
|
||||
business_activity: {
|
||||
consumer: {
|
||||
consumer_accounts: {
|
||||
some: {
|
||||
id: account_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
complexes: {
|
||||
complex: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
pos_list: {
|
||||
business_activity: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
@@ -30,87 +97,173 @@ export class AccountsService {
|
||||
},
|
||||
})
|
||||
|
||||
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,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
if (role === 'OPERATOR') {
|
||||
poses = posList?.map(pos => ({
|
||||
...pos,
|
||||
hasAccess: permissions.some(permission =>
|
||||
permission.pos_permissions.some(
|
||||
posPermission => posPermission.pos_id === pos.id,
|
||||
),
|
||||
),
|
||||
}))
|
||||
|
||||
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,
|
||||
return ResponseMapper.single({ poses })
|
||||
} else {
|
||||
const businessActivities = await this.prisma.businessActivity.findMany({
|
||||
where: {
|
||||
consumer: {
|
||||
consumer_accounts: {
|
||||
some: {
|
||||
account_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
complexes: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
pos_list: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
businessActivity.complexes.forEach(complex => {
|
||||
complexes.push({
|
||||
id: complex.id,
|
||||
name: complex.name,
|
||||
businessActivities.forEach(businessActivity => {
|
||||
businesses.push({
|
||||
id: businessActivity.id,
|
||||
name: businessActivity.name,
|
||||
hasAccess: false,
|
||||
role: null,
|
||||
})
|
||||
|
||||
complex.pos_list.forEach(pos => {
|
||||
poses.push({
|
||||
id: pos.id,
|
||||
name: pos.name,
|
||||
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,
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
return ResponseMapper.single({ businessActivities, complexes })
|
||||
}
|
||||
}
|
||||
|
||||
async create(consumer_id: string, account_id: string, data: UpdatePermissionDto) {
|
||||
const consumerAccount = await this.prisma.consumerAccount.findUniqueOrThrow({
|
||||
where: {
|
||||
id: account_id,
|
||||
consumer_id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
role: true,
|
||||
},
|
||||
})
|
||||
|
||||
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
|
||||
})
|
||||
const role = consumerAccount?.role
|
||||
|
||||
if (!role) {
|
||||
return new NotFoundException('کاربر مورد نظر شما یافت نشد.')
|
||||
}
|
||||
if (role === 'MANAGER') {
|
||||
return new ForbiddenException(
|
||||
'این امکان برای کاربران با سطح دسترسی مدیر اصلی وجود ندارد.',
|
||||
)
|
||||
}
|
||||
|
||||
const existingPermissionConsumer = await this.prisma.permissionConsumer.findUnique({
|
||||
where: {
|
||||
consumer_account_id: account_id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
})
|
||||
|
||||
if (!existingPermissionConsumer) {
|
||||
throw new NotFoundException(``)
|
||||
}
|
||||
|
||||
const permissionConsumerId = existingPermissionConsumer.id
|
||||
await this.prisma.$transaction(async tx => {
|
||||
await tx.permissionPos.deleteMany({
|
||||
where: { permission_id: permissionConsumerId },
|
||||
})
|
||||
await tx.permissionComplex.deleteMany({
|
||||
where: { permission_id: permissionConsumerId },
|
||||
})
|
||||
await tx.permissionBusiness.deleteMany({
|
||||
where: { permission_id: permissionConsumerId },
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const account = await this.prisma.account.findUnique({
|
||||
where: { id },
|
||||
})
|
||||
return ResponseMapper.single(account)
|
||||
}
|
||||
const createOperations: PrismaPromise<any>[] = []
|
||||
|
||||
async create(userId: string, data: CreateAccountDto) {
|
||||
if (data.poses && data.poses.length > 0) {
|
||||
data.poses.forEach(posId => {
|
||||
createOperations.push(
|
||||
tx.permissionPos.create({
|
||||
data: {
|
||||
role: POSRole.OPERATOR,
|
||||
permission: { connect: { id: permissionConsumerId } },
|
||||
pos: { connect: { id: posId } },
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
if (data.complexes && data.complexes.length > 0) {
|
||||
data.complexes.forEach(complexId => {
|
||||
createOperations.push(
|
||||
tx.permissionComplex.create({
|
||||
data: {
|
||||
role: ComplexRole.OPERATOR,
|
||||
permission: { connect: { id: permissionConsumerId } },
|
||||
complex: { connect: { id: complexId } },
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
if (data.business_activities && data.business_activities.length > 0) {
|
||||
data.business_activities.forEach(businessId => {
|
||||
createOperations.push(
|
||||
tx.permissionBusiness.create({
|
||||
data: {
|
||||
role: BusinessRole.OPERATOR,
|
||||
permission: { connect: { id: permissionConsumerId } },
|
||||
business: { connect: { id: businessId } },
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
if (createOperations.length > 0) {
|
||||
await Promise.all(createOperations)
|
||||
}
|
||||
})
|
||||
return ResponseMapper.create({})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,27 +20,27 @@ export class BusinessActivitiesService {
|
||||
created_at: true,
|
||||
} as BusinessActivitySelect
|
||||
|
||||
async findAll(user_id: string) {
|
||||
async findAll(consumer_id: string) {
|
||||
const businessActivities = await this.prisma.businessActivity.findMany({
|
||||
where: {
|
||||
user_id,
|
||||
consumer_id,
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
return ResponseMapper.list(businessActivities)
|
||||
}
|
||||
|
||||
async findOne(user_id: string, id: string) {
|
||||
async findOne(consumer_id: string, id: string) {
|
||||
const businessActivity = await this.prisma.businessActivity.findUniqueOrThrow({
|
||||
where: { user_id, id },
|
||||
where: { consumer_id, id },
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
return ResponseMapper.single(businessActivity)
|
||||
}
|
||||
|
||||
async update(user_id: string, id: string, data: any) {
|
||||
async update(consumer_id: string, id: string, data: any) {
|
||||
const businessActivity = await this.prisma.businessActivity.update({
|
||||
where: { id, user_id },
|
||||
where: { id, consumer_id },
|
||||
data,
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
@@ -16,13 +16,13 @@ export class BusinessActivityComplexesService {
|
||||
created_at: true,
|
||||
} as ComplexSelect
|
||||
|
||||
async findAll(user_id: string, business_activity_id: string) {
|
||||
async findAll(consumer_id: string, business_activity_id: string) {
|
||||
const accounts = await this.prisma.complex.findMany({
|
||||
where: {
|
||||
business_activity: {
|
||||
id: business_activity_id,
|
||||
user: {
|
||||
id: user_id,
|
||||
consumer: {
|
||||
id: consumer_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -31,13 +31,13 @@ export class BusinessActivityComplexesService {
|
||||
return ResponseMapper.list(accounts)
|
||||
}
|
||||
|
||||
async findOne(user_id: string, business_activity_id: string, id: string) {
|
||||
async findOne(consumer_id: string, business_activity_id: string, id: string) {
|
||||
const account = await this.prisma.complex.findUnique({
|
||||
where: {
|
||||
business_activity: {
|
||||
id: business_activity_id,
|
||||
user: {
|
||||
id: user_id,
|
||||
consumer: {
|
||||
id: consumer_id,
|
||||
},
|
||||
},
|
||||
id,
|
||||
@@ -48,7 +48,7 @@ export class BusinessActivityComplexesService {
|
||||
}
|
||||
|
||||
async update(
|
||||
user_id: string,
|
||||
consumer_id: string,
|
||||
business_activity_id: string,
|
||||
id: string,
|
||||
data: UpdateComplexDto,
|
||||
@@ -57,8 +57,8 @@ export class BusinessActivityComplexesService {
|
||||
where: {
|
||||
business_activity: {
|
||||
id: business_activity_id,
|
||||
user: {
|
||||
id: user_id,
|
||||
consumer: {
|
||||
id: consumer_id,
|
||||
},
|
||||
},
|
||||
id,
|
||||
|
||||
@@ -25,7 +25,7 @@ export class ConsumerComplexGoodsService {
|
||||
created_at: true,
|
||||
}
|
||||
|
||||
async findAll(user_id: string, business_activity_id: string, complex_id: string) {
|
||||
async findAll(consumer_id: string, business_activity_id: string, complex_id: string) {
|
||||
const goods = await this.prisma.good.findMany({
|
||||
where: {
|
||||
complex_id,
|
||||
@@ -33,7 +33,7 @@ export class ConsumerComplexGoodsService {
|
||||
id: complex_id,
|
||||
business_activity: {
|
||||
id: business_activity_id,
|
||||
user_id,
|
||||
consumer_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -44,7 +44,7 @@ export class ConsumerComplexGoodsService {
|
||||
}
|
||||
|
||||
async findOne(
|
||||
user_id: string,
|
||||
consumer_id: string,
|
||||
business_activity_id: string,
|
||||
complex_id: string,
|
||||
id: string,
|
||||
@@ -55,7 +55,7 @@ export class ConsumerComplexGoodsService {
|
||||
id: complex_id,
|
||||
business_activity: {
|
||||
id: business_activity_id,
|
||||
user_id,
|
||||
consumer_id,
|
||||
},
|
||||
},
|
||||
id,
|
||||
@@ -87,7 +87,7 @@ export class ConsumerComplexGoodsService {
|
||||
}
|
||||
|
||||
async update(
|
||||
user_id: string,
|
||||
consumer_id: string,
|
||||
business_activity_id: string,
|
||||
complex_id: string,
|
||||
id: string,
|
||||
@@ -100,7 +100,7 @@ export class ConsumerComplexGoodsService {
|
||||
id: complex_id,
|
||||
business_activity: {
|
||||
id: business_activity_id,
|
||||
user_id,
|
||||
consumer_id,
|
||||
},
|
||||
},
|
||||
id,
|
||||
|
||||
@@ -77,14 +77,14 @@ export class ComplexPosesService {
|
||||
} as PosCreateInput
|
||||
}
|
||||
|
||||
async findAll(user_id: string, business_activity_id: string, complex_id: string) {
|
||||
async findAll(consumer_id: string, business_activity_id: string, complex_id: string) {
|
||||
const poses = await this.prisma.pos.findMany({
|
||||
where: {
|
||||
complex: {
|
||||
id: complex_id,
|
||||
business_activity: {
|
||||
id: business_activity_id,
|
||||
user_id,
|
||||
consumer_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
+2
-2
@@ -7,14 +7,14 @@ import { Injectable } from '@nestjs/common'
|
||||
export class SalesInvoicesService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async findAll(user_id: string, complex_id: string, pos_id: string) {
|
||||
async findAll(consumer_id: string, complex_id: string, pos_id: string) {
|
||||
const defaultWhere: SalesInvoiceWhereInput = {
|
||||
pos_id,
|
||||
pos: {
|
||||
complex: {
|
||||
id: complex_id,
|
||||
business_activity: {
|
||||
user_id,
|
||||
consumer_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -9,7 +9,7 @@ export class ConsumerController {
|
||||
constructor(private service: ConsumerService) {}
|
||||
|
||||
@Get('')
|
||||
async findOne(@ConsumerInfo('user_id') userId: string) {
|
||||
return this.service.getInfo(userId)
|
||||
async findOne(@ConsumerInfo('id') consumerId: string) {
|
||||
return this.service.getInfo(consumerId)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,10 @@ export class ConsumerMiddleware implements NestMiddleware {
|
||||
try {
|
||||
const tokenAccount = checkAndDecodeJwtToken(req, this.jwtService)
|
||||
|
||||
if (tokenAccount?.type !== 'CONSUMER') {
|
||||
if (
|
||||
tokenAccount?.type !== 'CONSUMER' ||
|
||||
!(tokenAccount.role === 'OWNER' || tokenAccount.role === 'MANAGER')
|
||||
) {
|
||||
return doForbidden()
|
||||
}
|
||||
|
||||
@@ -27,8 +30,8 @@ export class ConsumerMiddleware implements NestMiddleware {
|
||||
id: tokenAccount.account_id,
|
||||
},
|
||||
select: {
|
||||
user_id: true,
|
||||
account_id: true,
|
||||
consumer_id: true,
|
||||
id: true,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -37,8 +40,8 @@ export class ConsumerMiddleware implements NestMiddleware {
|
||||
}
|
||||
|
||||
req.consumerData = {
|
||||
account_id: consumerAccount.account_id,
|
||||
user_id: consumerAccount.user_id,
|
||||
account_id: consumerAccount.id,
|
||||
id: consumerAccount.consumer_id,
|
||||
}
|
||||
req.decodedToken = tokenAccount
|
||||
|
||||
|
||||
@@ -11,30 +11,30 @@ export class consumerCustomersController {
|
||||
constructor(private readonly service: consumerCustomersService) {}
|
||||
|
||||
@Get()
|
||||
async findAll(@ConsumerInfo('user_id') userId: string) {
|
||||
return this.service.findAll(userId)
|
||||
async findAll(@ConsumerInfo('id') consumerId: string) {
|
||||
return this.service.findAll(consumerId)
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async findOne(@ConsumerInfo('user_id') userId: string, @Param('id') id: string) {
|
||||
return this.service.findOne(userId, id)
|
||||
async findOne(@ConsumerInfo('id') consumerId: string, @Param('id') id: string) {
|
||||
return this.service.findOne(consumerId, id)
|
||||
}
|
||||
|
||||
@Patch(':id/legal')
|
||||
async updateLegal(
|
||||
@ConsumerInfo('user_id') userId: string,
|
||||
@ConsumerInfo('id') consumerId: string,
|
||||
@Param('id') id: string,
|
||||
@Body() data: UpdateCustomerLegalDto,
|
||||
) {
|
||||
return this.service.updateLegal(userId, id, data)
|
||||
return this.service.updateLegal(consumerId, id, data)
|
||||
}
|
||||
|
||||
@Patch(':id/individual')
|
||||
async updateIndividual(
|
||||
@ConsumerInfo('user_id') userId: string,
|
||||
@ConsumerInfo('id') consumerId: string,
|
||||
@Param('id') id: string,
|
||||
@Body() data: UpdateCustomerIndividualDto,
|
||||
) {
|
||||
return this.service.updateIndividual(userId, id, data)
|
||||
return this.service.updateIndividual(consumerId, id, data)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,14 +35,14 @@ export class consumerCustomersService {
|
||||
},
|
||||
}
|
||||
|
||||
private defaultWhere(user_id: string): CustomerWhereInput {
|
||||
private defaultWhere(consumer_id: string): CustomerWhereInput {
|
||||
return {
|
||||
OR: [
|
||||
{
|
||||
customer_individual: {
|
||||
complex: {
|
||||
business_activity: {
|
||||
user_id,
|
||||
consumer_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -51,7 +51,7 @@ export class consumerCustomersService {
|
||||
customer_legal: {
|
||||
complex: {
|
||||
business_activity: {
|
||||
user_id,
|
||||
consumer_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -60,43 +60,45 @@ export class consumerCustomersService {
|
||||
}
|
||||
}
|
||||
|
||||
async findAll(user_id: string, page = 1, pageSize = 10) {
|
||||
async findAll(consumer_id: string, page = 1, pageSize = 10) {
|
||||
const [customers, count] = await this.prisma.$transaction(async tx => [
|
||||
await tx.customer.findMany({
|
||||
where: this.defaultWhere(user_id),
|
||||
where: this.defaultWhere(consumer_id),
|
||||
select: this.defaultSelect,
|
||||
skip: (page - 1) * pageSize,
|
||||
take: 10,
|
||||
}),
|
||||
await tx.customer.count({
|
||||
where: this.defaultWhere(user_id),
|
||||
where: this.defaultWhere(consumer_id),
|
||||
}),
|
||||
])
|
||||
return ResponseMapper.paginate(customers, { count, page, pageSize })
|
||||
}
|
||||
|
||||
async findOne(user_id: string, customer_id: string) {
|
||||
async findOne(consumer_id: string, customer_id: string) {
|
||||
const customer = await this.prisma.customer.findUniqueOrThrow({
|
||||
where: {
|
||||
...this.defaultWhere(user_id),
|
||||
...this.defaultWhere(consumer_id),
|
||||
id: customer_id,
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
console.log(customer)
|
||||
|
||||
return ResponseMapper.single(customer)
|
||||
}
|
||||
|
||||
async updateLegal(user_id: string, customer_id: string, data: UpdateCustomerLegalDto) {
|
||||
async updateLegal(
|
||||
consumer_id: string,
|
||||
customer_id: string,
|
||||
data: UpdateCustomerLegalDto,
|
||||
) {
|
||||
const customer = await this.prisma.customer.update({
|
||||
where: {
|
||||
id: customer_id,
|
||||
customer_legal: {
|
||||
complex: {
|
||||
business_activity: {
|
||||
user_id,
|
||||
consumer_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -115,7 +117,7 @@ export class consumerCustomersService {
|
||||
}
|
||||
|
||||
async updateIndividual(
|
||||
user_id: string,
|
||||
consumer_id: string,
|
||||
customer_id: string,
|
||||
data: UpdateCustomerIndividualDto,
|
||||
) {
|
||||
@@ -125,7 +127,7 @@ export class consumerCustomersService {
|
||||
customer_individual: {
|
||||
complex: {
|
||||
business_activity: {
|
||||
user_id,
|
||||
consumer_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -34,7 +34,7 @@ export class CustomerSaleInvoicesService {
|
||||
account: {
|
||||
select: {
|
||||
role: true,
|
||||
user: {
|
||||
consumer: {
|
||||
select: {
|
||||
first_name: true,
|
||||
last_name: true,
|
||||
@@ -50,13 +50,13 @@ export class CustomerSaleInvoicesService {
|
||||
created_at: true,
|
||||
}
|
||||
|
||||
async findAll(user_id: string, customer_id: string, page = 1, pageSize = 10) {
|
||||
async findAll(consumer_id: string, customer_id: string, page = 1, pageSize = 10) {
|
||||
const salesWhere: SalesInvoiceWhereInput = {
|
||||
customer_id,
|
||||
pos: {
|
||||
complex: {
|
||||
business_activity: {
|
||||
user_id,
|
||||
consumer_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -96,7 +96,7 @@ export class CustomerSaleInvoicesService {
|
||||
})
|
||||
}
|
||||
|
||||
async findOne(user_id: string, customer_id: string, id: string) {
|
||||
async findOne(consumer_id: string, customer_id: string, id: string) {
|
||||
const account = await this.prisma.salesInvoice.findUniqueOrThrow({
|
||||
where: {
|
||||
id,
|
||||
@@ -104,7 +104,7 @@ export class CustomerSaleInvoicesService {
|
||||
pos: {
|
||||
complex: {
|
||||
business_activity: {
|
||||
user_id,
|
||||
consumer_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user