udpate User

This commit is contained in:
aditya.siregar 2024-08-28 14:28:57 +07:00
parent 083e5537ec
commit c1cfd794fc
3 changed files with 21 additions and 29 deletions

View File

@ -72,7 +72,7 @@ func (h *Handler) Create(c *gin.Context) {
return
}
res, err := h.service.Create(ctx, req.ToEntity())
res, err := h.service.Create(ctx, req.ToEntity(ctx))
if err != nil {
response.ErrorWrapper(c, err)
return
@ -110,11 +110,6 @@ func (h *Handler) Create(c *gin.Context) {
func (h *Handler) Update(c *gin.Context) {
ctx := request.GetMyContext(c)
if !ctx.IsSuperAdmin() {
response.ErrorWrapper(c, errors.ErrorUnauthorized)
return
}
id := c.Param("id")
userID, err := strconv.ParseInt(id, 10, 64)
@ -135,7 +130,7 @@ func (h *Handler) Update(c *gin.Context) {
return
}
updatedUser, err := h.service.Update(ctx, userID, req.ToEntity())
updatedUser, err := h.service.Update(ctx, userID, req.ToEntity(ctx))
if err != nil {
response.ErrorWrapper(c, err)
return
@ -164,7 +159,7 @@ func (h *Handler) UpdateCustomer(c *gin.Context) {
return
}
updatedUser, err := h.service.Update(ctx, userID, req.ToEntity())
updatedUser, err := h.service.Update(ctx, userID, req.ToEntity(ctx))
if err != nil {
response.ErrorWrapper(c, err)
return

View File

@ -4,14 +4,13 @@ import (
"furtuna-be/internal/common/mycontext"
"furtuna-be/internal/constants/role"
"furtuna-be/internal/entity"
"github.com/go-playground/validator/v10"
)
type User struct {
Name string `json:"name" validate:"required"`
Email string `json:"email" validate:"required,email"`
Password string `json:"password" validate:"required"`
Password string `json:"password"`
PartnerID *int64 `json:"partner_id"`
SiteID *int64 `json:"site_id"`
RoleID role.Role `json:"role_id" validate:"required"`
@ -29,7 +28,11 @@ func (e *User) Validate() error {
return nil
}
func (u *User) ToEntity() *entity.User {
func (u *User) ToEntity(ctx mycontext.Context) *entity.User {
if !ctx.IsAdmin() {
u.PartnerID = ctx.GetPartnerID()
}
return &entity.User{
Name: u.Name,
Email: u.Email,

View File

@ -115,15 +115,9 @@ func (s *UserService) Update(ctx mycontext.Context, id int64, userReq *entity.Us
return nil, errors.New("user not found")
}
//if changed branch
// if userReq.PartnerID != nil {
// branch, err := s.branchRepo.GetBranchByID(ctx, *userReq.PartnerID)
// if err != nil {
// return nil, err
// }
// existingUser.PartnerName = branch.Name
// }
if *existingUser.PartnerID != *userReq.PartnerID {
return nil, errors.New("user not found")
}
err = existingUser.ToUpdatedUser(*userReq)
if err != nil {