Build wallet module for users

master
Mahdi 2 weeks ago
parent ead77d5ce5
commit 29f52575c0
  1. 2
      src/app.module.ts
  2. 1
      src/core/constants/index.ts
  3. 3
      src/core/database/database.providers.ts
  4. 13
      src/modules/users/entities/user.entity.ts
  5. 3
      src/modules/wallets/dto/update-wallet.dto.ts
  6. 26
      src/modules/wallets/entities/wallet.entity.ts
  7. 24
      src/modules/wallets/wallets.controller.ts
  8. 11
      src/modules/wallets/wallets.module.ts
  9. 9
      src/modules/wallets/wallets.providers.ts
  10. 45
      src/modules/wallets/wallets.service.ts

@ -8,6 +8,7 @@ import { UsersController } from './modules/users/users.controller';
import { ProductsModule } from './modules/products/products.module';
import { AuthModule } from './modules/auth/auth.module';
import { OrdersModule } from './modules/orders/orders.module';
import { WalletsModule } from './modules/wallets/wallets.module';
@Module({
imports: [
@ -17,6 +18,7 @@ import { OrdersModule } from './modules/orders/orders.module';
ProductsModule,
AuthModule,
OrdersModule,
WalletsModule,
],
controllers: [AppController, UsersController],
providers: [AppService],

@ -6,3 +6,4 @@ export const USER_REPOSITORY = 'USER_REPOSITORY';
export const PRODUCT_REPOSITORY = 'PRODUCT_REPOSITORY';
export const ORDER_REPOSITORY = 'ORDER_REPOSITORY';
export const ORDER_STATUSES_REPOSITORY = 'ORDER_STATUSES_REPOSITORY';
export const WALLET_REPOSITORY = 'WALLET_REPOSITORY';

@ -5,6 +5,7 @@ import { User } from 'src/modules/users/entities/user.entity';
import { Product } from 'src/modules/products/entities/product.entity';
import { OrderStatuses } from 'src/modules/orders/entities/order-status.entity';
import { Order } from 'src/modules/orders/entities/order.entity';
import { Wallet } from 'src/modules/wallets/entities/wallet.entity';
export const databaseProviders = [
{
@ -25,7 +26,7 @@ export const databaseProviders = [
config = databaseConfig.development;
}
const sequelize = new Sequelize(config);
sequelize.addModels([User, Product, Order, OrderStatuses]);
sequelize.addModels([User, Product, Order, OrderStatuses, Wallet]);
await sequelize.sync();
return sequelize;
},

@ -1,5 +1,13 @@
import { Table, Column, Model, DataType, HasMany } from 'sequelize-typescript';
import {
Table,
Column,
Model,
DataType,
HasMany,
HasOne,
} from 'sequelize-typescript';
import { Order } from '../../orders/entities/order.entity';
import { Wallet } from 'src/modules/wallets/entities/wallet.entity';
@Table({ tableName: 'users' })
export class User extends Model<User> {
@ -56,4 +64,7 @@ export class User extends Model<User> {
@HasMany(() => Order)
orders: Order[];
@HasOne(() => Wallet)
wallet: Wallet;
}

@ -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…
Cancel
Save