first commit
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
package kfa
|
||||
|
||||
// KFASearchParams menampung parameter pencarian produk KFA
|
||||
type KFASearchParams struct {
|
||||
Page int `form:"page,default=1"`
|
||||
Size int `form:"size,default=10"`
|
||||
ProductType string `form:"product_type"` // Contoh: farmasi, alkes
|
||||
Keyword string `form:"keyword"` // Kata kunci pencarian produk (jika didukung KFA)
|
||||
From string `form:"from_"` // Query param from_ (digunakan untuk memfilter tanggal/waktu spesifik)
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package kfa
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"service/internal/interfaces/satusehat"
|
||||
"service/pkg/errors"
|
||||
"service/pkg/logger"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
GetByCode(ctx context.Context, code string) (map[string]interface{}, error)
|
||||
GetProducts(ctx context.Context, params KFASearchParams) (map[string]interface{}, error)
|
||||
}
|
||||
|
||||
type repository struct {
|
||||
client satusehat.SatuSehatClient
|
||||
}
|
||||
|
||||
func NewRepository(client satusehat.SatuSehatClient) Repository {
|
||||
return &repository{client: client}
|
||||
}
|
||||
|
||||
func parseResponse(body []byte) (map[string]interface{}, error) {
|
||||
var res map[string]interface{}
|
||||
if err := json.Unmarshal(body, &res); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse KFA response: %w", err)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (r *repository) GetByCode(ctx context.Context, code string) (map[string]interface{}, error) {
|
||||
endpoint := fmt.Sprintf("/products?identifier=kfa&code=%s", url.QueryEscape(code))
|
||||
respBytes, err := r.client.DoKFA(ctx, "GET", endpoint, nil)
|
||||
if err != nil {
|
||||
logger.Default().Error("Gagal mencari produk KFA berdasarkan kode", logger.ErrorField(err))
|
||||
return nil, errors.InternalError().Message("Gagal mencari produk KFA").Cause(err).Build()
|
||||
}
|
||||
return parseResponse(respBytes)
|
||||
}
|
||||
|
||||
func (r *repository) GetProducts(ctx context.Context, params KFASearchParams) (map[string]interface{}, error) {
|
||||
q := url.Values{}
|
||||
q.Add("page", strconv.Itoa(params.Page))
|
||||
q.Add("size", strconv.Itoa(params.Size))
|
||||
if params.ProductType != "" {
|
||||
q.Add("product_type", params.ProductType)
|
||||
}
|
||||
if params.Keyword != "" {
|
||||
q.Add("keyword", params.Keyword)
|
||||
}
|
||||
if params.From != "" {
|
||||
q.Add("from_", params.From)
|
||||
}
|
||||
|
||||
endpoint := "/products/all?" + q.Encode()
|
||||
respBytes, err := r.client.DoKFA(ctx, "GET", endpoint, nil)
|
||||
if err != nil {
|
||||
logger.Default().Error("Gagal mencari daftar produk KFA", logger.ErrorField(err))
|
||||
return nil, errors.InternalError().Message("Gagal mengambil daftar produk KFA").Cause(err).Build()
|
||||
}
|
||||
return parseResponse(respBytes)
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package kfa
|
||||
|
||||
import "context"
|
||||
|
||||
type Service interface {
|
||||
GetByCode(ctx context.Context, code string) (map[string]interface{}, error)
|
||||
GetProducts(ctx context.Context, params KFASearchParams) (map[string]interface{}, error)
|
||||
}
|
||||
|
||||
type service struct {
|
||||
repo Repository
|
||||
}
|
||||
|
||||
func NewService(repo Repository) Service {
|
||||
return &service{repo: repo}
|
||||
}
|
||||
|
||||
func (s *service) GetByCode(ctx context.Context, code string) (map[string]interface{}, error) {
|
||||
return s.repo.GetByCode(ctx, code)
|
||||
}
|
||||
|
||||
func (s *service) GetProducts(ctx context.Context, params KFASearchParams) (map[string]interface{}, error) {
|
||||
return s.repo.GetProducts(ctx, params)
|
||||
}
|
||||
Reference in New Issue
Block a user