feat: refactor auth package and add get users for staff

This commit is contained in:
ericprd
2025-03-07 17:39:26 +08:00
parent c48a4b944b
commit fdf572cd3d
29 changed files with 203 additions and 78 deletions
+59
View File
@@ -0,0 +1,59 @@
package staffhttp
import (
authmiddleware "legalgo-BE-go/internal/api/http/middleware/auth"
authsvc "legalgo-BE-go/internal/services/auth"
"legalgo-BE-go/internal/utilities/response"
"legalgo-BE-go/internal/utilities/utils"
"net/http"
"github.com/go-chi/chi/v5"
)
func GetUsers(
router chi.Router,
authSvc authsvc.Auth,
) {
router.With(authmiddleware.Authorize()).Get("/staff/users", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
staffDetail, err := utils.GetTokenDetail(r)
if err != nil {
response.RespondJsonErrorWithCode(
ctx,
w,
err,
response.ErrBadRequest.Code,
response.ErrBadRequest.HttpCode,
"failed to get staff token",
)
return
}
if staffDetail.Role != "staff" {
response.RespondJsonErrorWithCode(
ctx,
w,
err,
response.ErrUnauthorized.Code,
response.ErrUnauthorized.HttpCode,
"unauthorized",
)
return
}
users, err := authSvc.GetUsers()
if err != nil {
response.RespondJsonErrorWithCode(
ctx,
w,
err,
response.ErrBadRequest.Code,
response.ErrBadRequest.HttpCode,
"failed to get users",
)
return
}
response.RespondJsonSuccess(ctx, w, users)
})
}