Skip to content

Commit

Permalink
Feat(Wallet): This allow the creation of wallet during account creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ezehlivinus committed Nov 20, 2022
1 parent 07f4f41 commit 2b3d7a4
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 22 deletions.
15 changes: 15 additions & 0 deletions migrations/20221119164750_wallets.ts
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');
}
17 changes: 17 additions & 0 deletions migrations/20221119164830_transactions.ts
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');
}
4 changes: 3 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AppService } from './app.service';
import { UsersModule } from './users/users.module';
import appConfig from './config/app.config';
import { AuthModule } from './auth/auth.module';
import { WalletsModule } from './wallets/wallets.module';
import jwtConfig from './config/jwt.config';
import databaseConfig from './config/database.config';

Expand All @@ -25,7 +26,8 @@ import databaseConfig from './config/database.config';
}),

UsersModule,
AuthModule
AuthModule,
WalletsModule
],
controllers: [AppController],
providers: []
Expand Down
37 changes: 20 additions & 17 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
import { Auth } from '../common/decorators/http.decorator';
import { ErrorResponseDTO } from '../common/dtos/response.dto';
import {
BadRequestException,
Body,
Controller,
HttpCode,
HttpStatus,
Post,
Put
} from '@nestjs/common';
import { Body, Controller, HttpCode, HttpStatus, Post } from '@nestjs/common';
import {
ApiBadRequestResponse,
ApiOkResponse,
ApiOperation,
ApiTags
} from '@nestjs/swagger';
import {
CreateUserDto,
CreateUserResponseDTO,
LoginDTO
// LoginResponseDTO
} from './auth.dto';
import { CreateUserDto, CreateUserResponseDTO, LoginDTO } from './auth.dto';
import { AuthService } from './auth.service';
import { WalletsService } from 'src/wallets/wallets.service';
import { CreateWalletDto } from 'src/wallets/wallet.dto';

@Controller('auth')
@ApiTags('auth')
export class AuthController {
constructor(private authService: AuthService) {}
constructor(
private authService: AuthService,
private walletService: WalletsService
) {}

@Post('/login')
@HttpCode(HttpStatus.OK)
Expand Down Expand Up @@ -55,6 +47,17 @@ export class AuthController {
type: ErrorResponseDTO
})
async auth(@Body() body: CreateUserDto) {
return await this.authService.auth(body);
const { data, access_token } = await this.authService.auth(body);
const createWalletDto: CreateWalletDto = {
owner: data.id
};

const { balance, address } = await this.walletService.create(
createWalletDto
);

return {
data: { ...data, wallet: { address, balance }, access_token }
};
}
}
3 changes: 3 additions & 0 deletions src/auth/auth.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class _CreateUserResponseDTO extends CreateUserDto {
@ApiProperty()
id: string;

@ApiProperty()
wallet: string;

@ApiProperty()
createdAt: Date;

Expand Down
2 changes: 2 additions & 0 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import { JwtModule } from '@nestjs/jwt';
import { SignOptions } from 'jsonwebtoken';
import { JwtStrategy } from './auth.strategy';
import { UsersService } from 'src/users/users.service';
import { WalletsModule } from 'src/wallets/wallets.module';

@Module({
imports: [
WalletsModule,
UsersModule,
JwtModule.registerAsync({
inject: [ConfigService],
Expand Down
2 changes: 0 additions & 2 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ export class UsersService {

const user = await this.findOne({ email: createUserDto.email });

console.log(user);

return { user };
}
}
44 changes: 44 additions & 0 deletions src/wallets/wallet.dto.ts
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;
}
8 changes: 8 additions & 0 deletions src/wallets/wallets.module.ts
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 {}
41 changes: 41 additions & 0 deletions src/wallets/wallets.service.ts
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];
}
}
7 changes: 5 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"target": "ES2021",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
Expand All @@ -16,6 +16,9 @@
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
"noFallthroughCasesInSwitch": false,
"lib": [
"ES2021.String"
]
}
}

0 comments on commit 2b3d7a4

Please sign in to comment.