feat(partners): add utility functions for partner business activity allocation limits and remaining licenses
- Implemented `getPartnerBusinessActivityAllocationLimits` to retrieve allocation limits for a partner's business activity. - Added `ensurePartnerBusinessActivityHasRemainingAllocation` to validate remaining allocation credits. - Created `getPartnerRemainingLicenses` to count remaining licenses for a partner. - Developed `getPartnerFirstRemainingLicense` to fetch the first unused license for a partner. - Introduced `ensurePartnerHasRemainingLicense` to ensure a partner has at least one unused license.
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { Body, Controller, Get, Param, Patch, Post } from '@nestjs/common'
|
||||
import { PartnerInfo } from '@/common/decorators/partnerInfo.decorator'
|
||||
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('PartnerConsumerAccounts')
|
||||
@Controller('partner/consumers/:consumerId/accounts')
|
||||
@@ -9,26 +10,39 @@ export class AccountsController {
|
||||
constructor(private readonly accountsService: AccountsService) {}
|
||||
|
||||
@Get()
|
||||
async findAll(@Param('consumerId') consumerId: string) {
|
||||
return this.accountsService.findAll(consumerId)
|
||||
async findAll(
|
||||
@PartnerInfo('id') partnerId: string,
|
||||
@Param('consumerId') consumerId: string,
|
||||
) {
|
||||
return this.accountsService.findAll(partnerId, consumerId)
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async findOne(@Param('id') id: string) {
|
||||
return this.accountsService.findOne(id)
|
||||
async findOne(
|
||||
@PartnerInfo('id') partnerId: string,
|
||||
@Param('consumerId') consumerId: string,
|
||||
@Param('id') id: string,
|
||||
) {
|
||||
return this.accountsService.findOne(partnerId, consumerId, id)
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(
|
||||
@Param('consumerId') consumerId: string,
|
||||
@Body() data: CreateConsumerAccountDto,
|
||||
) {
|
||||
return this.accountsService.create(consumerId, data)
|
||||
}
|
||||
// @Post()
|
||||
// async create(
|
||||
// @PartnerInfo('id') partnerId: string,
|
||||
// @Param('consumerId') consumerId: string,
|
||||
// @Body() data: CreateConsumerAccountDto,
|
||||
// ) {
|
||||
// return this.accountsService.create(partnerId, consumerId, data)
|
||||
// }
|
||||
|
||||
@Patch(':id')
|
||||
async update(@Param('id') id: string, @Body() data: UpdateAccountDto) {
|
||||
return this.accountsService.update(id, data)
|
||||
async update(
|
||||
@PartnerInfo('id') partnerId: string,
|
||||
@Param('consumerId') consumerId: string,
|
||||
@Param('id') id: string,
|
||||
@Body() data: UpdateAccountDto,
|
||||
) {
|
||||
return this.accountsService.update(partnerId, consumerId, id, data)
|
||||
}
|
||||
|
||||
// @Delete(':id')
|
||||
|
||||
@@ -1,82 +1,113 @@
|
||||
import { PasswordUtil } from '@/common/utils/password.util'
|
||||
import { AccountStatus, AccountType } from '@/generated/prisma/enums'
|
||||
import { ConsumerAccountSelect } from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreateConsumerAccountDto, UpdateAccountDto } from './dto/account.dto'
|
||||
import { UpdateAccountDto } from './dto/account.dto'
|
||||
|
||||
@Injectable()
|
||||
export class AccountsService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async findAll(consumer_id: string) {
|
||||
const accounts = await this.prisma.consumerAccount.findMany({
|
||||
where: { consumer_id },
|
||||
private readonly default_select: ConsumerAccountSelect = {
|
||||
id: true,
|
||||
role: true,
|
||||
created_at: true,
|
||||
pos: {
|
||||
select: {
|
||||
id: true,
|
||||
role: true,
|
||||
created_at: true,
|
||||
account: {
|
||||
name: true,
|
||||
complex: {
|
||||
select: {
|
||||
username: true,
|
||||
status: true,
|
||||
id: true,
|
||||
name: true,
|
||||
branch_code: true,
|
||||
business_activity: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
account: {
|
||||
select: {
|
||||
username: true,
|
||||
status: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
private readonly defaultWhere = (partner_id, consumer_id) => ({
|
||||
consumer_id,
|
||||
consumer: {
|
||||
partner_id,
|
||||
},
|
||||
})
|
||||
|
||||
async findAll(partner_id: string, consumer_id: string) {
|
||||
const accounts = await this.prisma.consumerAccount.findMany({
|
||||
where: this.defaultWhere(partner_id, consumer_id),
|
||||
select: this.default_select,
|
||||
})
|
||||
return ResponseMapper.list(accounts)
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
async findOne(partner_id: string, consumer_id: string, id: string) {
|
||||
const account = await this.prisma.consumerAccount.findMany({
|
||||
where: { id },
|
||||
select: {
|
||||
id: true,
|
||||
role: true,
|
||||
created_at: true,
|
||||
account: {
|
||||
select: {
|
||||
username: true,
|
||||
status: true,
|
||||
},
|
||||
},
|
||||
where: {
|
||||
...this.defaultWhere(partner_id, consumer_id),
|
||||
id,
|
||||
},
|
||||
select: this.default_select,
|
||||
})
|
||||
return ResponseMapper.single(account)
|
||||
}
|
||||
|
||||
async create(consumerId: string, data: CreateConsumerAccountDto) {
|
||||
const account = await this.prisma.consumerAccount.create({
|
||||
data: {
|
||||
role: data.role,
|
||||
account: {
|
||||
create: {
|
||||
password: await PasswordUtil.hash(data.password),
|
||||
status: AccountStatus.ACTIVE,
|
||||
type: AccountType.CONSUMER,
|
||||
username: data.username,
|
||||
},
|
||||
},
|
||||
consumer: {
|
||||
connect: {
|
||||
id: consumerId,
|
||||
},
|
||||
},
|
||||
permission: {
|
||||
create: {},
|
||||
},
|
||||
},
|
||||
})
|
||||
return ResponseMapper.create(account)
|
||||
}
|
||||
// async create(partner_id: string, consumer_id: string, data: CreateConsumerAccountDto) {
|
||||
// const account = await this.prisma.consumerAccount.create({
|
||||
// data: {
|
||||
// role: data.role,
|
||||
// account: {
|
||||
// create: {
|
||||
// password: await PasswordUtil.hash(data.password),
|
||||
// status: AccountStatus.ACTIVE,
|
||||
// type: AccountType.CONSUMER,
|
||||
// username: data.username,
|
||||
// },
|
||||
// },
|
||||
// consumer: {
|
||||
// connect: {
|
||||
// id: consumer_id,
|
||||
// },
|
||||
// },
|
||||
// permission: {
|
||||
// create: {},
|
||||
// },
|
||||
// },
|
||||
// })
|
||||
// return ResponseMapper.create(account)
|
||||
// }
|
||||
|
||||
async update(id: string, data: UpdateAccountDto) {
|
||||
const account = await this.prisma.account.update({ where: { id }, data })
|
||||
async update(
|
||||
partner_id: string,
|
||||
consumer_id: string,
|
||||
id: string,
|
||||
data: UpdateAccountDto,
|
||||
) {
|
||||
const account = await this.prisma.account.update({
|
||||
where: {
|
||||
...this.defaultWhere(partner_id, consumer_id),
|
||||
id,
|
||||
},
|
||||
data,
|
||||
})
|
||||
return ResponseMapper.update(account)
|
||||
}
|
||||
|
||||
async delete(id: string) {
|
||||
await this.prisma.account.delete({ where: { id } })
|
||||
return ResponseMapper.delete()
|
||||
}
|
||||
// async delete(partner_id: string, id: string) {
|
||||
// await this.prisma.account.delete({ where: { id } })
|
||||
// return ResponseMapper.delete()
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user