21 lines
642 B
TypeScript
21 lines
642 B
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { EntityManager, EntityRepository } from '@mikro-orm/postgresql';
|
|
import { CreditTransaction } from '../entities/credit-transaction.entity';
|
|
|
|
@Injectable()
|
|
export class CreditTransactionRepository extends EntityRepository<CreditTransaction> {
|
|
constructor(readonly em: EntityManager) {
|
|
super(em, CreditTransaction);
|
|
}
|
|
async getCurrentCredit(userId: string,): Promise<number> {
|
|
const lastRow = await this.em.findOne(
|
|
CreditTransaction,
|
|
{
|
|
user: { id: userId },
|
|
},
|
|
{ orderBy: { createdAt: 'desc' } },
|
|
);
|
|
return lastRow?.balance || 0;
|
|
}
|
|
}
|