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:
@@ -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