parent
ead77d5ce5
commit
29f52575c0
10 changed files with 135 additions and 2 deletions
@ -0,0 +1,3 @@ |
||||
export class UpdateWalletDto { |
||||
amount: number; |
||||
} |
@ -0,0 +1,26 @@ |
||||
import { |
||||
Table, |
||||
Column, |
||||
Model, |
||||
DataType, |
||||
ForeignKey, |
||||
BelongsTo, |
||||
} from 'sequelize-typescript'; |
||||
import { User } from 'src/modules/users/entities/user.entity'; |
||||
|
||||
@Table({ tableName: 'wallets', createdAt: false }) |
||||
export class Wallet extends Model<Wallet> { |
||||
@Column({ |
||||
type: DataType.FLOAT, |
||||
allowNull: false, |
||||
defaultValue: 0, |
||||
}) |
||||
balance: number; |
||||
|
||||
@ForeignKey(() => User) |
||||
@Column |
||||
userId: number; |
||||
|
||||
@BelongsTo(() => User) |
||||
user: User; |
||||
} |
@ -0,0 +1,24 @@ |
||||
import { Body, Controller, Get, Param, Patch } from '@nestjs/common'; |
||||
import { WalletsService } from './wallets.service'; |
||||
import { UUID } from 'crypto'; |
||||
import { UpdateWalletDto } from './dto/update-wallet.dto'; |
||||
|
||||
@Controller('wallets') |
||||
export class WalletsController { |
||||
constructor(private readonly walletsService: WalletsService) {} |
||||
|
||||
@Get('balance/:id') |
||||
getWallet(@Param('id') id: UUID) { |
||||
return this.walletsService.getWallet(id); |
||||
} |
||||
|
||||
@Patch('deposit/:id') |
||||
deposit(@Param('id') id: UUID, @Body() updateWalletDto: UpdateWalletDto) { |
||||
return this.walletsService.deposit(id, updateWalletDto); |
||||
} |
||||
|
||||
@Patch('withdraw/:id') |
||||
withdraw(@Param('id') id: UUID, @Body() updateWalletDto: UpdateWalletDto) { |
||||
return this.walletsService.withdraw(id, updateWalletDto); |
||||
} |
||||
} |
@ -0,0 +1,11 @@ |
||||
import { Module } from '@nestjs/common'; |
||||
import { WalletsService } from './wallets.service'; |
||||
import { WalletsController } from './wallets.controller'; |
||||
import { walletsProviders } from './wallets.providers'; |
||||
|
||||
@Module({ |
||||
controllers: [WalletsController], |
||||
providers: [WalletsService, ...walletsProviders], |
||||
exports: [WalletsService], |
||||
}) |
||||
export class WalletsModule {} |
@ -0,0 +1,9 @@ |
||||
import { Wallet } from './entities/wallet.entity'; |
||||
import { WALLET_REPOSITORY } from '../../core/constants'; |
||||
|
||||
export const walletsProviders = [ |
||||
{ |
||||
provide: WALLET_REPOSITORY, |
||||
useValue: Wallet, |
||||
}, |
||||
]; |
@ -0,0 +1,45 @@ |
||||
import { BadRequestException, Inject, Injectable } from '@nestjs/common'; |
||||
import { UUID } from 'crypto'; |
||||
import { WALLET_REPOSITORY } from 'src/core/constants'; |
||||
import { Wallet } from './entities/wallet.entity'; |
||||
import { User } from '../users/entities/user.entity'; |
||||
import { UpdateWalletDto } from './dto/update-wallet.dto'; |
||||
|
||||
@Injectable() |
||||
export class WalletsService { |
||||
constructor( |
||||
@Inject(WALLET_REPOSITORY) private readonly walletRepository: typeof Wallet, |
||||
) {} |
||||
|
||||
async getWallet(id: UUID) { |
||||
return await this.walletRepository.findAll({ |
||||
include: { |
||||
model: User, |
||||
where: { uuid: id }, |
||||
attributes: ['firstName'], |
||||
}, |
||||
}); |
||||
} |
||||
|
||||
async deposit(id: UUID, updateWalletDto: UpdateWalletDto) { |
||||
const { amount } = updateWalletDto; |
||||
|
||||
if (amount < 0) throw new BadRequestException(); |
||||
|
||||
const [wallet] = await this.getWallet(id); |
||||
|
||||
wallet.balance += amount; |
||||
return wallet.save(); |
||||
} |
||||
|
||||
async withdraw(id: UUID, updateWalletDto: UpdateWalletDto) { |
||||
const { amount } = updateWalletDto; |
||||
|
||||
if (amount < 0) throw new BadRequestException(); |
||||
|
||||
const [wallet] = await this.getWallet(id); |
||||
|
||||
wallet.balance -= amount; |
||||
return wallet.save(); |
||||
} |
||||
} |
Loading…
Reference in new issue