## General

This is the main repo for the backend of Walter.

## Features

- **Nexus framework:** The server uses the [nexus-framework](https://nexusjs.org/) which provides a lot of [features](https://nexusjs.org/adoption-guides/nexus-schema-users#differences-between-nexus-and-nexus-schema) out of the box that we don't need to integrate like testing, subscriptions and logging. It also easily integrates [graphql-shield](https://nexusjs.org/pluginss/graphql-shield), [jwt-auth](https://nexusjs.org/pluginss/jwt-auth) for authenticating and [Prisma as ORM with a plugin](https://nexusjs.org/pluginss/prisma/overview).

In this project the Nexus framework is the GraphQL API, [Prisma](https://www.prisma.io/docs/understand-prisma/introduction) is an programming API to use in our code to interact with the database.

For a fully-fledged **GraphQL & Node.js tutorial**, visit [How to GraphQL](https://www.howtographql.com/graphql-js/0-introduction/)

## Software requirements

1. You need **NodeJS** installed (version >= 14). Suggestion use the [n-install](https://github.com/mklement0/n-install) script to manage Node versions.
1. You need **Yarn** classic 1.x installed. See your OS install steps [here](https://classic.yarnpkg.com/en/docs/install)
1. You need **Docker** installed. See your OS install steps [here](https://docs.docker.com/get-docker/)
1. You need **Docker compose** installed. See your OS install steps [here](https://docs.docker.com/compose/install/)

## Getting started

### Setup

Our application requires 2 `.env` files. One that Prisma is looking for under `./prisma` and another for configuring everything else.

1. Create the `./prisma/.env` file and in it put `DATABASE_URL=postgresql://postgres:k0fpEoFJetxkObPD@localhost:5432/walter`. This login credential is the one you can find in `docker-compose.yml`
2. Ask a coworker for environment variables to fill in `.env` file at the root of the project. It needs it to know where to connect to Redis and configure different third parties.
3. Download the file from https://drive.google.com/file/d/1OXMIht11Yngxew8WoeeOY6ZVxj9ffxaF/view?usp=sharing saved under the name `staging-walter-backend-storage.json` to be at the root of the project and add the following to your .env file.
   This is used for file upload from your machine to staging for some API calls.

```
PROJECT_ID=staging-288420
KEY_FILENAME=staging-walter-backend-storage.json
FILES_BUCKET_NAME=walter-staging
```

### Starting up

```sh
# Start the required database (the -d option is to run it in daemon mode, in the background)
docker-compose up -d

# 1. Install depedencies
yarn install

# 2. Generate prisma code based on schema (datamodel.prisma).
yarn prisma generate

# 3. Create the database structure without data
yarn db:up

# 4. Add some seed data. Once the script is done, in the output of the console, take note of your manager and resident user names and of the manager user password once the script is done and of the instructions on where to use the provided credentials. To generate a manager user with a managing company, you can also start the manager-web frontend, go to http://localhost:3000/auth/register, then register a company and take note of your user email in the user profile (the password will be 12345678 by default).
yarn seed

# 5. Start dev server
yarn dev
```

### Commands

- `yarn dev` starts GraphQL server on `http://localhost:5000`
- `yarn worker-dev` Run worker jobs locally
- `yarn build` Build the code for production. It creates a .nexus folder with JS code and it's own node_modules.
- `yarn test:unit` runs the unit tests only
- `yarn test:int` runs the integration tests and needs the Postgres database to be accessible locally to work
- `yarn test` do both the above

### Project structure

| File name                         | Description                                                                                                                     |
| :-------------------------------- | :------------------------------------------------------------------------------------------------------------------------------ |
| `├── .env`                        | Defines environment variables                                                                                                   |
| `├── .graphqlconfig.yml`          | Configuration file based on [`graphql-config`](https://github.com/prisma/graphql-config) (e.g. used by GraphQL Playground).     |
| `└── prisma` (_directory_)        | _Contains all files that are related to the Prisma database service_                                                            | \  |
| `├── prisma/.env`                 | Prisma looks for this file for environment variables                                                                            |
| `└── prisma/schema.prisma`        | Defines your data model (written in [GraphQL SDL](https://blog.graph.cool/graphql-sdl-schema-definition-language-6755bcb9ce51)) |
| `└── api` (_directory_)           | _Contains the source files for your GraphQL server_                                                                             |
| `├── api/app.ts`                  | The entry point for your GraphQL server                                                                                         |
| `├── schema.graphql`              | The **application schema** defining the API exposed to client applications                                                      |
| `├── api/resolvers` (_directory_) | _Contains the implementation of the resolvers for the application schema_                                                       |
| `└── api/generated` (_directory_) | _Contains generated files_                                                                                                      |
| `└── prisma.grapghql`             | The **Prisma database schema** defining the Prisma GraphQL API                                                                  |

# Documentation

For further documention please look this project [Wiki](https://github.com/usewalter/walter-backend/wiki) or the wiki tab above.
