From 11bc19360c36c53a1ae25c7c432b7d497b1e459a Mon Sep 17 00:00:00 2001 From: ericprd Date: Fri, 14 Mar 2025 12:41:11 +0800 Subject: [PATCH] feat: update staff data --- internal/accessor/staff/get_by_email.go | 5 ++-- internal/accessor/staff/get_by_id.go | 5 ++-- internal/accessor/staff/update.go | 11 ++++---- internal/accessor/subscribe_plan/get_by_id.go | 3 ++- internal/accessor/user/get_user_by_email.go | 5 ++-- internal/accessor/user/get_user_by_id.go | 5 ++-- internal/accessor/user/get_user_profile.go | 4 +-- internal/api/http/news/update.go | 6 ++--- internal/api/http/router.go | 2 +- .../http/{staffhttp => staff}/get_users.go | 4 +-- .../api/http/{staffhttp => staff}/login.go | 6 ++--- .../api/http/{staffhttp => staff}/module.go | 0 .../api/http/{staffhttp => staff}/profile.go | 6 ++--- .../api/http/{staffhttp => staff}/register.go | 6 ++--- .../api/http/{staffhttp => staff}/update.go | 26 +++++++------------ internal/domain/auth/token.go | 1 + internal/domain/staff/spec.go | 6 +++++ internal/enums/jwt/jwt_claims.go | 1 + internal/services/auth/update_staff.go | 17 ------------ internal/services/module.go | 4 +-- .../{auth/get_staff.go => staffsvc/get.go} | 4 +-- .../services/{auth => staffsvc}/get_users.go | 2 +- internal/services/{auth => staffsvc}/impl.go | 10 +++---- .../login_as_staff.go => staffsvc/login.go} | 14 +++++----- .../register.go} | 14 +++++----- internal/services/staffsvc/update.go | 24 +++++++++++++++++ .../user/{login_as_user.go => login.go} | 10 +++---- .../user/{register_user.go => register.go} | 12 ++++----- internal/utilities/utils/get_token_detail.go | 8 +++--- internal/utilities/utils/jwt.go | 18 ++++++++----- internal/utilities/utils/struct_to_map.go | 6 ++--- 31 files changed, 130 insertions(+), 115 deletions(-) rename internal/api/http/{staffhttp => staff}/get_users.go (93%) rename internal/api/http/{staffhttp => staff}/login.go (93%) rename internal/api/http/{staffhttp => staff}/module.go (100%) rename internal/api/http/{staffhttp => staff}/profile.go (84%) rename internal/api/http/{staffhttp => staff}/register.go (93%) rename internal/api/http/{staffhttp => staff}/update.go (65%) delete mode 100644 internal/services/auth/update_staff.go rename internal/services/{auth/get_staff.go => staffsvc/get.go} (77%) rename internal/services/{auth => staffsvc}/get_users.go (89%) rename internal/services/{auth => staffsvc}/impl.go (80%) rename internal/services/{auth/login_as_staff.go => staffsvc/login.go} (71%) rename internal/services/{auth/register_staff.go => staffsvc/register.go} (76%) create mode 100644 internal/services/staffsvc/update.go rename internal/services/user/{login_as_user.go => login.go} (83%) rename internal/services/user/{register_user.go => register.go} (86%) diff --git a/internal/accessor/staff/get_by_email.go b/internal/accessor/staff/get_by_email.go index e670730..76d05fa 100644 --- a/internal/accessor/staff/get_by_email.go +++ b/internal/accessor/staff/get_by_email.go @@ -2,6 +2,7 @@ package staffrepository import ( "errors" + "fmt" staffdomain "legalgo-BE-go/internal/domain/staff" @@ -12,12 +13,12 @@ func (sr *accessor) GetStaffByEmail(email string) (*staffdomain.Staff, error) { var staff staffdomain.Staff if email == "" { - return nil, errors.New("email is required") + return nil, fmt.Errorf("email is required") } if err := sr.db.First(&staff, "email = ?", email).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { - return nil, errors.New("staff not found") + return nil, fmt.Errorf("staff not found") } return nil, err } diff --git a/internal/accessor/staff/get_by_id.go b/internal/accessor/staff/get_by_id.go index d1d6490..bfbb6b9 100644 --- a/internal/accessor/staff/get_by_id.go +++ b/internal/accessor/staff/get_by_id.go @@ -2,6 +2,7 @@ package staffrepository import ( "errors" + "fmt" staffdomain "legalgo-BE-go/internal/domain/staff" "gorm.io/gorm" @@ -11,12 +12,12 @@ func (sr *accessor) GetStaffByID(ID string) (*staffdomain.Staff, error) { var staff staffdomain.Staff if ID == "" { - return nil, errors.New("id is required") + return nil, fmt.Errorf("id is required") } if err := sr.db.First(&staff, "id = ? ", ID).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { - return nil, errors.New("staff not found") + return nil, fmt.Errorf("staff not found") } return nil, err } diff --git a/internal/accessor/staff/update.go b/internal/accessor/staff/update.go index 67030a8..c4c2c30 100644 --- a/internal/accessor/staff/update.go +++ b/internal/accessor/staff/update.go @@ -2,16 +2,15 @@ package staffrepository import ( staffdomain "legalgo-BE-go/internal/domain/staff" - "legalgo-BE-go/internal/utilities/utils" ) func (ur *accessor) Update(spec staffdomain.Staff) error { - val, err := utils.StructToMap(spec) - if err != nil { - return err - } + // val, err := utils.StructToMap(spec) + // if err != nil { + // return err + // } - if err := ur.db.Model(&staffdomain.Staff{}).Where("id = ?", spec.ID).Updates(val).Error; err != nil { + if err := ur.db.Model(&staffdomain.Staff{}).Where("id = ?", spec.ID).Updates(spec).Error; err != nil { return err } diff --git a/internal/accessor/subscribe_plan/get_by_id.go b/internal/accessor/subscribe_plan/get_by_id.go index 3f086b2..5ff11a2 100644 --- a/internal/accessor/subscribe_plan/get_by_id.go +++ b/internal/accessor/subscribe_plan/get_by_id.go @@ -2,6 +2,7 @@ package subscribeplanrepository import ( "errors" + "fmt" subscribeplandomain "legalgo-BE-go/internal/domain/subscribe_plan" "gorm.io/gorm" @@ -12,7 +13,7 @@ func (s *accessor) GetByID(id string) (*subscribeplandomain.SubscribePlan, error if err := s.db.First(&subscribePlan, "id = ? ", id).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { - return subscribePlan, errors.New("subscribe plan not found") + return subscribePlan, fmt.Errorf("subscribe plan not found") } return subscribePlan, err } diff --git a/internal/accessor/user/get_user_by_email.go b/internal/accessor/user/get_user_by_email.go index 5a37d91..769bd7a 100644 --- a/internal/accessor/user/get_user_by_email.go +++ b/internal/accessor/user/get_user_by_email.go @@ -2,6 +2,7 @@ package userrepository import ( "errors" + "fmt" userdomain "legalgo-BE-go/internal/domain/user" "gorm.io/gorm" @@ -11,12 +12,12 @@ func (ur *accessor) GetUserByEmail(email string) (*userdomain.User, error) { var user *userdomain.User if email == "" { - return nil, errors.New("email is empty") + return nil, fmt.Errorf("email is empty") } if err := ur.db.First(&user, "email = ?", email).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { - return nil, errors.New("user not found") + return nil, fmt.Errorf("user not found") } return nil, err } diff --git a/internal/accessor/user/get_user_by_id.go b/internal/accessor/user/get_user_by_id.go index c83336f..c66412e 100644 --- a/internal/accessor/user/get_user_by_id.go +++ b/internal/accessor/user/get_user_by_id.go @@ -1,8 +1,7 @@ package userrepository import ( - "errors" - + "fmt" userdomain "legalgo-BE-go/internal/domain/user" ) @@ -10,7 +9,7 @@ func (ur *accessor) GetUserByID(id string) (*userdomain.User, error) { var user userdomain.User if id == "" { - return nil, errors.New("id is empty") + return nil, fmt.Errorf("id is empty") } if err := ur.db. diff --git a/internal/accessor/user/get_user_profile.go b/internal/accessor/user/get_user_profile.go index 34e6a56..b1dfacc 100644 --- a/internal/accessor/user/get_user_profile.go +++ b/internal/accessor/user/get_user_profile.go @@ -1,7 +1,7 @@ package userrepository import ( - "errors" + "fmt" userdomain "legalgo-BE-go/internal/domain/user" ) @@ -9,7 +9,7 @@ func (ur *accessor) GetUserProfile(email string) (*userdomain.UserProfile, error var user *userdomain.User if email == "" { - return nil, errors.New("email is empty") + return nil, fmt.Errorf("email is empty") } if err := ur.db. diff --git a/internal/api/http/news/update.go b/internal/api/http/news/update.go index e86a2f5..1b193cf 100644 --- a/internal/api/http/news/update.go +++ b/internal/api/http/news/update.go @@ -4,8 +4,8 @@ import ( "fmt" authmiddleware "legalgo-BE-go/internal/api/http/middleware/auth" newsdomain "legalgo-BE-go/internal/domain/news" - authsvc "legalgo-BE-go/internal/services/auth" newssvc "legalgo-BE-go/internal/services/news" + staffsvc "legalgo-BE-go/internal/services/staffsvc" "legalgo-BE-go/internal/utilities/response" "legalgo-BE-go/internal/utilities/utils" "net/http" @@ -16,7 +16,7 @@ import ( func Update( router chi.Router, newsSvc newssvc.News, - authSvc authsvc.Auth, + authSvc staffsvc.Auth, ) { router.With(authmiddleware.Authorize()). Put("/news/{news_id}/update", func(w http.ResponseWriter, r *http.Request) { @@ -48,7 +48,7 @@ func Update( return } - staff, err := authSvc.GetStaffProfile(destructedToken.Email) + staff, err := authSvc.GetProfile(destructedToken.Email) if err != nil { response.ResponseWithErrorCode( ctx, diff --git a/internal/api/http/router.go b/internal/api/http/router.go index 4c440c7..b5c5454 100644 --- a/internal/api/http/router.go +++ b/internal/api/http/router.go @@ -5,7 +5,7 @@ import ( categoryhttp "legalgo-BE-go/internal/api/http/category" newshttp "legalgo-BE-go/internal/api/http/news" osshttp "legalgo-BE-go/internal/api/http/oss" - staffhttp "legalgo-BE-go/internal/api/http/staffhttp" + staffhttp "legalgo-BE-go/internal/api/http/staff" subscribehttp "legalgo-BE-go/internal/api/http/subscribe" subscribeplanhttp "legalgo-BE-go/internal/api/http/subscribe_plan" taghttp "legalgo-BE-go/internal/api/http/tag" diff --git a/internal/api/http/staffhttp/get_users.go b/internal/api/http/staff/get_users.go similarity index 93% rename from internal/api/http/staffhttp/get_users.go rename to internal/api/http/staff/get_users.go index 3d55c40..5fab9b3 100644 --- a/internal/api/http/staffhttp/get_users.go +++ b/internal/api/http/staff/get_users.go @@ -2,7 +2,7 @@ package staffhttp import ( authmiddleware "legalgo-BE-go/internal/api/http/middleware/auth" - authsvc "legalgo-BE-go/internal/services/auth" + staffsvc "legalgo-BE-go/internal/services/staffsvc" "legalgo-BE-go/internal/utilities/response" "legalgo-BE-go/internal/utilities/utils" "net/http" @@ -12,7 +12,7 @@ import ( func GetUsers( router chi.Router, - authSvc authsvc.Auth, + authSvc staffsvc.Auth, ) { router.With(authmiddleware.Authorize()).Get("/staff/users", func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() diff --git a/internal/api/http/staffhttp/login.go b/internal/api/http/staff/login.go similarity index 93% rename from internal/api/http/staffhttp/login.go rename to internal/api/http/staff/login.go index 650c2c9..871441f 100644 --- a/internal/api/http/staffhttp/login.go +++ b/internal/api/http/staff/login.go @@ -5,7 +5,7 @@ import ( responsedomain "legalgo-BE-go/internal/domain/reponse" staffdomain "legalgo-BE-go/internal/domain/staff" - authsvc "legalgo-BE-go/internal/services/auth" + staffsvc "legalgo-BE-go/internal/services/staffsvc" "legalgo-BE-go/internal/utilities/response" "legalgo-BE-go/internal/utilities/utils" @@ -16,7 +16,7 @@ import ( func Login( router chi.Router, - authSvc authsvc.Auth, + authSvc staffsvc.Auth, validate *validator.Validate, rdb *redis.Client, ) { @@ -49,7 +49,7 @@ func Login( return } - token, err := authSvc.LoginAsStaff(spec) + token, err := authSvc.Login(spec) if err != nil { response.ResponseWithErrorCode( ctx, diff --git a/internal/api/http/staffhttp/module.go b/internal/api/http/staff/module.go similarity index 100% rename from internal/api/http/staffhttp/module.go rename to internal/api/http/staff/module.go diff --git a/internal/api/http/staffhttp/profile.go b/internal/api/http/staff/profile.go similarity index 84% rename from internal/api/http/staffhttp/profile.go rename to internal/api/http/staff/profile.go index 8324a64..117329c 100644 --- a/internal/api/http/staffhttp/profile.go +++ b/internal/api/http/staff/profile.go @@ -1,7 +1,7 @@ package staffhttp import ( - authsvc "legalgo-BE-go/internal/services/auth" + staffsvc "legalgo-BE-go/internal/services/staffsvc" "legalgo-BE-go/internal/utilities/response" "legalgo-BE-go/internal/utilities/utils" "net/http" @@ -11,7 +11,7 @@ import ( func GetProfile( router chi.Router, - authSvc authsvc.Auth, + authSvc staffsvc.Auth, ) { router.Get("/staff/profile", func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() @@ -28,7 +28,7 @@ func GetProfile( return } - staffProfile, err := authSvc.GetStaffProfile(destructedToken.Email) + staffProfile, err := authSvc.GetProfile(destructedToken.Email) if err != nil { response.ResponseWithErrorCode( ctx, diff --git a/internal/api/http/staffhttp/register.go b/internal/api/http/staff/register.go similarity index 93% rename from internal/api/http/staffhttp/register.go rename to internal/api/http/staff/register.go index 0d891eb..2aa9c22 100644 --- a/internal/api/http/staffhttp/register.go +++ b/internal/api/http/staff/register.go @@ -5,7 +5,7 @@ import ( responsedomain "legalgo-BE-go/internal/domain/reponse" staffdomain "legalgo-BE-go/internal/domain/staff" - authsvc "legalgo-BE-go/internal/services/auth" + staffsvc "legalgo-BE-go/internal/services/staffsvc" "legalgo-BE-go/internal/utilities/response" "legalgo-BE-go/internal/utilities/utils" @@ -17,7 +17,7 @@ import ( func Register( router chi.Router, validate *validator.Validate, - authSvc authsvc.Auth, + authSvc staffsvc.Auth, rdb *redis.Client, ) { router.Post("/staff/register", func(w http.ResponseWriter, r *http.Request) { @@ -49,7 +49,7 @@ func Register( return } - token, err := authSvc.RegisterStaff(spec) + token, err := authSvc.Register(spec) if err != nil { response.ResponseWithErrorCode( ctx, diff --git a/internal/api/http/staffhttp/update.go b/internal/api/http/staff/update.go similarity index 65% rename from internal/api/http/staffhttp/update.go rename to internal/api/http/staff/update.go index eb99046..44b4ef4 100644 --- a/internal/api/http/staffhttp/update.go +++ b/internal/api/http/staff/update.go @@ -1,9 +1,8 @@ package staffhttp import ( - "errors" staffdomain "legalgo-BE-go/internal/domain/staff" - authsvc "legalgo-BE-go/internal/services/auth" + staffsvc "legalgo-BE-go/internal/services/staffsvc" "legalgo-BE-go/internal/utilities/response" "legalgo-BE-go/internal/utilities/utils" "net/http" @@ -13,25 +12,25 @@ import ( func Update( router chi.Router, - authSvc authsvc.Auth, + authSvc staffsvc.Auth, ) { - router.Patch("/staff/{id}/update", func(w http.ResponseWriter, r *http.Request) { + router.Put("/staff/update", func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() - id := chi.URLParam(r, "id") - if id == "" { + destructedToken, err := utils.GetTokenDetail(r) + if err != nil { response.ResponseWithErrorCode( ctx, w, - errors.New("provided id is empty"), + err, response.ErrBadRequest.Code, response.ErrBadRequest.HttpCode, - "required params is not provided", + err.Error(), ) return } - var spec staffdomain.StaffRegister + var spec staffdomain.StaffUpdate if err := utils.UnmarshalBody(r, &spec); err != nil { response.ResponseWithErrorCode( @@ -45,14 +44,7 @@ func Update( return } - staff := staffdomain.Staff{ - ID: id, - Email: spec.Email, - Password: spec.Password, - Name: spec.Name, - } - - if err := authSvc.UpdateStaff(staff); err != nil { + if err := authSvc.Update(destructedToken.ID, spec); err != nil { response.ResponseWithErrorCode( ctx, w, diff --git a/internal/domain/auth/token.go b/internal/domain/auth/token.go index 0bc17c2..438e076 100644 --- a/internal/domain/auth/token.go +++ b/internal/domain/auth/token.go @@ -4,4 +4,5 @@ type AuthToken struct { Email string SessionID string Role string + ID string } diff --git a/internal/domain/staff/spec.go b/internal/domain/staff/spec.go index f174d1b..7166017 100644 --- a/internal/domain/staff/spec.go +++ b/internal/domain/staff/spec.go @@ -15,6 +15,12 @@ type StaffRegister struct { ProfilePicture string `json:"profile_picture"` } +type StaffUpdate struct { + Email string `json:"email" validate:"required"` + Name string `json:"name" validate:"required"` + ProfilePicture string `json:"profile_picture"` +} + type StaffProfile struct { ID string `json:"id"` Name string `json:"name"` diff --git a/internal/enums/jwt/jwt_claims.go b/internal/enums/jwt/jwt_claims.go index 495ee0e..bd91d11 100644 --- a/internal/enums/jwt/jwt_claims.go +++ b/internal/enums/jwt/jwt_claims.go @@ -10,4 +10,5 @@ const ( ISSUED_AT JWTClaim = "iat" RESOURCES JWTClaim = "resources" ROLE JWTClaim = "role" + ID JWTClaim = "skb" ) diff --git a/internal/services/auth/update_staff.go b/internal/services/auth/update_staff.go deleted file mode 100644 index fb147bc..0000000 --- a/internal/services/auth/update_staff.go +++ /dev/null @@ -1,17 +0,0 @@ -package authsvc - -import ( - staffdomain "legalgo-BE-go/internal/domain/staff" -) - -func (as *impl) UpdateStaff(spec staffdomain.Staff) error { - if _, err := as.staffRepo.GetStaffByID(spec.ID); err != nil { - return err - } - - if err := as.staffRepo.Update(spec); err != nil { - return err - } - - return nil -} diff --git a/internal/services/module.go b/internal/services/module.go index d32ddf5..d8dbd13 100644 --- a/internal/services/module.go +++ b/internal/services/module.go @@ -2,10 +2,10 @@ package services import ( adssvc "legalgo-BE-go/internal/services/ads" - serviceauth "legalgo-BE-go/internal/services/auth" categorysvc "legalgo-BE-go/internal/services/category" newssvc "legalgo-BE-go/internal/services/news" "legalgo-BE-go/internal/services/oss" + staffsvc "legalgo-BE-go/internal/services/staffsvc" subscribesvc "legalgo-BE-go/internal/services/subscribe" subscribeplansvc "legalgo-BE-go/internal/services/subscribe_plan" tagsvc "legalgo-BE-go/internal/services/tag" @@ -16,7 +16,7 @@ import ( var Module = fx.Module("services", fx.Provide( - serviceauth.New, + staffsvc.New, subscribeplansvc.New, subscribesvc.New, tagsvc.New, diff --git a/internal/services/auth/get_staff.go b/internal/services/staffsvc/get.go similarity index 77% rename from internal/services/auth/get_staff.go rename to internal/services/staffsvc/get.go index d9f53e4..c4e2aaa 100644 --- a/internal/services/auth/get_staff.go +++ b/internal/services/staffsvc/get.go @@ -1,10 +1,10 @@ -package authsvc +package staffsvc import ( staffdomain "legalgo-BE-go/internal/domain/staff" ) -func (as *impl) GetStaffProfile(email string) (*staffdomain.StaffProfile, error) { +func (as *impl) GetProfile(email string) (*staffdomain.StaffProfile, error) { staff, err := as.staffRepo.GetStaffByEmail(email) if err != nil { return nil, err diff --git a/internal/services/auth/get_users.go b/internal/services/staffsvc/get_users.go similarity index 89% rename from internal/services/auth/get_users.go rename to internal/services/staffsvc/get_users.go index 1025d32..844dd8f 100644 --- a/internal/services/auth/get_users.go +++ b/internal/services/staffsvc/get_users.go @@ -1,4 +1,4 @@ -package authsvc +package staffsvc import userdomain "legalgo-BE-go/internal/domain/user" diff --git a/internal/services/auth/impl.go b/internal/services/staffsvc/impl.go similarity index 80% rename from internal/services/auth/impl.go rename to internal/services/staffsvc/impl.go index 71d82bd..827a41e 100644 --- a/internal/services/auth/impl.go +++ b/internal/services/staffsvc/impl.go @@ -1,4 +1,4 @@ -package authsvc +package staffsvc import ( staffrepository "legalgo-BE-go/internal/accessor/staff" @@ -17,11 +17,11 @@ type impl struct { } type Auth interface { - LoginAsStaff(staffdomain.StaffLogin) (string, error) - RegisterStaff(staffdomain.StaffRegister) (string, error) - GetStaffProfile(string) (*staffdomain.StaffProfile, error) + Login(staffdomain.StaffLogin) (string, error) + Register(staffdomain.StaffRegister) (string, error) + GetProfile(string) (*staffdomain.StaffProfile, error) GetUsers() ([]userdomain.UserProfile, error) - UpdateStaff(staffdomain.Staff) error + Update(string, staffdomain.StaffUpdate) error } func New( diff --git a/internal/services/auth/login_as_staff.go b/internal/services/staffsvc/login.go similarity index 71% rename from internal/services/auth/login_as_staff.go rename to internal/services/staffsvc/login.go index 3698db6..3d44a33 100644 --- a/internal/services/auth/login_as_staff.go +++ b/internal/services/staffsvc/login.go @@ -1,8 +1,7 @@ -package authsvc +package staffsvc import ( - "errors" - + "fmt" authdomain "legalgo-BE-go/internal/domain/auth" staffdomain "legalgo-BE-go/internal/domain/staff" "legalgo-BE-go/internal/utilities/utils" @@ -10,26 +9,27 @@ import ( "github.com/google/uuid" ) -func (sv *impl) LoginAsStaff(spec staffdomain.StaffLogin) (string, error) { +func (sv *impl) Login(spec staffdomain.StaffLogin) (string, error) { staff, err := sv.staffRepo.GetStaffByEmail(spec.Email) if err != nil { - return "", errors.New(err.Error()) + return "", err } matchPassword := utils.ComparePassword(staff.Password, spec.Password) if !matchPassword { - return "", errors.New("wrong password") + return "", fmt.Errorf("wrong password") } authToken := authdomain.AuthToken{ Email: staff.Email, SessionID: uuid.NewString(), Role: "staff", + ID: staff.ID, } token, err := utils.GenerateToken(authToken) if err != nil { - return "", errors.New(err.Error()) + return "", err } return token, nil diff --git a/internal/services/auth/register_staff.go b/internal/services/staffsvc/register.go similarity index 76% rename from internal/services/auth/register_staff.go rename to internal/services/staffsvc/register.go index 04d91ea..82566af 100644 --- a/internal/services/auth/register_staff.go +++ b/internal/services/staffsvc/register.go @@ -1,8 +1,7 @@ -package authsvc +package staffsvc import ( - "errors" - + "fmt" authdomain "legalgo-BE-go/internal/domain/auth" staffdomain "legalgo-BE-go/internal/domain/staff" "legalgo-BE-go/internal/utilities/utils" @@ -10,10 +9,10 @@ import ( "github.com/google/uuid" ) -func (a *impl) RegisterStaff(spec staffdomain.StaffRegister) (string, error) { +func (a *impl) Register(spec staffdomain.StaffRegister) (string, error) { _, err := a.staffRepo.GetStaffByEmail(spec.Email) if err == nil { - return "", errors.New("this email address is already in use") + return "", fmt.Errorf("this email address is already in use") } hashedPwd, err := utils.HashPassword(spec.Password) if err != nil { @@ -30,18 +29,19 @@ func (a *impl) RegisterStaff(spec staffdomain.StaffRegister) (string, error) { err = a.staffRepo.Create(staff) if err != nil { - return "", errors.New(err.Error()) + return "", err } authToken := authdomain.AuthToken{ Email: staff.Email, SessionID: uuid.NewString(), Role: "staff", + ID: staff.ID, } token, err := utils.GenerateToken(authToken) if err != nil { - return "", errors.New(err.Error()) + return "", err } return token, nil } diff --git a/internal/services/staffsvc/update.go b/internal/services/staffsvc/update.go new file mode 100644 index 0000000..240a0de --- /dev/null +++ b/internal/services/staffsvc/update.go @@ -0,0 +1,24 @@ +package staffsvc + +import ( + staffdomain "legalgo-BE-go/internal/domain/staff" +) + +func (as *impl) Update(id string, spec staffdomain.StaffUpdate) error { + if _, err := as.staffRepo.GetStaffByID(id); err != nil { + return err + } + + newSpec := staffdomain.Staff{ + ID: id, + Email: spec.Email, + Name: spec.Name, + ProfilePicture: spec.ProfilePicture, + } + + if err := as.staffRepo.Update(newSpec); err != nil { + return err + } + + return nil +} diff --git a/internal/services/user/login_as_user.go b/internal/services/user/login.go similarity index 83% rename from internal/services/user/login_as_user.go rename to internal/services/user/login.go index ac37350..c9ad83e 100644 --- a/internal/services/user/login_as_user.go +++ b/internal/services/user/login.go @@ -1,8 +1,7 @@ package usersvc import ( - "errors" - + "fmt" authdomain "legalgo-BE-go/internal/domain/auth" userdomain "legalgo-BE-go/internal/domain/user" "legalgo-BE-go/internal/utilities/utils" @@ -13,23 +12,24 @@ import ( func (i *impl) LoginAsUser(spec userdomain.UserLogin) (string, error) { user, err := i.userRepo.GetUserByEmail(spec.Email) if err != nil { - return "", errors.New(err.Error()) + return "", err } matchPassword := utils.ComparePassword(user.Password, spec.Password) if !matchPassword { - return "", errors.New("wrong password") + return "", fmt.Errorf("wrong password") } authToken := authdomain.AuthToken{ Email: user.Email, SessionID: uuid.NewString(), Role: "user", + ID: user.ID, } token, err := utils.GenerateToken(authToken) if err != nil { - return "", errors.New(err.Error()) + return "", err } return token, nil diff --git a/internal/services/user/register_user.go b/internal/services/user/register.go similarity index 86% rename from internal/services/user/register_user.go rename to internal/services/user/register.go index 28a9c68..68feee1 100644 --- a/internal/services/user/register_user.go +++ b/internal/services/user/register.go @@ -1,8 +1,7 @@ package usersvc import ( - "errors" - + "fmt" authdomain "legalgo-BE-go/internal/domain/auth" userdomain "legalgo-BE-go/internal/domain/user" "legalgo-BE-go/internal/utilities/utils" @@ -14,7 +13,7 @@ func (i *impl) RegisterUser(spec userdomain.UserRegister) (string, error) { _, err := i.userRepo.GetUserByEmail(spec.Email) if err == nil { - return "", errors.New("this email address is already in use") + return "", fmt.Errorf("this email address is already in use") } if spec.SubscribePlanID == "" { @@ -28,7 +27,7 @@ func (i *impl) RegisterUser(spec userdomain.UserRegister) (string, error) { _, err = i.subsPlanRepo.GetByID(spec.SubscribePlanID) if err != nil { - return "", errors.New(err.Error()) + return "", err } subsId, err := i.subsRepo.Create(spec.SubscribePlanID) @@ -51,18 +50,19 @@ func (i *impl) RegisterUser(spec userdomain.UserRegister) (string, error) { err = i.userRepo.CreateUser(user) if err != nil { - return "", errors.New(err.Error()) + return "", fmt.Errorf(err.Error()) } authToken := authdomain.AuthToken{ Email: user.Email, SessionID: uuid.NewString(), Role: "user", + ID: user.ID, } token, err := utils.GenerateToken(authToken) if err != nil { - return "", errors.New(err.Error()) + return "", fmt.Errorf(err.Error()) } return token, nil } diff --git a/internal/utilities/utils/get_token_detail.go b/internal/utilities/utils/get_token_detail.go index 22d8cea..6a83d37 100644 --- a/internal/utilities/utils/get_token_detail.go +++ b/internal/utilities/utils/get_token_detail.go @@ -1,7 +1,7 @@ package utils import ( - "errors" + "fmt" authdomain "legalgo-BE-go/internal/domain/auth" "net/http" "strings" @@ -13,16 +13,16 @@ func GetTokenDetail(r *http.Request) (authdomain.AuthToken, error) { var data authdomain.AuthToken if authHeader == "" { - return data, errors.New("unauthorized") + return data, fmt.Errorf("unauthorized") } if !strings.HasPrefix(authHeader, "Bearer") { - return data, errors.New("invalid token") + return data, fmt.Errorf("invalid token") } token := strings.Split(authHeader, " ") if len(token) < 2 { - return data, errors.New("invalid token") + return data, fmt.Errorf("invalid token") } data, err := DestructToken(token[1]) diff --git a/internal/utilities/utils/jwt.go b/internal/utilities/utils/jwt.go index 8aa2b60..b5feeaa 100644 --- a/internal/utilities/utils/jwt.go +++ b/internal/utilities/utils/jwt.go @@ -1,7 +1,6 @@ package utils import ( - "errors" "fmt" "time" @@ -37,6 +36,7 @@ func GenerateToken(data authdomain.AuthToken) (string, error) { string(jwtclaimenum.EMAIL): data.Email, string(jwtclaimenum.ROLE): data.Role, string(jwtclaimenum.SESSION_ID): data.SessionID, + string(jwtclaimenum.ID): data.ID, string(jwtclaimenum.EXPIRED_AT): now.Add(time.Minute * time.Duration(config.REDIS_TIMEOUT)).Unix(), } @@ -66,32 +66,38 @@ func DestructToken(s string) (authdomain.AuthToken, error) { claims, ok := token.Claims.(jwt.MapClaims) if !ok { - return data, errors.New("failed to parse token") + return data, fmt.Errorf("failed to parse token") } if !token.Valid { - return data, errors.New("invalid token") + return data, fmt.Errorf("invalid token") } email, ok := claims[string(jwtclaimenum.EMAIL)].(string) if !ok { - return data, errors.New("invalid email") + return data, fmt.Errorf("invalid email") } sessionId, ok := claims[string(jwtclaimenum.SESSION_ID)].(string) if !ok { - return data, errors.New("invalid session_id") + return data, fmt.Errorf("invalid session_id") } role, ok := claims[string(jwtclaimenum.ROLE)].(string) if !ok { - return data, errors.New("invalid role") + return data, fmt.Errorf("invalid role") + } + + id, ok := claims[string(jwtclaimenum.ID)].(string) + if !ok { + return data, fmt.Errorf("invalid id") } data = authdomain.AuthToken{ Email: email, SessionID: sessionId, Role: role, + ID: id, } return data, nil diff --git a/internal/utilities/utils/struct_to_map.go b/internal/utilities/utils/struct_to_map.go index 23b7efd..95ac7cb 100644 --- a/internal/utilities/utils/struct_to_map.go +++ b/internal/utilities/utils/struct_to_map.go @@ -1,7 +1,7 @@ package utils import ( - "errors" + "fmt" "reflect" ) @@ -11,14 +11,14 @@ func StructToMap(s any) (map[string]any, error) { val := reflect.ValueOf(s) if val.Kind() != reflect.Struct { - return nil, errors.New("provided value is not struct") + return nil, fmt.Errorf("provided value is not struct") } for i := range val.NumField() { field := val.Type().Field(i) value := val.Field(i) - if value.IsZero() || value.IsNil() { + if !value.IsValid() { continue }