From 064bc3dda5e5ba2d1b7c2f86f025e29b6ea0b8f8 Mon Sep 17 00:00:00 2001 From: nutcas3 Date: Sat, 2 May 2026 04:41:23 +0300 Subject: [PATCH] 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. --- .../integration/tenant_integration.go | 100 ----------------- .../integration/tenant_integration_core.go | 105 ++++++++++++++++++ 2 files changed, 105 insertions(+), 100 deletions(-) create mode 100644 apps/carrier-connector/internal/integration/tenant_integration_core.go diff --git a/apps/carrier-connector/internal/integration/tenant_integration.go b/apps/carrier-connector/internal/integration/tenant_integration.go index 57e8ccc..1b06c24 100644 --- a/apps/carrier-connector/internal/integration/tenant_integration.go +++ b/apps/carrier-connector/internal/integration/tenant_integration.go @@ -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()) -} diff --git a/apps/carrier-connector/internal/integration/tenant_integration_core.go b/apps/carrier-connector/internal/integration/tenant_integration_core.go new file mode 100644 index 0000000..2375ce6 --- /dev/null +++ b/apps/carrier-connector/internal/integration/tenant_integration_core.go @@ -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") + } +}