# Real-Time Insights backend

Backend for the [Real-Time Insights app] and [Amadeus]

### Getting started

Install node

    brew install node

Select the correct version of node

    nvm install
    nvm use

Install dependencies

    npm install

Setup env variables

    cp .env.example .env.local
    $EDITOR .env.local
    ln -s .env.local .env # symlinking makes it easier to switch envs

Setup AWS

If this is your first time using the aws cli on a machine, you will need to create an Access Key from the web console. Save the key and secret somewhere, and have the cli ask you for these.

    aws configure

Now, you can generate a token as the cross_account_developer role, using the script in rti-scripts

    git clone github.com/filtr/rti-scripts
    cd rti-scripts
    npm run aws-set-session-token \
      cross_account_developer@gdb-artistapp-dev \
      flastname@gdb-infra-dev \
      MFA_TOKEN

Develop

    npm run watch

## Building + Deploying

To update the backend, we use [Jenkins](https://jenkins.apollo.stream) and [Octopus](https://octopus.delphi.zone)

Octopus is how we manage what build of ama-backend[^1] is deployed to the various environments.

You can view [those deployments here](https://octopus.delphi.zone/app#/Spaces-1/projects/sma-api/deployments)

The healthchecks for the deployed API can be accessed at:

- Development: https://dev-api.insights.stream/_health
- Staging: https://stage-api.insights.stream/_health
- Production: https://api.insights.stream/_health

[^1]: called `sma.api` in the octopus ui and aws

### Development

Development builds and deployments happen automatically when code gets pushed or merged to `develop` branch.

### Staging + Production

Builds are triggered on changes to `master`, then deployed with Octopus.

First, get the code into `master`

    git switch develop
    gh pr create --base master

Review and merge the PR

    gh pr view --web

Next, bump the version to create a known release artifact

    git switch master
    npm version [major|minor|patch] # creates a new commit and tags it
    git show # review the patch
    git push --follow-tags # Jenkins will build when it sees a new tag, and put it in Octopus

Make sure to update develop with the version commit

    git switch develop
    git merge master
    git push

Before moving the development build to production, we want to make sure it does not break the current production frontend.

Once the build completes, use [Octopus][] to deploy the newly-tagged release to the staging or production environments.

## Documentation

The RTI backend is an aggregating proxy. It accepts requests from the [RTI app] and [Amadeus] users, and requests general data from the Delphi API and returns application screen specific responses.

### User/Artist Authorization

For each request from a frontend, the backend checks that the user is authorized to access data for a requested artist, based on the token sent along with each request.

To fake a superuser in development without sending an access token, use `NO_AUTH=true`. This is useful for developing GraphQL requests.

### Delphi

The backend authenticates itself directly with the Delphi API using a client id and secret, and receives an access token.
The access token is sent along with each request to Delphi.

### Third parties

- Sentry
- Datadog

## Frontends

The [RTI app] documentation is in the [frontend repository](https://github.com/filtr/ama-frontend)
[Amadeus] is our web frontend.

### Tech stack

- TypeScript
- Node.js
- Koa: Node.js web framework
- io-ts: Delphi response validation
- GraphQL
  - graphql-codegen for generating TypeScript types from GraphQL schema
  - Apollo server

[real-time insights app]: https://github.com/filtr/ama-frontend
[rti app]: https://github.com/filtr/ama-frontend
[amadeus]: https://github.com/filtr/rti-amadeus
[octopus]: https://octopus.delphi.zone/app#/Spaces-1/projects/sma-api/deployments

### Environment variables

`NO_AUTH` can be set when developing locally to make requests without sending a valid `Authorization` header with access token. This is useful when making requests using e.g. curl or GraphQL playground.

`REDIS_URL` can be set to enable caching.

### GraphQL Development

- Start local server with `NO_AUTH=true npm run watch`.
- Access GraphQL playground for making requests http://localhost:8080/graphql
- Making changes to schema sources files (`*.graphql`) automatically re-generates concatenated schema `/src/graphql/__generated__/schema.graphql` and TypeScript types in `/src/graphql/__generated__/types.ts`

### Caching

All successful and valid responses from Delphi are cached using Redis and MessagePack. Caching is enabled by setting the `REDIS_URL` environment variable.

### Debugging using Visual Studio Code

1. Add something like the following in `.vscode/launch.json`

```json
{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "attach",
      "name": "Node: Nodemon",
      "processId": "${command:PickProcess}",
      "restart": true,
      "protocol": "inspector",
      "outFiles": ["${workspaceFolder}/dist/**/*.js"]
    }
  ]
}
```

2. Start the backend using something like `npm run watch`
3. Run -> Start Debugging
4. Pick the process `node --inspect dist/server.js` from the dropdown

## Superusers and Supported Artists

We need to know what artists are supported by RTI, that has verified good data that RTI uses by querying delphi.

This data is currently in a json file in an S3 bucket, which maps `GRAS_` IDs to Atlas image service urls.

If the backend has access to the AWS S3 bucket, then superusers will be able to access all artists. If the backend does not have access to the AWS S3 bucket, Superusers will need to have explicit artistIds listed in their Auth0 `app_metadata.ama.artistPermissions` (`[ "GRAS_6257" ]`).
