Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
feat(money): add delete transaction to api
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Feb 25, 2019
1 parent 526619c commit 1838538
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 2 deletions.
4 changes: 4 additions & 0 deletions back/src/db/EntitySaver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ export class EntitySaver {
public async save<Entity>(...entities: Entity[]): Promise<void> {
await this.em.save(entities)
}

public async remove<Entity>(...entities: Entity[]): Promise<void> {
await this.em.remove(entities)
}
}
35 changes: 34 additions & 1 deletion back/src/money/application/Accountant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ import { Injectable } from '@nestjs/common'

import { IncomeModel } from '@shared/models/money/IncomeModel'
import { OutcomeModel } from '@shared/models/money/OutcomeModel'

import { EntitySaver } from '@back/db/EntitySaver'
import { UserRepository } from '@back/user/domain/UserRepository'
import { IdGenerator } from '@back/utils/infrastructure/IdGenerator/IdGenerator'
import { LogicException } from '@back/utils/infrastructure/exception/LogicException'
import { EntityNotFoundException } from '@back/utils/domain/EntityNotFoundException'

import { Income } from '../domain/Income.entity'
import { Outcome } from '../domain/Outcome.entity'
import { IncomeRepository } from '../domain/IncomeRepository'
import { OutcomeRepository } from '../domain/OutcomeRepository'

@Injectable()
export class Accountant {
public constructor(
private readonly userRepo: UserRepository,
private readonly idGenerator: IdGenerator,
private readonly entitySaver: EntitySaver,
private readonly incomeRepo: IncomeRepository,
private readonly outcomeRepo: OutcomeRepository,
) {}

public async income(
Expand Down Expand Up @@ -63,4 +68,32 @@ export class Accountant {

await this.entitySaver.save(outcome)
}

public async remove(transactionId: string, userLogin: string): Promise<void> {
const [income, outcome] = await Promise.all([
this.incomeRepo.findForUser(transactionId, userLogin),
this.outcomeRepo.findForUser(transactionId, userLogin),
])

if (income.nonEmpty() && outcome.nonEmpty()) {
throw new LogicException(
`Id collision (${transactionId}) between Income and Outcome`,
)
}

if (income.isEmpty() && outcome.isEmpty()) {
throw new EntityNotFoundException('Transaction', {
id: transactionId,
authorLogin: userLogin,
})
}

if (income.nonEmpty()) {
await this.entitySaver.remove(income.get())
}

if (outcome.nonEmpty()) {
await this.entitySaver.remove(outcome.get())
}
}
}
15 changes: 15 additions & 0 deletions back/src/money/domain/IncomeRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ class IncomeRepo {
private readonly incomeRepo: Repository<Income>,
) {}

public async findForUser(
id: string,
userLogin: string,
): Promise<Option<Income>> {
const income = await this.incomeRepo
.createQueryBuilder('income')
.innerJoin('income.author', 'author', 'author.login = :userLogin', {
userLogin,
})
.where('income.id = :id', { id })
.getOne()

return Option.of(income)
}

public async findEarliest(userLogin: string): Promise<Option<Income>> {
const income = await this.incomeRepo
.createQueryBuilder('income')
Expand Down
15 changes: 15 additions & 0 deletions back/src/money/domain/OutcomeRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ class OutomeRepo {
private readonly outcomeRepo: Repository<Outcome>,
) {}

public async findForUser(
id: string,
userLogin: string,
): Promise<Option<Outcome>> {
const outcome = await this.outcomeRepo
.createQueryBuilder('outcome')
.innerJoin('outcome.author', 'author', 'author.login = :userLogin', {
userLogin,
})
.where('outcome.id = :id', { id })
.getOne()

return Option.of(outcome)
}

public async findEarliest(userLogin: string): Promise<Option<Outcome>> {
const income = await this.outcomeRepo
.createQueryBuilder('outcome')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Body, Controller, Post } from '@nestjs/common'
import { Body, Controller, Post, Query, Delete, Param } from '@nestjs/common'
import {
ApiBearerAuth,
ApiCreatedResponse,
ApiOperation,
ApiUseTags,
ApiOkResponse,
} from '@nestjs/swagger'

import { Accountant } from '@back/money/application/Accountant'
Expand Down Expand Up @@ -52,4 +53,14 @@ export class TransactionController {

return request
}

@Delete('/:id')
@ApiOperation({ title: 'Delete transaction' })
@ApiOkResponse({ description: 'Transaction deleted' })
public async remove(
@Param('id') id: string,
@CurrentUser() { login }: TokenPayload,
) {
await this.accountant.remove(id, login)
}
}

0 comments on commit 1838538

Please sign in to comment.