# Mobicoop V3 - User Service

User-related data management.

## Requirements

You need [Docker](https://docs.docker.com/engine/) and its [compose](https://docs.docker.com/compose/) plugin.

You also need NodeJS installed locally : we **strongly** advise to install [Node Version Manager](https://github.com/nvm-sh/nvm) and use the latest LTS version of Node (check that your local version matches with the one used in the Dockerfile).

The API will run inside a docker container, **but** the install itself is made outside the container, because during development we need tools that need to be available locally (eg. ESLint, Prettier...).

A RabbitMQ instance is also required to send / receive messages when data has been inserted/updated/deleted.

## Installation

- copy `.env.dist` to `.env` :

    ```bash
    cp .env.dist .env
    ```

    Modify it if needed.

- install the dependencies :

    ```bash
    npm install
    ```
    
- start the containers :

    ```bash
    docker compose up -d
    ```

    The app runs automatically on port **5001**.


## Database migration

Before using the app, you need to launch the database migration (it will be launched inside the container) :

```bash
npm run migrate
```

## Usage

The app exposes the following [gRPC](https://grpc.io/) services :

-   **FindByUuid** : find a user by its uuid

    ```json
    {
        "uuid": "80126a61-d128-4f96-afdb-92e33c75a3e1"
    }
    ```

-   **FindAll** : find all users; you can use pagination with `page` (default:_1_) and `perPage` (default:_10_)

    ```json
    {
        "page": 1,
        "perPage": 10
    }
    ```

-   **Create** : create a user (note that uuid is optional, a uuid will be automatically attributed if it is not provided)

    ```json
    {
        "email": "jezabel.doe@email.com",
        "firstName": "Jezabel",
        "lastName": "Doe",
        "phone": "+33611223344"
    }
    ```

-   **Update** : update a user

    ```json
    {
        "firstName": "Jezabel-Katarina",
        "email": "jezabel-katarina.doe@email.com",
        "uuid": "30f49838-3f24-42bb-a489-8ffb480173ae"
    }
    ```

-   **Delete** : delete a user by its uuid

    ```json
    {
        "uuid": "80126a61-d128-4f96-afdb-92e33c75a3e1"
    }
    ```

## Messages

As mentionned earlier, RabbitMQ messages are sent after these events :

-   **Create** (message : the created user informations)

-   **Update** (message : the updated user informations)

-   **Delete** (message : the uuid of the deleted user)

Various messages are also sent for logging purpose.

## Tests

Tests are run outside the container for ease of use (switching between different environments inside containers using prisma is complicated and error prone).
The integration tests use a dedicated database (see *db-test* section of *docker-compose.yml*).

```bash
# run all tests (unit + integration)
npm run test

# unit tests only
npm run test:unit

# integration tests only
npm run test:integration

# coverage
npm run test:cov
```

## License

Mobicoop V3 - User Service is [AGPL licensed](LICENSE).