implemented mongo gridFS for file upload
This commit is contained in:
@@ -50,6 +50,9 @@ type Company struct {
|
||||
Phone *string `bson:"phone,omitempty" json:"phone,omitempty"`
|
||||
Email *string `bson:"email,omitempty" json:"email,omitempty"`
|
||||
|
||||
// File references stored in GridFS
|
||||
DocumentFileIDs []string `bson:"document_file_ids,omitempty" json:"document_file_ids,omitempty"`
|
||||
|
||||
// Tags for categorization and search
|
||||
Tags *CompanyTags `bson:"tags,omitempty" json:"tags,omitempty"`
|
||||
|
||||
|
||||
@@ -24,6 +24,8 @@ type (
|
||||
// Contact information
|
||||
Phone *string `json:"phone,omitempty" valid:"optional,length(10|20)" example:"+1234567890"`
|
||||
Email *string `json:"email,omitempty" valid:"optional,email" example:"info@opplens.com"`
|
||||
// GridFS document references (uploaded via /api/v1/files/upload or /admin/v1/files/upload)
|
||||
DocumentFileIDs []string `json:"document_file_ids,omitempty" valid:"optional"`
|
||||
|
||||
// Tags for categorization and search
|
||||
Tags *CompanyTagsForm `json:"tags,omitempty"`
|
||||
@@ -122,6 +124,7 @@ type (
|
||||
Address *Address `json:"address,omitempty"`
|
||||
Phone *string `json:"phone,omitempty"`
|
||||
Email *string `json:"email,omitempty"`
|
||||
DocumentFileIDs []string `json:"document_file_ids,omitempty"`
|
||||
Tags *CompanyTagsResponse `json:"tags,omitempty"`
|
||||
IsVerified bool `json:"is_verified"`
|
||||
IsCompliant bool `json:"is_compliant"`
|
||||
@@ -160,6 +163,7 @@ func (c *Company) ToResponse(categories []*CategoryResponse) *CompanyResponse {
|
||||
Address: c.Address,
|
||||
Phone: c.Phone,
|
||||
Email: c.Email,
|
||||
DocumentFileIDs: c.DocumentFileIDs,
|
||||
Tags: c.Tags.ToResponse(categories),
|
||||
IsVerified: c.IsVerified,
|
||||
IsCompliant: c.IsCompliant,
|
||||
@@ -189,6 +193,7 @@ type CompanyProfileResponse struct {
|
||||
RegistrationNumber string `json:"registration_number"`
|
||||
Industry string `json:"industry"`
|
||||
FoundedYear *int `json:"founded_year,omitempty"`
|
||||
DocumentFileIDs []string `json:"document_file_ids,omitempty"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
}
|
||||
|
||||
@@ -200,6 +205,7 @@ func (c *Company) ToProfileResponse() *CompanyProfileResponse {
|
||||
RegistrationNumber: c.RegistrationNumber,
|
||||
Industry: c.Industry,
|
||||
FoundedYear: c.FoundedYear,
|
||||
DocumentFileIDs: c.DocumentFileIDs,
|
||||
CreatedAt: c.CreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,6 +98,7 @@ func (s *companyService) Create(ctx context.Context, form *CompanyForm) (*Compan
|
||||
Address: s.convertAddressForm(form.Address),
|
||||
Phone: form.Phone,
|
||||
Email: form.Email,
|
||||
DocumentFileIDs: form.DocumentFileIDs,
|
||||
Tags: s.convertCompanyTagsForm(form.Tags),
|
||||
IsVerified: false,
|
||||
IsCompliant: false,
|
||||
@@ -259,6 +260,10 @@ func (s *companyService) Update(ctx context.Context, id string, form *CompanyFor
|
||||
company.Email = form.Email
|
||||
}
|
||||
|
||||
if form.DocumentFileIDs != nil {
|
||||
company.DocumentFileIDs = form.DocumentFileIDs
|
||||
}
|
||||
|
||||
if form.Tags != nil {
|
||||
company.Tags = s.convertCompanyTagsForm(form.Tags)
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"tm/pkg/response"
|
||||
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
mongopkg "go.mongodb.org/mongo-driver/v2/mongo"
|
||||
)
|
||||
|
||||
// Repository defines methods for user data access
|
||||
@@ -26,8 +27,9 @@ type Repository interface {
|
||||
|
||||
// userRepository implements the Repository interface using the MongoDB ORM
|
||||
type userRepository struct {
|
||||
ormRepo orm.Repository[User]
|
||||
logger logger.Logger
|
||||
ormRepo orm.Repository[User]
|
||||
collection *mongopkg.Collection
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
// NewUserRepository creates a new user repository
|
||||
@@ -46,11 +48,13 @@ func NewRepository(mongoManager *orm.ConnectionManager, logger logger.Logger) Re
|
||||
}
|
||||
|
||||
// Create ORM repository
|
||||
ormRepo := orm.NewRepository[User](mongoManager.GetCollection("users"), logger)
|
||||
collection := mongoManager.GetCollection("users")
|
||||
ormRepo := orm.NewRepository[User](collection, logger)
|
||||
|
||||
return &userRepository{
|
||||
ormRepo: ormRepo,
|
||||
logger: logger,
|
||||
ormRepo: ormRepo,
|
||||
collection: collection,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,6 +121,35 @@ func (r *userRepository) Update(ctx context.Context, user *User) error {
|
||||
// Use ORM to update user
|
||||
err := r.ormRepo.Update(ctx, user)
|
||||
if err != nil {
|
||||
// Backward compatibility for legacy records with string _id.
|
||||
legacyFilterID := user.ID.Hex()
|
||||
updateDoc := bson.M{
|
||||
"$set": bson.M{
|
||||
"full_name": user.FullName,
|
||||
"username": user.Username,
|
||||
"email": user.Email,
|
||||
"password": user.Password,
|
||||
"role": user.Role,
|
||||
"status": user.Status,
|
||||
"department": user.Department,
|
||||
"position": user.Position,
|
||||
"phone": user.Phone,
|
||||
"profile_image": user.ProfileImage,
|
||||
"device_token": user.DeviceToken,
|
||||
"is_verified": user.IsVerified,
|
||||
"last_login_at": user.LastLoginAt,
|
||||
"updated_at": user.UpdatedAt,
|
||||
},
|
||||
}
|
||||
legacyResult, legacyErr := r.collection.UpdateOne(ctx, bson.M{"_id": legacyFilterID}, updateDoc)
|
||||
if legacyErr == nil && legacyResult.MatchedCount > 0 {
|
||||
r.logger.Info("User updated successfully using legacy string ID", map[string]interface{}{
|
||||
"user_id": legacyFilterID,
|
||||
"email": user.Email,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.logger.Error("Failed to update user", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"user_id": user.ID,
|
||||
@@ -137,7 +170,19 @@ func (r *userRepository) GetByID(ctx context.Context, id string) (*User, error)
|
||||
user, err := r.ormRepo.FindByID(ctx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, orm.ErrDocumentNotFound) {
|
||||
return nil, errors.New("user not found")
|
||||
// Backward compatibility: some legacy seed data may store _id as string instead of ObjectID.
|
||||
legacyUser, legacyErr := r.ormRepo.FindOne(ctx, bson.M{"_id": id})
|
||||
if legacyErr == nil {
|
||||
return legacyUser, nil
|
||||
}
|
||||
if errors.Is(legacyErr, orm.ErrDocumentNotFound) {
|
||||
return nil, errors.New("user not found")
|
||||
}
|
||||
r.logger.Error("Failed to get user by legacy string ID", map[string]interface{}{
|
||||
"error": legacyErr.Error(),
|
||||
"user_id": id,
|
||||
})
|
||||
return nil, legacyErr
|
||||
}
|
||||
r.logger.Error("Failed to get user by ID", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
|
||||
Reference in New Issue
Block a user