Skip to content

mthuong/crypto-calculator-backend

Repository files navigation

Crypto Savings Calculator

Project Description

Calculate your savings investment

Repositories

🖥 Backend

🦠 Front end

Installation

$ npm install
$ cp .env.dev .env

MYSQL - my.cnf

default-time-zone = "+00:00"

Migration & Seeding

# default db engine is mysql
$ npm run migration:run

# db seeding with small database
$ npm run migration:seed

# update entities and migrate db
$ npm run database:initialize

Running Migration & Seeding (After release)

# clear all table first
$ npm run migration:down

# migrate db
$ npm run migration:run

Running middleware

Run software which the app depends on for development

  • mailhog (Web and API based SMTP testing)
  • mysql
docker network create red-network
docker-compose up

Running the app

$ cp .env.dev .env

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Running the app as scheduler

$ cp .env.dev .env

# set APPLICATION_MODE 'scheduler'
$ vi .env

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Test

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

Rules

Create a new module

When you want to create a new module, the following command is preferred to run.

# create a new module
$ npm run generate:module <your_module_name>
CREATE /src/modules/<your_module_name>/<your_module_name>.module.ts (xx bytes)
UPDATE /src/modules/app.module.ts (xx bytes)

# create a controller in created module
$ npm run generate:controller <your_module_name>
CREATE /src/modules/<your_module_name>/<your_module_name>.controller.spec.ts (xx bytes)
CREATE /src/modules/<your_module_name>/<your_module_name>.controller.ts (xx bytes)
UPDATE /src/modules/<your_module_name>/<your_module_name>.module.ts (xx bytes)

# create a service in created module, if needed
$ npm run generate:service <your_module_name>
CREATE /src/modules/<your_module_name>/<your_module_name>.service.spec.ts (xx bytes)
CREATE /src/modules/<your_module_name>/<your_module_name>.service.ts (xx bytes)
UPDATE /src/modules/<your_module_name>/<your_module_name>.module.ts (xx bytes)

Unit test & Coverage

You have to write unit test codes in order to maintain code coverage and reduce runtime errors. Unit test code is required when you create a class such as service, controller or etc.

import { Test, TestingModule } from '@nestjs/testing';
import { EmailVerificationService } from './email-verification.service';
import { EmailService } from '@modules/email';
import { EmailVerification } from './email-verification.entity';
import { getRepositoryToken } from '@nestjs/typeorm';
import { DatabaseModule } from '@test/database';
import { DatabaseService } from '@test/database/database.service';
import { TestUtils } from '@test/test.utils';
import { Repository } from 'typeorm';
import {
  EmailVerificationTokenExpiredException,
  DataNotFoundException,
} from '@modules/common/exceptions';

describe('EmailVerificationService', () => {
  let service: EmailVerificationService;
  let repository: Repository<unknown>;
  let testUtils: TestUtils;

  beforeEach(async () => {
    const testModule = await Test.createTestingModule({
      imports: [DatabaseModule],
      providers: [DatabaseService, TestUtils],
    }).compile();
    testUtils = testModule.get<TestUtils>(TestUtils);
    const emailVerificaitonRepository = await testUtils.getRepository(
      EmailVerification,
    );
    await testUtils.reloadFixtures();

    const module: TestingModule = await Test.createTestingModule({
      providers: [
        EmailVerificationService,
        {
          provide: EmailService,
          useValue: {}, // TODO: Mock
        },
        {
          provide: getRepositoryToken(EmailVerification),
          useValue: await testUtils.getRepository(EmailVerification),
        },
      ],
    }).compile();

    service = module.get<EmailVerificationService>(EmailVerificationService);
    repository = emailVerificaitonRepository;
  });

  afterEach(async () => {
    await testUtils.closeDbConnection();
  });

  it('should be defined', () => {
    expect(service).toBeDefined();
  });

  describe('verifyEmailToken', () => {
    it('token verification sould be success', async () => {
      const token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
      await expect(service.verifyEmailToken(token)).resolves.toBeDefined();
    });

    it('token verification sould be failed', async () => {
      const token = '1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1';
      await expect(service.verifyEmailToken(token)).rejects.toBeInstanceOf(
        DataNotFoundException,
      );
    });

    it('token verification sould be expired', async () => {
      const token = new EmailVerification({
        userId: 1,
        email: '[email protected]',
      });
      token.expiresAt = new Date();
      await repository.save(token);
      await expect(
        service.verifyEmailToken(token.verificationToken),
      ).rejects.toBeInstanceOf(EmailVerificationTokenExpiredException);
    });
  });
});

How to deploy into ECS

act -s AWS_ACCESS_KEY_ID=<AWS_ACCESS_KEY_ID> -s AWS_SECRET_ACCESS_KEY=<AWS_SECRET_ACCESS_KEY> -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -j deploy

Style Guide

TypeScript Deep Dive StyleGuide

REST API Design Principal

paypal/api-standards

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published