feat: add cooperation tariff APIs

This commit is contained in:
2026-08-22 10:14:12 +07:00
parent b9eb934364
commit d183e321e8
7 changed files with 798 additions and 1 deletions

View File

@@ -34,12 +34,18 @@ type PaketRepository interface {
GetPaket(ctx context.Context, id string) (*repository.PaketDetail, error)
}
type TarifKerjasamaRepository interface {
ListTarifKerjasama(ctx context.Context, params repository.TarifKerjasamaListParams) (repository.TarifKerjasamaPage, error)
GetTarifKerjasama(ctx context.Context, id int64, params repository.TarifKerjasamaDetailParams) (*repository.TarifKerjasamaDetail, error)
}
type Handler struct {
layananRepo LayananRepository
labRepo LabRegistrationRepository
patientRepo PatientRepository
labResultRepo LabResultRepository
paketRepo PaketRepository
tarifRepo TarifKerjasamaRepository
}
type CPONERepository interface {
@@ -48,12 +54,13 @@ type CPONERepository interface {
PatientRepository
LabResultRepository
PaketRepository
TarifKerjasamaRepository
}
func NewHandler(repo CPONERepository) Handler {
return Handler{
layananRepo: repo, labRepo: repo, patientRepo: repo,
labResultRepo: repo, paketRepo: repo,
labResultRepo: repo, paketRepo: repo, tarifRepo: repo,
}
}

View File

@@ -0,0 +1,179 @@
package http
import (
"net/http"
"strconv"
"strings"
"time"
"unicode/utf8"
"primaya-api/cpone-middleware/internal/repository"
)
type TarifKerjasamaPaginationResponse struct {
CurrentPage int `json:"current_page"`
Data []repository.TarifKerjasamaSummary `json:"data"`
FirstPageURL string `json:"first_page_url"`
From *int `json:"from"`
LastPage int `json:"last_page"`
LastPageURL string `json:"last_page_url"`
Links map[string]any `json:"links"`
NextPageURL *string `json:"next_page_url"`
Total int `json:"total"`
PerPage int `json:"per_page"`
}
type TarifKerjasamaResponse struct {
Success bool `json:"success"`
Status string `json:"status"`
Message string `json:"message"`
Data any `json:"data"`
}
func (h Handler) ListTarifKerjasama(w http.ResponseWriter, r *http.Request) {
params, message := parseTarifKerjasamaListParams(r)
if message != "" {
writeTarifKerjasamaValidationError(w, message)
return
}
page, err := h.tarifRepo.ListTarifKerjasama(r.Context(), params)
if err != nil {
writeTarifKerjasamaInternalError(w)
return
}
lastPage := (page.Total + params.PerPage - 1) / params.PerPage
if lastPage < 1 {
lastPage = 1
}
var from *int
if len(page.Items) > 0 {
value := (params.Page-1)*params.PerPage + 1
from = &value
}
var nextPageURL *string
if params.Page < lastPage {
value := currentRequestURL(r, params.Page+1)
nextPageURL = &value
}
WriteJSON(w, http.StatusOK, TarifKerjasamaPaginationResponse{
CurrentPage: params.Page, Data: page.Items, FirstPageURL: "", From: from,
LastPage: lastPage, LastPageURL: "", Links: map[string]any{},
NextPageURL: nextPageURL, Total: page.Total, PerPage: params.PerPage,
})
}
func (h Handler) GetTarifKerjasama(w http.ResponseWriter, r *http.Request) {
id, params, message := parseTarifKerjasamaDetailParams(r)
if message != "" {
writeTarifKerjasamaValidationError(w, message)
return
}
detail, err := h.tarifRepo.GetTarifKerjasama(r.Context(), id, params)
if err != nil {
writeTarifKerjasamaInternalError(w)
return
}
if detail == nil {
WriteJSON(w, http.StatusNotFound, TarifKerjasamaResponse{
Success: false, Status: "tarif_kerjasama_not_found",
Message: "Master tarif kerja sama tidak ditemukan", Data: nil,
})
return
}
WriteJSON(w, http.StatusOK, TarifKerjasamaResponse{
Success: true, Status: "tarif_kerjasama_found",
Message: "Data tarif kerja sama berhasil diambil", Data: detail,
})
}
func parseTarifKerjasamaListParams(r *http.Request) (repository.TarifKerjasamaListParams, string) {
query := r.URL.Query()
params := repository.TarifKerjasamaListParams{
Search: strings.TrimSpace(query.Get("search")),
PerusahaanID: strings.TrimSpace(query.Get("perusahaan_id")),
Page: 1, PerPage: 15,
}
if utf8.RuneCountInString(params.Search) > 255 {
return params, "The search may not be greater than 255 characters."
}
if utf8.RuneCountInString(params.PerusahaanID) > 50 {
return params, "The perusahaan id may not be greater than 50 characters."
}
if raw, exists := firstQueryValue(r, "tarif_id"); exists && raw != "" {
value, err := positiveInt64(raw)
if err != nil {
return params, "The tarif id must be a positive integer."
}
params.TarifID = &value
}
if raw, exists := firstQueryValue(r, "page"); exists && raw != "" {
value, err := strconv.Atoi(raw)
if err != nil || value < 1 {
return params, "The page must be at least 1."
}
params.Page = value
}
if raw, exists := firstQueryValue(r, "per_page"); exists && raw != "" {
value, err := strconv.Atoi(raw)
if err != nil || value < 1 || value > 100 {
return params, "The per page must be between 1 and 100."
}
params.PerPage = value
}
return params, ""
}
func parseTarifKerjasamaDetailParams(r *http.Request) (int64, repository.TarifKerjasamaDetailParams, string) {
params := repository.TarifKerjasamaDetailParams{Active: "N"}
id, err := positiveInt64(strings.TrimSpace(r.PathValue("id")))
if err != nil {
return 0, params, "The id must be a positive integer."
}
if raw, exists := firstQueryValue(r, "group_tarif_id"); exists && raw != "" {
value, err := positiveInt64(raw)
if err != nil {
return 0, params, "The group tarif id must be a positive integer."
}
params.GroupTarifID = &value
}
if raw, exists := firstQueryValue(r, "active"); exists && raw != "" {
if raw != "N" && raw != "Y" {
return 0, params, "The selected active is invalid."
}
params.Active = raw
}
params.EffectiveDate = strings.TrimSpace(r.URL.Query().Get("effective_date"))
if params.EffectiveDate != "" {
if _, err := time.Parse("2006-01-02", params.EffectiveDate); err != nil {
return 0, params, "The effective date does not match the format Y-m-d."
}
}
return id, params, ""
}
func positiveInt64(raw string) (int64, error) {
value, err := strconv.ParseInt(strings.TrimSpace(raw), 10, 64)
if err != nil || value < 1 {
return 0, strconv.ErrSyntax
}
return value, nil
}
func writeTarifKerjasamaValidationError(w http.ResponseWriter, message string) {
WriteJSON(w, http.StatusUnprocessableEntity, TarifKerjasamaResponse{
Success: false, Status: "validation_error", Message: message, Data: nil,
})
}
func writeTarifKerjasamaInternalError(w http.ResponseWriter) {
WriteJSON(w, http.StatusInternalServerError, Response{
Success: false, Message: "internal server error", Data: nil,
})
}

View File

@@ -0,0 +1,146 @@
package http
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"primaya-api/cpone-middleware/internal/repository"
)
type fakeTarifKerjasamaRepository struct {
page repository.TarifKerjasamaPage
detail *repository.TarifKerjasamaDetail
err error
listParams repository.TarifKerjasamaListParams
detailParams repository.TarifKerjasamaDetailParams
detailID int64
}
func (f *fakeTarifKerjasamaRepository) ListTarifKerjasama(_ context.Context, params repository.TarifKerjasamaListParams) (repository.TarifKerjasamaPage, error) {
f.listParams = params
return f.page, f.err
}
func (f *fakeTarifKerjasamaRepository) GetTarifKerjasama(_ context.Context, id int64, params repository.TarifKerjasamaDetailParams) (*repository.TarifKerjasamaDetail, error) {
f.detailID = id
f.detailParams = params
return f.detail, f.err
}
func TestListTarifKerjasamaResponseAndFilters(t *testing.T) {
kode, pic := "INS-001", "Bapak A"
repo := &fakeTarifKerjasamaRepository{page: repository.TarifKerjasamaPage{
Total: 16,
Items: []repository.TarifKerjasamaSummary{{
PerusahaanTarifID: 1098, PerusahaanID: "COMP-001", KodePerusahaan: &kode,
NamaPerusahaan: "24 Medicare", PIC: &pic, JumlahGroupTarif: 2,
}},
}}
handler := Handler{tarifRepo: repo}
request := httptest.NewRequest(http.MethodGet, "http://localhost/api/cpone/tarif-kerjasama?search=medicare&perusahaan_id=COMP-001&tarif_id=1098&page=1&per_page=15", nil)
recorder := httptest.NewRecorder()
handler.ListTarifKerjasama(recorder, request)
if recorder.Code != http.StatusOK {
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
}
if repo.listParams.Search != "medicare" || repo.listParams.PerusahaanID != "COMP-001" || repo.listParams.TarifID == nil || *repo.listParams.TarifID != 1098 {
t.Fatalf("params mismatch: %+v", repo.listParams)
}
for _, expected := range []string{
`"PerusahaanTarifID":1098`, `"NamaPerusahaan":"24 Medicare"`,
`"JumlahGroupTarif":2`, `"last_page":2`,
} {
if !strings.Contains(recorder.Body.String(), expected) {
t.Fatalf("response does not contain %s: %s", expected, recorder.Body.String())
}
}
}
func TestGetTarifKerjasamaNestedResponse(t *testing.T) {
layananID, namaLayanan := "LAB-001", "Darah Lengkap"
kelasID, namaKelas := "KLS-1", "Kelas I"
detail := &repository.TarifKerjasamaDetail{
TarifKerjasamaSummary: repository.TarifKerjasamaSummary{
PerusahaanTarifID: 1098, PerusahaanID: "COMP-001",
NamaPerusahaan: "24 Medicare", JumlahGroupTarif: 1,
},
GroupTarif: []repository.TarifKerjasamaGroup{{
GroupTarifID: 501, PerusahaanTarifID: 1098,
NamaGroupTarif: "Tarif Corporate 2026", NA: "N", Aktif: true,
JumlahLayanan: 1,
Layanan: []repository.TarifKerjasamaLayanan{{
LayananID: &layananID, NamaLayanan: &namaLayanan, JumlahDetail: 1,
Detail: []repository.TarifKerjasamaLayananDetail{{
TarifLayananID: 1, KelasID: &kelasID, NamaKelas: &namaKelas, Harga: 125000,
}},
}},
}},
}
repo := &fakeTarifKerjasamaRepository{detail: detail}
handler := Handler{tarifRepo: repo}
request := httptest.NewRequest(http.MethodGet, "/api/cpone/tarif-kerjasama/1098?group_tarif_id=501&effective_date=2026-08-22", nil)
request.SetPathValue("id", "1098")
recorder := httptest.NewRecorder()
handler.GetTarifKerjasama(recorder, request)
if recorder.Code != http.StatusOK || repo.detailID != 1098 {
t.Fatalf("status=%d id=%d body=%s", recorder.Code, repo.detailID, recorder.Body.String())
}
if repo.detailParams.GroupTarifID == nil || *repo.detailParams.GroupTarifID != 501 || repo.detailParams.Active != "N" || repo.detailParams.EffectiveDate != "2026-08-22" {
t.Fatalf("params mismatch: %+v", repo.detailParams)
}
for _, expected := range []string{
`"status":"tarif_kerjasama_found"`, `"GroupTarifID":501`,
`"LayananID":"LAB-001"`, `"Harga":125000`,
} {
if !strings.Contains(recorder.Body.String(), expected) {
t.Fatalf("response does not contain %s: %s", expected, recorder.Body.String())
}
}
}
func TestTarifKerjasamaValidationAndNotFound(t *testing.T) {
tests := []struct {
path string
id string
}{
{"/api/cpone/tarif-kerjasama/abc", "abc"},
{"/api/cpone/tarif-kerjasama/1?group_tarif_id=0", "1"},
{"/api/cpone/tarif-kerjasama/1?active=X", "1"},
{"/api/cpone/tarif-kerjasama/1?effective_date=22-08-2026", "1"},
}
for _, test := range tests {
request := httptest.NewRequest(http.MethodGet, test.path, nil)
request.SetPathValue("id", test.id)
recorder := httptest.NewRecorder()
Handler{tarifRepo: &fakeTarifKerjasamaRepository{}}.GetTarifKerjasama(recorder, request)
if recorder.Code != http.StatusUnprocessableEntity || !strings.Contains(recorder.Body.String(), `"status":"validation_error"`) {
t.Fatalf("path=%s status=%d body=%s", test.path, recorder.Code, recorder.Body.String())
}
}
request := httptest.NewRequest(http.MethodGet, "/api/cpone/tarif-kerjasama/9999", nil)
request.SetPathValue("id", "9999")
recorder := httptest.NewRecorder()
Handler{tarifRepo: &fakeTarifKerjasamaRepository{}}.GetTarifKerjasama(recorder, request)
if recorder.Code != http.StatusNotFound || !strings.Contains(recorder.Body.String(), `"status":"tarif_kerjasama_not_found"`) {
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
}
}
func TestTarifKerjasamaRepositoryErrorsAreHidden(t *testing.T) {
handler := Handler{tarifRepo: &fakeTarifKerjasamaRepository{err: errors.New("database unavailable")}}
request := httptest.NewRequest(http.MethodGet, "/api/cpone/tarif-kerjasama", nil)
recorder := httptest.NewRecorder()
handler.ListTarifKerjasama(recorder, request)
if recorder.Code != http.StatusInternalServerError || recorder.Body.String() != `{"success":false,"message":"internal server error","data":null}`+"\n" {
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
}
}