# Jaga API V5 Implementation

Reimplementation of [codeigniter](https://dev.kpk.go.id/gitlab/JAGA/cms-webservice) version.

## Getting Started

1. Install Phalcon 3.4.1 based by following [official guide](https://docs.phalconphp.com/en/3.4/installation)
2. Install [composer](https://getcomposer.org/) and run `composer install` on project directory
3. Copy configuration file

- From `app.ini.example` to `app.ini`
- From `.env.example` to `.env`
- From `public/.htaccess.example` to `public/.htaccess`

4. Run database migration from project directory

   In Windows:

   ```bat
   .\vendor\phalcon\devtools\phalcon.bat migration run
   ```

   In Unix:

   ```bash
   ./vendor/phalcon/devtools/phalcon migration run
   ```

5. Import Postman collection and environment inside `postman` folder and Test JAGA API

## Getting Started with Docker (for Dev only)
1. Copy configuration file

- From `app.ini.example` to `app.ini`
- From `.env.example` to `.env`
- From `public/.htaccess.example` to `public/.htaccess`
- From `docker/dev/.env.docker` to `docker/dev/.env`

2. Setup app.ini

   ```bat
   host = jaga_postgres_dev
   dbname = jaga
   ```

3. Setup .env

   ```bat
   DB_HOST=jaga_postgres_dev
   PRIVATE_FOLDER=path\in\localhost # contoh: D:\jaga-phalcon-private-attachments\
   USER_IMAGE_FOLDER=path\in\localhost # contoh: D:\jaga-phalcon-public-attachments\

   # dan variabel lainnya
   ```

4. Setup docker/dev/.env

   ```bat
   HOST_PRIVATE_FOLDER=path\in\localhost # disamakan dengan variabel PRIVATE_FOLDER pada .env di atas
   HOST_USER_IMAGE_FOLDER=path\in\localhost # disamakan dengan variabel USER_IMAGE_FOLDER pada .env di atas
   ```

5. Build and run the containers

   At project directory
   ```bat
   docker compose -f docker/dev/docker-compose.yml up
   ```

6. Run database migration from project directory

   <sup>For lighter migration without heavy sql's [download](https://dev.kpk.go.id/gitlab/JAGA/jaga-phalcon/-/wikis/uploads/f24b47fc71d793d3b2ce7847a4d7de60/light_migrations.zip)</sup>
   

   ```bash
   docker compose -f docker/dev/docker-compose.yml exec jaga_postgres_dev psql -U postgres -c "CREATE DATABASE jaga"
   docker compose -f docker/dev/docker-compose.yml exec jaga_postgres_dev psql -U postgres -c "CREATE EXTENSION IF NOT EXISTS pgcrypto"
   docker compose -f docker/dev/docker-compose.yml exec jaga_postgres_dev psql -U postgres -c "CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\""
   docker compose -f docker/dev/docker-compose.yml exec jaga_phalcon_dev ./jaga_phalcon/vendor/phalcon/devtools/phalcon migration run
   ```   

7. The logs directory needs more permission
   ```bash
   chmod -Rf 777 logs/
   ```

## Run Scheduled Task

Scheduled task logic could be found in `cli/tasks/ScheduledTask.php`. To check whether the task is executed or not, please check application log located in `logs` folder

1. Sekolah

   ```bat
   php cli\cli.php scheduled main sekolah daily
   ```

2. Puskesmas

   ```bat
   php cli\cli.php scheduled main puskesmas
   ```

3. OMSPAN Dana Desa. Comprises of two part

   Fetch Pagu, Penyaluran, & SP2D Dana Desa. Use `0` as the last paramater

   ```bat
   php cli\cli.php scheduled main omspan_desa 0
   ```

   Fetch penyerapan/realisasi dana desa. Use `N`, where between 1 and (NUMBER_OF_PEMDA/10)

   ```bat
   php cli\cli.php scheduled main omspan_desa [N]
   ```

4. Notifikasi Email kepada developer jika ada API Error (Server KLOP) dalam 1 jam terakhir

   ```bat
   php cli\cli.php scheduled main log_api_error
   ```

5. Cache table ELHKPN

   ```bat
   php cli\cli.php scheduled main elhkpn_mart 2018
   ```

## Automated Testing

Currently API end-to-end testing is achieved using [PHPUnit and Guzzle](https://blog.cloudflare.com/using-guzzle-and-phpunit-for-rest-api-testing/)

1. Make sure application is up and running
2. Execute test: `./vendor/bin/phpunit` or `composer test`

## OpenApi Specs

1. Install [Optic](https://useoptic.com/docs/)
   
2. Install [redoc-cli](https://github.com/Redocly/redoc/blob/master/cli/README.md)
   
3. Copy configuration file and set accordingly
   -  from `optic.yml.example` to `optic.yml`
   
   
4. Check if everything is ready
   ```bash
   api check start
   ```
   
5. Start listen to http request
   ```bash
   api start
   ```
   You should see
   ```bash
   [optic] Review the API Diff at http://localhost:34444/apis/1/review
   [optic] Optic is observing requests made to http://localhost:8888
   ```
6. Create an http request to `inboundUrl`: 
   ```
   GET http://localhost:8888/api-endpoint
   ```
   
7. Review the API Diff at `http://localhost:34444/apis/1/review`

8. Export to OpenApi using `api generate:oas --json`

9. Starts the server with spec rendered with ReDoc `redoc-cli serve .optic/generated/openapi.json`

## Meilisearch

NOTE: If you already use `docker-compose` method in `docker\meilisearch` directory, please continue to step `3. Configuration`

1. Fetch Meilisearch image version v0.19.0 using docker (Because this project using Phalcon 3.4.1 that limited the PHP Version to PHP 7.2, so the meilisearch itself limited to the version v0.19.0)

    ```bash
    docker pull getmeili/meilisearch:v0.19.0
    ```
   
   Note: use ```sudo``` if permission denied
   
2. Launch Meilisearch 
    
    ```bash
   docker run -it --name meiliesearch_meili -p 7700:7700 getmeili/meilisearch:v0.19.0 ./meilisearch --no-analytics true --master-key="KPKJagaMeiliSearch2022"
    ```
   
   Note: You can change the master key value to your liking. Please remember your master key and match the ```MEILI_SEARCH_MASTER_KEY``` value in your ```.env``` to the master key that you have set before.
   
3. Configuration
    
    - Set ```MEILI_SEARCH_ACTIVE``` value in your ```.env``` to ```TRUE```. This parameter is for updating value when requesting to API detailPT & Edit Berita
    - Set ```MEILI_SEARCH_BASE_URL``` value in your ```.env``` to the base url where you deploy your meilisearch.
    - Set ```MEILI_SEARCH_MASTER_KEY``` value in your ```.env``` to match the Master Key value that you set when you launch the meilisearch.
    - Set ```MEILI_SEARCH_API_ACCESS_PASS``` value, please **DO NOT** set it as the same as the Master Key. This value will be used later on setup your Meilisearch for the first time.
   
4. Setup (First Time Only After Launch)

    After you set everything, the next thing you have to do is setup your meilisearch. To do this, please call to this API below (you can use postman) once:
    
    ```
    POST {Back_End_Base_URL}/v5/search/setup
    
    Header
    Access-Key:{MEILI_SEARCH_API_ACCESS_PASS}
    ```
   
    Don't forget to set ```Access-Key``` header to your ```MEILI_SEARCH_API_ACCESS_PASS``` value you set before.
    
    Note: You only have to do this once after you Install & Launch your meilisearch
    
5. Setup the scheduler

    To populate the data in meilisearch, you have to run one of the scheduled task. The best way to do it is by using the crontab, to execute in every minute in production environment, you can use this crontab definition
    
    ```*/5 * * * * apache cd /var/jaga/deploy/current && /usr/bin/php cli/cli.php Scheduled main meilisearch > /dev/null 2>&1```
    
    or you can check ```.crontab.example```
    
    If you are using Windows, please check [Microsoft docs](https://docs.microsoft.com/en-us/troubleshoot/windows-client/system-management-components/use-at-command-to-schedule-tasks) how to make Task Scheduler to run this command
    
    ```php cli\cli.php scheduled main meilisearch```
    
    inside your project directory 