-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
2,682 additions
and
13,896 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { createParamDecorator, ExecutionContext } from '@nestjs/common'; | ||
import { User } from 'src/users/entities/user.entity'; | ||
|
||
export const GetUser = createParamDecorator((_data, ctx: ExecutionContext): User => { | ||
const req = ctx.switchToHttp().getRequest(); | ||
return req.user; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { IsNotEmpty, IsEnum, IsOptional } from 'class-validator'; | ||
import { TransactionStatus } from '../transaction-status.enum'; | ||
|
||
import type { User } from 'src/users/entities/user.entity'; | ||
import type { Organization } from 'src/organizations/entities/organization.entity'; | ||
import type { Asset } from 'src/assets/entities/asset.entity'; | ||
|
||
export class CreateTransactionDto { | ||
@IsNotEmpty() | ||
donater_user: User; | ||
|
||
@IsOptional() | ||
donater_organization: Organization; | ||
|
||
@IsOptional() | ||
recipient: Organization; | ||
|
||
// custom message for example, not necessary to code | ||
@IsNotEmpty({ message: 'asset_id is required' }) | ||
asset: Asset; | ||
|
||
@IsOptional() | ||
@IsEnum(TransactionStatus) | ||
status: TransactionStatus; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { PartialType } from '@nestjs/mapped-types'; | ||
|
||
import { CreateTransactionDto } from './create-transaction.dto'; | ||
|
||
export class GetTransactionsDto extends PartialType(CreateTransactionDto) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { PickType } from '@nestjs/mapped-types'; | ||
|
||
import { CreateTransactionDto } from './create-transaction.dto'; | ||
|
||
export class UpdateTransactionDto extends PickType(CreateTransactionDto, ['status'] as const) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { | ||
Column, | ||
CreateDateColumn, | ||
Entity, | ||
JoinColumn, | ||
ManyToOne, | ||
PrimaryGeneratedColumn, | ||
} from 'typeorm'; | ||
import { TransactionStatus } from '../transaction-status.enum'; | ||
import { User } from '../../users/entities/user.entity'; | ||
import { Asset } from '../../assets/entities/asset.entity'; | ||
import { Organization } from '../../organizations/entities/organization.entity'; | ||
|
||
@Entity('transactions') | ||
export class Transaction { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column({ | ||
type: 'enum', | ||
enum: TransactionStatus, | ||
default: TransactionStatus.IN_PROGRESS, | ||
}) | ||
status: TransactionStatus; | ||
|
||
@CreateDateColumn() | ||
created_date: Date; | ||
|
||
@ManyToOne((_type) => User, (user) => user.transactions) | ||
@JoinColumn() | ||
donater_user: User; | ||
|
||
@ManyToOne((_type) => Organization, (organization) => organization.transactions) | ||
@JoinColumn() | ||
donater_organization?: Organization; | ||
|
||
@ManyToOne((_type) => Asset, (asset) => asset.transactions, { eager: true }) | ||
@JoinColumn() | ||
asset: Asset; | ||
|
||
@ManyToOne((_type) => Organization, (recipient) => recipient.transactions) | ||
@JoinColumn() | ||
recipient: Organization; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export enum TransactionStatus { | ||
IN_PROGRESS = 'IN_PROGRESS', | ||
COMPLETED = 'COMPLETED', | ||
CANCELLED = 'CANCELLED', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Controller, Post, Body, Get, Query, Param, Delete, Patch } from '@nestjs/common'; | ||
|
||
import { TransactionsService } from './transactions.service'; | ||
import { CreateTransactionDto } from './dto/create-transaction.dto'; | ||
import { Transaction } from './entities/transaction.entity'; | ||
import { GetTransactionsDto } from './dto/get-transactions-filter.dto'; | ||
import { UpdateTransactionDto } from './dto/update-transaction.dto'; | ||
|
||
@Controller('transactions') | ||
export class TransactionsController { | ||
constructor(private transactionsService: TransactionsService) {} | ||
|
||
@Post() | ||
create(@Body() createTransactionDto: CreateTransactionDto): Promise<Transaction> { | ||
return this.transactionsService.createTransaction(createTransactionDto); | ||
} | ||
|
||
@Get() | ||
get(@Query() getTransactionsDto: GetTransactionsDto): Promise<Transaction[]> { | ||
return this.transactionsService.getTransactions(getTransactionsDto); | ||
} | ||
|
||
@Get('/:id') | ||
getTransactionById(@Param('id') id: number): Promise<Transaction> { | ||
return this.transactionsService.getTransactionById(id); | ||
} | ||
|
||
@Patch('/:id') | ||
update( | ||
@Param('id') id: number, | ||
@Body() updateTransactionStatusDto: UpdateTransactionDto, | ||
): Promise<Transaction> { | ||
return this.transactionsService.updateTransaction(id, updateTransactionStatusDto); | ||
} | ||
|
||
@Delete('/:id') | ||
delete(@Param('id') id: number): Promise<void> { | ||
return this.transactionsService.deleteTransaction(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TransactionsController } from './transactions.controller'; | ||
import { TransactionsService } from './transactions.service'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { Transaction } from './entities/transaction.entity'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([Transaction])], | ||
controllers: [TransactionsController], | ||
providers: [TransactionsService], | ||
exports: [TransactionsService], | ||
}) | ||
export class TransactionsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Injectable, NotFoundException } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
|
||
import { CreateTransactionDto } from './dto/create-transaction.dto'; | ||
import { Transaction } from './entities/transaction.entity'; | ||
import { GetTransactionsDto } from './dto/get-transactions-filter.dto'; | ||
import { UpdateTransactionDto } from './dto/update-transaction.dto'; | ||
|
||
@Injectable() | ||
export class TransactionsService { | ||
constructor( | ||
@InjectRepository(Transaction) | ||
private transactionsRepository: Repository<Transaction>, | ||
) {} | ||
|
||
async createTransaction(createTransactionDto: CreateTransactionDto): Promise<Transaction> { | ||
return this.transactionsRepository.save(createTransactionDto); | ||
} | ||
|
||
async getTransactions(getTransactionsDto: GetTransactionsDto): Promise<Transaction[]> { | ||
return this.transactionsRepository.find({ where: getTransactionsDto }); | ||
} | ||
|
||
async getTransactionById(id: number): Promise<Transaction> { | ||
const found = await this.transactionsRepository.findOne(id); | ||
if (!found) { | ||
throw new NotFoundException(); | ||
} | ||
return found; | ||
} | ||
|
||
async updateTransaction( | ||
id: number, | ||
updateTransactionDto: UpdateTransactionDto, | ||
): Promise<Transaction> { | ||
const transaction = await this.getTransactionById(id); | ||
const newTransaction = await this.transactionsRepository.save({ | ||
...transaction, | ||
...updateTransactionDto, | ||
}); | ||
return newTransaction; | ||
} | ||
|
||
async deleteTransaction(id: number): Promise<void> { | ||
const transactionToDelete = await this.transactionsRepository.delete(id); | ||
if (transactionToDelete.affected === 0) { | ||
throw new NotFoundException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters