From 2c432a7bef5f09a80b501fe950d36b8032a439a8 Mon Sep 17 00:00:00 2001 From: Munawwirul Jamal Date: Sun, 26 Oct 2025 21:29:41 +0700 Subject: [PATCH] feat/authentication + moved pkg/auth-helper to internal/lib/auth + update AuthInfo + cleaning --- assets/language/id/data.json | 3 +- internal/domain/main-entities/adime/dto.go | 7 +- internal/domain/main-entities/chemo/dto.go | 12 ++- .../domain/main-entities/consultation/dto.go | 9 +- .../domain/main-entities/device-order/dto.go | 13 ++- .../domain/main-entities/encounter/dto.go | 18 +++- .../main-entities/material-order/dto.go | 13 ++- .../domain/main-entities/mcu-order/dto.go | 19 ++-- .../main-entities/medication-item-dist/dto.go | 15 ++- .../domain/main-entities/medication/dto.go | 14 ++- .../domain/main-entities/prescription/dto.go | 4 + internal/domain/main-entities/sbar/dto.go | 9 +- internal/domain/main-entities/soapi/dto.go | 2 +- .../interface/main-handler/adime/handler.go | 2 +- .../main-handler/authentication/handler.go | 2 +- .../interface/main-handler/chemo/handler.go | 2 +- .../main-handler/consultation/handler.go | 2 +- .../main-handler/device-order/handler.go | 2 +- .../main-handler/encounter/handler.go | 7 +- .../interface/main-handler/main-handler.go | 31 +++--- .../main-handler/material-order/handler.go | 2 +- .../main-handler/mcu-order/handler.go | 2 +- .../medication-item-dist/handler.go | 2 +- .../main-handler/medication/handler.go | 2 +- .../main-handler/prescription/handler.go | 8 ++ .../interface/main-handler/sbar/handler.go | 2 +- .../interface/main-handler/soapi/handler.go | 2 +- internal/lib/.keep | 0 .../lib/auth/auth.go | 0 .../lib/auth}/tycovar.go | 13 ++- .../main-use-case/authentication/case.go | 98 +++++++++++-------- .../main-use-case/authentication/helper.go | 4 - .../use-case/main-use-case/encounter/lib.go | 27 +++-- 33 files changed, 227 insertions(+), 121 deletions(-) create mode 100644 internal/lib/.keep rename pkg/auth-helper/auth-helper.go => internal/lib/auth/auth.go (100%) rename {pkg/auth-helper => internal/lib/auth}/tycovar.go (84%) diff --git a/assets/language/id/data.json b/assets/language/id/data.json index 9e064e9a..4f76a8ff 100644 --- a/assets/language/id/data.json +++ b/assets/language/id/data.json @@ -5,7 +5,8 @@ "payload-bad": "struktur data tidak sesuai standar", "auth-required": "butuh autentikasi", - "auth-missingHeader": "Header autentikasi tidak ditemukan", + "auth-getData-failed": "gagal mengambil data user", + "auth-missingHeader": "header autentikasi tidak ditemukan", "auth-forbidden": "tidak diijinkan", "auth-login-success": "login berhasil", "auth-login-incorrect": "Username atau Password Tidak Sesuai", diff --git a/internal/domain/main-entities/adime/dto.go b/internal/domain/main-entities/adime/dto.go index 49257154..cb9b675a 100644 --- a/internal/domain/main-entities/adime/dto.go +++ b/internal/domain/main-entities/adime/dto.go @@ -1,13 +1,16 @@ package adime import ( + // std "time" + // internal - lib + pa "simrs-vx/internal/lib/auth" + + // internal - domain ecore "simrs-vx/internal/domain/base-entities/core" eem "simrs-vx/internal/domain/main-entities/employee" ee "simrs-vx/internal/domain/main-entities/encounter" - - pa "simrs-vx/pkg/auth-helper" ) type CreateDto struct { diff --git a/internal/domain/main-entities/chemo/dto.go b/internal/domain/main-entities/chemo/dto.go index 19e5f803..8317b259 100644 --- a/internal/domain/main-entities/chemo/dto.go +++ b/internal/domain/main-entities/chemo/dto.go @@ -1,16 +1,20 @@ package chemo import ( + // std "time" + // internal - lib + pa "simrs-vx/internal/lib/auth" + + // internal - domain - references + erc "simrs-vx/internal/domain/references/common" + + // internal - domain - main-entities ecore "simrs-vx/internal/domain/base-entities/core" ee "simrs-vx/internal/domain/main-entities/encounter" eun "simrs-vx/internal/domain/main-entities/unit" eus "simrs-vx/internal/domain/main-entities/user" - - erc "simrs-vx/internal/domain/references/common" - - pa "simrs-vx/pkg/auth-helper" ) type CreateDto struct { diff --git a/internal/domain/main-entities/consultation/dto.go b/internal/domain/main-entities/consultation/dto.go index 1b0e3cc5..33868dd0 100644 --- a/internal/domain/main-entities/consultation/dto.go +++ b/internal/domain/main-entities/consultation/dto.go @@ -1,14 +1,19 @@ package consultation import ( + // std "time" + // internal - lib + pa "simrs-vx/internal/lib/auth" + + // internal - domain - base-entities ecore "simrs-vx/internal/domain/base-entities/core" + + // internal - domain - main-entities ed "simrs-vx/internal/domain/main-entities/doctor" ee "simrs-vx/internal/domain/main-entities/encounter" eu "simrs-vx/internal/domain/main-entities/unit" - - pa "simrs-vx/pkg/auth-helper" ) type CreateDto struct { diff --git a/internal/domain/main-entities/device-order/dto.go b/internal/domain/main-entities/device-order/dto.go index 01ef34b2..3d944941 100644 --- a/internal/domain/main-entities/device-order/dto.go +++ b/internal/domain/main-entities/device-order/dto.go @@ -1,13 +1,18 @@ package deviceorder import ( - ecore "simrs-vx/internal/domain/base-entities/core" - ed "simrs-vx/internal/domain/main-entities/doctor" - ee "simrs-vx/internal/domain/main-entities/encounter" + // internal - lib + pa "simrs-vx/internal/lib/auth" + // internal - domain - base-entities + ecore "simrs-vx/internal/domain/base-entities/core" + + // internal - domain - references erc "simrs-vx/internal/domain/references/common" - pa "simrs-vx/pkg/auth-helper" + // internal - domain - main-entities + ed "simrs-vx/internal/domain/main-entities/doctor" + ee "simrs-vx/internal/domain/main-entities/encounter" ) type CreateDto struct { diff --git a/internal/domain/main-entities/encounter/dto.go b/internal/domain/main-entities/encounter/dto.go index 4a30281d..684d4bb3 100644 --- a/internal/domain/main-entities/encounter/dto.go +++ b/internal/domain/main-entities/encounter/dto.go @@ -1,9 +1,20 @@ package encounter import ( + // std "time" + // internal - lib + pa "simrs-vx/internal/lib/auth" + + // internal - domain - base-entities ecore "simrs-vx/internal/domain/base-entities/core" + + // internal - domain - references + erc "simrs-vx/internal/domain/references/common" + ere "simrs-vx/internal/domain/references/encounter" + + // internal - domain - main-entities evs "simrs-vx/internal/domain/bpjs-entities/vclaim-sep" ea "simrs-vx/internal/domain/main-entities/appointment" ed "simrs-vx/internal/domain/main-entities/doctor" @@ -13,11 +24,6 @@ import ( es "simrs-vx/internal/domain/main-entities/specialist" ess "simrs-vx/internal/domain/main-entities/subspecialist" eu "simrs-vx/internal/domain/main-entities/unit" - - erc "simrs-vx/internal/domain/references/common" - ere "simrs-vx/internal/domain/references/encounter" - - pa "simrs-vx/pkg/auth-helper" ) type CreateDto struct { @@ -48,6 +54,8 @@ type ReadListDto struct { FilterDto Includes string `json:"includes"` Pagination ecore.Pagination + + pa.AuthInfo } type FilterDto struct { diff --git a/internal/domain/main-entities/material-order/dto.go b/internal/domain/main-entities/material-order/dto.go index 5485d2dd..bd4f5c91 100644 --- a/internal/domain/main-entities/material-order/dto.go +++ b/internal/domain/main-entities/material-order/dto.go @@ -1,13 +1,18 @@ package materialorder import ( - ecore "simrs-vx/internal/domain/base-entities/core" - ed "simrs-vx/internal/domain/main-entities/doctor" - ee "simrs-vx/internal/domain/main-entities/encounter" + // internal - lib + pa "simrs-vx/internal/lib/auth" + // internal - domain - base-entities + ecore "simrs-vx/internal/domain/base-entities/core" + + // internal - domain - references erc "simrs-vx/internal/domain/references/common" - pa "simrs-vx/pkg/auth-helper" + // internal - domain - main-entities + ed "simrs-vx/internal/domain/main-entities/doctor" + ee "simrs-vx/internal/domain/main-entities/encounter" ) type CreateDto struct { diff --git a/internal/domain/main-entities/mcu-order/dto.go b/internal/domain/main-entities/mcu-order/dto.go index caf3ef58..a8ce83de 100644 --- a/internal/domain/main-entities/mcu-order/dto.go +++ b/internal/domain/main-entities/mcu-order/dto.go @@ -1,15 +1,22 @@ package mcuorder import ( - ecore "simrs-vx/internal/domain/base-entities/core" - ed "simrs-vx/internal/domain/main-entities/doctor" - ee "simrs-vx/internal/domain/main-entities/encounter" + // std "time" + // internal - lib + pa "simrs-vx/internal/lib/auth" + + // internal - domain - base-entities + ecore "simrs-vx/internal/domain/base-entities/core" + + // internal - domain - references ercl "simrs-vx/internal/domain/references/clinical" erc "simrs-vx/internal/domain/references/common" - pa "simrs-vx/pkg/auth-helper" + // internal - domain - main-entities + ed "simrs-vx/internal/domain/main-entities/doctor" + ee "simrs-vx/internal/domain/main-entities/encounter" ) type CreateDto struct { @@ -20,7 +27,7 @@ type CreateDto struct { ExaminationDate *time.Time `json:"examinationDate"` Number uint8 `json:"number"` Temperature float64 `json:"temperature"` - McuUrgencyLevel_Code ercl.McuUrgencyLevelCode `json:"mcuUrgencyLevel_code""` + McuUrgencyLevel_Code ercl.McuUrgencyLevelCode `json:"mcuUrgencyLevel_code"` pa.AuthInfo } @@ -39,7 +46,7 @@ type FilterDto struct { ExaminationDate *time.Time `json:"examinationDate"` Number uint8 `json:"number"` Temperature float64 `json:"temperature"` - McuUrgencyLevel_Code ercl.McuUrgencyLevelCode `json:"mcuUrgencyLevel-code""` + McuUrgencyLevel_Code ercl.McuUrgencyLevelCode `json:"mcuUrgencyLevel-code"` } type ReadDetailDto struct { Id uint `json:"id"` diff --git a/internal/domain/main-entities/medication-item-dist/dto.go b/internal/domain/main-entities/medication-item-dist/dto.go index 9790895a..6936bdbc 100644 --- a/internal/domain/main-entities/medication-item-dist/dto.go +++ b/internal/domain/main-entities/medication-item-dist/dto.go @@ -1,12 +1,17 @@ package medicationitem import ( - ecore "simrs-vx/internal/domain/base-entities/core" - emi "simrs-vx/internal/domain/main-entities/medication-item" - - pa "simrs-vx/pkg/auth-helper" - + // std "time" + + // internal - lib + pa "simrs-vx/internal/lib/auth" + + // internal - domain - base-entities + ecore "simrs-vx/internal/domain/base-entities/core" + + // internal - domain - main-entities + emi "simrs-vx/internal/domain/main-entities/medication-item" ) type CreateDto struct { diff --git a/internal/domain/main-entities/medication/dto.go b/internal/domain/main-entities/medication/dto.go index c2cf9d14..928b926c 100644 --- a/internal/domain/main-entities/medication/dto.go +++ b/internal/domain/main-entities/medication/dto.go @@ -1,15 +1,21 @@ package medication import ( + // std "time" - ecore "simrs-vx/internal/domain/base-entities/core" - ee "simrs-vx/internal/domain/main-entities/encounter" - ep "simrs-vx/internal/domain/main-entities/pharmacist" + // internal - lib + pa "simrs-vx/internal/lib/auth" + // internal - domain - base-entities + ecore "simrs-vx/internal/domain/base-entities/core" + + // internal - domain - references erc "simrs-vx/internal/domain/references/common" - pa "simrs-vx/pkg/auth-helper" + // internal - domain - main-entities + ee "simrs-vx/internal/domain/main-entities/encounter" + ep "simrs-vx/internal/domain/main-entities/pharmacist" ) type CreateDto struct { diff --git a/internal/domain/main-entities/prescription/dto.go b/internal/domain/main-entities/prescription/dto.go index 431f1e2d..3a3ad7b5 100644 --- a/internal/domain/main-entities/prescription/dto.go +++ b/internal/domain/main-entities/prescription/dto.go @@ -7,6 +7,8 @@ import ( ed "simrs-vx/internal/domain/main-entities/doctor" ee "simrs-vx/internal/domain/main-entities/encounter" + pa "simrs-vx/internal/lib/auth" + erc "simrs-vx/internal/domain/references/common" ) @@ -15,6 +17,8 @@ type CreateDto struct { Doctor_Id *uint `json:"doctor_id"` IssuedAt *time.Time `json:"issuedAt"` Status_Code erc.DataStatusCode `json:"status_code"` + + pa.AuthInfo } type ReadListDto struct { diff --git a/internal/domain/main-entities/sbar/dto.go b/internal/domain/main-entities/sbar/dto.go index 46cf69a8..1664fb5f 100644 --- a/internal/domain/main-entities/sbar/dto.go +++ b/internal/domain/main-entities/sbar/dto.go @@ -1,13 +1,18 @@ package sbar import ( + // std "time" + // internal - lib + pa "simrs-vx/internal/lib/auth" + + // internal - domain - base-entities ecore "simrs-vx/internal/domain/base-entities/core" + + // internal - domain - main-entities eem "simrs-vx/internal/domain/main-entities/employee" ee "simrs-vx/internal/domain/main-entities/encounter" - - pa "simrs-vx/pkg/auth-helper" ) type CreateDto struct { diff --git a/internal/domain/main-entities/soapi/dto.go b/internal/domain/main-entities/soapi/dto.go index 418a7fb8..b8f1b827 100644 --- a/internal/domain/main-entities/soapi/dto.go +++ b/internal/domain/main-entities/soapi/dto.go @@ -9,7 +9,7 @@ import ( erc "simrs-vx/internal/domain/references/clinical" - pa "simrs-vx/pkg/auth-helper" + pa "simrs-vx/internal/lib/auth" ) type CreateDto struct { diff --git a/internal/interface/main-handler/adime/handler.go b/internal/interface/main-handler/adime/handler.go index 9eabb0af..715f5faf 100644 --- a/internal/interface/main-handler/adime/handler.go +++ b/internal/interface/main-handler/adime/handler.go @@ -11,7 +11,7 @@ import ( e "simrs-vx/internal/domain/main-entities/adime" u "simrs-vx/internal/use-case/main-use-case/adime" - pa "simrs-vx/pkg/auth-helper" + pa "simrs-vx/internal/lib/auth" d "github.com/karincake/dodol" ) diff --git a/internal/interface/main-handler/authentication/handler.go b/internal/interface/main-handler/authentication/handler.go index 01d4fb89..709f33a3 100644 --- a/internal/interface/main-handler/authentication/handler.go +++ b/internal/interface/main-handler/authentication/handler.go @@ -10,7 +10,7 @@ import ( m "simrs-vx/internal/domain/main-entities/user" s "simrs-vx/internal/use-case/main-use-case/authentication" - pa "simrs-vx/pkg/auth-helper" + pa "simrs-vx/internal/lib/auth" ) func Login(w http.ResponseWriter, r *http.Request) { diff --git a/internal/interface/main-handler/chemo/handler.go b/internal/interface/main-handler/chemo/handler.go index ee8c0435..90552020 100644 --- a/internal/interface/main-handler/chemo/handler.go +++ b/internal/interface/main-handler/chemo/handler.go @@ -14,7 +14,7 @@ import ( u "simrs-vx/internal/use-case/main-use-case/chemo" - pa "simrs-vx/pkg/auth-helper" + pa "simrs-vx/internal/lib/auth" d "github.com/karincake/dodol" ) diff --git a/internal/interface/main-handler/consultation/handler.go b/internal/interface/main-handler/consultation/handler.go index 2be1cbdd..2810d2f3 100644 --- a/internal/interface/main-handler/consultation/handler.go +++ b/internal/interface/main-handler/consultation/handler.go @@ -11,7 +11,7 @@ import ( e "simrs-vx/internal/domain/main-entities/consultation" u "simrs-vx/internal/use-case/main-use-case/consultation" - pa "simrs-vx/pkg/auth-helper" + pa "simrs-vx/internal/lib/auth" d "github.com/karincake/dodol" ) diff --git a/internal/interface/main-handler/device-order/handler.go b/internal/interface/main-handler/device-order/handler.go index 42135e58..12999301 100644 --- a/internal/interface/main-handler/device-order/handler.go +++ b/internal/interface/main-handler/device-order/handler.go @@ -9,7 +9,7 @@ import ( e "simrs-vx/internal/domain/main-entities/device-order" u "simrs-vx/internal/use-case/main-use-case/device-order" - pa "simrs-vx/pkg/auth-helper" + pa "simrs-vx/internal/lib/auth" d "github.com/karincake/dodol" ) diff --git a/internal/interface/main-handler/encounter/handler.go b/internal/interface/main-handler/encounter/handler.go index df3320ef..a35f6ba9 100644 --- a/internal/interface/main-handler/encounter/handler.go +++ b/internal/interface/main-handler/encounter/handler.go @@ -12,7 +12,7 @@ import ( u "simrs-vx/internal/use-case/main-use-case/encounter" erc "simrs-vx/internal/domain/references/common" - pa "simrs-vx/pkg/auth-helper" + pa "simrs-vx/internal/lib/auth" d "github.com/karincake/dodol" ) @@ -36,7 +36,12 @@ func (obj myBase) Create(w http.ResponseWriter, r *http.Request) { } func (obj myBase) GetList(w http.ResponseWriter, r *http.Request) { + authInfo, err := pa.GetAuthInfo(r) + if err != nil { + rw.WriteJSON(w, http.StatusUnauthorized, d.IS{"message": err.Error()}, nil) + } dto := e.ReadListDto{} + dto.AuthInfo = *authInfo sf.UrlQueryParam(&dto, *r.URL) res, err := u.ReadList(dto) rw.DataResponse(w, res, err) diff --git a/internal/interface/main-handler/main-handler.go b/internal/interface/main-handler/main-handler.go index 6a98487f..f4871af2 100644 --- a/internal/interface/main-handler/main-handler.go +++ b/internal/interface/main-handler/main-handler.go @@ -135,6 +135,19 @@ func SetRoutes() http.Handler { hc.RegCrud(r, "/v1/prescription-item", prescriptionitem.O) hc.RegCrud(r, "/v1/device-order-item", deviceorderitem.O) hc.RegCrud(r, "/v1/material-order-item", materialorderitem.O) + + hk.GroupRoutes("/v1/encounter", r, auth.GuardMW, hk.MapHandlerFunc{ + "GET /": encounter.O.GetList, + "GET /{id}": encounter.O.GetDetail, + "POST /": encounter.O.Create, + "PATCH /{id}": encounter.O.Update, + "DELETE /{id}": encounter.O.Delete, + "PATCH /{id}/checkout": encounter.O.CheckOut, + "PATCH /{id}/proccess": encounter.O.Process, + "PATCH /{id}/cancel": encounter.O.Cancel, + "PATCH /{id}/reject": encounter.O.Reject, + "PATCH /{id}/skip": encounter.O.Skip, + }) hk.GroupRoutes("/v1/mcu-order", r, auth.GuardMW, hk.MapHandlerFunc{ "GET /": mcuorder.O.GetList, "GET /{id}": mcuorder.O.GetDetail, @@ -153,7 +166,7 @@ func SetRoutes() http.Handler { "PATCH /{id}/complete": mcuorderitem.O.Complete, "PATCH /{id}/set-schedule": mcuorderitem.O.SetSchedule, }) - hk.GroupRoutes("/v1/prescription", r, hk.MapHandlerFunc{ + hk.GroupRoutes("/v1/prescription", r, auth.GuardMW, hk.MapHandlerFunc{ "GET /": prescription.O.GetList, "GET /{id}": prescription.O.GetDetail, "POST /": prescription.O.Create, @@ -161,7 +174,7 @@ func SetRoutes() http.Handler { "DELETE /{id}": prescription.O.Delete, "PATCH /{id}/approve": prescription.O.Approve, }) - hk.GroupRoutes("/v1/mcu-order-sub-item", r, hk.MapHandlerFunc{ + hk.GroupRoutes("/v1/mcu-order-sub-item", r, auth.GuardMW, hk.MapHandlerFunc{ "GET /": mcuordersubitem.O.GetList, "GET /{id}": mcuordersubitem.O.GetDetail, "POST /": mcuordersubitem.O.Create, @@ -169,18 +182,6 @@ func SetRoutes() http.Handler { "DELETE /{id}": mcuordersubitem.O.Delete, "PATCH /{id}/complete": mcuordersubitem.O.Complete, }) - hk.GroupRoutes("/v1/encounter", r, auth.GuardMW, hk.MapHandlerFunc{ - "GET /": encounter.O.GetList, - "GET /{id}": encounter.O.GetDetail, - "POST /": encounter.O.Create, - "PATCH /{id}": encounter.O.Update, - "DELETE /{id}": encounter.O.Delete, - "PATCH /{id}/checkout": encounter.O.CheckOut, - "PATCH /{id}/proccess": encounter.O.Process, - "PATCH /{id}/cancel": encounter.O.Cancel, - "PATCH /{id}/reject": encounter.O.Reject, - "PATCH /{id}/skip": encounter.O.Skip, - }) hk.GroupRoutes("/v1/medication", r, auth.GuardMW, hk.MapHandlerFunc{ "GET /": medication.O.GetList, "GET /{id}": medication.O.GetDetail, @@ -190,7 +191,7 @@ func SetRoutes() http.Handler { "PATCH /{id}/complete": medication.O.Complete, }) - hk.GroupRoutes("/v1/medication-item", r, hk.MapHandlerFunc{ + hk.GroupRoutes("/v1/medication-item", r, auth.GuardMW, hk.MapHandlerFunc{ "GET /": medicationitem.O.GetList, "GET /{id}": medicationitem.O.GetDetail, "POST /": medicationitem.O.Create, diff --git a/internal/interface/main-handler/material-order/handler.go b/internal/interface/main-handler/material-order/handler.go index ae8777cb..34c2b142 100644 --- a/internal/interface/main-handler/material-order/handler.go +++ b/internal/interface/main-handler/material-order/handler.go @@ -11,7 +11,7 @@ import ( e "simrs-vx/internal/domain/main-entities/material-order" u "simrs-vx/internal/use-case/main-use-case/material-order" - pa "simrs-vx/pkg/auth-helper" + pa "simrs-vx/internal/lib/auth" d "github.com/karincake/dodol" ) diff --git a/internal/interface/main-handler/mcu-order/handler.go b/internal/interface/main-handler/mcu-order/handler.go index bcd07a05..2cdb5c32 100644 --- a/internal/interface/main-handler/mcu-order/handler.go +++ b/internal/interface/main-handler/mcu-order/handler.go @@ -11,7 +11,7 @@ import ( e "simrs-vx/internal/domain/main-entities/mcu-order" u "simrs-vx/internal/use-case/main-use-case/mcu-order" - pa "simrs-vx/pkg/auth-helper" + pa "simrs-vx/internal/lib/auth" d "github.com/karincake/dodol" ) diff --git a/internal/interface/main-handler/medication-item-dist/handler.go b/internal/interface/main-handler/medication-item-dist/handler.go index 6c4b659f..9dd00d2b 100644 --- a/internal/interface/main-handler/medication-item-dist/handler.go +++ b/internal/interface/main-handler/medication-item-dist/handler.go @@ -6,7 +6,7 @@ import ( rw "github.com/karincake/risoles" sf "github.com/karincake/semprit" - pa "simrs-vx/pkg/auth-helper" + pa "simrs-vx/internal/lib/auth" e "simrs-vx/internal/domain/main-entities/medication-item-dist" u "simrs-vx/internal/use-case/main-use-case/medication-item-dist" diff --git a/internal/interface/main-handler/medication/handler.go b/internal/interface/main-handler/medication/handler.go index 272eae56..12fb6c84 100644 --- a/internal/interface/main-handler/medication/handler.go +++ b/internal/interface/main-handler/medication/handler.go @@ -6,7 +6,7 @@ import ( rw "github.com/karincake/risoles" sf "github.com/karincake/semprit" - pa "simrs-vx/pkg/auth-helper" + pa "simrs-vx/internal/lib/auth" e "simrs-vx/internal/domain/main-entities/medication" u "simrs-vx/internal/use-case/main-use-case/medication" diff --git a/internal/interface/main-handler/prescription/handler.go b/internal/interface/main-handler/prescription/handler.go index fe73e3fe..24d73372 100644 --- a/internal/interface/main-handler/prescription/handler.go +++ b/internal/interface/main-handler/prescription/handler.go @@ -7,6 +7,9 @@ import ( sf "github.com/karincake/semprit" // ua "github.com/karincake/tumpeng/auth/svc" + d "github.com/karincake/dodol" + + pa "simrs-vx/internal/lib/auth" e "simrs-vx/internal/domain/main-entities/prescription" u "simrs-vx/internal/use-case/main-use-case/prescription" @@ -21,6 +24,11 @@ func (obj myBase) Create(w http.ResponseWriter, r *http.Request) { if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res { return } + authInfo, err := pa.GetAuthInfo(r) + if err != nil { + rw.WriteJSON(w, http.StatusUnauthorized, d.IS{"message": err.Error()}, nil) + } + dto.AuthInfo = *authInfo res, err := u.Create(dto) rw.DataResponse(w, res, err) } diff --git a/internal/interface/main-handler/sbar/handler.go b/internal/interface/main-handler/sbar/handler.go index 7fac1e00..bcf63c8a 100644 --- a/internal/interface/main-handler/sbar/handler.go +++ b/internal/interface/main-handler/sbar/handler.go @@ -11,7 +11,7 @@ import ( e "simrs-vx/internal/domain/main-entities/sbar" u "simrs-vx/internal/use-case/main-use-case/sbar" - pa "simrs-vx/pkg/auth-helper" + pa "simrs-vx/internal/lib/auth" d "github.com/karincake/dodol" ) diff --git a/internal/interface/main-handler/soapi/handler.go b/internal/interface/main-handler/soapi/handler.go index f5446b82..fe50b7d2 100644 --- a/internal/interface/main-handler/soapi/handler.go +++ b/internal/interface/main-handler/soapi/handler.go @@ -11,7 +11,7 @@ import ( e "simrs-vx/internal/domain/main-entities/soapi" u "simrs-vx/internal/use-case/main-use-case/soapi" - pa "simrs-vx/pkg/auth-helper" + pa "simrs-vx/internal/lib/auth" d "github.com/karincake/dodol" ) diff --git a/internal/lib/.keep b/internal/lib/.keep new file mode 100644 index 00000000..e69de29b diff --git a/pkg/auth-helper/auth-helper.go b/internal/lib/auth/auth.go similarity index 100% rename from pkg/auth-helper/auth-helper.go rename to internal/lib/auth/auth.go diff --git a/pkg/auth-helper/tycovar.go b/internal/lib/auth/tycovar.go similarity index 84% rename from pkg/auth-helper/tycovar.go rename to internal/lib/auth/tycovar.go index 06618350..654115fc 100644 --- a/pkg/auth-helper/tycovar.go +++ b/internal/lib/auth/tycovar.go @@ -6,16 +6,23 @@ import ( type AuthKey struct{} +// const AuthKey = struct{}{} type AuthInfo struct { Uuid string User_Id uint User_Name string User_ContractPosition_code string Employee_Position_Code *string + Employee_Id *int + Doctor_Id *int + Nurse_Id *int + Midwife_Id *int + Nutritionist_Id *int + Laborant_Id *int + Pharmachist_Id *int Intern_Position_Code *string - User_DivisionPositions []DivisionPosition - // User_DivisionPositions []DivisionPosition - // User_Position_Code string + Roles []string + // User_DivisionPositions []DivisionPosition } type DivisionPosition struct { diff --git a/internal/use-case/main-use-case/authentication/case.go b/internal/use-case/main-use-case/authentication/case.go index f38e82d0..18ae9b8a 100644 --- a/internal/use-case/main-use-case/authentication/case.go +++ b/internal/use-case/main-use-case/authentication/case.go @@ -13,12 +13,13 @@ import ( "simrs-vx/internal/domain/main-entities/intern" eu "simrs-vx/internal/domain/main-entities/user" - pa "simrs-vx/pkg/auth-helper" + pa "simrs-vx/internal/lib/auth" el "simrs-vx/pkg/logger" p "simrs-vx/pkg/password" ed "simrs-vx/internal/domain/main-entities/doctor" ee "simrs-vx/internal/domain/main-entities/employee" + em "simrs-vx/internal/domain/main-entities/midwife" en "simrs-vx/internal/domain/main-entities/nurse" erc "simrs-vx/internal/domain/references/common" erg "simrs-vx/internal/domain/references/organization" @@ -147,13 +148,21 @@ func GenToken(input eu.LoginDto) (*d.Data, error) { outputData["subspecialist_id"] = doctor.Subspecialist_Id } case erg.EPCNur: - nurse := en.Nurse{} - dg.I.Where("\"Employee_Id\" = ?", employee.Id).First(&nurse) - if nurse.Id == 0 { + empData := en.Nurse{} + dg.I.Where("\"Employee_Id\" = ?", employee.Id).First(&empData) + if empData.Id == 0 { return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noNurse", Message: el.GenMessage("auth-noNurse")}} } - atClaims["nurse_id"] = nurse.Id - outputData["nurse_id"] = nurse.Id + atClaims["nurse_id"] = empData.Id + outputData["nurse_id"] = empData.Id + case erg.EPCMwi: + empData := em.Midwife{} + dg.I.Where("\"Employee_Id\" = ?", employee.Id).First(&empData) + if empData.Id == 0 { + return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noNurse", Message: el.GenMessage("auth-noNurse")}} + } + atClaims["nurse_id"] = empData.Id + outputData["nurse_id"] = empData.Id } errorGetPosition := d.FieldErrors{"authentication": d.FieldError{Code: "auth-getData-failed", Message: el.GenMessage("auth-getData-failed")}} @@ -289,43 +298,22 @@ func ExtractToken(r *http.Request, tokenType TokenType) (data *pa.AuthInfo, err if accessUuidRedis.String() == "" { return nil, d.FieldError{Code: "token-unidentified", Message: el.GenMessage("token-unidentified")} } - user_name := fmt.Sprintf("%v", claims["user_name"]) - // user_email := "" - // if v, exist := claims["user_email"]; exist && v != nil { - // user_email = v.(string) - // } - // ref_id := 0 - // if v, exist := claims["user_ref_id"]; exist && v != nil { - // tmp := v.(float64) - // ref_id = int(tmp) - // } - // position_code := "" - // if v, exist := claims["user_position_code"]; exist && v != nil { - // position_code = v.(string) - // } - // data = &AuthInfo{ - // Uuid: accessUuid, - // User_Id: int(user_id), - // User_Name: user_name, - // User_Email: user_email, - // User_Ref_Id: ref_id, - // User_Position_Code: position_code, - contractPosition_code := "" - if v, exist := claims["contractPosition_code"]; exist && v != nil { - contractPosition_code = v.(string) - } - employee_position_code := "" - if v, exist := claims["employee_position_code"]; exist && v != nil { - employee_position_code = v.(string) - } data = &pa.AuthInfo{ - Uuid: accessUuid, - User_Id: uint(user_id), - User_Name: user_name, - User_ContractPosition_code: contractPosition_code, - Employee_Position_Code: &employee_position_code, + Uuid: accessUuid, + User_Id: uint(user_id), + User_Name: fmt.Sprintf("%v", claims["user_name"]), } + + data.User_ContractPosition_code = checkStrClaims(claims, "contractPosition_code") + data.Employee_Position_Code = checkStrPtrClaims(claims, "employee_position_code") + data.Doctor_Id = checkIntPtrClaims(claims, "doctor_id") + data.Nurse_Id = checkIntPtrClaims(claims, "nurse_id") + data.Midwife_Id = checkIntPtrClaims(claims, "midwife_id") + data.Nutritionist_Id = checkIntPtrClaims(claims, "nutritionist_id") + data.Laborant_Id = checkIntPtrClaims(claims, "laborant_id") + data.Pharmachist_Id = checkIntPtrClaims(claims, "pharmachist_id") + data.Intern_Position_Code = checkStrPtrClaims(claims, "intern_position_code") return } return nil, d.FieldError{Code: "token", Message: "token-invalid"} @@ -334,3 +322,33 @@ func ExtractToken(r *http.Request, tokenType TokenType) (data *pa.AuthInfo, err func GetConfig() { a.ParseCfg(&authCfg) } + +func checkStrClaims(claim map[string]interface{}, key string) string { + if v, exist := claim[key]; exist && v != nil { + return v.(string) + } + return "" +} + +func checkStrPtrClaims(claim map[string]interface{}, key string) *string { + if v, exist := claim[key]; exist && v != nil { + val := v.(string) + return &val + } + return nil +} + +func checkIntClaims(claim map[string]interface{}, key string) int { + if v, exist := claim[key]; exist && v != nil { + return v.(int) + } + return 0 +} + +func checkIntPtrClaims(claim map[string]interface{}, key string) *int { + if v, exist := claim[key]; exist && v != nil { + val := int(v.(float64)) + return &val + } + return nil +} diff --git a/internal/use-case/main-use-case/authentication/helper.go b/internal/use-case/main-use-case/authentication/helper.go index 87473386..bec64518 100644 --- a/internal/use-case/main-use-case/authentication/helper.go +++ b/internal/use-case/main-use-case/authentication/helper.go @@ -28,10 +28,6 @@ func getAndCheck(input, condition any) (eCode string) { return "" } -func getDocName(id uint) string { - return "authentication" -} - func getDivisionPosition(employee_id uint) ([]string, error) { var result []string diff --git a/internal/use-case/main-use-case/encounter/lib.go b/internal/use-case/main-use-case/encounter/lib.go index ccc4b811..fce7db74 100644 --- a/internal/use-case/main-use-case/encounter/lib.go +++ b/internal/use-case/main-use-case/encounter/lib.go @@ -1,15 +1,21 @@ package encounter import ( - e "simrs-vx/internal/domain/main-entities/encounter" + // std + "errors" + // external + dg "github.com/karincake/apem/db-gorm-pg" + gh "github.com/karincake/getuk" + "gorm.io/gorm" + + // pkg plh "simrs-vx/pkg/lib-helper" pl "simrs-vx/pkg/logger" pu "simrs-vx/pkg/use-case-helper" - dg "github.com/karincake/apem/db-gorm-pg" - gh "github.com/karincake/getuk" - "gorm.io/gorm" + // internal + e "simrs-vx/internal/domain/main-entities/encounter" ) func CreateData(input e.CreateDto, event *pl.Event, dbx ...*gorm.DB) (*e.Encounter, error) { @@ -34,6 +40,10 @@ func CreateData(input e.CreateDto, event *pl.Event, dbx ...*gorm.DB) (*e.Encount } func ReadListData(input e.ReadListDto, event *pl.Event, dbx ...*gorm.DB) ([]e.Encounter, *e.MetaDto, error) { + if input.AuthInfo.User_Id == 0 { + return nil, nil, plh.HandleListError(input, event, errors.New("user_id is required")) + } + pl.SetLogInfo(event, input, "started", "DBReadList") data := []e.Encounter{} pagination := gh.Pagination{} @@ -46,10 +56,13 @@ func ReadListData(input e.ReadListDto, event *pl.Event, dbx ...*gorm.DB) ([]e.En } else { tx = dg.I } + tx = tx.Model(&e.Encounter{}) - tx = tx. - Model(&e.Encounter{}). - Scopes(gh.Preload(input.Includes)). + if input.AuthInfo.Doctor_Id != nil { + tx.Where("\"Responsible_Doctor_Id\" = ?", *input.AuthInfo.Doctor_Id) + } + + tx.Scopes(gh.Preload(input.Includes)). Scopes(gh.Filter(input.FilterDto)). Count(&count). Scopes(gh.Paginate(input, &pagination)).