nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.92k stars 7.55k forks source link

how to make found transactions in nestjs with typeorm #10311

Closed PatrickAngrezani closed 2 years ago

PatrickAngrezani commented 2 years ago

Is there an existing issue for this?

Current behavior

I'm making a project for a cryptobank, but I dont know how to pass this part. I already tried a lot of things and searched for help, but didnt find it. follow my userservice

 async cryptoTransfer(TransactionsDto: TransactionsDto, Id: string, @Request() req) {
    const queryRunner = this.DataSource.createQueryRunner();

    await queryRunner.connect();
    await queryRunner.startTransaction();

    try {
      let fromUser = await this.repository.findOne({where: {Id}})
      let toUser = await this.repository.findOne({where: {Id}})
      let quantityTransfered = req.body.quantityTransfered

      fromUser.CryptoWallet -= quantityTransfered;
      toUser.CryptoWallet += quantityTransfered;

      await queryRunner.manager.save(fromUser);
      await queryRunner.manager.save(toUser);

      await queryRunner.commitTransaction();

    } catch (error) {
      await queryRunner.rollbackTransaction();
      throw new InternalServerErrorException();
    } finally {
      await queryRunner.release();
    }
  }

here, the transactionDto

import { UserEntity } from 'src/user/entities/user.entity';
import {ApiProperty} from '@nestjs/swagger';
import {IsNotEmpty} from 'class-validator';

export class TransactionsDto {
  @ApiProperty()
  @IsNotEmpty()
  fromUser: UserEntity;

  @ApiProperty()
  @IsNotEmpty()
  toUser: UserEntity;

  @ApiProperty()
  @IsNotEmpty()
  quantityTransfered: number
}

Minimum reproduction code

https://github.com/PatrickAngrezani/lama-test

Steps to reproduce

No response

Expected behavior

i hope someone could help me to find a soluction

Package

Other package

typeorm

NestJS version

9.0.0

Packages versions

[System Information] OS Version : Windows 10 NodeJS Version : v18.9.0 NPM Version : 8.12.1

[Nest CLI] Nest CLI Version : 9.0.0

[Nest Platform Information] platform-express version : 9.0.11 mapped-types version : 1.1.0 schematics version : 9.0.1 passport version : 9.0.0 swagger version : 6.0.5 typeorm version : 9.0.1 testing version : 9.0.11 common version : 9.0.11 config version : 2.2.0 core version : 9.0.11 jwt version : 9.0.0 cli version : 9.0.0 PS C:\Users\clemi\Desktop\lama-test>

Node.js version

v18.9.0

In which operating systems have you tested?

Other

No response

micalevisk commented 2 years ago

Please use our Discord.

Also, you need to tell what's wrong with your code.