# apollo-user-data


## Overview

- **User Data management**
  api for managing user's settings and a list of favorite entities, a user can mark some entities (tracks, playlists) as interesting to get additional information about them.

- **User messages management**
   api for managing user notifications. Notification is data about some event associated with favorite entity of the user (for example, the entry of an interesting track in the top chart).


- **Messages delivery**
  api to deliver various messages to users. For example push-messages to mobile devices delivery, [Expo API](https://docs.expo.io/push-notifications/sending-notifications/) is used. The service, together with the infrastructure, provides the possibility of reliable sending with retries and handling of known expo issues.


Read this service [full documentation](https://data-analytics.atlassian.net/wiki/spaces/AP/pages/2318041097/User+Data+Messages+System).

## Dependencies

### Technologies
- [aiohttp](https://docs.aiohttp.org/en/stable/) as a main framework
- [marshmallow](https://marshmallow.readthedocs.io/en/stable/) for data (de)serialization and validation
- [SQLAlchemy ORM](https://docs.sqlalchemy.org/en/14/orm/) for asynchronous interactions with database
- [PostgreSQL](https://www.postgresql.org/about/) as a database
- [Sqitch](https://sqitch.org/docs/) as a database migrations tool
- [Pytest](https://pytest.org) for tests running

### Another services
- apollo-api
- rti-api



## Development

### Running application with Docker
1. Copy '.env.sample' file and save as '.env' at the same level. Fill the '.env' file with real values for development.

2. Install [Docker](https://www.docker.com/).

3. Run the application

```bash
$ make docker/local/up
```

It will run application on port 8000. Check locally swagger at 0.0.0.0:8000/api/doc/

### Run tests with test database

```bash
$ make test
```

### Run checks before commit
```bash
$ make pre-commit
```

## Database migrations
Database changes are managed with [sqitch](https://sqitch.org/docs/manual/sqitch-configuration/). First [install it](https://sqitch.org/download/) to add migrations or run them locally.

### Configuration
```bash
cd ./migrations
sqitch config --user engine.pg.client <path to psql>  # for example /usr/local/pgsql/bin/psql
sqitch config --user user.name '<your name>'
sqitch config --user user.email '<your email>'
```

### Adding migration
First create files

```bash
cd ./migrations
sqitch add <migration file name> -m '<comment to the migartion>'  # specify migration file name without '.sql'
```

The previous command creates empty .sql files with the appropriate name,
one in each of the "/migrations" sub-directories
- /deploy - content of the migration
- /revert - commands to rollback the migration
- /verify - commands to verify if it is possible to apply the migration

Fill these files. To apply the migration one needs to "deploy" it (in terms of sqitch) to your database.

### Deployment
Apply changes starting from the last one
```bash
sqitch deploy <your database uri>
```
Example:
```bash
sqitch deploy -t postgresql://postgres:pass@127.0.0.1:5432/pg_db_test
```

Apply specific changes
```bash
sqitch deploy <your database uri> <migration file name>  # specify migration file name without '.sql'
```
