# greendirect-api

> greendirect-api

Latest version **1.10.76** (published 2022-05-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install greendirect-api
pnpm add greendirect-api
yarn add greendirect-api
bun add greendirect-api
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.10.76 |
| Published | 2022-05-25 |
| First published | 2022-05-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 50 |
| Unpacked size | 11.5 KB |
| Known vulnerabilities | 0 (+29 in 14 direct dependencies) |
| Install scripts | no |
| Author | isbx |
| Maintainers | rajakkmesh |

## Links

- npm: https://www.npmjs.com/package/greendirect-api
- npm.io page: https://npm.io/package/greendirect-api

## Dependencies (50)

- [pg](https://npm.io/package/pg.md) ^7.6.0
- [csv](https://npm.io/package/csv.md) ^5.1.1
- [pm2](https://npm.io/package/pm2.md) ^4.4.0
- [knox](https://npm.io/package/knox.md) ^0.9.2
- [rxjs](https://npm.io/package/rxjs.md) ^6.3.3
- [uuid](https://npm.io/package/uuid.md) ^3.4.0
- [zlib](https://npm.io/package/zlib.md) ^1.0.5
- [tslib](https://npm.io/package/tslib.md) ^1.9.1
- [bcrypt](https://npm.io/package/bcrypt.md) ^3.0.8
- [lodash](https://npm.io/package/lodash.md) ^4.17.11
- [stripe](https://npm.io/package/stripe.md) ^8.29.0
- [aws-sdk](https://npm.io/package/aws-sdk.md) ^2.286.2
- [convict](https://npm.io/package/convict.md) ^4.2.0
- [hashids](https://npm.io/package/hashids.md) ^1.2.2
- [sitemap](https://npm.io/package/sitemap.md) 4.0
- [typeorm](https://npm.io/package/typeorm.md) ^0.2.14
- [winston](https://npm.io/package/winston.md) ^3.3.3
- [date-fns](https://npm.io/package/date-fns.md) ^1.29.0
- [passport](https://npm.io/package/passport.md) ^0.4.0
- [s3-proxy](https://npm.io/package/s3-proxy.md) ^1.2.1
- [unzipper](https://npm.io/package/unzipper.md) ^0.10.11
- [csvtojson](https://npm.io/package/csvtojson.md) ^2.0.10
- [fancy-log](https://npm.io/package/fancy-log.md) ^1.3.2
- [validator](https://npm.io/package/validator.md) ^12.2.0
- [googleapis](https://npm.io/package/googleapis.md) ^47.0.0
- [typescript](https://npm.io/package/typescript.md) ^3.1.6
- [compression](https://npm.io/package/compression.md) ^1.7.4
- [@google/maps](https://npm.io/package/@google/maps.md) 0.5.5
- [@nestjs/core](https://npm.io/package/@nestjs/core.md) ^5.4.0
- [gulp-install](https://npm.io/package/gulp-install.md) ^1.1.0
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^8.3.0
- [nest-winston](https://npm.io/package/nest-winston.md) ^1.5.0
- [passport-jwt](https://npm.io/package/passport-jwt.md) ^4.0.0
- [@nestjs/common](https://npm.io/package/@nestjs/common.md) ^5.4.0
- [@nestjs/swagger](https://npm.io/package/@nestjs/swagger.md) ^2.5.1
- [@nestjs/testing](https://npm.io/package/@nestjs/testing.md) ^5.4.0
- [@nestjs/typeorm](https://npm.io/package/@nestjs/typeorm.md) ^5.2.2
- [class-validator](https://npm.io/package/class-validator.md) ^0.8.5
- [serialize-error](https://npm.io/package/serialize-error.md) ^4.1.0
- [@nestjs/passport](https://npm.io/package/@nestjs/passport.md) ^1.1.0
- [fastify-formbody](https://npm.io/package/fastify-formbody.md) ^2.0.0
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.12
- [class-transformer](https://npm.io/package/class-transformer.md) ^0.1.9
- [@nestjs/websockets](https://npm.io/package/@nestjs/websockets.md) ^5.4.0
- [awesome-phonenumber](https://npm.io/package/awesome-phonenumber.md) ^2.27.0
- [@nest-modules/mailer](https://npm.io/package/@nest-modules/mailer.md) 1.0.4
- [@nestjs/microservices](https://npm.io/package/@nestjs/microservices.md) ^5.4.0
- [@sierralabs/nest-utils](https://npm.io/package/@sierralabs/nest-utils.md) 0.0.16
- [@sierralabs/nest-identity](https://npm.io/package/@sierralabs/nest-identity.md) 0.2.8
- [@egodigital/appstore-connect](https://npm.io/package/@egodigital/appstore-connect.md) ^0.1.5

## Recent versions

- 1.10.76 (latest) — 2022-05-25

## README

# **greendirect-api**

GreenDirect API server application powered by NestJS, TypeORM, Postgres, and Docker.

## Requirements

Make sure to have the following installed:

- `Node 10+ / NPM 6.1+` for application
- `docker` for postgres database
- `jest` for unit testing
- `tslint` for TypeScript linting (tslint in VSCode to automate linting)
- `prettier` for auto formatting in VSCode
- Make sure you setup an [npmjs.com](http://www.npmjs.com) account and request access to the `@sierralabs` private repos for the NPM dependencies.

## Installation

Using your npmjs.com account from above, run:

```bash
$ npm login
$ npm install
```

# Development Guide

## Dev Database Setup

Setup the Postgres database instance. When running the below command for the first time the `db/greendirect-schema.sql` will be applied.

Database Name: _greendirect_

```bash
# Rebuilds the database with a new Docker container.
$ npm run db

# Load initial mock data via tests.
$ npm run mocks
```

`db` runs migrations automatically.

## Run the app

```bash
# development
$ npm run start

# development: watch mode (restarts on changes)
$ npm run start:dev
```

For development, you can explore the API endpoints via Swagger:
http://localhost:3000/explorer/

```bash
# production mode (uses compiled JS)
$ npm run start:prod
```

# Testing Guide

## Test Database Setup

Creates a separate database instance for test. Database name: _greendirect-test_

```bash
# Rebuilds the test database with a new Docker container.
$ npm run test:db

# Load initial mock data
$ npm run test:mocks
```

`test:db` will automatically run migrations.

## Run automated tests

(requires the above database setup)

```bash
# unit tests
$ npm run test

# end-to-end tests
$ npm run test:e2e

# test coverage
$ npm run test:cov
```

## Run local test server (manual test)

Requires: greendirect-test database, greendirect-web running at localhost:5050

```bash
npm run local-server
```

# Deployments

## Version bump

Use `npm version` command to set a git tag and update package.json with a new version number.

## Deploying to AWS Fargate ECS/ECR environment

Create a `deploy.[NODE_ENV].json` file in `config/` with the following information:

```json
{
  "deploy": {
    "projectName": "greendirect",
    "aws": {
      "accessKeyId": "",
      "secretAccessKey": "",
      "region": "us-west-2",
      "ecr": {
        "uri": "241940929212.dkr.ecr.us-west-2.amazonaws.com/greendirect-staging",
        "tag": "latest"
      },
      "ecs": {
        "service": "greendirect-staging-service",
        "cluster": "greendirect-staging-cluster"
      }
    },
    "npmToken": ""
  }
}
```

> NOTE: `npmToken` is for `@sierralabs/` NPM package installation on docker instance. You can create a NPM_TOKEN by following these instructions: https://docs.npmjs.com/getting-started/working_with_tokens (make sure you create a readonly token using `--read-only` flag.)

After creating the deploy config file you can execute the following replacing `NODE_ENV=staging` with the desired environment.

```bash
$ NODE_ENV=staging gulp deploy
```

# Database Migrations

IMPORTANT: Since migrations are possiby destructive operations, DO NOT run migrations scripts with automation. Do it manually so that any issues can be caught immediately.

## Creating Migrations

First, modify your entity classes with property changes. Second, follow the procedures below.

When naming migrations, include the operation and affected column/table.

```bash
$ npm run db:migrate:new -- "AlterOrderAddTypeColumn"
```

This will **automatically generate** your migration file with changes based on your entiy classes. The file will be placed in `db/migrations/[timestamp]_AlterOrderAddTypeColumn.ts` with both UP and DOWN sql scripts.

> NOTE: Auto generating the migration files guarantees that the entity definition matches the database schema exactly.

**YAH!** To run (add NODE_ENV to change environment):

```bash
$ npm run db # already includes run migrations
$ npm run db:migrate:up # executes all
$ npm run db:migrate:down # undoes the last migration, run again for each migration.
```

# Troubleshooting

## Clear persistent NPM packages

Delete you node_modules folder. Then:

```bash
$ npm install
$ npm cache verify
```

## Recreating Database Schema

When making changes to the database schema you can create a sql dump to replace `db/greendirect-schema.sql` by running:

```bash
docker exec -t greendirect-api_db_1 pg_dump -U root greendirect > db/greendirect-schema.sql
```

> Development: `greendirect-api_db_1` - is the name of the docker container on your machine (development).

> Test: `greendirect-api_db-test_1` - is the name of the docker container on your machine.

## Cleaning and resetting the Docker DB

If you want to reset the DB back to the emtpy schema you'll need to delete the docker container and the docker image.

Just re-run the npm scripts for database:

```bash
# for development
$ npm run db

# for test
$ npm run test:db
```

The script already automates the following commands:

```bash
# stops, removes volumes, and removes containers
$ docker-compose down -v --rmi local

# rebuilds and starts containers in background
$ docker-compose up --build -d
```

### Troubleshooting

```bash
# to start  with fresh docker build, remove all (both development and test):
$ docker-compose down -v --rmi all --remove-orphans
```

---
_Source: https://npm.io/package/greendirect-api · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
