refactor: Extract tenant integration helper types to separate file and replace ID generation with id package

- Move TenantAwareServices, TenantResourceQuotaChecker, TenantEventLogger from tenant_integration.go to tenant_integration_core.go
- Move wrapCurrencyService, NewTenantResourceQuotaChecker, NewTenantEventLogger, CheckQuota, UpdateUsage methods to tenant_integration_core.go
- Move LogResourceAccess, LogQuotaViolation methods to tenant_integration_core.go
- Replace generateID helper with id.
This commit is contained in:
nutcas3
2026-05-02 04:41:23 +03:00
parent 9b9d73fed9
commit 064bc3dda5
2 changed files with 105 additions and 100 deletions
@@ -3,12 +3,10 @@ package integration
import (
"context"
"fmt"
"time"
"github.com/nutcas3/telecom-platform/apps/carrier-connector/internal/currency"
"github.com/nutcas3/telecom-platform/apps/carrier-connector/internal/repository"
"github.com/nutcas3/telecom-platform/apps/carrier-connector/internal/services"
"github.com/nutcas3/telecom-platform/apps/carrier-connector/internal/tenant"
"github.com/sirupsen/logrus"
)
@@ -173,101 +171,3 @@ func (m *TenantIntegrationManager) GetTenantAwareServices(ctx context.Context, t
return services, nil
}
// TenantAwareServices provides tenant-aware service instances
type TenantAwareServices struct {
TenantID string
TenantContext *tenant.TenantContext
Config *tenant.TenantConfig
CurrencyService currency.BillingService
}
// wrapCurrencyService creates a tenant-aware currency service
func (m *TenantIntegrationManager) wrapCurrencyService(tenantID string) currency.BillingService {
// This would wrap the existing currency service with tenant isolation
// Implementation depends on the actual currency service structure
return m.currencyService // Placeholder - would need actual wrapping
}
// TenantResourceQuotaChecker checks resource quotas before operations
type TenantResourceQuotaChecker struct {
tenantService *services.TenantServiceImpl
logger *logrus.Logger
}
// NewTenantResourceQuotaChecker creates a new quota checker
func NewTenantResourceQuotaChecker(tenantService *services.TenantServiceImpl, logger *logrus.Logger) *TenantResourceQuotaChecker {
return &TenantResourceQuotaChecker{
tenantService: tenantService,
logger: logger,
}
}
// CheckQuota checks if tenant has sufficient quota for a resource operation
func (c *TenantResourceQuotaChecker) CheckQuota(ctx context.Context, tenantID, resourceType string, count int) error {
return c.tenantService.CheckQuota(ctx, tenantID, resourceType, count)
}
// UpdateUsage updates resource usage after an operation
func (c *TenantResourceQuotaChecker) UpdateUsage(ctx context.Context, tenantID, resourceType string, count int) error {
return c.tenantService.UpdateUsage(ctx, tenantID, resourceType, count)
}
// TenantEventLogger logs tenant events for audit purposes
type TenantEventLogger struct {
tenantService *services.TenantServiceImpl
logger *logrus.Logger
}
// NewTenantEventLogger creates a new tenant event logger
func NewTenantEventLogger(tenantService *services.TenantServiceImpl, logger *logrus.Logger) *TenantEventLogger {
return &TenantEventLogger{
tenantService: tenantService,
logger: logger,
}
}
// LogResourceAccess logs resource access events
func (l *TenantEventLogger) LogResourceAccess(ctx context.Context, tenantID, userID, resourceType, resourceID, action string) {
event := &tenant.TenantEvent{
ID: generateID(),
TenantID: tenantID,
UserID: userID,
EventType: tenant.TenantEventType("resource_access"),
EventData: map[string]any{
"resource_type": resourceType,
"resource_id": resourceID,
"action": action,
},
Timestamp: time.Now(),
}
if err := l.tenantService.LogTenantEvent(ctx, event); err != nil {
l.logger.WithError(err).Error("Failed to log resource access event")
}
}
// LogQuotaViolation logs quota violation events
func (l *TenantEventLogger) LogQuotaViolation(ctx context.Context, tenantID, resourceType string, usage, limit int) {
event := &tenant.TenantEvent{
ID: generateID(),
TenantID: tenantID,
UserID: "",
EventType: tenant.TenantEventQuotaExceeded,
EventData: map[string]any{
"resource_type": resourceType,
"usage": usage,
"limit": limit,
},
Timestamp: time.Now(),
}
if err := l.tenantService.LogTenantEvent(ctx, event); err != nil {
l.logger.WithError(err).Error("Failed to log quota violation event")
}
}
// Helper functions
func generateID() string {
return fmt.Sprintf("tnt_%d", time.Now().UnixNano())
}
@@ -0,0 +1,105 @@
package integration
import (
"context"
"time"
"github.com/nutcas3/telecom-platform/apps/carrier-connector/internal/currency"
"github.com/nutcas3/telecom-platform/apps/carrier-connector/internal/id"
"github.com/nutcas3/telecom-platform/apps/carrier-connector/internal/services"
"github.com/nutcas3/telecom-platform/apps/carrier-connector/internal/tenant"
"github.com/sirupsen/logrus"
)
// TenantAwareServices provides tenant-aware service instances
type TenantAwareServices struct {
TenantID string
TenantContext *tenant.TenantContext
Config *tenant.TenantConfig
CurrencyService currency.BillingService
}
// wrapCurrencyService creates a tenant-aware currency service
func (m *TenantIntegrationManager) wrapCurrencyService(tenantID string) currency.BillingService {
// This would wrap the existing currency service with tenant isolation
// Implementation depends on the actual currency service structure
return m.currencyService // Placeholder - would need actual wrapping
}
// TenantResourceQuotaChecker checks resource quotas before operations
type TenantResourceQuotaChecker struct {
tenantService *services.TenantServiceImpl
logger *logrus.Logger
}
// NewTenantResourceQuotaChecker creates a new quota checker
func NewTenantResourceQuotaChecker(tenantService *services.TenantServiceImpl, logger *logrus.Logger) *TenantResourceQuotaChecker {
return &TenantResourceQuotaChecker{
tenantService: tenantService,
logger: logger,
}
}
// CheckQuota checks if tenant has sufficient quota for a resource operation
func (c *TenantResourceQuotaChecker) CheckQuota(ctx context.Context, tenantID, resourceType string, count int) error {
return c.tenantService.CheckQuota(ctx, tenantID, resourceType, count)
}
// UpdateUsage updates resource usage after an operation
func (c *TenantResourceQuotaChecker) UpdateUsage(ctx context.Context, tenantID, resourceType string, count int) error {
return c.tenantService.UpdateUsage(ctx, tenantID, resourceType, count)
}
// TenantEventLogger logs tenant events for audit purposes
type TenantEventLogger struct {
tenantService *services.TenantServiceImpl
logger *logrus.Logger
}
// NewTenantEventLogger creates a new tenant event logger
func NewTenantEventLogger(tenantService *services.TenantServiceImpl, logger *logrus.Logger) *TenantEventLogger {
return &TenantEventLogger{
tenantService: tenantService,
logger: logger,
}
}
// LogResourceAccess logs resource access events
func (l *TenantEventLogger) LogResourceAccess(ctx context.Context, tenantID, userID, resourceType, resourceID, action string) {
event := &tenant.TenantEvent{
ID: id.GeneratePrefixed("tnt"),
TenantID: tenantID,
UserID: userID,
EventType: tenant.TenantEventType("resource_access"),
EventData: map[string]any{
"resource_type": resourceType,
"resource_id": resourceID,
"action": action,
},
Timestamp: time.Now(),
}
if err := l.tenantService.LogTenantEvent(ctx, event); err != nil {
l.logger.WithError(err).Error("Failed to log resource access event")
}
}
// LogQuotaViolation logs quota violation events
func (l *TenantEventLogger) LogQuotaViolation(ctx context.Context, tenantID, resourceType string, usage, limit int) {
event := &tenant.TenantEvent{
ID: id.GeneratePrefixed("tnt"),
TenantID: tenantID,
UserID: "",
EventType: tenant.TenantEventQuotaExceeded,
EventData: map[string]any{
"resource_type": resourceType,
"usage": usage,
"limit": limit,
},
Timestamp: time.Now(),
}
if err := l.tenantService.LogTenantEvent(ctx, event); err != nil {
l.logger.WithError(err).Error("Failed to log quota violation event")
}
}