-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(Wallet): This allow the creation of wallet during account creation
- Loading branch information
1 parent
07f4f41
commit 2b3d7a4
Showing
11 changed files
with
158 additions
and
22 deletions.
There are no files selected for viewing
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,15 @@ | ||
import { Knex } from 'knex'; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
return knex.schema.createTable('wallets', (table) => { | ||
table.increments('id'); | ||
table.string('address', 255).unique().notNullable(); | ||
table.double('balance').notNullable().defaultTo(0.0).unsigned(); | ||
table.timestamps(true, true, true); | ||
table.integer('owner').index().references('id').inTable('users').unsigned(); | ||
}); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
return knex.schema.dropTable('wallets'); | ||
} |
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,17 @@ | ||
import { Knex } from 'knex'; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
return knex.schema.createTable('transactions', (table) => { | ||
table.increments('id'); | ||
table.enum('type', ['credit', 'debit']); | ||
table.string('purpose', 255).nullable(); | ||
table.double('amount').notNullable().unsigned(); | ||
table.timestamps(true, true, true); | ||
table.string('from').references('address').inTable('wallets'); | ||
table.string('to').references('address').inTable('wallets'); | ||
}); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
return knex.schema.dropTable('transactions'); | ||
} |
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
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,44 @@ | ||
import { | ||
IsNotEmpty, | ||
IsNumber, | ||
IsOptional, | ||
IsPositive, | ||
IsString | ||
} from 'class-validator'; | ||
|
||
export class CreateWalletDto { | ||
@IsString() | ||
@IsNotEmpty() | ||
address?: string; | ||
|
||
@IsNumber() | ||
@IsPositive() | ||
@IsNotEmpty() | ||
balance? = 0; | ||
|
||
@IsNumber() | ||
@IsPositive() | ||
@IsNotEmpty() | ||
owner: number; | ||
} | ||
|
||
export class UpdateWalletDto { | ||
@IsNumber() | ||
@IsPositive() | ||
@IsOptional() | ||
balance?: number; | ||
} | ||
|
||
export class FindWalletDto { | ||
@IsString() | ||
@IsOptional() | ||
address?: string; | ||
|
||
@IsNumber() | ||
@IsOptional() | ||
id?: string; | ||
|
||
@IsNumber() | ||
@IsOptional() | ||
owner?: number; | ||
} |
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,8 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { WalletsService } from './wallets.service'; | ||
|
||
@Module({ | ||
providers: [WalletsService], | ||
exports: [WalletsService] | ||
}) | ||
export class WalletsModule {} |
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,41 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Knex } from 'knex'; | ||
import { InjectConnection } from 'nest-knexjs'; | ||
import { CreateWalletDto, FindWalletDto, UpdateWalletDto } from './wallet.dto'; | ||
import { randomUUID } from 'crypto'; | ||
|
||
@Injectable() | ||
export class WalletsService { | ||
constructor(@InjectConnection() private readonly knex: Knex) {} | ||
|
||
async create(createWalletDto: CreateWalletDto) { | ||
const newAddress = randomUUID().replaceAll('-', ''); | ||
createWalletDto.address = newAddress; | ||
|
||
const result = await this.knex.table('wallets').insert(createWalletDto); | ||
|
||
const wallet = await this.findOne({ address: newAddress }); | ||
|
||
return wallet; | ||
} | ||
|
||
async update(filter: FindWalletDto, update: UpdateWalletDto) { | ||
const count = await this.knex('wallets') | ||
.where({ FindWalletDto }) | ||
.update(update); | ||
|
||
const updates = await this.findOne(filter); | ||
|
||
return updates[0]; | ||
} | ||
|
||
async findOne(filter: FindWalletDto) { | ||
const wallet = await this.knex.table('wallets').where(filter); | ||
|
||
if (!wallet.length) { | ||
return undefined; | ||
} | ||
|
||
return wallet[0]; | ||
} | ||
} |
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