udpate User
This commit is contained in:
parent
083e5537ec
commit
c1cfd794fc
@ -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
|
||||
@ -320,7 +315,7 @@ func (h *Handler) toUserResponse(resp *entity.User) response.User {
|
||||
Name: resp.Name,
|
||||
Email: resp.Email,
|
||||
PhoneNumber: resp.PhoneNumber,
|
||||
NIK: resp.NIK,
|
||||
NIK: resp.NIK,
|
||||
Status: string(resp.Status),
|
||||
RoleID: int64(resp.RoleID),
|
||||
RoleName: resp.RoleName,
|
||||
|
||||
@ -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,16 +28,20 @@ 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,
|
||||
Password: u.Password,
|
||||
RoleID: role.Role(u.RoleID),
|
||||
PartnerID: u.PartnerID,
|
||||
SiteID: u.SiteID,
|
||||
NIK: u.NIK,
|
||||
UserType: u.UserType,
|
||||
Name: u.Name,
|
||||
Email: u.Email,
|
||||
Password: u.Password,
|
||||
RoleID: role.Role(u.RoleID),
|
||||
PartnerID: u.PartnerID,
|
||||
SiteID: u.SiteID,
|
||||
NIK: u.NIK,
|
||||
UserType: u.UserType,
|
||||
PhoneNumber: u.PhoneNumber,
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user