import { Injectable } from '@nestjs/common'; import { MiningAccountRepository } from '../../infrastructure/persistence/repositories/mining-account.repository'; import { PrismaService } from '../../infrastructure/persistence/prisma/prisma.service'; export interface MiningAccountDto { accountSequence: string; totalMined: string; availableBalance: string; frozenBalance: string; totalBalance: string; totalContribution: string; lastSyncedAt: Date | null; } export interface MiningRecordDto { id: string; miningMinute: Date; contributionRatio: string; totalContribution: string; secondDistribution: string; minedAmount: string; createdAt: Date; } export interface MiningTransactionDto { id: string; type: string; amount: string; balanceBefore: string; balanceAfter: string; referenceId: string | null; referenceType: string | null; description: string | null; createdAt: Date; } @Injectable() export class GetMiningAccountQuery { constructor( private readonly accountRepository: MiningAccountRepository, private readonly prisma: PrismaService, ) {} async execute(accountSequence: string): Promise { const account = await this.accountRepository.findByAccountSequence(accountSequence); if (!account) { return null; } return { accountSequence: account.accountSequence, totalMined: account.totalMined.toString(), availableBalance: account.availableBalance.toString(), frozenBalance: account.frozenBalance.toString(), totalBalance: account.totalBalance.toString(), totalContribution: account.totalContribution.toString(), lastSyncedAt: account.lastSyncedAt, }; } async getMiningRecords( accountSequence: string, page: number = 1, pageSize: number = 50, ): Promise<{ data: MiningRecordDto[]; total: number }> { const [records, total] = await Promise.all([ this.prisma.miningRecord.findMany({ where: { accountSequence }, orderBy: { miningMinute: 'desc' }, skip: (page - 1) * pageSize, take: pageSize, }), this.prisma.miningRecord.count({ where: { accountSequence } }), ]); return { data: records.map((r) => ({ id: r.id, miningMinute: r.miningMinute, contributionRatio: r.contributionRatio.toString(), totalContribution: r.totalContribution.toString(), secondDistribution: r.secondDistribution.toString(), minedAmount: r.minedAmount.toString(), createdAt: r.createdAt, })), total, }; } async getTransactions( accountSequence: string, page: number = 1, pageSize: number = 50, ): Promise<{ data: MiningTransactionDto[]; total: number }> { const [records, total] = await Promise.all([ this.prisma.miningTransaction.findMany({ where: { accountSequence }, orderBy: { createdAt: 'desc' }, skip: (page - 1) * pageSize, take: pageSize, }), this.prisma.miningTransaction.count({ where: { accountSequence } }), ]); return { data: records.map((r) => ({ id: r.id, type: r.type, amount: r.amount.toString(), balanceBefore: r.balanceBefore.toString(), balanceAfter: r.balanceAfter.toString(), referenceId: r.referenceId, referenceType: r.referenceType, description: r.description, createdAt: r.createdAt, })), total, }; } }