update sync
This commit is contained in:
@@ -181,7 +181,6 @@ func (c *BpjsClient) BacaKamar(ctx context.Context, start, limit int) ([]map[str
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// PostKamar upsert kamar ke BPJS — coba update dulu, kalau gagal baru create
|
||||
func (c *BpjsClient) PostKamar(ctx context.Context, bed BedData) error {
|
||||
payload := map[string]interface{}{
|
||||
"kodekelas": bed.KodeKelas,
|
||||
@@ -195,17 +194,36 @@ func (c *BpjsClient) PostKamar(ctx context.Context, bed BedData) error {
|
||||
}
|
||||
|
||||
// Coba update dulu
|
||||
code, _, err := c.post(ctx, fmt.Sprintf("aplicaresws/rest/bed/update/%s", c.kodePPK), payload)
|
||||
code, updateMsg, err := c.post(ctx, fmt.Sprintf("aplicaresws/rest/bed/update/%s", c.kodePPK), payload)
|
||||
if err == nil && code == 1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Log kenapa update gagal
|
||||
if err != nil {
|
||||
fmt.Printf(" [DEBUG] update %s gagal: err=%v\n", bed.KodeRuang, err)
|
||||
} else {
|
||||
fmt.Printf(" [DEBUG] update %s gagal: code=%d msg=%s\n", bed.KodeRuang, code, updateMsg)
|
||||
}
|
||||
|
||||
// Fallback ke create
|
||||
code, msg, err := c.post(ctx, fmt.Sprintf("aplicaresws/rest/bed/create/%s", c.kodePPK), payload)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create kamar %s gagal: %w", bed.KodeRuang, err)
|
||||
}
|
||||
if code != 1 {
|
||||
// Kalau "sudah ada" → coba update sekali lagi
|
||||
if strings.Contains(strings.ToLower(msg), "sudah ada") {
|
||||
fmt.Printf(" [DEBUG] create %s sudah ada, coba update lagi\n", bed.KodeRuang)
|
||||
code, msg, err = c.post(ctx, fmt.Sprintf("aplicaresws/rest/bed/update/%s", c.kodePPK), payload)
|
||||
if err != nil {
|
||||
return fmt.Errorf("update ulang %s gagal: %w", bed.KodeRuang, err)
|
||||
}
|
||||
if code != 1 {
|
||||
return fmt.Errorf("update ulang %s: %s", bed.KodeRuang, msg)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("create kamar %s: %s", bed.KodeRuang, msg)
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user