Enhance company document management with file ID sanitization and detachment functionality
continuous-integration/drone/push Build is passing

- Updated the `UploadDocuments` method to sanitize document file IDs before saving, ensuring only valid references are stored.
- Introduced `DetachDocumentFileID` method in the `companyService` to remove file IDs from all companies referencing a deleted file, improving data integrity.
- Enhanced the `companyRepository` with a new method to handle the removal of document file IDs from the database.
- Updated the `filestore` handler to utilize the new detachment functionality when files are deleted, ensuring consistent state across domain entities.

This update improves the management of document file IDs within the company domain, enhancing data integrity and reference handling.
This commit is contained in:
Mazyar
2026-06-22 22:37:50 +03:30
parent 3bfed0dc74
commit 5fbfcd4149
5 changed files with 113 additions and 14 deletions
+42 -5
View File
@@ -11,6 +11,7 @@ import (
"tm/pkg/response"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
)
// Repository defines the interface for company data operations
@@ -26,12 +27,14 @@ type Repository interface {
Delete(ctx context.Context, id string) error
UpdateStatus(ctx context.Context, id string, status CompanyStatus) error
UpdateVerification(ctx context.Context, id string, isVerified, isCompliant bool, complianceNotes *string) error
RemoveDocumentFileID(ctx context.Context, fileID string) error
}
// companyRepository implements the Repository interface using the MongoDB ORM
type companyRepository struct {
ormRepo orm.Repository[Company]
logger logger.Logger
ormRepo orm.Repository[Company]
collection *mongo.Collection
logger logger.Logger
}
// NewCompanyRepository creates a new company repository
@@ -49,12 +52,15 @@ func NewRepository(mongoManager *orm.ConnectionManager, logger logger.Logger) Re
})
}
collection := mongoManager.GetCollection("companies")
// Create ORM repository
ormRepo := orm.NewRepository[Company](mongoManager.GetCollection("companies"), logger)
ormRepo := orm.NewRepository[Company](collection, logger)
return &companyRepository{
ormRepo: ormRepo,
logger: logger,
ormRepo: ormRepo,
collection: collection,
logger: logger,
}
}
@@ -413,3 +419,34 @@ func (r *companyRepository) UpdateVerification(ctx context.Context, id string, i
return nil
}
// RemoveDocumentFileID removes a file ID from all companies that reference it.
func (r *companyRepository) RemoveDocumentFileID(ctx context.Context, fileID string) error {
if fileID == "" {
return nil
}
filter := bson.M{"document_file_ids": fileID}
update := bson.M{
"$pull": bson.M{"document_file_ids": fileID},
"$set": bson.M{"updated_at": time.Now().Unix()},
}
result, err := r.collection.UpdateMany(ctx, filter, update)
if err != nil {
r.logger.Error("Failed to remove document file ID from companies", map[string]interface{}{
"error": err.Error(),
"file_id": fileID,
})
return err
}
if result.ModifiedCount > 0 {
r.logger.Info("Removed document file ID from companies", map[string]interface{}{
"file_id": fileID,
"modified_count": result.ModifiedCount,
})
}
return nil
}