B-Dmitriy / cashflow

0 stars 0 forks source link

πŸ’» Transactions service #1

Closed B-Dmitriy closed 9 months ago

B-Dmitriy commented 9 months ago

Π Π°Π·Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ сСрвис Ρ‚Ρ€Π°Π½Π·Π°ΠΊΡ†ΠΈΠΈ

[!CAUTION] НС Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Π½Π°Ρ‡ΠΈΠ½Π°Ρ‚ΡŒ, ΠΏΠΎΠΊΠ° Π½Π΅ Π±ΡƒΠ΄Π΅Ρ‚ Π³ΠΎΡ‚ΠΎΠ²Π° Ρ‚Π°Π±Π»ΠΈΡ†Π° ΠΊΠΎΠ½Ρ‚Ρ€Π°Π³Π΅Π½Ρ‚ΠΎΠ²

На Π΄Π°Π½Π½Ρ‹ΠΉ ΠΌΠΎΠΌΠ΅Π½Ρ‚ Π½ΡƒΠΆΠ½Ρ‹ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠ΅ ΠΊΠΎΠ»ΠΎΠ½ΠΊΠΈ: 1) id 2) comment 3) amount - сумма, ΠΊΠΎΠ»-Π²ΠΎ Π΄Π΅Π½Π΅Π³ 4) currency - Π²Π°Π»ΡŽΡ‚Π° 5) in/out - входящий/исходящий (для Ρ„ΠΈΠ»ΡŒΡ€Π°Ρ†ΠΈΠΈ) 5) counterpartyId - ссылка Π½Π° ΠΊΠΎΠ½Ρ‚Ρ€Π°Π³Π΅Π½Ρ‚Π° 6) createdAt

НСобходимо Ρ€Π΅Π°Π»ΠΈΠ·ΠΎΠ²Π°Ρ‚ΡŒ Π² ΠΏΡ€ΠΎΡ‚ΠΎΡ‚ΠΈΠΏΠ΅ (v0)

B-Dmitriy commented 9 months ago
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { Injectable, NotFoundException } from '@nestjs/common';

import { TransactionDto } from './dto/transaction.dto';
import { Transaction } from './entities/transaction.entity';
import { CreateTransactionDto } from './dto/create-transaction.dto';
import { UpdateTransactionDto } from './dto/update-transaction.dto';
import { GetTransactionsQueryDto } from './dto/get-transaction-query.dto';
import { Counterparty } from '../counterparties/entities/counterparty.entity';

@Injectable()
export class TransactionsService {
  constructor(
    @InjectRepository(Counterparty)
    private counterpartyRepository: Repository<Counterparty>,
    @InjectRepository(Transaction)
    private transactionsRepository: Repository<Transaction>,
  ) {}

  async create(
    createTransactionDto: CreateTransactionDto,
  ): Promise<TransactionDto> {
    const counterpartyId = createTransactionDto.counterpartyId;

    const counterparty = await this.counterpartyRepository.findOneBy({
      id: counterpartyId,
    });

    if (!counterparty) {
      throw new NotFoundException({
        statusCode: 404,
        message: `counterparty with id ${counterpartyId} not found`,
      });
    }

    return await this.transactionsRepository.save(createTransactionDto);
  }

  // TODO: Common return type for tables get-counterparties-response.dto.ts
  async findAll(queryParams: GetTransactionsQueryDto) {
    const transactions = await this.transactionsRepository.query(`
        select t.id,t.amount,t.currency,
               c.name as counterparty,
               c.description as counterpartyDescription,
               t.createdAt,t.comment
        from "transaction" AS t
        LEFT JOIN counterparty AS c
        ON t.counterpartyId = c.id
        LIMIT ${queryParams.limit}
        OFFSET ${(+queryParams.page - 1) * +queryParams.limit};
    `);

    const total = await this.transactionsRepository.count();

    return {
      items: transactions,
      page: +queryParams.page,
      limit: +queryParams.limit,
      total,
    };
  }

  async findOne(id: number) {
    const transaction = await this.transactionsRepository.findOneBy({ id });

    if (!transaction) throw new NotFoundException();

    return transaction;
  }

  async update(id: number, updateTransactionDto: UpdateTransactionDto) {
    const counterpartyId = updateTransactionDto.counterpartyId;

    const counterparty = await this.counterpartyRepository.findOneBy({
      id: counterpartyId,
    });

    if (!counterparty) {
      throw new NotFoundException({
        statusCode: 404,
        message: `counterparty with id ${counterpartyId} not found`,
      });
    }

    const updateResult = await this.transactionsRepository.update(
      id,
      updateTransactionDto,
    );

    if (updateResult.affected === 0) throw new NotFoundException();

    return await this.transactionsRepository.findOneBy({ id });
  }

  async remove(id: number) {
    const deletedResult = await this.transactionsRepository.delete(id);

    if (deletedResult.affected === 0) throw new NotFoundException();

    return;
  }
}
B-Dmitriy commented 9 months ago

Closed in pr #6