# ๐Ÿš€ API Service - Sistem Manajemen Pasien > **Sistem manajemen pasien modern dengan arsitektur bersih untuk pengelolaan data kesehatan dan integrasi layanan kesehatan** ## ๐Ÿ“‘ Daftar Isi - [โœจ Fitur Utama](#-fitur-utama) - [๐Ÿ—๏ธ Arsitektur](#๏ธ-arsitektur) - [โšก Quick Start](#-quick-start) - [๐Ÿ” Autentikasi](#-autentikasi) - [๐Ÿ“Š API Endpoints](#-api-endpoints) - [๐Ÿ› ๏ธ Development](#๏ธ-development) - [๐Ÿš€ Deployment](#-deployment) - [๐Ÿ“š Dokumentasi](#-dokumentasi) - [๐Ÿšจ Troubleshooting](#-troubleshooting) *** ## โœจ Fitur Utama ### Core Features - **๐Ÿ”’ JWT Authentication** - Sistem autentikasi dengan Keycloak integration - **๐Ÿ‘ฅ Patient Management** - CRUD lengkap untuk data pasien - **๐Ÿ” Dynamic Filtering** - Filter dan pencarian data pasien secara dinamis - **๐Ÿ“Š Advanced Search** - Pencarian dengan multiple fields dan operators - **๐Ÿฅ BPJS Integration** - Integrasi dengan layanan kesehatan BPJS - **๐Ÿฉบ SATUSEHAT Integration** - Integrasi dengan platform kesehatan SATUSEHAT - **๐Ÿ“– API Documentation** - Swagger/OpenAPI yang interaktif ### Developer Experience - **๐Ÿ”ฅ Hot Reload** - Development dengan auto-restart - **๐Ÿณ Docker Ready** - Deployment yang mudah - **โšก Code Generator** - Tools untuk generate handler dan model - **๐Ÿงช Testing Suite** - Unit dan integration tests - **๐Ÿ“Š Health Monitoring** - Monitoring kesehatan aplikasi - **๐Ÿ—„๏ธ Multi Database** - Support PostgreSQL, MySQL, dan MongoDB *** ## ๐Ÿ—๏ธ Arsitektur ### Clean Architecture Layers ``` โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Presentation Layer โ”‚ โ† handlers/, routes/ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ Application Layer โ”‚ โ† middleware/, services/ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ Domain Layer โ”‚ โ† models/, validation/ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ Infrastructure Layer โ”‚ โ† database/, external APIs โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` ### Struktur Project ``` api-service/ โ”œโ”€โ”€ ๐Ÿ“ cmd/ # Entry points aplikasi โ”‚ โ”œโ”€โ”€ api/main.go # API server โ”‚ โ””โ”€โ”€ logging/main.go # Logging service โ”œโ”€โ”€ ๐Ÿ“ internal/ # Core business logic โ”‚ โ”œโ”€โ”€ config/ # Configuration management โ”‚ โ”œโ”€โ”€ database/ # Database connections โ”‚ โ”œโ”€โ”€ handlers/ # HTTP controllers โ”‚ โ”‚ โ”œโ”€โ”€ auth/ # Authentication handlers โ”‚ โ”‚ โ”œโ”€โ”€ healthcheck/ # Health check handlers โ”‚ โ”‚ โ””โ”€โ”€ pasien/ # Pasien handlers โ”‚ โ”œโ”€โ”€ middleware/ # Auth & validation middleware โ”‚ โ”œโ”€โ”€ models/ # Data structures โ”‚ โ”‚ โ”œโ”€โ”€ auth/ # Auth models โ”‚ โ”‚ โ””โ”€โ”€ pasien/ # Pasien models โ”‚ โ”œโ”€โ”€ routes/ # API routing โ”‚ โ”œโ”€โ”€ services/ # Business logic services โ”‚ โ”‚ โ””โ”€โ”€ auth/ # Auth services โ”‚ โ”œโ”€โ”€ utils/ # Utility functions โ”‚ โ”‚ โ”œโ”€โ”€ filters/ # Dynamic filtering โ”‚ โ”‚ โ””โ”€โ”€ validation/ # Data validation โ”‚ โ””โ”€โ”€ server/ # HTTP server setup โ”œโ”€โ”€ ๐Ÿ“ docs/ # Documentation โ”œโ”€โ”€ ๐Ÿ“ examples/ # Example files โ”œโ”€โ”€ ๐Ÿ“ scripts/ # Automation scripts โ””โ”€โ”€ ๐Ÿ“ tools/ # Development tools ``` *** ## โšก Quick Start ### 1๏ธโƒฃ Setup Environment (2 menit) ```bash # Clone repository git clone cd api-service # Setup environment cp example.env .env ``` ### 2๏ธโƒฃ Pilih Method Setup **๐Ÿณ Docker (Recommended)** ```bash make docker-run ``` **๐Ÿ”ง Manual Setup** ```bash # Install dependencies go mod download # Start server go run cmd/api/main.go ``` ### 3๏ธโƒฃ Update Swagger Documentation ```bash swag init -g cmd/api/main.go -o docs/ ``` ### 4๏ธโƒฃ Verify Installation | Service | URL | Status | | :-- | :-- | :-- | | **API** | http://localhost:8080/api/v1 | โœ… | | **Swagger** | http://localhost:8080/swagger/index.html | ๐Ÿ“– | | **Health Check** | http://localhost:8080/api/sistem/health | ๐Ÿ’š | *** ## ๐Ÿ” Autentikasi ### Login & Mendapatkan Token ```bash curl -X POST http://localhost:8080/api/v1/auth/login \ -H "Content-Type: application/json" \ -d '{ "username": "admin", "password": "password" }' ``` **Response:** ```json { "access_token": "eyJhbGciOiJIUzI1NiIs...", "expires_in": 3600, "user": { "id": "123", "username": "admin", "role": "admin" } } ``` ### Menggunakan Token ```bash curl -X GET http://localhost:8080/api/v1/pasien \ -H "Authorization: Bearer " ``` ### Demo Accounts | Username | Password | Role | Akses | | :-- | :-- | :-- | :-- | | `admin` | `password` | Admin | Semua endpoint | | `dokter` | `password` | Dokter | Akses terbatas | | `perawat` | `password` | Perawat | Read-only | *** ## ๐Ÿ“Š API Endpoints ### ๐ŸŒ Public Endpoints | Method | Endpoint | Deskripsi | | :-- | :-- | :-- | | `POST` | `/api/v1/auth/login` | Login pengguna | | `POST` | `/api/v1/auth/register` | Registrasi pengguna baru | | `GET` | `/api/sistem/health` | Status kesehatan API | | `GET` | `/api/sistem/info` | Informasi sistem | ### ๐Ÿ”’ Protected Endpoints #### System Information | Method | Endpoint | Deskripsi | | :-- | :-- | :-- | | `GET` | `/api/sistem/databases` | Informasi database connections | #### Patient Management | Method | Endpoint | Deskripsi | | :-- | :-- | :-- | | `GET` | `/api/v1/pasien` | List semua pasien dengan pagination | | `GET` | `/api/v1/pasien/dynamic` | Query pasien dengan filter dinamis | | `GET` | `/api/v1/pasien/search` | Pencarian pasien advanced | | `GET` | `/api/v1/pasien/id/:id` | Detail pasien by ID | | `GET` | `/api/v1/pasien/nik/:nik` | Detail pasien by NIK | | `POST` | `/api/v1/pasien` | Buat pasien baru | | `PUT` | `/api/v1/pasien/id/:id` | Update data pasien | | `DELETE` | `/api/v1/pasien/id/:id` | Hapus data pasien (soft delete) | #### Medical Records | Method | Endpoint | Deskripsi | | :-- | :-- | :-- | | `GET` | `/api/v1/pasien/:id/medical-records` | Riwayat medis pasien | | `POST` | `/api/v1/pasien/:id/medical-records` | Tambah rekam medis | | `GET` | `/api/v1/pasien/:id/medical-records/:recordId` | Detail rekam medis | #### Dynamic Query Examples **Filter berdasarkan jenis kelamin:** ```bash GET /api/v1/pasien/dynamic?filter[jenis_kelamin][_eq]=L ``` **Kombinasi filter:** ```bash GET /api/v1/pasien/dynamic?filter[status][_eq]=active&filter[umur][_gt]=18 ``` **Pagination dan sorting:** ```bash GET /api/v1/pasien/dynamic?sort=-tanggal_daftar&limit=10&offset=20 ``` **Advanced search:** ```bash GET /api/v1/pasien/search?q=andi&limit=20&offset=0 ``` ### ๐Ÿฅ External Integrations #### BPJS Integration | Method | Endpoint | Deskripsi | | :-- | :-- | :-- | | `GET` | `/api/v1/bpjs/peserta/:no` | Data peserta BPJS | | `GET` | `/api/v1/bpjs/rujukan/:no` | Data rujukan | | `POST` | `/api/v1/bpjs/rujukan` | Buat rujukan baru | #### SATUSEHAT Integration | Method | Endpoint | Deskripsi | | :-- | :-- | :-- | | `GET` | `/api/v1/satusehat/patient/:id` | Data pasien SATUSEHAT | | `POST` | `/api/v1/satusehat/patient` | Buat pasien di SATUSEHAT | | `GET` | `/api/v1/satusehat/encounter/:id` | Data kunjungan | | `POST` | `/api/v1/satusehat/encounter` | Buat kunjungan baru | *** ## ๐Ÿ› ๏ธ Development ### Development Commands ```bash # ๐Ÿ”ฅ Development dengan hot reload make watch # ๐Ÿƒ Run server make run # ๐Ÿงช Testing make test # Unit tests make itest # Integration tests # ๐Ÿณ Docker operations make docker-run # Start all services make docker-down # Stop all services # ๐Ÿ” Code quality make build # Build application make clean # Clean build artifacts ``` ### Environment Configuration **๐Ÿ“ .env File:** ```bash # Server Configuration PORT=8080 GIN_MODE=debug # Database Configuration DB_CONNECTION=postgres DB_HOST=localhost DB_PORT=5432 DB_USERNAME=postgres DB_PASSWORD=postgres DB_DATABASE=api_service # PostgreSQL Satudata Database POSTGRES_SATUDATA_CONNECTION=postgres POSTGRES_SATUDATA_HOST=localhost POSTGRES_SATUDATA_PORT=5432 POSTGRES_SATUDATA_USERNAME=postgres POSTGRES_SATUDATA_PASSWORD=postgres POSTGRES_SATUDATA_DATABASE=satu_db # MongoDB Configuration MONGODB_MONGOHL7_CONNECTION=mongodb MONGODB_MONGOHL7_HOST=localhost MONGODB_MONGOHL7_PORT=27017 MONGODB_MONGOHL7_USER=admin MONGODB_MONGOHL7_PASS=password # MySQL Medical Database MYSQL_MEDICAL_CONNECTION=mysql MYSQL_MEDICAL_HOST=localhost MYSQL_MEDICAL_PORT=3306 MYSQL_MEDICAL_USERNAME=user MYSQL_MEDICAL_PASSWORD=password MYSQL_MEDICAL_DATABASE=healtcare_database # JWT Configuration JWT_SECRET=your-super-secret-key-change-in-production # Keycloak Configuration KEYCLOAK_ISSUER=https://auth.rssa.top/realms/sandbox KEYCLOAK_AUDIENCE=nuxtsim-pendaftaran KEYCLOAK_JWKS_URL=https://auth.rssa.top/realms/sandbox/protocol/openid-connect/certs KEYCLOAK_ENABLED=true # BPJS Configuration BPJS_BASEURL=https://apijkn.bpjs-kesehatan.go.id/vclaim-dev BPJS_CONSID=52667757 BPJS_USERKEY=4cf1cbef811314fvdgrc008440bbe9ef9ba789e482 BPJS_SECRETKEY=1bV36ASDQQ3512D # SATUSEHAT Configuration SATUSEHAT_BASEURL=https://api-satusehat.kemkes.go.id/fhir-r4/v1 SATUSEHAT_CLIENT_ID=your-client-id SATUSEHAT_CLIENT_SECRET=your-client-secret ``` ### Code Generation **Generate Handler untuk Pasien:** ```bash # Config go run tools/general/generate-handler.go --config tools/general/services-config.yaml --verbose ``` *** ## ๐Ÿš€ Deployment ### ๐Ÿณ Docker Deployment **Development:** ```bash # Start semua services make docker-run # Stop services make docker-down ``` **Production:** ```bash # Build production image docker build -t api-service:prod . # Run production container docker run -d \ --name api-service \ -p 8080:8080 \ --env-file .env.prod \ api-service:prod ``` ### ๐Ÿ”ง Manual Deployment ```bash # Build aplikasi make build # Start server ./main ``` ### ๐Ÿ“‹ Environment Variables untuk Production ```bash # Server Configuration APP_ENV=production PORT=8080 GIN_MODE=release # Database Configuration DB_CONNECTION=postgres DB_HOST=10.10.123.165 DB_PORT=5432 DB_USERNAME=stim DB_PASSWORD=stim*RS54 DB_DATABASE=satu_db # Security JWT_SECRET=your-production-secret-key KEYCLOAK_ENABLED=true # External Services BPJS_BASEURL=https://apijkn.bpjs-kesehatan.go.id/vclaim-rest SATUSEHAT_BASEURL=https://api-satusehat.kemkes.go.id/fhir-r4/v1 ``` *** ## ๐Ÿ“š Dokumentasi ### ๐Ÿ“– Interactive API Documentation Kunjungi **Swagger UI** di: http://localhost:8080/swagger/index.html **Cara menggunakan:** 1. ๐Ÿ”‘ Login melalui `/auth/login` endpoint 2. ๐Ÿ“‹ Copy token dari response 3. ๐Ÿ”“ Klik tombol "Authorize" di Swagger 4. ๐Ÿ“ Masukkan: `Bearer ` 5. โœ… Test semua endpoint yang tersedia ### ๐Ÿงช Testing Examples **JavaScript/Axios:** ```javascript // Login dan set token const auth = await axios.post('/api/v1/auth/login', { username: 'admin', password: 'password' }); axios.defaults.headers.common['Authorization'] = `Bearer ${auth.data.access_token}`; // Fetch patient data const patients = await axios.get('/api/v1/pasien'); console.log(patients.data); // Create new patient const newPatient = await axios.post('/api/v1/pasien', { nama: "John Doe", nik: "1234567890123456", tanggal_lahir: "1990-01-01", jenis_kelamin: "L", alamat: "Jl. Contoh No. 123" }); ``` **cURL Examples:** ```bash # Login TOKEN=$(curl -s -X POST http://localhost:8080/api/v1/auth/login \ -H "Content-Type: application/json" \ -d '{"username":"admin","password":"password"}' | jq -r '.access_token') # Get patient data curl -H "Authorization: Bearer $TOKEN" \ http://localhost:8080/api/v1/pasien # Dynamic filtering curl -H "Authorization: Bearer $TOKEN" \ "http://localhost:8080/api/v1/pasien/dynamic?filter[jenis_kelamin][_eq]=L" # Create new patient curl -X POST -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d '{ "nama": "Jane Doe", "nik": "6543210987654321", "tanggal_lahir": "1985-05-15", "jenis_kelamin": "P", "alamat": "Jl. Contoh No. 456" }' \ http://localhost:8080/api/v1/pasien ``` ### ๐Ÿ” Health Monitoring ```bash # Basic health check curl http://localhost:8080/api/sistem/health # Database status curl http://localhost:8080/api/sistem/databases # System info curl http://localhost:8080/api/sistem/info ``` **Response:** ```json { "status": "healthy", "timestamp": "2025-01-10T05:39:00Z", "services": { "database": "connected", "keycloak": "accessible", "bpjs_api": "accessible", "satusehat_api": "accessible" }, "version": "1.0.0" } ``` *** ## ๐Ÿšจ Troubleshooting ### Masalah Umum **โŒ Database Connection Error** ```bash # Cek status database make docker-run # Check logs docker logs api-service # Verify environment variables cat .env | grep DB_ ``` **โŒ Authentication Error** - ๐Ÿ”„ Pastikan Keycloak service berjalan - โœ… Cek KEYCLOAK_ISSUER URL - ๐Ÿ“ Format token harus: `Bearer ` **โŒ Dynamic Filter Error** - โœ… Pastikan field name sesuai dengan database - ๐Ÿ” Cek syntax filter: `filter[field][operator]=value` - ๐Ÿ“ Operator yang didukung: `_eq`, `_neq`, `_gt`, `_lt`, `_contains` **โŒ BPJS/SATUSEHAT Integration Error** - โœ… Verifikasi kredensial API - ๐Ÿ”„ Cek koneksi ke layanan eksternal - ๐Ÿ“ Pastikan format data sesuai dengan spesifikasi ### Debug Mode ```bash # Enable debug logging export GIN_MODE=debug # Run dengan verbose output make run # Monitor dengan hot reload make watch ``` *** ## ๐ŸŽฏ Next Steps ### ๐Ÿ“‹ Development Roadmap - [x] Setup environment selesai - [x] Implementasi patient management - [x] Setup authentication dengan Keycloak - [ ] Integrasi BPJS dan SATUSEHAT