Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General Improvements to Codebase #11

Merged
merged 15 commits into from
Jul 18, 2023
23 changes: 23 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
PORT=4000 #development port
NODE_ENV=development
PAYSTACK_SECRET_KEY=XXXX
PAYSTACK_API_BASE_URL=https://api.paystack.co
POSTGRES_HOST=localhost #development
POSTGRES_PORT=5432 #development
POSTGRES_USER= #development
POSTGRES_PASSWORD= #development
POSTGRES_DB=account #development
DB_TYPE=postgres
JWT_SECRET=XXXX
JWT_EXPIRY=XXXX
MININUM_APPROVAL_AMOUNT=1000000


TEST_PAYSTACK_SECRET_KEY=XXXX
TEST_PAYSTACK_API_BASE_URL=https://api.paystack.co
TEST_POSTGRES_HOST=localhost #test
TEST_POSTGRES_PORT=5432 #test
TEST_POSTGRES_USER= #test
TEST_POSTGRES_PASSWORD=
TEST_POSTGRES_DB=account_test #test
DB_TYPE=postgres
11 changes: 0 additions & 11 deletions .env.test

This file was deleted.

5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ docker build -t ${IMAGETAG} -f Dockerfile .
#### Test

```bash
$ yarn run test
$ yarn test:e2e
```

#### Available Endpoints
Expand Down Expand Up @@ -143,7 +143,8 @@ docker build -t ${IMAGETAG} -f Dockerfile .

#### Postman Documentation

- Please see `/postman_docs` on the root directory.
- Please see `/postman_docs` on the root directory OR
- Navigate to `http://localhost:4000/docs` on your computer to view the openapi documentation.

### Improvement Points

Expand Down
18 changes: 10 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
"scripts": {
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start": "cross-env NODE_ENV=development nest start",
"start:dev": "cross-env NODE_ENV=development nest start --watch",
"start:debug": "cross-env NODE_ENV=development nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch --maxWorkers=1",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
"test": "cross-env NODE_ENV=test jest",
"test:watch": "cross-env NODE_ENV=test jest --watch --maxWorkers=1",
"test:cov": "cross-env NODE_ENV=test jest --coverage",
"test:debug": "cross-env NODE_ENV=test node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "cross-env NODE_ENV=test jest --config ./test/jest-e2e.json --detectOpenHandles --maxWorkers=1"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
Expand All @@ -27,6 +27,7 @@
"@nestjs/mapped-types": "^2.0.2",
"@nestjs/passport": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^7.1.1",
"@nestjs/typeorm": "^10.0.0",
"@types/compression": "^1.7.2",
"@types/express": "^4.17.17",
Expand All @@ -36,6 +37,7 @@
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"compression": "^1.7.4",
"cross-env": "^7.0.3",
"dotenv": "^16.3.1",
"helmet": "^7.0.0",
"nest-winston": "^1.9.3",
Expand Down
31 changes: 10 additions & 21 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,41 @@ import 'dotenv/config';
import { WinstonModule } from 'nest-winston';
import { APP_FILTER } from '@nestjs/core';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { DatabaseModule } from './database/database.module';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UsersModule } from './users/users.module';
import { WalletsModule } from './wallets/wallets.module';
import { WalletTransactionsModule } from './wallet-transactions/wallet-transactions.module';
import { TransfersModule } from './transfers/transfers.module';
import { User } from './users/user.entity';
import { Wallet } from './wallets/wallet.entity';
import { WalletTransaction } from './wallet-transactions/wallet-transaction.entity';
import { Transfer } from './transfers/transfer.entity';
import { AuthModule } from './auth/auth.module';
import { HashModule } from './hash/hash.module';
import { PaystackModule } from './utilities/paystack.module';
import { HelpersModule } from './utilities/helpers.module';
import { HttpExceptionFilter } from './exceptions/http-exception.filter';
import { JwtStrategy } from './auth/strategies/jwt.strategy';
import winstonLogger from './utilities/logger';
@Module({
imports: [
WinstonModule.forRoot({
...winstonLogger,
}),

ConfigModule.forRoot({
isGlobal: true,
envFilePath: `.env.${process.env.NODE_ENV}`,
}),
UsersModule,
TypeOrmModule.forRoot({
type: 'postgres',
host: process.env.POSTGRES_HOST,
port: +process.env.POSTGRES_PORT,
username: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DB,
entities: [User, Wallet, WalletTransaction, Transfer],
synchronize: process.env.NODE_ENV !== 'production',
}),
UsersModule,
DatabaseModule,
WalletsModule,
WalletTransactionsModule,
TransfersModule,
AuthModule,
HashModule,
PaystackModule,
HelpersModule,
ConfigModule.forRoot({
isGlobal: true,
}),
],
controllers: [AppController],
providers: [
JwtStrategy,
AppService,
{
provide: APP_FILTER,
Expand All @@ -70,5 +56,8 @@ export class AppModule implements NestModule {
consumer
.apply()
.forRoutes({ path: 'transfers*', method: RequestMethod.ALL });
consumer
.apply()
.forRoutes({ path: 'wallet-transactions*', method: RequestMethod.ALL });
}
}
24 changes: 23 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
import { INestApplication, RequestMethod } from '@nestjs/common';
import {
INestApplication,
RequestMethod,
ValidationPipe,
} from '@nestjs/common';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';

import helmet from 'helmet';
import * as compression from 'compression';

export const appMiddleware = (app: INestApplication) => {
app.useGlobalPipes(
// remove any additional properites not defined in the DTO
new ValidationPipe({
whitelist: true,
}),
);

app.setGlobalPrefix('api/v1', {
exclude: [{ path: '/', method: RequestMethod.GET }],
});

app.enableCors();
app.use(helmet());
app.use(compression());

const options = new DocumentBuilder()
.setTitle('Wallet System')
.setDescription('API for a minimalistic wallet system')
.setVersion('1.0')
.addBearerAuth()
.build();

const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('/docs', app, document);
};
18 changes: 0 additions & 18 deletions src/auth/auth.controller.spec.ts

This file was deleted.

24 changes: 17 additions & 7 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Controller, Post, Body, UseGuards, Request } from '@nestjs/common';
import { Controller, Post, Body, HttpStatus, HttpCode } from '@nestjs/common';
import { CreateUserDTO } from '../users/dtos/user.dto';
import { LoginUserDTO } from './dtos/auth.dto';
import { AuthService } from '../auth/auth.service';
import { LocalAuthGuard } from './guards/local-auth.guard';
import { Helpers } from '../utilities/helpers';
import { SuccessResponse } from 'src/interface/types';

Expand All @@ -13,14 +13,24 @@ export class AuthController {
const user = await this.authService.signup(body);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { password, ...details } = user;
const { password: undefined, ...details } = user;

return this.helpers.successResponse(201, { ...details }, 'User created');
return this.helpers.successResponse(
HttpStatus.CREATED,
{ ...details },
'User created',
);
}

@UseGuards(LocalAuthGuard)
@Post('/login')
async login(@Request() req) {
return this.authService.login(req.user);
@HttpCode(HttpStatus.OK)
async login(@Body() authLogin: LoginUserDTO) {
const loginDetail = await this.authService.login(authLogin);

return this.helpers.successResponse(
HttpStatus.OK,
loginDetail,
'Login successful',
);
}
}
4 changes: 1 addition & 3 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { AuthController } from './auth.controller';
import { UsersModule } from '../users/users.module';
import { HashModule } from '../hash/hash.module';
import { PassportModule } from '@nestjs/passport';
import { LocalStrategy } from './strategies/local.strategy';
import { JwtStrategy } from './strategies/jwt.strategy';
import { JwtModule } from '@nestjs/jwt';
import { HelpersModule } from '../utilities/helpers.module';

Expand All @@ -21,7 +19,7 @@ import { HelpersModule } from '../utilities/helpers.module';
signOptions: { expiresIn: process.env.JWT_EXPIRY },
}),
],
providers: [AuthService, LocalStrategy, JwtStrategy],
providers: [AuthService],
controllers: [AuthController],
exports: [AuthService],
})
Expand Down
100 changes: 0 additions & 100 deletions src/auth/auth.service.spec.ts

This file was deleted.

Loading