2026-04-13 15:47:59 +03:30
|
|
|
import { PasswordUtil } from '@/common/utils/password.util'
|
|
|
|
|
import { AccountStatus, AccountType } from '@/generated/prisma/enums'
|
|
|
|
|
import { PrismaService } from '@/prisma/prisma.service'
|
|
|
|
|
import { Injectable } from '@nestjs/common'
|
|
|
|
|
import { ResponseMapper } from 'common/response/response-mapper'
|
|
|
|
|
import { CreatePartnerAccountDto, UpdateAccountDto } from './dto/account.dto'
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class AccountsService {
|
|
|
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
|
|
2026-05-05 22:42:09 +03:30
|
|
|
async findAll(partner_id: string, page = 1, perPage = 10) {
|
|
|
|
|
const where = { partner_id }
|
|
|
|
|
const [accounts, total] = await this.prisma.$transaction(async tx => [
|
|
|
|
|
await tx.partnerAccount.findMany({
|
|
|
|
|
where,
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
role: true,
|
|
|
|
|
created_at: true,
|
|
|
|
|
account: {
|
|
|
|
|
select: {
|
|
|
|
|
username: true,
|
|
|
|
|
status: true,
|
|
|
|
|
},
|
2026-04-13 15:47:59 +03:30
|
|
|
},
|
|
|
|
|
},
|
2026-05-05 22:42:09 +03:30
|
|
|
skip: (page - 1) * perPage,
|
|
|
|
|
take: perPage,
|
|
|
|
|
}),
|
|
|
|
|
await tx.partnerAccount.count({ where }),
|
|
|
|
|
])
|
|
|
|
|
return ResponseMapper.paginate(accounts, { total, page, perPage })
|
2026-04-13 15:47:59 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findOne(id: string) {
|
|
|
|
|
const account = await this.prisma.partnerAccount.findUniqueOrThrow({
|
|
|
|
|
where: { id },
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
role: true,
|
|
|
|
|
created_at: true,
|
|
|
|
|
account: {
|
|
|
|
|
select: {
|
|
|
|
|
username: true,
|
|
|
|
|
status: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
return ResponseMapper.single(account)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async create(partnerId: string, data: CreatePartnerAccountDto) {
|
|
|
|
|
const account = await this.prisma.partnerAccount.create({
|
|
|
|
|
data: {
|
|
|
|
|
role: data.role,
|
|
|
|
|
account: {
|
|
|
|
|
create: {
|
|
|
|
|
password: await PasswordUtil.hash(data.password),
|
|
|
|
|
status: AccountStatus.ACTIVE,
|
|
|
|
|
type: AccountType.PARTNER,
|
|
|
|
|
username: data.username,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
partner: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: partnerId,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
return ResponseMapper.create(account)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async update(id: string, data: UpdateAccountDto) {
|
|
|
|
|
const account = await this.prisma.account.update({ where: { id }, data })
|
|
|
|
|
return ResponseMapper.update(account)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// async delete(id: string) {
|
|
|
|
|
// await this.prisma.account.delete({ where: { id } })
|
|
|
|
|
// return ResponseMapper.delete()
|
|
|
|
|
// }
|
|
|
|
|
}
|