feat: Refactor accounts service and related DTOs to enhance consumer account management
- Updated AccountsService to filter out OWNER roles in findAll method. - Removed CreateConsumerAccountDto and its role property from account creation logic. - Enhanced BusinessActivitiesService to include complex counts in responses. - Modified ComplexesController and ComplexesService to handle consumer-specific logic. - Introduced CreateConsumerComplexDto and UpdateConsumerComplexDto for complex management. - Updated PosesController and PosesService to manage POS creation and updates with consumer context. - Added utility function to calculate remaining accounts for business activities. - Updated Prisma migration to add account_id to poses and enforce unique constraints.
This commit is contained in:
@@ -8,6 +8,10 @@ export class BusinessActivitiesService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
defaultSelect: BusinessActivitySelect = {
|
||||
id: true,
|
||||
name: true,
|
||||
economic_code: true,
|
||||
created_at: true,
|
||||
guild: {
|
||||
select: {
|
||||
id: true,
|
||||
@@ -15,9 +19,15 @@ export class BusinessActivitiesService {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
id: true,
|
||||
name: true,
|
||||
created_at: true,
|
||||
complexes: {
|
||||
select: {
|
||||
_count: {
|
||||
select: {
|
||||
pos_list: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
license_activation: {
|
||||
select: {
|
||||
id: true,
|
||||
@@ -38,7 +48,7 @@ export class BusinessActivitiesService {
|
||||
}
|
||||
|
||||
private readonly prepareBusinessActivityResponse = (businessActivity: any) => {
|
||||
const { license_activation, ...rest } = businessActivity
|
||||
const { license_activation, complexes, ...rest } = businessActivity
|
||||
const { license, ...license_activation_rest } = license_activation
|
||||
const { accounts_limit, _count } = license
|
||||
|
||||
@@ -47,6 +57,10 @@ export class BusinessActivitiesService {
|
||||
license_info: {
|
||||
...license_activation_rest,
|
||||
accounts_limit: accounts_limit + _count.account_allocations,
|
||||
allocated_account_count: complexes.reduce(
|
||||
(acc: number, complex: any) => acc + complex._count.pos_list,
|
||||
0,
|
||||
),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { TokenAccount } from '@/common/decorators/tokenInfo.decorator'
|
||||
import { Body, Controller, Get, Param, Patch } from '@nestjs/common'
|
||||
import { ConsumerInfo } from '@/common/decorators/consumerInfo.decorator'
|
||||
import { Body, Controller, Get, Param, Patch, Post } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { BusinessActivityComplexesService } from './complexes.service'
|
||||
import { UpdateComplexDto } from './dto/complex.dto'
|
||||
import { CreateConsumerComplexDto, UpdateConsumerComplexDto } from './dto/complex.dto'
|
||||
|
||||
@ApiTags('AdminBusinessActivityComplexes')
|
||||
@Controller('consumer/business_activities/:businessActivityId/complexes')
|
||||
@@ -11,28 +11,37 @@ export class BusinessActivityComplexesController {
|
||||
|
||||
@Get()
|
||||
async findAll(
|
||||
@TokenAccount('userId') userId: string,
|
||||
@ConsumerInfo('id') consumerId: string,
|
||||
@Param('businessActivityId') businessActivityId: string,
|
||||
) {
|
||||
return this.service.findAll(userId, businessActivityId)
|
||||
return this.service.findAll(consumerId, businessActivityId)
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async findOne(
|
||||
@TokenAccount('userId') userId: string,
|
||||
@ConsumerInfo('id') consumerId: string,
|
||||
@Param('businessActivityId') businessActivityId: string,
|
||||
@Param('id') id: string,
|
||||
) {
|
||||
return this.service.findOne(userId, businessActivityId, id)
|
||||
return this.service.findOne(consumerId, businessActivityId, id)
|
||||
}
|
||||
|
||||
@Post('')
|
||||
async create(
|
||||
@ConsumerInfo('id') consumerId: string,
|
||||
@Param('businessActivityId') businessActivityId: string,
|
||||
@Body() data: CreateConsumerComplexDto,
|
||||
) {
|
||||
return this.service.create(consumerId, businessActivityId, data)
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
async update(
|
||||
@TokenAccount('userId') userId: string,
|
||||
@ConsumerInfo('id') consumerId: string,
|
||||
@Param('businessActivityId') businessActivityId: string,
|
||||
@Param('id') id: string,
|
||||
@Body() data: UpdateComplexDto,
|
||||
@Body() data: UpdateConsumerComplexDto,
|
||||
) {
|
||||
return this.service.update(userId, businessActivityId, id, data)
|
||||
return this.service.update(consumerId, businessActivityId, id, data)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { ComplexSelect } from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { BadRequestException, Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { UpdateComplexDto } from './dto/complex.dto'
|
||||
import { getBusinessActivityRemainingAccounts } from '../../utils/getBusinessActivityRemainingAccounts.util'
|
||||
import { CreateConsumerComplexDto, UpdateConsumerComplexDto } from './dto/complex.dto'
|
||||
|
||||
@Injectable()
|
||||
export class BusinessActivityComplexesService {
|
||||
@@ -47,11 +48,42 @@ export class BusinessActivityComplexesService {
|
||||
return ResponseMapper.single(account)
|
||||
}
|
||||
|
||||
async create(
|
||||
consumer_id: string,
|
||||
business_activity_id: string,
|
||||
data: CreateConsumerComplexDto,
|
||||
) {
|
||||
const complex = await this.prisma.$transaction(async tx => {
|
||||
const quota = await getBusinessActivityRemainingAccounts(tx, {
|
||||
consumer_id,
|
||||
business_activity_id,
|
||||
})
|
||||
|
||||
if (quota.remaining_accounts <= 0) {
|
||||
throw new BadRequestException(
|
||||
`متاسفانه به دلیل به پایان رسیدن محدودیت تعریف کاربران برای این فعالیت اقتصادی،امکان ایجاد شعبهی جدید وجود ندارد.`,
|
||||
)
|
||||
}
|
||||
|
||||
return await tx.complex.create({
|
||||
data: {
|
||||
...data,
|
||||
business_activity: {
|
||||
connect: {
|
||||
id: business_activity_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
return ResponseMapper.create(complex)
|
||||
}
|
||||
|
||||
async update(
|
||||
consumer_id: string,
|
||||
business_activity_id: string,
|
||||
id: string,
|
||||
data: UpdateComplexDto,
|
||||
data: UpdateConsumerComplexDto,
|
||||
) {
|
||||
const account = await this.prisma.complex.update({
|
||||
where: {
|
||||
|
||||
@@ -1,18 +1,19 @@
|
||||
import { ApiProperty, PartialType } from '@nestjs/swagger'
|
||||
import { IsString } from 'class-validator'
|
||||
import { IsOptional, IsString } from 'class-validator'
|
||||
|
||||
export class CreateComplexDto {
|
||||
export class CreateConsumerComplexDto {
|
||||
@IsString()
|
||||
@ApiProperty({})
|
||||
@ApiProperty({ required: true })
|
||||
name: string
|
||||
|
||||
@IsString()
|
||||
@ApiProperty({})
|
||||
tax_id: string
|
||||
@ApiProperty({ required: true })
|
||||
branch_code: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@ApiProperty({})
|
||||
address: string
|
||||
}
|
||||
|
||||
export class UpdateComplexDto extends PartialType(CreateComplexDto) {}
|
||||
export class UpdateConsumerComplexDto extends PartialType(CreateConsumerComplexDto) {}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { POSStatus, POSType } from '@/generated/prisma/enums'
|
||||
import { ApiProperty, PartialType } from '@nestjs/swagger'
|
||||
import { CreateConsumerAccountDto } from '@/modules/consumer/accounts/dto/account.dto'
|
||||
import { ApiProperty, OmitType, PartialType } from '@nestjs/swagger'
|
||||
import { IsEnum, IsOptional, IsString } from 'class-validator'
|
||||
|
||||
export class CreatePosDto {
|
||||
export class CreatePosDto extends CreateConsumerAccountDto {
|
||||
@IsString()
|
||||
@ApiProperty({})
|
||||
name: string
|
||||
@@ -31,7 +32,11 @@ export class CreatePosDto {
|
||||
provider_id: string
|
||||
}
|
||||
|
||||
export class UpdatePosDto extends PartialType(CreatePosDto) {
|
||||
export class UpdatePosDto extends OmitType(PartialType(CreatePosDto), [
|
||||
'username',
|
||||
'password',
|
||||
'pos_type',
|
||||
]) {
|
||||
@IsEnum(POSStatus)
|
||||
@IsOptional()
|
||||
@ApiProperty({ enum: POSStatus })
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { TokenAccount } from '@/common/decorators/tokenInfo.decorator'
|
||||
import { ConsumerInfo } from '@/common/decorators/consumerInfo.decorator'
|
||||
import { Body, Controller, Get, Param, Patch, Post } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { CreatePosDto, UpdatePosDto } from './dto/pos.dto'
|
||||
@@ -11,35 +11,42 @@ export class ComplexPosesController {
|
||||
|
||||
@Get()
|
||||
async findAll(
|
||||
@TokenAccount('userId') userId: string,
|
||||
@ConsumerInfo('id') consumerId: string,
|
||||
@Param('businessActivityId') businessActivityId: string,
|
||||
@Param('complexId') complexId: string,
|
||||
) {
|
||||
return this.service.findAll(userId, businessActivityId, complexId)
|
||||
return this.service.findAll(consumerId, businessActivityId, complexId)
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async findOne(
|
||||
@ConsumerInfo('id') consumerId: string,
|
||||
@Param('businessActivityId') businessActivityId: string,
|
||||
@Param('complexId') complexId: string,
|
||||
@Param('id') id: string,
|
||||
) {
|
||||
return this.service.findOne(businessActivityId, complexId, id)
|
||||
return this.service.findOne(consumerId, businessActivityId, complexId, id)
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(@Param('complexId') complexId: string, @Body() data: CreatePosDto) {
|
||||
return this.service.create(complexId, data)
|
||||
async create(
|
||||
@ConsumerInfo('id') consumerId: string,
|
||||
@Param('businessActivityId') businessActivityId: string,
|
||||
@Param('complexId') complexId: string,
|
||||
@Body() data: CreatePosDto,
|
||||
) {
|
||||
return this.service.create(consumerId, businessActivityId, complexId, data)
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
async update(
|
||||
@ConsumerInfo('id') consumerId: string,
|
||||
@Param('businessActivityId') businessActivityId: string,
|
||||
@Param('complexId') complexId: string,
|
||||
@Param('id') id: string,
|
||||
@Body() data: UpdatePosDto,
|
||||
) {
|
||||
return this.service.update(businessActivityId, complexId, id, data)
|
||||
return this.service.update(consumerId, businessActivityId, complexId, id, data)
|
||||
}
|
||||
|
||||
// @Delete(':id')
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
import { POSStatus } from '@/generated/prisma/enums'
|
||||
import { PosCreateInput, PosSelect } from '@/generated/prisma/models'
|
||||
import { PasswordUtil } from '@/common/utils/password.util'
|
||||
import {
|
||||
AccountStatus,
|
||||
AccountType,
|
||||
ConsumerRole,
|
||||
POSStatus,
|
||||
} from '@/generated/prisma/enums'
|
||||
import { PosCreateInput, PosSelect, PosWhereInput } from '@/generated/prisma/models'
|
||||
import { getBusinessActivityRemainingAccounts } from '@/modules/consumer/utils/getBusinessActivityRemainingAccounts.util'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { BadRequestException, Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreatePosDto, UpdatePosDto } from './dto/pos.dto'
|
||||
|
||||
@@ -9,7 +16,7 @@ import { CreatePosDto, UpdatePosDto } from './dto/pos.dto'
|
||||
export class ComplexPosesService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
defaultSelect = {
|
||||
defaultSelect: PosSelect = {
|
||||
id: true,
|
||||
name: true,
|
||||
model: true,
|
||||
@@ -48,10 +55,28 @@ export class ComplexPosesService {
|
||||
},
|
||||
},
|
||||
},
|
||||
} as PosSelect
|
||||
}
|
||||
|
||||
defaultInsert = (data: CreatePosDto, complex_id: string) => {
|
||||
const { device_id, provider_id, ...rest } = data
|
||||
defaultWhere = (
|
||||
consumer_id: string,
|
||||
business_activity_id: string,
|
||||
complex_id: string,
|
||||
): PosWhereInput => ({
|
||||
complex: {
|
||||
id: complex_id,
|
||||
business_activity: {
|
||||
id: business_activity_id,
|
||||
consumer_id,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
defaultInsert = async (
|
||||
consumer_id: string,
|
||||
complex_id: string,
|
||||
data: CreatePosDto,
|
||||
): Promise<PosCreateInput> => {
|
||||
const { device_id, provider_id, username, password, ...rest } = data
|
||||
|
||||
return {
|
||||
...rest,
|
||||
@@ -60,6 +85,24 @@ export class ComplexPosesService {
|
||||
id: complex_id,
|
||||
},
|
||||
},
|
||||
account: {
|
||||
create: {
|
||||
role: ConsumerRole.OPERATOR,
|
||||
account: {
|
||||
create: {
|
||||
username,
|
||||
password: await PasswordUtil.hash(password),
|
||||
type: AccountType.CONSUMER,
|
||||
status: AccountStatus.ACTIVE,
|
||||
},
|
||||
},
|
||||
consumer: {
|
||||
connect: {
|
||||
id: consumer_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
provider: provider_id
|
||||
? {
|
||||
connect: {
|
||||
@@ -74,67 +117,73 @@ export class ComplexPosesService {
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
} as PosCreateInput
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
consumer_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
where: this.defaultWhere(consumer_id, business_activity_id, complex_id),
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
return ResponseMapper.list(poses)
|
||||
}
|
||||
|
||||
async findOne(business_activity_id: string, complex_id: string, id: string) {
|
||||
const account = await this.prisma.pos.findUnique({
|
||||
async findOne(
|
||||
consumer_id: string,
|
||||
business_activity_id: string,
|
||||
complex_id: string,
|
||||
id: string,
|
||||
) {
|
||||
const pos = await this.prisma.pos.findUnique({
|
||||
where: {
|
||||
complex: {
|
||||
id: complex_id,
|
||||
business_activity: {
|
||||
id: business_activity_id,
|
||||
},
|
||||
},
|
||||
...(this.defaultWhere(consumer_id, business_activity_id, complex_id) as any),
|
||||
id,
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
return ResponseMapper.single(account)
|
||||
return ResponseMapper.single(pos)
|
||||
}
|
||||
|
||||
async create(complex_id: string, data: CreatePosDto) {
|
||||
const account = await this.prisma.pos.create({
|
||||
data: {
|
||||
...this.defaultInsert(data, complex_id),
|
||||
status: POSStatus.ACTIVE,
|
||||
},
|
||||
async create(
|
||||
consumer_id: string,
|
||||
business_activity_id: string,
|
||||
complex_id: string,
|
||||
data: CreatePosDto,
|
||||
) {
|
||||
const pos = await this.prisma.$transaction(async tx => {
|
||||
const quota = await getBusinessActivityRemainingAccounts(tx, {
|
||||
consumer_id,
|
||||
business_activity_id,
|
||||
})
|
||||
|
||||
if (quota.remaining_accounts <= 0) {
|
||||
throw new BadRequestException(
|
||||
`تعداد کاربرهای تخصیص داده شده برای این فعالیت تجاری به پایان رسیده است.`,
|
||||
)
|
||||
}
|
||||
|
||||
return await tx.pos.create({
|
||||
data: {
|
||||
...(await this.defaultInsert(consumer_id, complex_id, data)),
|
||||
status: POSStatus.ACTIVE,
|
||||
},
|
||||
})
|
||||
})
|
||||
return ResponseMapper.create(account)
|
||||
return ResponseMapper.create(pos)
|
||||
}
|
||||
|
||||
async update(
|
||||
consumer_id: string,
|
||||
business_activity_id: string,
|
||||
complex_id: string,
|
||||
id: string,
|
||||
data: UpdatePosDto,
|
||||
) {
|
||||
const { device_id, provider_id, ...rest } = data
|
||||
const account = await this.prisma.pos.update({
|
||||
const pos = await this.prisma.pos.update({
|
||||
where: {
|
||||
complex: {
|
||||
id: complex_id,
|
||||
business_activity: {
|
||||
id: business_activity_id,
|
||||
},
|
||||
},
|
||||
...(this.defaultWhere(consumer_id, business_activity_id, complex_id) as any),
|
||||
id,
|
||||
},
|
||||
data: {
|
||||
@@ -160,11 +209,6 @@ export class ComplexPosesService {
|
||||
...rest,
|
||||
},
|
||||
})
|
||||
return ResponseMapper.update(account)
|
||||
return ResponseMapper.update(pos)
|
||||
}
|
||||
|
||||
// async delete(id: string) {
|
||||
// await this.prisma.complex.delete({ where: { id } })
|
||||
// return ResponseMapper.delete()
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user