Perubahan tool dan dokumentasi
This commit is contained in:
192
internal/handlers/satusehat/patient_handler.go
Normal file
192
internal/handlers/satusehat/patient_handler.go
Normal file
@@ -0,0 +1,192 @@
|
||||
package satusehat
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"api-service/internal/services/satusehat"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type PatientHandler struct {
|
||||
service *satusehat.SatuSehatService
|
||||
}
|
||||
|
||||
func NewPatientHandler(service *satusehat.SatuSehatService) *PatientHandler {
|
||||
return &PatientHandler{
|
||||
service: service,
|
||||
}
|
||||
}
|
||||
|
||||
// SearchPatientByNIK godoc
|
||||
// @Summary Search patient by NIK
|
||||
// @Description Search patient data from SatuSehat by National Identity Number (NIK)
|
||||
// @Tags SatuSehat
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param nik query string true "National Identity Number (NIK)"
|
||||
// @Success 200 {object} map[string]interface{}
|
||||
// @Failure 400 {object} map[string]interface{}
|
||||
// @Failure 500 {object} map[string]interface{}
|
||||
// @Router /satusehat/patient/search/nik [get]
|
||||
func (h *PatientHandler) SearchPatientByNIK(c *gin.Context) {
|
||||
nik := c.Query("nik")
|
||||
if nik == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "Bad Request",
|
||||
"message": "NIK parameter is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
patientResp, err := h.service.SearchPatientByNIK(c.Request.Context(), nik)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": "Internal Server Error",
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
patientInfo, err := satusehat.ExtractPatientInfo(patientResp)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"error": "Not Found",
|
||||
"message": "Patient not found",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": patientInfo,
|
||||
})
|
||||
}
|
||||
|
||||
// SearchPatientByName godoc
|
||||
// @Summary Search patient by name
|
||||
// @Description Search patient data from SatuSehat by name
|
||||
// @Tags SatuSehat
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param name query string true "Patient name"
|
||||
// @Success 200 {object} map[string]interface{}
|
||||
// @Failure 400 {object} map[string]interface{}
|
||||
// @Failure 500 {object} map[string]interface{}
|
||||
// @Router /satusehat/patient/search/name [get]
|
||||
func (h *PatientHandler) SearchPatientByName(c *gin.Context) {
|
||||
name := c.Query("name")
|
||||
if name == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "Bad Request",
|
||||
"message": "Name parameter is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
patientResp, err := h.service.SearchPatientByName(c.Request.Context(), name)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": "Internal Server Error",
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if patientResp == nil || len(patientResp.Entry) == 0 {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"error": "Not Found",
|
||||
"message": "Patient not found",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Return all found patients
|
||||
var patients []map[string]interface{}
|
||||
for _, entry := range patientResp.Entry {
|
||||
patientInfo := map[string]interface{}{
|
||||
"id": entry.Resource.ID,
|
||||
"name": satusehat.ExtractPatientName(entry.Resource.Name),
|
||||
"nik": satusehat.ExtractNIK(entry.Resource.Identifier),
|
||||
"gender": entry.Resource.Gender,
|
||||
"birthDate": entry.Resource.BirthDate,
|
||||
"address": satusehat.ExtractAddress(entry.Resource.Address),
|
||||
"phone": satusehat.ExtractPhone(entry.Resource.Telecom),
|
||||
"lastUpdated": entry.Resource.Meta.LastUpdated,
|
||||
}
|
||||
patients = append(patients, patientInfo)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": patients,
|
||||
"total": len(patients),
|
||||
})
|
||||
}
|
||||
|
||||
// CreatePatient godoc
|
||||
// @Summary Create new patient
|
||||
// @Description Create new patient data in SatuSehat
|
||||
// @Tags SatuSehat
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param patient body map[string]interface{} true "Patient data"
|
||||
// @Success 201 {object} map[string]interface{}
|
||||
// @Failure 400 {object} map[string]interface{}
|
||||
// @Failure 500 {object} map[string]interface{}
|
||||
// @Router /satusehat/patient [post]
|
||||
func (h *PatientHandler) CreatePatient(c *gin.Context) {
|
||||
var patientData map[string]interface{}
|
||||
if err := c.ShouldBindJSON(&patientData); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "Bad Request",
|
||||
"message": "Invalid JSON format",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
response, err := h.service.CreatePatient(c.Request.Context(), patientData)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": "Internal Server Error",
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, gin.H{
|
||||
"success": true,
|
||||
"data": response,
|
||||
})
|
||||
}
|
||||
|
||||
// GetAccessToken godoc
|
||||
// @Summary Get access token
|
||||
// @Description Get SatuSehat access token
|
||||
// @Tags SatuSehat
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} map[string]interface{}
|
||||
// @Failure 500 {object} map[string]interface{}
|
||||
// @Router /satusehat/token [get]
|
||||
func (h *PatientHandler) GetAccessToken(c *gin.Context) {
|
||||
token, err := h.service.GetAccessToken(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": "Internal Server Error",
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": map[string]interface{}{
|
||||
"access_token": token.AccessToken,
|
||||
"token_type": token.TokenType,
|
||||
"expires_in": token.ExpiresIn,
|
||||
"scope": token.Scope,
|
||||
"issued_at": token.IssuedAt,
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user