Update Mitra
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"furtuna-be/internal/common/mycontext"
|
||||
"gorm.io/gorm"
|
||||
@@ -101,6 +102,10 @@ func (s *UserService) Update(ctx mycontext.Context, id int64, userReq *entity.Us
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if existingUser == nil {
|
||||
return nil, errors.New("user not found")
|
||||
}
|
||||
|
||||
//if changed branch
|
||||
if *userReq.PartnerID > 0 {
|
||||
branch, err := s.branchRepo.GetBranchByID(ctx, *userReq.PartnerID)
|
||||
@@ -125,6 +130,34 @@ func (s *UserService) Update(ctx mycontext.Context, id int64, userReq *entity.Us
|
||||
return updatedUserDB.ToUser(), nil
|
||||
}
|
||||
|
||||
func (s *UserService) UpdateWithTx(ctx mycontext.Context, tx *gorm.DB, req *entity.User) (*entity.User, error) {
|
||||
existingUser, err := s.repo.GetUserByID(ctx, req.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if existingUser == nil {
|
||||
return nil, errors.New("user not found")
|
||||
}
|
||||
|
||||
if *existingUser.PartnerID != *req.PartnerID {
|
||||
return nil, errors.New("invalid request")
|
||||
}
|
||||
|
||||
err = existingUser.ToUpdatedUser(*req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
updatedUserDB, err := s.repo.UpdateUserWithTx(ctx, tx, existingUser)
|
||||
if err != nil {
|
||||
logger.ContextLogger(ctx).Error("error when update user", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return updatedUserDB.ToUser(), nil
|
||||
}
|
||||
|
||||
func (s *UserService) Delete(ctx mycontext.Context, id int64) error {
|
||||
userDB, err := s.repo.GetUserByID(ctx, id)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user