80 lines
2.1 KiB
TypeScript
80 lines
2.1 KiB
TypeScript
|
|
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) {}
|
||
|
|
|
||
|
|
async findAll(partner_id: string) {
|
||
|
|
const accounts = await this.prisma.partnerAccount.findMany({
|
||
|
|
where: { partner_id },
|
||
|
|
select: {
|
||
|
|
id: true,
|
||
|
|
role: true,
|
||
|
|
created_at: true,
|
||
|
|
account: {
|
||
|
|
select: {
|
||
|
|
username: true,
|
||
|
|
status: true,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
})
|
||
|
|
return ResponseMapper.list(accounts)
|
||
|
|
}
|
||
|
|
|
||
|
|
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()
|
||
|
|
// }
|
||
|
|
}
|