update models and create consumer domain accounts permissions
This commit is contained in:
@@ -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,
|
||||
@@ -57,11 +57,14 @@ export class AccountsService {
|
||||
username: data.username,
|
||||
},
|
||||
},
|
||||
user: {
|
||||
consumer: {
|
||||
connect: {
|
||||
id: consumerId,
|
||||
},
|
||||
},
|
||||
permission: {
|
||||
create: {},
|
||||
},
|
||||
},
|
||||
})
|
||||
return ResponseMapper.create(account)
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
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
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
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 {}
|
||||
@@ -1,116 +0,0 @@
|
||||
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({})
|
||||
}
|
||||
}
|
||||
@@ -21,25 +21,25 @@ export class BusinessActivitiesService {
|
||||
created_at: true,
|
||||
} as BusinessActivitySelect
|
||||
|
||||
async findAll(userId: string) {
|
||||
async findAll(consumer_id: string) {
|
||||
const businessActivities = await this.prisma.businessActivity.findMany({
|
||||
where: {
|
||||
user_id: userId,
|
||||
consumer_id,
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
return ResponseMapper.list(businessActivities)
|
||||
}
|
||||
|
||||
async findOne(userId: string, id: string) {
|
||||
async findOne(consumer_id: string, id: string) {
|
||||
const businessActivity = await this.prisma.businessActivity.findUnique({
|
||||
where: { user_id: userId, id },
|
||||
where: { consumer_id, id },
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
return ResponseMapper.single(businessActivity)
|
||||
}
|
||||
|
||||
async create(userId: string, data: CreateBusinessActivitiesDto) {
|
||||
async create(consumer_id: string, data: CreateBusinessActivitiesDto) {
|
||||
const businessActivity = await this.prisma.businessActivity.create({
|
||||
data: {
|
||||
name: data.name,
|
||||
@@ -48,9 +48,9 @@ export class BusinessActivitiesService {
|
||||
id: data.guild_id,
|
||||
},
|
||||
},
|
||||
user: {
|
||||
consumer: {
|
||||
connect: {
|
||||
id: userId,
|
||||
id: consumer_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -59,9 +59,9 @@ export class BusinessActivitiesService {
|
||||
return ResponseMapper.create(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,
|
||||
@@ -62,7 +62,7 @@ export class BusinessActivityComplexesService {
|
||||
}
|
||||
|
||||
async update(
|
||||
user_id: string,
|
||||
consumer_id: string,
|
||||
business_activity_id: string,
|
||||
id: string,
|
||||
data: UpdateComplexDto,
|
||||
@@ -71,8 +71,8 @@ export class BusinessActivityComplexesService {
|
||||
where: {
|
||||
business_activity: {
|
||||
id: business_activity_id,
|
||||
user: {
|
||||
id: user_id,
|
||||
consumer: {
|
||||
id: consumer_id,
|
||||
},
|
||||
},
|
||||
id,
|
||||
|
||||
@@ -74,7 +74,7 @@ export class AdminUsersService {
|
||||
const { username, password, license, ...rest } = data
|
||||
const dataToCreate: ConsumerCreateInput = {
|
||||
...rest,
|
||||
accounts: {
|
||||
consumer_accounts: {
|
||||
create: {
|
||||
role: ConsumerRole.OWNER,
|
||||
account: {
|
||||
|
||||
@@ -22,9 +22,9 @@ export class AccountsService {
|
||||
},
|
||||
} as PartnerAccountSelect
|
||||
|
||||
async findAll(partnerId: string) {
|
||||
async findAll(partner_id: string) {
|
||||
const accounts = await this.prisma.partnerAccount.findMany({
|
||||
where: { user_id: partnerId },
|
||||
where: { partner_id },
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
return ResponseMapper.list(accounts)
|
||||
@@ -55,7 +55,7 @@ export class AccountsService {
|
||||
type: AccountType.PARTNER,
|
||||
},
|
||||
},
|
||||
user: {
|
||||
partner: {
|
||||
connect: {
|
||||
id: partnerId,
|
||||
},
|
||||
|
||||
@@ -46,7 +46,7 @@ export class PartnersService {
|
||||
data: {
|
||||
...rest,
|
||||
status: PartnerStatus.ACTIVE,
|
||||
accounts: {
|
||||
partner_accounts: {
|
||||
create: {
|
||||
role: PartnerRole.OWNER,
|
||||
account: {
|
||||
|
||||
@@ -27,7 +27,7 @@ export class ProvidersService {
|
||||
const provider = await this.prisma.provider.create({
|
||||
data: {
|
||||
...rest,
|
||||
accounts: {
|
||||
provider_accounts: {
|
||||
create: {
|
||||
role: ProviderRole.OWNER,
|
||||
account: {
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
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
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
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 {}
|
||||
@@ -1,116 +0,0 @@
|
||||
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({})
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ export interface ITokenPayload {
|
||||
type: AccountType
|
||||
username: string
|
||||
account_id: string
|
||||
role: string
|
||||
user?: Record<string, any>
|
||||
}
|
||||
@Injectable()
|
||||
@@ -43,7 +44,7 @@ export class AuthUtils {
|
||||
admin_account: {
|
||||
select: {
|
||||
id: true,
|
||||
user: {
|
||||
admin: {
|
||||
select: {
|
||||
id: true,
|
||||
first_name: true,
|
||||
@@ -60,12 +61,13 @@ export class AuthUtils {
|
||||
if (!adminAccount || !adminAccount.admin_account)
|
||||
throw new NotFoundException('متاسفانه کاربر شما یافت نشد')
|
||||
|
||||
const { user, id } = adminAccount.admin_account
|
||||
const { admin, id } = adminAccount.admin_account
|
||||
return {
|
||||
account_id: id,
|
||||
role: 'owner',
|
||||
user: {
|
||||
...user,
|
||||
fullname: `${user.first_name} ${user.last_name}`,
|
||||
...admin,
|
||||
fullname: `${admin.first_name} ${admin.last_name}`,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -79,7 +81,8 @@ export class AuthUtils {
|
||||
consumer_account: {
|
||||
select: {
|
||||
id: true,
|
||||
user: {
|
||||
role: true,
|
||||
consumer: {
|
||||
select: {
|
||||
id: true,
|
||||
first_name: true,
|
||||
@@ -95,12 +98,13 @@ export class AuthUtils {
|
||||
if (!consumerAccount || !consumerAccount.consumer_account)
|
||||
throw new NotFoundException('متاسفانه کاربر شما یافت نشد')
|
||||
|
||||
const { user, id } = consumerAccount.consumer_account
|
||||
const { consumer, id, role } = consumerAccount.consumer_account
|
||||
return {
|
||||
account_id: id,
|
||||
role,
|
||||
user: {
|
||||
...user,
|
||||
fullname: `${user.first_name} ${user.last_name}`,
|
||||
...consumer,
|
||||
fullname: `${consumer.first_name} ${consumer.last_name}`,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -113,8 +117,9 @@ export class AuthUtils {
|
||||
select: {
|
||||
provider_account: {
|
||||
select: {
|
||||
role: true,
|
||||
id: true,
|
||||
user: {
|
||||
provider: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
@@ -130,12 +135,13 @@ export class AuthUtils {
|
||||
if (!providerAccount || !providerAccount.provider_account)
|
||||
throw new NotFoundException('متاسفانه کاربر شما یافت نشد')
|
||||
|
||||
const { user, id } = providerAccount.provider_account
|
||||
const { provider, id, role } = providerAccount.provider_account
|
||||
return {
|
||||
account_id: id,
|
||||
role,
|
||||
user: {
|
||||
...user,
|
||||
fullname: user.name,
|
||||
...provider,
|
||||
fullname: provider.name,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -149,7 +155,8 @@ export class AuthUtils {
|
||||
partner_account: {
|
||||
select: {
|
||||
id: true,
|
||||
user: {
|
||||
role: true,
|
||||
partner: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
@@ -165,12 +172,13 @@ export class AuthUtils {
|
||||
if (!partnerAccount || !partnerAccount.partner_account)
|
||||
throw new NotFoundException('متاسفانه کاربر شما یافت نشد')
|
||||
|
||||
const { user, id } = partnerAccount.partner_account
|
||||
const { partner, id, role } = partnerAccount.partner_account
|
||||
return {
|
||||
account_id: id,
|
||||
role,
|
||||
user: {
|
||||
...user,
|
||||
fullname: user.name,
|
||||
...partner,
|
||||
fullname: partner.name,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -186,6 +194,7 @@ export class AuthUtils {
|
||||
let accountInfo = {
|
||||
user: {},
|
||||
account_id: '',
|
||||
role: '',
|
||||
}
|
||||
|
||||
switch (account.type) {
|
||||
@@ -204,6 +213,7 @@ export class AuthUtils {
|
||||
}
|
||||
|
||||
preparedAccount.account_id = accountInfo.account_id
|
||||
preparedAccount.role = accountInfo.role
|
||||
preparedAccount.user = accountInfo.user
|
||||
|
||||
if (!preparedAccount.user) {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { PublicWithToken } from '@/common/decorators/withToken.decorator'
|
||||
import { Controller, Get, Param } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { CatalogsService } from './catalog.service'
|
||||
@@ -8,26 +9,31 @@ export class CatalogsController {
|
||||
constructor(private readonly catalogService: CatalogsService) {}
|
||||
|
||||
@Get('guilds')
|
||||
@PublicWithToken()
|
||||
async getGuilds() {
|
||||
return this.catalogService.getGuilds()
|
||||
}
|
||||
|
||||
@Get('providers')
|
||||
@PublicWithToken()
|
||||
async getProviders() {
|
||||
return this.catalogService.getProviders()
|
||||
}
|
||||
|
||||
@Get('devices')
|
||||
@PublicWithToken()
|
||||
async getDevices() {
|
||||
return this.catalogService.getDevices()
|
||||
}
|
||||
|
||||
@Get('device_brands')
|
||||
@PublicWithToken()
|
||||
async getDeviceBrands() {
|
||||
return this.catalogService.getDeviceBrands()
|
||||
}
|
||||
|
||||
@Get('guilds/:guildId/good_categories')
|
||||
@PublicWithToken()
|
||||
async getGuildGoodCategories(@Param('guildId') guild_id: string) {
|
||||
return this.catalogService.getGuildGoodCategories(guild_id)
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -107,4 +107,10 @@ export class EnumsController {
|
||||
async getGoodPricingModel() {
|
||||
return this.enumsService.getEnumValues('GoodPricingModel')
|
||||
}
|
||||
|
||||
@Get('consumer_role')
|
||||
@PublicWithToken()
|
||||
async getConsumerRoleModel() {
|
||||
return this.enumsService.getEnumValues('ConsumerRole')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
AccountRole,
|
||||
AccountStatus,
|
||||
BusinessRole,
|
||||
ConsumerRole,
|
||||
GoodPricingModel,
|
||||
LicenseStatus,
|
||||
LicenseType,
|
||||
@@ -47,6 +48,7 @@ export class EnumsService {
|
||||
TokenType: this.prepareData(TokenType),
|
||||
UnitType: this.prepareData(UnitType),
|
||||
GoodPricingModel: this.prepareData(GoodPricingModel),
|
||||
ConsumerRole: this.prepareData(ConsumerRole),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { PosInfo } from '@/common/decorators/posInfo.decorator'
|
||||
import { Controller, Get } from '@nestjs/common'
|
||||
import { TokenAccount } from '@/common/decorators/tokenInfo.decorator'
|
||||
import { Controller, Get, Res } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { PosService } from './pos.service'
|
||||
|
||||
@@ -9,7 +10,24 @@ export class PosController {
|
||||
constructor(private service: PosService) {}
|
||||
|
||||
@Get()
|
||||
async getInfo(@PosInfo('pos_id') pos_id: string) {
|
||||
return await this.service.getInfo(pos_id)
|
||||
async getInfo(@PosInfo('pos_id') pos_id: string, @Res({ passthrough: true }) res) {
|
||||
const result = await this.service.getInfo(pos_id)
|
||||
|
||||
if (result) {
|
||||
// @ts-ignore
|
||||
res.cookie('posId', pos_id, {
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
sameSite: 'lax',
|
||||
maxAge: 1000 * 60 * 60 * 24 * 7, // 7 days
|
||||
})
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
@Get('/accessible')
|
||||
async getAccessiblePos(@TokenAccount('account_id') account_id) {
|
||||
return await this.service.getAccessible(account_id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
import { checkAndDecodeJwtToken } from '@/common/utils/jwt-user.util'
|
||||
import { ConsumerRole } from '@/generated/prisma/enums'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { ForbiddenException, Injectable, NestMiddleware } from '@nestjs/common'
|
||||
import {
|
||||
ForbiddenException,
|
||||
Injectable,
|
||||
MisdirectedException,
|
||||
NestMiddleware,
|
||||
} from '@nestjs/common'
|
||||
import { JwtService } from '@nestjs/jwt'
|
||||
import { NextFunction, Request, Response } from 'express'
|
||||
|
||||
@@ -18,79 +24,114 @@ export class PosMiddleware implements NestMiddleware {
|
||||
try {
|
||||
const tokenAccount = checkAndDecodeJwtToken(req, this.jwtService)
|
||||
|
||||
// const posId = req.cookies['posId']
|
||||
const posId = req.headers.pos_id as string
|
||||
const { type, role, account_id } = tokenAccount!
|
||||
|
||||
if (tokenAccount?.type !== 'CONSUMER' || !posId || typeof posId !== 'string') {
|
||||
let posId = req.cookies['posId']
|
||||
|
||||
if (type !== 'CONSUMER') {
|
||||
return doForbidden()
|
||||
}
|
||||
|
||||
const consumerAccount = await this.prisma.consumerAccount.findUnique({
|
||||
where: {
|
||||
id: tokenAccount.account_id,
|
||||
},
|
||||
})
|
||||
if (!consumerAccount) {
|
||||
return doForbidden()
|
||||
}
|
||||
|
||||
const complex = await this.prisma.complex.findFirst({
|
||||
where: {
|
||||
pos_list: {
|
||||
some: {
|
||||
id: posId,
|
||||
if (!posId || typeof posId !== 'string') {
|
||||
const pos = await this.prisma.pos.findMany({
|
||||
where: {
|
||||
complex: {
|
||||
business_activity: {
|
||||
consumer: {
|
||||
consumer_accounts: {
|
||||
some: {
|
||||
id: account_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (!pos?.length) {
|
||||
throw new ForbiddenException('پایانهی فروشی برای شما یافت نشد')
|
||||
}
|
||||
if (pos.length === 1) {
|
||||
posId = pos[0].id
|
||||
} else throw new MisdirectedException()
|
||||
}
|
||||
|
||||
const pos = await this.prisma.pos.findUnique({
|
||||
where: {
|
||||
id: posId,
|
||||
},
|
||||
include: {
|
||||
business_activity: {
|
||||
include: {
|
||||
guild: true,
|
||||
select: {
|
||||
id: true,
|
||||
complex: {
|
||||
select: {
|
||||
id: true,
|
||||
business_activity: {
|
||||
select: {
|
||||
id: true,
|
||||
guild_id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
if (!complex) {
|
||||
|
||||
if (!pos) {
|
||||
return doForbidden()
|
||||
}
|
||||
|
||||
// if (account?.role !== ConsumerRole.OWNER) {
|
||||
// const accountPermissions = await this.prisma.permissionConsumer.findUnique({
|
||||
// where: {
|
||||
// account_id: account?.id,
|
||||
// },
|
||||
// include: {
|
||||
// posPermissions: true,
|
||||
// businessPermissions: true,
|
||||
// complexPermissions: true,
|
||||
// },
|
||||
// })
|
||||
if (role !== ConsumerRole.OWNER) {
|
||||
const accountPermissions = await this.prisma.permissionConsumer.findUnique({
|
||||
where: {
|
||||
consumer_account_id: account_id,
|
||||
},
|
||||
select: {
|
||||
pos_permissions: {
|
||||
select: {
|
||||
pos_id: true,
|
||||
},
|
||||
},
|
||||
business_permissions: {
|
||||
select: {
|
||||
business_id: true,
|
||||
},
|
||||
},
|
||||
complex_permissions: {
|
||||
select: {
|
||||
complex_id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// if (
|
||||
// !(
|
||||
// accountPermissions?.businessPermissions.some(
|
||||
// permission => permission.business_id,
|
||||
// ) ||
|
||||
// accountPermissions?.complexPermissions.some(
|
||||
// permission => permission.complex_id,
|
||||
// ) ||
|
||||
// accountPermissions?.posPermissions.some(permission => permission.pos_id)
|
||||
// )
|
||||
// ) {
|
||||
// return doForbidden()
|
||||
// }
|
||||
// }
|
||||
if (
|
||||
!// accountPermissions?.business_permissions.some(
|
||||
// permission => permission.id,
|
||||
// ) ||
|
||||
// accountPermissions?.complex_permissions.some(
|
||||
// permission => permission.complex_id,
|
||||
// ) ||
|
||||
accountPermissions?.pos_permissions.some(permission => permission.pos_id)
|
||||
) {
|
||||
return doForbidden()
|
||||
}
|
||||
}
|
||||
req.posData = {
|
||||
pos_id: posId,
|
||||
complex_id: complex.id,
|
||||
business_id: complex.business_activity_id,
|
||||
guild_id: complex.business_activity.guild_id,
|
||||
consumer_account_id: consumerAccount.id,
|
||||
complex_id: pos.complex.id,
|
||||
business_id: pos.complex.id,
|
||||
guild_id: pos.complex.business_activity.guild_id,
|
||||
consumer_account_id: account_id,
|
||||
}
|
||||
req.decodedToken = tokenAccount
|
||||
req.decodedToken = tokenAccount!
|
||||
|
||||
return next()
|
||||
} catch (error) {
|
||||
if (error && typeof error === 'object') {
|
||||
throw error
|
||||
}
|
||||
|
||||
return doForbidden()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,4 +53,24 @@ export class PosService {
|
||||
guild,
|
||||
})
|
||||
}
|
||||
|
||||
async getAccessible(account_id: string) {
|
||||
const poses = await this.prisma.pos.findMany({
|
||||
where: {
|
||||
complex: {
|
||||
business_activity: {
|
||||
consumer: {
|
||||
consumer_accounts: {
|
||||
some: {
|
||||
id: account_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
return ResponseMapper.list(poses)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,8 +40,6 @@ export class SalesInvoicesService {
|
||||
}
|
||||
|
||||
async create(data: CreateSalesInvoiceDto, posInfo: IPosPayload) {
|
||||
console.log(posInfo)
|
||||
|
||||
data.invoice_date = new Date(data.invoice_date).toISOString() as any
|
||||
|
||||
const { complex_id, pos_id, consumer_account_id } = posInfo
|
||||
|
||||
Reference in New Issue
Block a user