import { Inject , Injectable } from '@nestjs/common' ; import { CommandBus } from '@nestjs/cqrs' ; import { Model } from 'mongoose' ; import { DEBT_TRANSACTION_MODEL , DebtTransactionView , } from '../read-model/schema/debt-transaction.schema' ; import { DEPOSIT_TRANSACTION_MODEL , DepositTransactionView , } from '../read-model/schema/deposit-transaction.schema' ; import { CreateDebtTransactionCommand } from '../../application/command/create-debt-transaction.command' ; import { CreateDepositTransactionCommand } from '../../application/command/create-deposit-transaction.command' ; import { CreateTransferTransactionCommand } from '../../application/command/create-transfer-transaction.command' ; import { TRANSFER_TRANSACTION_MODEL , TransferTransactionView , } from '../read-model/schema/transfer-transaction.schema' ; @ Injectable ( ) export class TransactionService { constructor ( private readonly commandBus , @ Inject ( DEBT_TRANSACTION_MODEL ) private readonly debtTransactionModel , @ Inject ( DEPOSIT_TRANSACTION_MODEL ) private readonly depositTransactionModel , @ Inject ( TRANSFER_TRANSACTION_MODEL ) private readonly transferTransactionModel ) { } async createDebtTransaction ( transactionId , memberId , billId , money , currencyCode ) { return this . commandBus . execute ( new CreateDebtTransactionCommand ( transactionId , memberId , billId , money , currencyCode ) ) ; } async createDepositTransaction ( transactionId , memberId , billId , money , currencyCode ) { return this . commandBus . execute ( new CreateDepositTransactionCommand ( transactionId , memberId , billId , money , currencyCode ) ) ; } async createTransferTransaction ( transactionId , senderId , beneficiaryId , money , currencyCode , groupId ) { return this . commandBus . execute ( new CreateTransferTransactionCommand ( transactionId , senderId , beneficiaryId , money , currencyCode , groupId ) ) ; } async getDebtTransactions ( memberId ) { return this . debtTransactionModel . find ( { memberId : '' + memberId + '' } ) . exec ( ) ; } async getDepositTransactions ( memberId ) { return this . depositTransactionModel . find ( { memberId : '' + memberId + '' } ) . exec ( ) ; } async getTransferTransactionsBySender ( senderId ) { return this . transferTransactionModel . find ( { idSender : '' + senderId + '' } ) . exec ( ) ; } async getTransferTransactionsByBeneficiary ( beneficiaryId ) { return this . transferTransactionModel . find ( { idBeneficiary : '' + beneficiaryId + '' } ) . exec ( ) ; } async getDebtTransactionIdsByBillId ( billId ) { return this . debtTransactionModel . distinct ( '_id' , { idBill : billId } ) . exec ( ) ; } async getDepositTransactionIdsByBillId ( billId ) { return this . depositTransactionModel . distinct ( '_id' , { idBill : billId } ) . exec ( ) ; } async getTransferTransactionIdsByGroupId ( groupId ) { return this . transferTransactionModel . distinct ( '_id' , { idGroup : groupId } ) . exec ( ) ; } async getDebtTransactionByBillIdAndMemberId ( billId , memberId ) { return this . debtTransactionModel . findOne ( { idBill : billId , idMember : memberId } , { _id : 1 } ) . exec ( ) ; } async getDepositTransactionByBillIdAndMemberId ( billId , memberId ) { return this . depositTransactionModel . findOne ( { idBill : billId , idMember : memberId } , { _id : 1 } ) . exec ( ) ; } }