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 { CreateConsumerAccountDto, UpdateAccountDto } from './dto/account.dto' @Injectable() export class AccountsService { constructor(private readonly prisma: PrismaService) {} async findAll(consumerId: string) { const accounts = await this.prisma.consumerAccount.findMany({ where: { user_id: consumerId }, 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.consumerAccount.findMany({ where: { id }, select: { id: true, role: true, created_at: true, account: { select: { username: true, status: true, }, }, }, }) 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, }, }, user: { connect: { id: consumerId, }, }, }, }) 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() } }