# Standup the user invite ecosystem locally
---

Ticket ref: [POD-1852](https://jira.theorchard.com/browse/POD-1852)

## Overview

This is a call to `ows-podcast` create user endpoint.

```bash
curl --location --request POST 'localhost:5020/users' \
--header 'Orchard-User-Id: 6fd98648-38da-4e07-910e-f41ee53cdbae' \
--header 'Content-Type: application/json' \
--data-raw '{
    "name": "Rishi Satsangi",
    "organization": "orchard",
    "email": "rsatsangi@theorchard.com",
    "role": "admin"
}'
```

Additionally, you will need to set some fixture data in your local Neo4j graph.

## Requirements

- Dev `ows-podcast` mysql db
- `dev-orchard` Auth0 tenant
- local Neo4j graph
- local `ows-users`
- local `ows-podcast`

## Configuration

### Dev `ows-podcast` mysql db

Point your local `ows-podcast` to the dev `ows-podcast` database with this .env var:

```bash
export PODCAST_DB_URL=mysql+pymysql://ows_master:POASSWORD@dev-ows-podcast.cluster-co2yckwm6hcp.us-east-1.rds.amazonaws.com/ows_podcast
```

### `dev-orchard` Auth0 tenant

In your local `ows-users`, use the client id and secret for the `orchard-machine-to-machine` Application in the `dev-orchard` tenant in the .env file.

### local Neo4j graph

Make sure you have the Neo4j Desktop client. Create a new database. At the time of writing, the version you should use is `Neo4j 3.5.18`. Remember the username ans password, and set the following in your local `ows-users` .env:

```bash
NEO4J_URL='bolt://localhost:7687'
NEO4J_USERNAME=neo4j
NEO4J_PASSWORD=test

NEO4J_dbms_connectors_default__listen__address=0.0.0.0
NEO4J_dbms_connector_bolt_listen__address=0.0.0.0:7687
NEO4J_dbms_ssl_policy_default_client__auth=NONE
NEO4J_dbms_ssl_policy_legacy_allow__key__generation=true
NEO4J_dbms_ssl_policy_legacy_client__auth=NONE
NEO4J_dbms_ssl_policy_legacy_trust__all=true
NEO4J_dbms_connector_https_enabled=false
```

#### Enable APOC plugin

You will need to [enable the APOC plugin](https://github.com/neo4j-contrib/neo4j-apoc-procedures) in your Neo4j desktop client.


### local `ows-users`

Both your local `ows-podcast` and Auth0 dev tenant's will be talking to your local `ows-users`, so you will need to tunnel it. Note the port number, the default is `5000` but you can change it in your .env with:

```bash
export PORT=5020
```

Run the microservice with

```bash
python dev.py
```

#### localhost.run

[Localhost.run](https://localhost.run) allows you to tunnel an http port to the general public. If the port is `5000`, you can say:


```bash
ssh -R 80:localhost:5000 nokey@localhost.run
```

You will get a public url like `XXXXX.localhost.run`

### local `ows-podcast`

Set your `ows-podcast` port to something other than the one on `ows-users` so they don't conflict.

```bash
export PORT=5020 # or whatever
```

To get `ows-podcast` to talk to your tunneled `ows-users`, use this .env var:

```bash
OWSREQUEST_SERVICE_MAP="{\"ows-users\": \"XXXXX.localhost.run\"}"
```

## Fixture Data


### Sample Prod data

For [POD-1852](https://jira.theorchard.com/browse/POD-1852), 3 `Identities` and their `Profiles` were sampled from the Prod Neo4j graph and imported into local Neo4j graph.

```sql
MATCH (i:Identity)-[r]->(p:Profile)
WHERE i.email IN ['rsatsangi@theorchard.com', 'pascal@theorchard.com', 'amccown@theorchard.com']
WITH i, coalesce(r) as r, coalesce(p) as p
RETURN *;
```

Run that query against the Prod Ne04j graph, and download the results as JSON.


<a href="https://ibb.co/TDG2T1f"><img src="https://i.ibb.co/cK93vwB/download-neo4j-result.png" alt="download-neo4j-result" border="0"></a>

### Import sampled data

Now disconnect from Prod and connect to your local graph, and click to manage it.

<a href="https://ibb.co/6JjsTQf"><img src="https://i.ibb.co/k6NcwZW/Screen-Shot-2021-06-02-at-5-41-06-PM.png" alt="Screen-Shot-2021-06-02-at-5-41-06-PM" border="0"></a>

Open the `Import` folder, and drop the `.json` file into it.

<a href="https://ibb.co/XtdkDFD"><img src="https://i.ibb.co/KWQzDVD/Screen-Shot-2021-06-02-at-5-43-17-PM.png" alt="Screen-Shot-2021-06-02-at-5-43-17-PM" border="0"></a>

Then run the folliwing cypher to import the `.json` into your graph.

```sql
CALL apoc.load.json("file:/podcast-profiles.json")
YIELD value
WITH value
CALL apoc.merge.node(value.i.labels, value.i.properties, {}, {}) YIELD node as i
WITH value, i
CALL apoc.merge.node(value.p.labels, value.p.properties, {}, {}) YIELD node as p
WITH value, i, p
CALL apoc.merge.relationship(i, value.r.type, NULL, NULL, p, NULL) YIELD rel as r
WITH value, i, p, r
RETURN *;
```

To verify, run this cypher.

```sql
/* find all */
MATCH (i:Identity)-->(p:Profile)
RETURN *;
```

## Destroy Data

While debugging you may get into a partial state where an Auth0 user was created, And maybe there was a change to the graph, but not to the database. This is a list of queries to reset to local environment and run the invite handler again.

### reset `ows-podcast` database

You will want to remove your user from the podcast database.

Find your user id:

```sql
SELECT * FROM `user` WHERE email = 'rsatsangi@theorchard.com';
```

Delete the things.

```sql
DELETE FROM `user_podcast_settings` WHERE user_id = XXX;

DELETE FROM `user` WHERE email = 'rsatsangi@theorchard.com';
```

### reset local graph

```sql
/* DELETE ALL */

MATCH (i:Identity)
DETACH DELETE i;

MATCH (p:Profile)
DETACH DELETE p;
```

Then reinjest the `.json`.

### reset Auth0

Delete the `art-relations` version of your Auth0 from the dev tenant.

## Verify

If it works successfully, both the `orchard-gsuite` and `art-relations` versions of your Auth0 users should have the same `orchardIdentityId` in the `user_metadata`.
