Files
psp_api/src/modules/partners/accounts/accounts.service.ts
T

86 lines
2.4 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, UpdatePartnerAccountDto } from './dto/account.dto'
@Injectable()
export class AccountsService {
constructor(private readonly prisma: PrismaService) {}
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,
},
},
},
skip: (page - 1) * perPage,
take: perPage,
}),
await tx.partnerAccount.count({ where }),
])
return ResponseMapper.paginate(accounts, { total, page, perPage })
}
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: UpdatePartnerAccountDto) {
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()
// }
}