# streamlit-fansifter-fileupload-app
Streamlit app for CRM to upload files to Fansifter.

* Project setup

After cloning this repository run following command to
create virtual environment and install required packages:

```poetry install```


* Prerequisites:

Make sure you have Snowflake CLI installed:
https://docs.snowflake.com/en/developer-guide/snowflake-cli/installation/installation

Make sure your that for Snowflake CLI you have connection profile called fileupload that uses
FANSIFTER_ENGINEERING_PRIVACY profile to deploy/manage Streamlit app related resources. 
This role is also used by Streamlit app to run queries.

```
[connections.fileupload]
account = "delphi.us-east-1"
user = "xxxxxx@sonymusic-pde.com"
role = "FANSIFTER_ENGINEERING_PRIVACY"
database = "FANSIFTER_APP_REPORTING"
schema = "PROD"
warehouse = "EXPLORATION_WH"
authenticator = "SNOWFLAKE_JWT"
private_key_file = "/Users/xxxxx/.ssh/snowflake/rsa_key.p8"
```

Makefile is used to run related tasks. To see all available tasks:
type ```make help```

Most of the commands take advantage of Snowflake CLI.
Here are Snow CLI commands for Streamlit:
https://docs.snowflake.com/en/developer-guide/snowflake-cli/command-reference/streamlit-commands/overview

* Updating Streamlit app

As the app is only used in PROD environment then run following code to apply changes:
```make deploy_prod_streamlit```

* Running Streamlit app in Snowflake's QA schema

Sometimes there is a need to validate bigger changes in Snowflake environment
or upload some data to Fansifter QA.
Before running ```make deploy_qa_streamlit``` make sure to modify lines 22-26 in
functions.py file to make sure that constants_qa will be read. This will ensure
that Streamlit app reads/writes data from/to QA schema.
This change is needed because currently env variables cannot be passed to Snowflake when deploying Streamlit
app using CLI.
After QA version is not needed anymore, remove it and related stage by running:
```make drop_qa_stremlit```


* Running streamlit app locally

To run Streamlit app locally, type:
```make run_streamlit_locally```

Local deployment takes advantage of connections.json file that
contains profile used by Snowpark to connect to Snowflake.
Inside the Snowflake's Streamlit app, the connection is automatically
assigned but not in local Streamlit deployment.


* User permissions (managed by Terraform)

Users belonging to Snowflake role called CRM_TEAM_ROLE 
should have access to these Streamlit apps.

This role is granted database roles ```FANSIFTER_APP_REPORTING.DB_QA_SCHEMA_FILEUPLOAD_STREAMLIT```
and ```FANSIFTER_APP_REPORTING.DB_PROD_SCHEMA_FILEUPLOAD_STREAMLIT```.

And these roles are results of ```streamlit_database_role_FILEUPLOAD```
module in Terraform.

This means that there is currently no need to manage
user permissions directly from Streamlit app's share button.
