diff --git a/cmd/main-api/config.yml-example b/cmd/main-api/config.yml-example index 3f5caa33..5915c46c 100644 --- a/cmd/main-api/config.yml-example +++ b/cmd/main-api/config.yml-example @@ -72,7 +72,8 @@ syncUrlCfg: enable: false targetHost: prefix: new-to-old - source: new-app + oldSource: new-app + newSource: new-app secretKey: new-world-order!! docsCfg: diff --git a/internal/infra/sync-cfg/tycovar.go b/internal/infra/sync-cfg/tycovar.go index ebc3ca19..a7534f82 100644 --- a/internal/infra/sync-cfg/tycovar.go +++ b/internal/infra/sync-cfg/tycovar.go @@ -1,9 +1,9 @@ package synccfg -var O PartnerCfg = PartnerCfg{} // old +var O SyncPartnerCfg = SyncPartnerCfg{} // old // Used by sync itself, so any partner should be stated here -type PartnerCfg struct { +type SyncPartnerCfg struct { OldSource string `yaml:"oldSource"` OldSecretKey string `yaml:"oldSecretKey"` OldHost string `yaml:"oldHost"` diff --git a/internal/infra/sync-consumer-cfg/tycovar.go b/internal/infra/sync-consumer-cfg/tycovar.go index 1a5cba26..f60cc588 100644 --- a/internal/infra/sync-consumer-cfg/tycovar.go +++ b/internal/infra/sync-consumer-cfg/tycovar.go @@ -1,11 +1,12 @@ package synccfg -var O SyncUrlCfg = SyncUrlCfg{} // old +var O SyncUrlCfg = SyncUrlCfg{} // new type SyncUrlCfg struct { Prefix string `yaml:"prefix"` TargetHost string `yaml:"targetHost"` Enable bool `yaml:"enable"` - Source string `yaml:"source"` + OldSource string `yaml:"oldSource"` + NewSource string `yaml:"newSource"` SecretKey string `yaml:"secretKey"` } diff --git a/internal/interface/main-handler/authentication/handler.go b/internal/interface/main-handler/authentication/handler.go index f0625af7..ffaf145b 100644 --- a/internal/interface/main-handler/authentication/handler.go +++ b/internal/interface/main-handler/authentication/handler.go @@ -9,12 +9,13 @@ import ( sp "github.com/karincake/semprit" sr "github.com/karincake/serabi" + is "simrs-vx/internal/infra/sync-consumer-cfg" + pa "simrs-vx/internal/lib/auth" + m "simrs-vx/internal/domain/main-entities/user" mf "simrs-vx/internal/domain/main-entities/user-fes" - pa "simrs-vx/internal/lib/auth" - s "simrs-vx/internal/use-case/main-use-case/authentication" - esga "simrs-vx/internal/domain/sync-entities/authentication" + s "simrs-vx/internal/use-case/main-use-case/authentication" ) func Login(w http.ResponseWriter, r *http.Request) { @@ -68,6 +69,10 @@ func Logout(w http.ResponseWriter, r *http.Request) { } func GuardMW(next http.Handler) http.Handler { + var ( + accessDetail *pa.AuthInfo + err error + ) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Check if it's from sync credential := esga.CredentialDto{} @@ -75,23 +80,26 @@ func GuardMW(next http.Handler) http.Handler { credential.SecretKey = r.Header.Get("X-Sync-SecretKey") credential.UserName = r.Header.Get("X-Sync-UserName") if credential.Source != "" || credential.SecretKey != "" || credential.UserName != "" { - // TODO: ngecall fungsi untuk dapat dari DB menlengkapi authinfo - accessDetail, err := s.GetAuthInfoByUserName(credential.UserName) + // validate secretKey and source + if credential.SecretKey != is.O.SecretKey || credential.Source != is.O.OldSource { + rw.WriteJSON(w, http.StatusUnauthorized, d.IS{"message": "invalid consumer credential"}, nil) + return + } + + accessDetail, err = s.GetAuthInfoByUserName(credential.UserName) + if err != nil { + rw.WriteJSON(w, http.StatusUnauthorized, err.(d.FieldError), nil) + return + } + } else { + // Normal flow goes here + accessDetail, err = s.ExtractToken(r, s.AccessToken) if err != nil { rw.WriteJSON(w, http.StatusUnauthorized, err.(d.FieldError), nil) return } - ctx := context.WithValue(r.Context(), pa.AuthKey{}, accessDetail) - next.ServeHTTP(w, r.WithContext(ctx)) - return } - // Normal flow goes here - accessDetail, err := s.ExtractToken(r, s.AccessToken) - if err != nil { - rw.WriteJSON(w, http.StatusUnauthorized, err.(d.FieldError), nil) - return - } ctx := context.WithValue(r.Context(), pa.AuthKey{}, accessDetail) next.ServeHTTP(w, r.WithContext(ctx)) }) diff --git a/internal/interface/simgos-sync-handler/simgos-sync-handler.go b/internal/interface/simgos-sync-handler/simgos-sync-handler.go index 81b1c48b..f120b1cb 100644 --- a/internal/interface/simgos-sync-handler/simgos-sync-handler.go +++ b/internal/interface/simgos-sync-handler/simgos-sync-handler.go @@ -6,7 +6,7 @@ import ( /******************** infra ********************/ gs "simrs-vx/internal/infra/gorm-setting" simgosdb "simrs-vx/internal/infra/simgos-db" - sync "simrs-vx/internal/infra/sync-consumer-cfg" + sync "simrs-vx/internal/infra/sync-cfg" /******************** pkg ********************/ cmw "simrs-vx/pkg/cors-manager-mw" diff --git a/internal/use-case/main-use-case/authentication/case.go b/internal/use-case/main-use-case/authentication/case.go index 2f47c43d..04a88c9b 100644 --- a/internal/use-case/main-use-case/authentication/case.go +++ b/internal/use-case/main-use-case/authentication/case.go @@ -180,9 +180,7 @@ func VerifyToken(r *http.Request, tokenType TokenType) (data *jwt.Token, errCode } func GetAuthInfoByUserName(userName string) (data *pa.AuthInfo, err error) { - // disini isi var `data` - // return error jika terjadi apa2 - return + return getAuthByUserName(userName) } func ExtractToken(r *http.Request, tokenType TokenType) (data *pa.AuthInfo, err error) { diff --git a/internal/use-case/main-use-case/authentication/helper.go b/internal/use-case/main-use-case/authentication/helper.go index 7af3afa5..d8c54751 100644 --- a/internal/use-case/main-use-case/authentication/helper.go +++ b/internal/use-case/main-use-case/authentication/helper.go @@ -2,6 +2,7 @@ package authentication import ( "fmt" + pa "simrs-vx/internal/lib/auth" "strconv" "time" @@ -10,10 +11,13 @@ import ( dg "github.com/karincake/apem/db-gorm-pg" ms "github.com/karincake/apem/ms-redis" d "github.com/karincake/dodol" + gh "github.com/karincake/getuk" l "github.com/karincake/lepet" pl "simrs-vx/pkg/logger" + erg "simrs-vx/internal/domain/references/organization" + edp "simrs-vx/internal/domain/main-entities/division-position" ed "simrs-vx/internal/domain/main-entities/doctor" ee "simrs-vx/internal/domain/main-entities/employee" @@ -26,7 +30,6 @@ import ( essp "simrs-vx/internal/domain/main-entities/subspecialist-position" eup "simrs-vx/internal/domain/main-entities/unit-position" eu "simrs-vx/internal/domain/main-entities/user" - erg "simrs-vx/internal/domain/references/organization" udp "simrs-vx/internal/use-case/main-use-case/division-position" uip "simrs-vx/internal/use-case/main-use-case/installation-position" @@ -334,3 +337,71 @@ func populateRoles(user *eu.User, input eu.LoginDto, atClaims jwt.MapClaims, out return nil } + +func getAuthByUserName(username string) (auth *pa.AuthInfo, err error) { + // get employee + emp := ee.Employee{} + if err = dg.I. + Joins(`JOIN "User" u ON u."Id" = "Employee"."User_Id"`). + Where(`u."Name" = ?`, username). + Scopes(gh.Preload("User")). + First(&emp).Error; err != nil { + return nil, err + } + + auth = &pa.AuthInfo{ + User_Id: *emp.User_Id, + User_Name: emp.User.Name, + User_ContractPosition_Code: string(emp.User.ContractPosition_Code), + Employee_Position_Code: strPtr(string(*emp.Position_Code)), + Employee_Id: &emp.Id, + Sync: true, + } + + // set map table + tableMap := map[erg.EmployeePositionCode]string{ + erg.EPCDoc: "Doctor", + erg.EPCNur: "Nurse", + erg.EPCMwi: "Midwife", + erg.EPCNut: "Nutritionist", + erg.EPCLab: "Laborant", + erg.EPCPha: "Pharmachist", + } + + table := tableMap[*emp.Position_Code] + if table == "" { + return + } + + type CodeResult struct { + Code string + } + var result CodeResult + + err = dg.I.Table(table). + Select("Code"). + Where("\"Employee_Id\" = ?", emp.Id). + First(&result).Error + if err != nil { + return nil, err + } + + // set map for code + setterMap := map[erg.EmployeePositionCode]func(string){ + erg.EPCDoc: func(v string) { auth.Doctor_Code = &v }, + erg.EPCNur: func(v string) { auth.Nurse_Code = &v }, + erg.EPCMwi: func(v string) { auth.Midwife_Code = &v }, + erg.EPCNut: func(v string) { auth.Nutritionist_Code = &v }, + erg.EPCLab: func(v string) { auth.Laborant_Code = &v }, + erg.EPCPha: func(v string) { auth.Pharmachist_Code = &v }, + } + + setter := setterMap[*emp.Position_Code] + setter(result.Code) + + return +} + +func strPtr(s string) *string { + return &s +} diff --git a/internal/use-case/simgos-sync-plugin/new/division/plugin.go b/internal/use-case/simgos-sync-plugin/new/division/plugin.go index b67f19d6..f4d6a34b 100644 --- a/internal/use-case/simgos-sync-plugin/new/division/plugin.go +++ b/internal/use-case/simgos-sync-plugin/new/division/plugin.go @@ -1,165 +1,35 @@ package division import ( - "bytes" - "encoding/json" "fmt" - "io" - "net/http" + helper "simrs-vx/internal/use-case/simgos-sync-plugin/new" sync "simrs-vx/internal/infra/sync-consumer-cfg" e "simrs-vx/internal/domain/main-entities/division" elog "simrs-vx/internal/domain/sync-entities/log" - - d "github.com/karincake/dodol" ) func Create(input *e.CreateDto) error { - endpoint := getPrefixEndpoint() - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("POST", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "POST", getPrefixEndpoint()) } func CreateLog(input *elog.SimxLogDto) error { prefixEndpoint := getPrefixEndpoint() endpoint := prefixEndpoint + "/log" - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("POST", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "POST", endpoint) } func Update(input *e.UpdateDto) error { prefixEndpoint := getPrefixEndpoint() endpoint := fmt.Sprintf("%s/%v", prefixEndpoint, *input.Id) - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("PATCH", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "PATCH", endpoint) } func Delete(input *e.DeleteDto) error { prefixEndpoint := getPrefixEndpoint() endpoint := fmt.Sprintf("%s/%v", prefixEndpoint, *input.Id) - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("DELETE", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "DELETE", endpoint) } func getPrefixEndpoint() string { diff --git a/internal/use-case/simgos-sync-plugin/new/installation/plugin.go b/internal/use-case/simgos-sync-plugin/new/installation/plugin.go index 6f1c12d6..40f0c153 100644 --- a/internal/use-case/simgos-sync-plugin/new/installation/plugin.go +++ b/internal/use-case/simgos-sync-plugin/new/installation/plugin.go @@ -1,165 +1,35 @@ package installation import ( - "bytes" - "encoding/json" "fmt" - "io" - "net/http" sync "simrs-vx/internal/infra/sync-consumer-cfg" + helper "simrs-vx/internal/use-case/simgos-sync-plugin/new" e "simrs-vx/internal/domain/main-entities/installation" elog "simrs-vx/internal/domain/sync-entities/log" - - d "github.com/karincake/dodol" ) func Create(input *e.CreateDto) error { - endpoint := getPrefixEndpoint() - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("POST", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "POST", getPrefixEndpoint()) } func CreateLog(input *elog.SimxLogDto) error { prefixEndpoint := getPrefixEndpoint() endpoint := prefixEndpoint + "/log" - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("POST", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "POST", endpoint) } func Update(input *e.UpdateDto) error { prefixEndpoint := getPrefixEndpoint() endpoint := fmt.Sprintf("%s/%v", prefixEndpoint, *input.Id) - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("PATCH", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "PATCH", endpoint) } func Delete(input *e.DeleteDto) error { prefixEndpoint := getPrefixEndpoint() endpoint := fmt.Sprintf("%s/%v", prefixEndpoint, *input.Id) - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("DELETE", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "DELETE", endpoint) } func getPrefixEndpoint() string { diff --git a/internal/use-case/simgos-sync-plugin/new/patient/plugin.go b/internal/use-case/simgos-sync-plugin/new/patient/plugin.go index 6f03299a..7678da47 100644 --- a/internal/use-case/simgos-sync-plugin/new/patient/plugin.go +++ b/internal/use-case/simgos-sync-plugin/new/patient/plugin.go @@ -1,11 +1,11 @@ package patient import ( - "bytes" "encoding/json" "fmt" "io" "net/http" + helper "simrs-vx/internal/use-case/simgos-sync-plugin/new" d "github.com/karincake/dodol" @@ -16,150 +16,25 @@ import ( ) func Create(input *e.Patient) error { - endpoint := getPrefixEndpoint() - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("POST", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "POST", getPrefixEndpoint()) } func CreateLog(input *elog.SimxLogDto) error { prefixEndpoint := getPrefixEndpoint() endpoint := prefixEndpoint + "/log" - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("POST", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "POST", endpoint) } func Update(input *e.Patient) error { prefixEndpoint := getPrefixEndpoint() endpoint := fmt.Sprintf("%s/%v", prefixEndpoint, input.Id) - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("PATCH", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "PATCH", endpoint) } func Delete(input *e.DeleteDto) error { prefixEndpoint := getPrefixEndpoint() endpoint := fmt.Sprintf("%s/%v", prefixEndpoint, input.Id) - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("DELETE", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "DELETE", endpoint) } func GenerateNomrPatient() (*string, error) { diff --git a/internal/use-case/simgos-sync-plugin/new/specialist/plugin.go b/internal/use-case/simgos-sync-plugin/new/specialist/plugin.go index 370a66dd..29da9d0a 100644 --- a/internal/use-case/simgos-sync-plugin/new/specialist/plugin.go +++ b/internal/use-case/simgos-sync-plugin/new/specialist/plugin.go @@ -1,165 +1,35 @@ package specialist import ( - "bytes" - "encoding/json" "fmt" - "io" - "net/http" + helper "simrs-vx/internal/use-case/simgos-sync-plugin/new" sync "simrs-vx/internal/infra/sync-consumer-cfg" e "simrs-vx/internal/domain/main-entities/specialist" elog "simrs-vx/internal/domain/sync-entities/log" - - d "github.com/karincake/dodol" ) func Create(input *e.CreateDto) error { - endpoint := getPrefixEndpoint() - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("POST", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "POST", getPrefixEndpoint()) } func CreateLog(input *elog.SimxLogDto) error { prefixEndpoint := getPrefixEndpoint() endpoint := prefixEndpoint + "/log" - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("POST", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "POST", endpoint) } func Update(input *e.UpdateDto) error { prefixEndpoint := getPrefixEndpoint() endpoint := fmt.Sprintf("%s/%v", prefixEndpoint, *input.Id) - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("PATCH", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "PATCH", endpoint) } func Delete(input *e.DeleteDto) error { prefixEndpoint := getPrefixEndpoint() endpoint := fmt.Sprintf("%s/%v", prefixEndpoint, *input.Id) - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("DELETE", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "DELETE", endpoint) } func getPrefixEndpoint() string { diff --git a/internal/use-case/simgos-sync-plugin/new/subspecialist/plugin.go b/internal/use-case/simgos-sync-plugin/new/subspecialist/plugin.go index 5fa831d1..0167936c 100644 --- a/internal/use-case/simgos-sync-plugin/new/subspecialist/plugin.go +++ b/internal/use-case/simgos-sync-plugin/new/subspecialist/plugin.go @@ -1,165 +1,35 @@ package subspecialist import ( - "bytes" - "encoding/json" "fmt" - "io" - "net/http" + helper "simrs-vx/internal/use-case/simgos-sync-plugin/new" sync "simrs-vx/internal/infra/sync-consumer-cfg" e "simrs-vx/internal/domain/main-entities/subspecialist" elog "simrs-vx/internal/domain/sync-entities/log" - - d "github.com/karincake/dodol" ) func Create(input *e.CreateDto) error { - endpoint := getPrefixEndpoint() - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("POST", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "POST", getPrefixEndpoint()) } func CreateLog(input *elog.SimxLogDto) error { prefixEndpoint := getPrefixEndpoint() endpoint := prefixEndpoint + "/log" - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("POST", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "POST", endpoint) } func Update(input *e.UpdateDto) error { prefixEndpoint := getPrefixEndpoint() endpoint := fmt.Sprintf("%s/%v", prefixEndpoint, *input.Id) - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("PATCH", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "PATCH", endpoint) } func Delete(input *e.DeleteDto) error { prefixEndpoint := getPrefixEndpoint() endpoint := fmt.Sprintf("%s/%v", prefixEndpoint, *input.Id) - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("DELETE", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "DELETE", endpoint) } func getPrefixEndpoint() string { diff --git a/internal/use-case/simgos-sync-plugin/new/unit/plugin.go b/internal/use-case/simgos-sync-plugin/new/unit/plugin.go index 36d240b5..f646f450 100644 --- a/internal/use-case/simgos-sync-plugin/new/unit/plugin.go +++ b/internal/use-case/simgos-sync-plugin/new/unit/plugin.go @@ -1,165 +1,35 @@ package unit import ( - "bytes" - "encoding/json" "fmt" - "io" - "net/http" + helper "simrs-vx/internal/use-case/simgos-sync-plugin/new" sync "simrs-vx/internal/infra/sync-consumer-cfg" e "simrs-vx/internal/domain/main-entities/unit" elog "simrs-vx/internal/domain/sync-entities/log" - - d "github.com/karincake/dodol" ) func Create(input *e.CreateDto) error { - endpoint := getPrefixEndpoint() - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("POST", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "POST", getPrefixEndpoint()) } func CreateLog(input *elog.SimxLogDto) error { prefixEndpoint := getPrefixEndpoint() endpoint := prefixEndpoint + "/log" - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("POST", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "POST", endpoint) } func Update(input *e.UpdateDto) error { prefixEndpoint := getPrefixEndpoint() endpoint := fmt.Sprintf("%s/%v", prefixEndpoint, *input.Id) - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("PATCH", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "PATCH", endpoint) } func Delete(input *e.DeleteDto) error { prefixEndpoint := getPrefixEndpoint() endpoint := fmt.Sprintf("%s/%v", prefixEndpoint, *input.Id) - - jsonData, err := json.Marshal(input) - if err != nil { - return fmt.Errorf("failed to encode JSON: %w", err) - } - - req, err := http.NewRequest("DELETE", endpoint, bytes.NewReader(jsonData)) - if err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - errors := d.FieldError{} - _ = json.Unmarshal(bodyBytes, &errors) - - return fmt.Errorf(errors.Message) - } - - return nil + return helper.DoJsonRequest(input, "DELETE", endpoint) } func getPrefixEndpoint() string { diff --git a/internal/use-case/simgos-sync-use-case/new/encounter/helper.go b/internal/use-case/simgos-sync-use-case/new/encounter/helper.go index bbbc8687..3a190d56 100644 --- a/internal/use-case/simgos-sync-use-case/new/encounter/helper.go +++ b/internal/use-case/simgos-sync-use-case/new/encounter/helper.go @@ -381,9 +381,7 @@ func updatePatientCaraBayar(input etp.TPendaftaran, event *pl.Event, dbx ...*gor if err := tx.Model(&ep.MPasien{}). Where("\"nomr\" = ?", input.Nomr). - Updates(map[string]interface{}{ - "kdcarabayar": input.Kdcarabayar, - }).Error; err != nil { + Update("kdcarabayar", input.Kdcarabayar).Error; err != nil { event.Status = "failed" event.ErrInfo = pl.ErrorInfo{ Code: "update-fail", diff --git a/pkg/dualtrx-helper/dualtrx-helper.go b/pkg/dualtrx-helper/dualtrx-helper.go index 2f16203c..144337c5 100644 --- a/pkg/dualtrx-helper/dualtrx-helper.go +++ b/pkg/dualtrx-helper/dualtrx-helper.go @@ -11,9 +11,12 @@ type Dualtx struct { } func NewDualtx() *Dualtx { + simgosTx := dg.IS["simrs"].Begin() + simgosTx.Exec(`SET LOCAL simx.sync_source = 'new'`) + return &Dualtx{ Sync: dg.I.Begin(), - Simgos: dg.IS["simrs"].Begin(), + Simgos: simgosTx, } }