# SYNT Backend

> Nodejs API Backend service for [Synt](https://synt.be), access through [Synt API](https://synt.be/api) or [Syntbeheer](https://syntbeheer.be/api)
> Author: Adriaan De Bolle - [adriaandebolle.com](https://adriaandebolle.com)

## SETUP

- Requirements

> npm

> mysql

```shell
$ npm install

```

- Database

```shell
$ cd mysql
$ npx sequelize-cli db:migrate && npx sequelize-cli db:seed:all
```

> ONLY for local development, reinit database

```shell
$ npx sequelize-cli db:seed:undo:all && npx sequelize-cli db:migrate:undo:all && npx sequelize-cli db:migrate && npx sequelize-cli db:seed:all
```

NODE_ENV=production npx sequelize-cli db:seed:undo:all && NODE_ENV=production npx sequelize-cli db:migrate:undo:all && NODE_ENV=production npx sequelize-cli db:migrate && NODE_ENV=production npx sequelize-cli db:seed:all

NODE_ENV=production npx sequelize-cli db:seed --seed 20210118103211-notification-settings.js
NODE_ENV=production npx sequelize-cli db:seed --seed 20210728100151-associate-notification-settings.js
NODE_ENV=production npx sequelize-cli db:seed:undo --seed 20210728100151-associate-notification-settings.js

> Create model (model + migration)

```shell
$ npx sequelize-cli model:generate --name Yeas --attributes type:enum,version:integer,consent_given_at:date
```

> Create association (migration)

```shell
$ npx sequelize-cli migration:create --name example
```

## RUN

> Use babel-node before node starts index

- start app

```shell
$ npm run start:dev
```

> Server

- check running process

```
$ forever list
```

- stop process

```
$ forever stop [pid]
```

## Coding Extra

[More info: Private Git packages](https://brianmorrison.me/blog/private-npm-packages-with-github-actions-&-packages/)

[Install CodeDeploy Agent on EC2(image)](https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install-ubuntu.html)

[Verify all EC2 setup config](https://stackoverflow.com/questions/44574120/code-deploy-fails-without-any-error-message)

## TODO / Improvements

Get rid off relative path by using [typescript](https://betterprogramming.pub/best-practice-use-module-path-import-instead-of-relative-path-import-4baaf1b3b4db)
Use `npm run build` to precompile babel in production: [info](https://github.com/babel/example-node-server)

## Background information

![ERD Diagram - Synt](ERD.png?raw=true "ERD Diagram - Synt")

## The fix when 'No space left on device @ dir_s_mkdir - /opt/codedeploy-agent/deployment-root/..' occurs

- ssh to `ssh -i ~/.ssh/synt-key-pair.pem ubuntu@34.242.114.0`
- change `:max_revisions: 1` in `/etc/codedeploy-agent/conf/codedeployagent.yml`
- clean up the folder `sudo rm -rf /opt/codedeploy-agent/deployment-root/ca203350-bc6e-40a1-b01f-d4ca17fcdff7/*`
- restart the service `sudo service codedeploy-agent stop` and then `sudo service codedeploy-agent start`

## Start database with docker

- `docker-compose -f docker-compose.yml up --build`

- `docker-compose -f docker-compose.yml up db`

- `docker-compose up --build`

## Start production backend with docker

- `docker build -t synt-backend .`

- `docker run -p 8000:8000 --network synt_backend_my-network synt-backend:latest`

aws ecr --profile synt get-login-password --region eu-west-1 | docker login --username AWS --password-stdin 635199656487.dkr.ecr.eu-west-1.amazonaws.com

docker tag synt-backend:latest 635199656487.dkr.ecr.eu-west-1.amazonaws.com/synt-backend:latest

docker push 635199656487.dkr.ecr.eu-west-1.amazonaws.com/synt-backend:latest
