Refactor user domain to utilize string IDs and integrate MongoDB ORM

- Updated User entity to embed MongoDB model and replace uuid.UUID with string for ID fields.
- Modified repository methods to accept string IDs instead of uuid.UUID, enhancing compatibility with MongoDB.
- Refactored service and handler methods to align with the new ID type, ensuring consistent usage across the user management functionality.
- Improved response handling in UserListResponse to directly use string IDs.
- Streamlined index creation in the user repository using the MongoDB ORM for better maintainability.
This commit is contained in:
n.nakhostin
2025-08-11 13:45:46 +03:30
parent 8c1e593686
commit 08cf927294
6 changed files with 252 additions and 355 deletions
+47 -19
View File
@@ -1,7 +1,7 @@
package user
import (
"github.com/google/uuid"
"tm/pkg/mongo"
)
// UserRole represents user roles in the system
@@ -25,22 +25,50 @@ const (
// User represents a system user (admin/manager/operator)
type User struct {
ID uuid.UUID `bson:"_id"`
FullName string `bson:"full_name"`
Username string `bson:"username"`
Email string `bson:"email"`
Password string `bson:"password"` // Never serialize password
Role UserRole `bson:"role"`
Status UserStatus `bson:"status"`
CompanyID *uuid.UUID `bson:"company_id,omitempty"`
Department *string `bson:"department,omitempty"`
Position *string `bson:"position,omitempty"`
Phone *string `bson:"phone,omitempty"`
ProfileImage *string `bson:"profile_image,omitempty"`
IsVerified bool `bson:"is_verified"`
LastLoginAt *int64 `bson:"last_login_at,omitempty"` // Unix timestamp
CreatedAt int64 `bson:"created_at"` // Unix timestamp
UpdatedAt int64 `bson:"updated_at"` // Unix timestamp
CreatedBy *uuid.UUID `bson:"created_by,omitempty"`
UpdatedBy *uuid.UUID `bson:"updated_by,omitempty"`
mongo.Model
FullName string `bson:"full_name" json:"full_name"`
Username string `bson:"username" json:"username"`
Email string `bson:"email" json:"email"`
Password string `bson:"password" json:"-"` // Never serialize password
Role UserRole `bson:"role" json:"role"`
Status UserStatus `bson:"status" json:"status"`
CompanyID *string `bson:"company_id,omitempty" json:"company_id,omitempty"`
Department *string `bson:"department,omitempty" json:"department,omitempty"`
Position *string `bson:"position,omitempty" json:"position,omitempty"`
Phone *string `bson:"phone,omitempty" json:"phone,omitempty"`
ProfileImage *string `bson:"profile_image,omitempty" json:"profile_image,omitempty"`
IsVerified bool `bson:"is_verified" json:"is_verified"`
LastLoginAt *int64 `bson:"last_login_at,omitempty" json:"last_login_at,omitempty"` // Unix timestamp
CreatedBy *string `bson:"created_by,omitempty" json:"created_by,omitempty"`
UpdatedBy *string `bson:"updated_by,omitempty" json:"updated_by,omitempty"`
}
// SetID sets the user ID (implements IDSetter interface)
func (u *User) SetID(id string) {
u.ID = id
}
// GetID returns the user ID (implements IDGetter interface)
func (u *User) GetID() string {
return u.ID
}
// SetCreatedAt sets the created timestamp (implements Timestampable interface)
func (u *User) SetCreatedAt(timestamp int64) {
u.CreatedAt = timestamp
}
// SetUpdatedAt sets the updated timestamp (implements Timestampable interface)
func (u *User) SetUpdatedAt(timestamp int64) {
u.UpdatedAt = timestamp
}
// GetCreatedAt returns the created timestamp (implements Timestampable interface)
func (u *User) GetCreatedAt() int64 {
return u.CreatedAt
}
// GetUpdatedAt returns the updated timestamp (implements Timestampable interface)
func (u *User) GetUpdatedAt() int64 {
return u.UpdatedAt
}