diff --git a/action_update.go b/action_update.go new file mode 100644 index 0000000..d47818f --- /dev/null +++ b/action_update.go @@ -0,0 +1,57 @@ +package usermngmt + +import ( + "context" + + "github.com/Selly-Modules/mongodb" + "go.mongodb.org/mongo-driver/bson" +) + +// UpdateOptions ... +type UpdateOptions struct { + Name string + Phone string + Email string + RoleID string + Other string +} + +// UpdateByUserID ... +func (s Service) UpdateByUserID(userID string, payload UpdateOptions) error { + var ( + ctx = context.Background() + ) + + // Validate payload + err := payload.validate(ctx) + if err != nil { + return err + } + + // Setup condition + id, _ := mongodb.NewIDFromString(userID) + cond := bson.M{ + "_id": id, + } + + // Setup update data + roleID, _ := mongodb.NewIDFromString(payload.RoleID) + updateData := bson.M{ + "$set": bson.M{ + "name": payload.Name, + "phone": payload.Phone, + "email": payload.Email, + "roleId": roleID, + "other": payload.Other, + "updatedAt": now(), + }, + } + + // Update + err = s.userUpdateOneByCondition(ctx, cond, updateData) + if err != nil { + return err + } + + return nil +} diff --git a/db.go b/db.go index c5b0d5b..8e8c25e 100644 --- a/db.go +++ b/db.go @@ -83,3 +83,21 @@ func (s Service) userCreate(ctx context.Context, doc User) error { return nil } + +func (s Service) userUpdateOneByCondition(ctx context.Context, cond interface{}, payload interface{}) error { + var ( + col = s.getUserCollection() + ) + + _, err := col.UpdateOne(ctx, cond, payload) + if err != nil { + logger.Error("usermngmt - Update", logger.LogData{ + "cond": cond, + "payload": payload, + "err": err.Error(), + }) + return fmt.Errorf("error when update user: %s", err.Error()) + } + + return err +} \ No newline at end of file diff --git a/validate.go b/validate.go index 71a2b8e..c559f08 100644 --- a/validate.go +++ b/validate.go @@ -73,3 +73,53 @@ func (co CreateOptions) validate(ctx context.Context) error { return nil } + +func (co UpdateOptions) validate(ctx context.Context) error { + // Name + if co.Name == "" { + logger.Error("usermngmt - Update: no name data", logger.LogData{ + "payload": co, + }) + return errors.New("no name data") + } + + // Phone + if co.Phone == "" { + logger.Error("usermngmt - Update: no phone data", logger.LogData{ + "payload": co, + }) + return errors.New("no phone data") + } + + // Email + if co.Email == "" { + logger.Error("usermngmt - Update: no email data", logger.LogData{ + "payload": co, + }) + return errors.New("no email data") + } + + // RoleID + if co.RoleID == "" { + logger.Error("usermngmt - Update: no roleID data", logger.LogData{ + "payload": co, + }) + return errors.New("no role id data") + } + + // Find roleID exists or not + roleID, isValid := mongodb.NewIDFromString(co.RoleID) + if !isValid { + return errors.New("invalid role id data") + } + if !s.isRoleIDExisted(ctx, roleID) { + return errors.New("role id does not exist") + } + + // Find phone number,email exists or not + if s.isPhoneNumberOrEmailExisted(ctx, co.Phone, co.Email) { + return errors.New("phone number or email already existed") + } + + return nil +} \ No newline at end of file