parent
04716f6973
commit
0359e22e40
8 changed files with 183 additions and 43 deletions
@ -0,0 +1,43 @@ |
|||||||
|
'use strict'; |
||||||
|
|
||||||
|
module.exports = { |
||||||
|
up: async (queryInterface, Sequelize) => { |
||||||
|
await queryInterface.dropTable("Transactions", { cascade: true }); |
||||||
|
await queryInterface.createTable('Transactions', { |
||||||
|
id: { |
||||||
|
type: Sequelize.INTEGER, |
||||||
|
autoIncrement: true, |
||||||
|
primaryKey: true, |
||||||
|
allowNull: false, |
||||||
|
}, |
||||||
|
walletId: { |
||||||
|
type: Sequelize.INTEGER, |
||||||
|
allowNull: false, |
||||||
|
references: { |
||||||
|
model: 'Wallets',
|
||||||
|
key: 'id', |
||||||
|
}, |
||||||
|
onDelete: 'CASCADE', |
||||||
|
}, |
||||||
|
amount: { |
||||||
|
type: Sequelize.STRING, |
||||||
|
allowNull: false, |
||||||
|
defaultValue: '0', |
||||||
|
}, |
||||||
|
createdAt: { |
||||||
|
type: Sequelize.DATE, |
||||||
|
allowNull: false, |
||||||
|
defaultValue: Sequelize.fn('NOW'), |
||||||
|
}, |
||||||
|
updatedAt: { |
||||||
|
type: Sequelize.DATE, |
||||||
|
allowNull: false, |
||||||
|
defaultValue: Sequelize.fn('NOW'), |
||||||
|
}, |
||||||
|
}); |
||||||
|
}, |
||||||
|
|
||||||
|
down: async (queryInterface, Sequelize) => { |
||||||
|
await queryInterface.dropTable('Transactions'); |
||||||
|
}, |
||||||
|
}; |
@ -0,0 +1,19 @@ |
|||||||
|
import { Model, Table, Column, ForeignKey, BelongsTo, DataType } from 'sequelize-typescript'; |
||||||
|
import { Wallet } from './wallet.entity'; |
||||||
|
|
||||||
|
@Table |
||||||
|
export class Transaction extends Model<Transaction> { |
||||||
|
@ForeignKey(() => Wallet) |
||||||
|
@Column |
||||||
|
walletId: number; |
||||||
|
|
||||||
|
@BelongsTo(() => Wallet, { onDelete: 'CASCADE' })
|
||||||
|
wallet: Wallet; |
||||||
|
|
||||||
|
@Column({ |
||||||
|
type: DataType.STRING, |
||||||
|
allowNull: false, |
||||||
|
defaultValue: "0",
|
||||||
|
}) |
||||||
|
amount: string; |
||||||
|
} |
@ -1,12 +1,37 @@ |
|||||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from "@nestjs/common"; |
import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Request, forwardRef, Inject } from "@nestjs/common"; |
||||||
import { WalletService } from "./wallet.service"; |
import { WalletService } from "./wallet.service"; |
||||||
|
import { JwtAuthGuard } from "src/guard/auth.guard"; |
||||||
|
import { PaymentService } from "src/payment/payment.service"; |
||||||
|
|
||||||
@Controller("wallet") |
@Controller("wallet") |
||||||
export class WalletController { |
export class WalletController { |
||||||
constructor(private readonly walletService: WalletService) {} |
constructor( |
||||||
@Get(":userId") |
private readonly walletService: WalletService, |
||||||
async getBalance(@Param("userId") userId: number) { |
@Inject(forwardRef(() => PaymentService)) |
||||||
|
private paymentService: PaymentService, |
||||||
|
) {} |
||||||
|
|
||||||
|
//getting wallet balance by user
|
||||||
|
@UseGuards(JwtAuthGuard) |
||||||
|
@Get() |
||||||
|
async getBalance(@Request() req) { |
||||||
|
const userId = req.user.id; |
||||||
return this.walletService.getBalance(userId); |
return this.walletService.getBalance(userId); |
||||||
} |
} |
||||||
|
|
||||||
|
@UseGuards(JwtAuthGuard) |
||||||
|
@Post("charge") |
||||||
|
async addBalance(@Body("amount") amount: number, @Request() req) { |
||||||
|
const userId = req.user.id; |
||||||
|
const callbackUrl = `http://localhost:3000/payment/verify?userId=${userId}&amount=${amount}`; |
||||||
|
const paymentUrl = this.paymentService.requestPayment(amount, "Wallet Charge", callbackUrl); |
||||||
|
return paymentUrl; |
||||||
|
} |
||||||
|
|
||||||
|
@UseGuards(JwtAuthGuard) |
||||||
|
@Get("transaction") |
||||||
|
async getTransactionById(@Request() req){ |
||||||
|
const userId = req.user.id |
||||||
|
return this.walletService.getTransactionById(userId) |
||||||
|
} |
||||||
} |
} |
||||||
|
@ -1,13 +1,24 @@ |
|||||||
import { Module } from '@nestjs/common'; |
import { Module } from "@nestjs/common"; |
||||||
import { WalletService } from './wallet.service'; |
import { WalletService } from "./wallet.service"; |
||||||
import { WalletController } from './wallet.controller'; |
import { WalletController } from "./wallet.controller"; |
||||||
import { Wallet } from './entities/wallet.entity'; |
import { Wallet } from "./entities/wallet.entity"; |
||||||
import { SequelizeModule } from '@nestjs/sequelize'; |
import { SequelizeModule } from "@nestjs/sequelize"; |
||||||
|
import { JwtModule } from "@nestjs/jwt"; |
||||||
|
import { RoleGuard } from "src/guard/role.guard"; |
||||||
|
import { JwtAuthGuard } from "src/guard/auth.guard"; |
||||||
|
import { PaymentService } from "src/payment/payment.service"; |
||||||
|
import { Transaction } from "./entities/transaction.entity"; |
||||||
|
|
||||||
@Module({ |
@Module({ |
||||||
imports: [SequelizeModule.forFeature([Wallet])], |
imports: [ |
||||||
|
SequelizeModule.forFeature([Wallet,Transaction]), |
||||||
|
JwtModule.register({ |
||||||
|
secret: process.env.JWT_SECRET, |
||||||
|
signOptions: { expiresIn: "1h" }, |
||||||
|
}), |
||||||
|
], |
||||||
controllers: [WalletController], |
controllers: [WalletController], |
||||||
providers: [WalletService], |
providers: [WalletService, JwtAuthGuard, RoleGuard,PaymentService], |
||||||
exports: [WalletService],
|
exports: [WalletService], |
||||||
}) |
}) |
||||||
export class WalletModule {} |
export class WalletModule {} |
||||||
|
Loading…
Reference in new issue