package handlers import ( "context" "fmt" "net/http" "time" "api-service/internal/config" services "api-service/internal/services/bpjs" "github.com/gin-gonic/gin" ) // DiagnosaHandler handles BPJS diagnosa operations type DiagnosaHandler struct { bpjsService services.VClaimService } // NewDiagnosaHandler creates a new DiagnosaHandler instance func NewDiagnosaHandler(cfg config.BpjsConfig) *DiagnosaHandler { return &DiagnosaHandler{ bpjsService: services.NewService(cfg), } } // GetAll godoc // @Summary Get all diagnosa reference data // @Description Get all diagnosa reference data // @Tags bpjs/reference // @Accept json // @Produce json // @Success 200 {object} models.DiagnosaResponse "Success response" // @Failure 400 {object} map[string]interface{} "Bad request" // @Failure 404 {object} map[string]interface{} "Data not found" // @Failure 500 {object} map[string]interface{} "Internal server error" // @Router /api/v1/bpjs/reference/referensi/diagnosa [get] func (h *DiagnosaHandler) GetAll(c *gin.Context) { // Create context with timeout ctx, cancel := context.WithTimeout(c.Request.Context(), 30*time.Second) defer cancel() // Build endpoint URL endpoint := "/referensi/diagnosa" // Call BPJS service var result map[string]interface{} if err := h.bpjsService.Get(ctx, endpoint, &result); err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "error": "Failed to fetch diagnosa data", "message": err.Error(), }) return } // Return successful response c.JSON(http.StatusOK, gin.H{ "message": "Data diagnosa berhasil diambil", "data": result, }) } // Helper methods for error handling and response formatting // handleBPJSError handles BPJS service errors and returns appropriate HTTP responses func (h *DiagnosaHandler) handleBPJSError(c *gin.Context, err error, operation string) { c.JSON(http.StatusInternalServerError, gin.H{ "error": fmt.Sprintf("Failed to %s", operation), "message": err.Error(), }) } // validateDateFormat validates if the date string is in yyyy-MM-dd format func (h *DiagnosaHandler) validateDateFormat(dateStr string) error { _, err := time.Parse("2006-01-02", dateStr) return err } // buildSuccessResponse builds a standardized success response func (h *DiagnosaHandler) buildSuccessResponse(message string, data interface{}) gin.H { return gin.H{ "message": message, "data": data, } }