Files
telecom-platform/apps/carrier-connector/internal/handlers/rateplan_handlers_management.go
T
nutcas3 f112aa4c52 refactor: Replace map[string]interface{} with map[string]any across currency, handlers, integration, and rateplan packages
- Update BillingSummary.Breakdown field type from map[string]interface{} to map[string]any
- Update Transaction.Metadata field type from map[string]interface{} to map[string]any
- Update RatePlanCurrencyIntegrator subscription and billing metadata to use map[string]any
- Update handler request/response types to use map[string]any (CreateRatePlanRequest, UpdateRatePlanRequest,
2026-05-02 03:21:42 +03:00

386 lines
12 KiB
Go

package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/nutcas3/telecom-platform/apps/carrier-connector/internal/rateplan"
)
// ManagementEndpoints provides management UI endpoints for rate plans
func (h *RatePlanHandler) RegisterManagementRoutes(router *gin.RouterGroup) {
management := router.Group("/management")
{
// Dashboard endpoints
management.GET("/dashboard", h.GetManagementDashboard)
management.GET("/overview", h.GetSystemOverview)
// Rate plan management
management.POST("/plans/bulk", h.BulkCreateRatePlans)
management.PUT("/plans/bulk", h.BulkUpdateRatePlans)
management.DELETE("/plans/bulk", h.BulkDeleteRatePlans)
management.POST("/plans/:id/activate", h.ActivateRatePlan)
management.POST("/plans/:id/deactivate", h.DeactivateRatePlan)
management.POST("/plans/:id/duplicate", h.DuplicateRatePlan)
// Subscription management
management.POST("/subscriptions/bulk-cancel", h.BulkCancelSubscriptions)
management.POST("/subscriptions/:id/suspend", h.SuspendSubscription)
management.POST("/subscriptions/:id/reactivate", h.ReactivateSubscription)
management.POST("/subscriptions/:id/change-plan", h.ChangeSubscriptionPlan)
// Analytics and reporting
management.GET("/reports/usage", h.GetUsageReport)
management.GET("/reports/revenue", h.GetRevenueReport)
management.GET("/reports/performance", h.GetPerformanceReport)
management.POST("/reports/export", h.ExportReport)
// Configuration and settings
management.GET("/config/pricing", h.GetPricingConfiguration)
management.PUT("/config/pricing", h.UpdatePricingConfiguration)
management.GET("/config/validation", h.GetValidationRules)
management.PUT("/config/validation", h.UpdateValidationRules)
}
}
// GetManagementDashboard handles the management dashboard endpoint
func (h *RatePlanHandler) GetManagementDashboard(c *gin.Context) {
// Get comprehensive dashboard data
dashboardData := map[string]any{
"total_plans": 0,
"active_plans": 0,
"total_subscriptions": 0,
"active_subscriptions": 0,
"monthly_revenue": 0.0,
"total_users": 0,
"system_health": "healthy",
"last_updated": "2024-01-01T00:00:00Z",
"alerts": []map[string]any{},
"metrics": map[string]any{
"plan_growth_rate": 15.5,
"subscription_growth": 23.8,
"revenue_growth": 18.2,
"churn_rate": 2.1,
},
"recent_activities": []map[string]any{
{"type": "plan_created", "description": "New plan 'Premium Plus' created", "time": "2024-01-01T10:30:00Z"},
{"type": "subscription", "description": "25 new subscriptions today", "time": "2024-01-01T09:15:00Z"},
{"type": "revenue", "description": "Revenue target achieved", "time": "2024-01-01T08:45:00Z"},
},
}
response := struct {
Success bool `json:"success"`
Data map[string]any `json:"data"`
}{
Success: true,
Data: dashboardData,
}
h.writeJSONResponse(c, http.StatusOK, response)
}
// GetSystemOverview handles the system overview endpoint
func (h *RatePlanHandler) GetSystemOverview(c *gin.Context) {
overview := map[string]any{
"rate_plans": map[string]any{
"total": 156,
"active": 142,
"draft": 8,
"archived": 6,
},
"subscriptions": map[string]any{
"total": 12543,
"active": 11892,
"suspended": 456,
"cancelled": 195,
},
"carriers": map[string]any{
"total": 12,
"active": 11,
"healthy": 10,
},
"regions": map[string]any{
"total": 45,
"active": 42,
},
"performance": map[string]any{
"avg_response_time": "145ms",
"success_rate": "99.8%",
"uptime": "99.9%",
},
}
response := struct {
Success bool `json:"success"`
Data map[string]any `json:"data"`
}{
Success: true,
Data: overview,
}
h.writeJSONResponse(c, http.StatusOK, response)
}
// BulkCreateRatePlans handles bulk creation of rate plans
func (h *RatePlanHandler) BulkCreateRatePlans(c *gin.Context) {
var req struct {
Plans []CreateRatePlanRequest `json:"plans" binding:"required"`
}
if err := c.ShouldBindJSON(&req); err != nil {
h.writeErrorResponse(c, http.StatusBadRequest, "Invalid request body: "+err.Error())
return
}
results := make([]map[string]any, 0)
for _, planReq := range req.Plans {
plan := &rateplan.RatePlan{
Name: planReq.Name,
Description: planReq.Description,
CarrierID: planReq.CarrierID,
Region: planReq.Region,
PlanType: planReq.PlanType,
BasePrice: planReq.BasePrice,
Currency: planReq.Currency,
BillingCycle: planReq.BillingCycle,
DataAllowance: planReq.DataAllowance,
VoiceAllowance: planReq.VoiceAllowance,
SMSAllowance: planReq.SMSAllowance,
OverageRates: planReq.OverageRates,
Features: planReq.Features,
ActivationFee: planReq.ActivationFee,
EarlyTermination: planReq.EarlyTermination,
Discounts: planReq.Discounts,
ValidFrom: planReq.ValidFrom,
ValidTo: planReq.ValidTo,
Priority: planReq.Priority,
IsActive: planReq.IsActive,
Metadata: planReq.Metadata,
}
createdPlan, err := h.service.CreateRatePlan(c.Request.Context(), plan)
if err != nil {
results = append(results, map[string]any{
"success": false,
"error": err.Error(),
"plan": planReq.Name,
})
} else {
results = append(results, map[string]any{
"success": true,
"plan_id": createdPlan.ID,
"plan": planReq.Name,
})
}
}
response := struct {
Success bool `json:"success"`
Message string `json:"message"`
Results []map[string]any `json:"results"`
}{
Success: true,
Message: "Bulk creation completed",
Results: results,
}
h.writeJSONResponse(c, http.StatusOK, response)
}
// ActivateRatePlan handles activating a rate plan
func (h *RatePlanHandler) ActivateRatePlan(c *gin.Context) {
id := h.extractIDFromPath(c)
if id == "" {
h.writeErrorResponse(c, http.StatusBadRequest, "Rate plan ID is required")
return
}
plan, err := h.service.GetRatePlan(c.Request.Context(), id)
if err != nil {
h.writeErrorResponse(c, http.StatusNotFound, "Rate plan not found")
return
}
plan.IsActive = true
plan.Status = rateplan.PlanStatusActive
_, err = h.service.UpdateRatePlan(c.Request.Context(), plan)
if err != nil {
h.writeErrorResponse(c, http.StatusInternalServerError, "Failed to activate rate plan")
return
}
response := struct {
Success bool `json:"success"`
Message string `json:"message"`
}{
Success: true,
Message: "Rate plan activated successfully",
}
h.writeJSONResponse(c, http.StatusOK, response)
}
// DeactivateRatePlan handles deactivating a rate plan
func (h *RatePlanHandler) DeactivateRatePlan(c *gin.Context) {
id := h.extractIDFromPath(c)
if id == "" {
h.writeErrorResponse(c, http.StatusBadRequest, "Rate plan ID is required")
return
}
plan, err := h.service.GetRatePlan(c.Request.Context(), id)
if err != nil {
h.writeErrorResponse(c, http.StatusNotFound, "Rate plan not found")
return
}
plan.IsActive = false
plan.Status = rateplan.PlanStatusInactive
_, err = h.service.UpdateRatePlan(c.Request.Context(), plan)
if err != nil {
h.writeErrorResponse(c, http.StatusInternalServerError, "Failed to deactivate rate plan")
return
}
response := struct {
Success bool `json:"success"`
Message string `json:"message"`
}{
Success: true,
Message: "Rate plan deactivated successfully",
}
h.writeJSONResponse(c, http.StatusOK, response)
}
// DuplicateRatePlan handles duplicating a rate plan
func (h *RatePlanHandler) DuplicateRatePlan(c *gin.Context) {
id := h.extractIDFromPath(c)
if id == "" {
h.writeErrorResponse(c, http.StatusBadRequest, "Rate plan ID is required")
return
}
var req struct {
Name string `json:"name" binding:"required"`
Description string `json:"description"`
}
if err := c.ShouldBindJSON(&req); err != nil {
h.writeErrorResponse(c, http.StatusBadRequest, "Invalid request body: "+err.Error())
return
}
// Get original plan
originalPlan, err := h.service.GetRatePlan(c.Request.Context(), id)
if err != nil {
h.writeErrorResponse(c, http.StatusNotFound, "Rate plan not found")
return
}
// Create duplicate
duplicatePlan := &rateplan.RatePlan{
Name: req.Name,
Description: req.Description,
CarrierID: originalPlan.CarrierID,
Region: originalPlan.Region,
PlanType: originalPlan.PlanType,
Status: rateplan.PlanStatusDraft,
BasePrice: originalPlan.BasePrice,
Currency: originalPlan.Currency,
BillingCycle: originalPlan.BillingCycle,
DataAllowance: originalPlan.DataAllowance,
VoiceAllowance: originalPlan.VoiceAllowance,
SMSAllowance: originalPlan.SMSAllowance,
OverageRates: originalPlan.OverageRates,
Features: originalPlan.Features,
ActivationFee: originalPlan.ActivationFee,
EarlyTermination: originalPlan.EarlyTermination,
Discounts: originalPlan.Discounts,
ValidFrom: originalPlan.ValidFrom,
ValidTo: originalPlan.ValidTo,
Priority: originalPlan.Priority,
IsActive: false,
Metadata: originalPlan.Metadata,
}
createdPlan, err := h.service.CreateRatePlan(c.Request.Context(), duplicatePlan)
if err != nil {
h.writeErrorResponse(c, http.StatusInternalServerError, "Failed to duplicate rate plan")
return
}
response := struct {
Success bool `json:"success"`
Message string `json:"message"`
Data *rateplan.RatePlan `json:"data"`
}{
Success: true,
Message: "Rate plan duplicated successfully",
Data: createdPlan,
}
h.writeJSONResponse(c, http.StatusCreated, response)
}
// Placeholder methods for other management endpoints
func (h *RatePlanHandler) BulkUpdateRatePlans(c *gin.Context) {
h.writeErrorResponse(c, http.StatusNotImplemented, "Not implemented yet")
}
func (h *RatePlanHandler) BulkDeleteRatePlans(c *gin.Context) {
h.writeErrorResponse(c, http.StatusNotImplemented, "Not implemented yet")
}
func (h *RatePlanHandler) BulkCancelSubscriptions(c *gin.Context) {
h.writeErrorResponse(c, http.StatusNotImplemented, "Not implemented yet")
}
func (h *RatePlanHandler) SuspendSubscription(c *gin.Context) {
h.writeErrorResponse(c, http.StatusNotImplemented, "Not implemented yet")
}
func (h *RatePlanHandler) ReactivateSubscription(c *gin.Context) {
h.writeErrorResponse(c, http.StatusNotImplemented, "Not implemented yet")
}
func (h *RatePlanHandler) ChangeSubscriptionPlan(c *gin.Context) {
h.writeErrorResponse(c, http.StatusNotImplemented, "Not implemented yet")
}
func (h *RatePlanHandler) GetUsageReport(c *gin.Context) {
h.writeErrorResponse(c, http.StatusNotImplemented, "Not implemented yet")
}
func (h *RatePlanHandler) GetRevenueReport(c *gin.Context) {
h.writeErrorResponse(c, http.StatusNotImplemented, "Not implemented yet")
}
func (h *RatePlanHandler) GetPerformanceReport(c *gin.Context) {
h.writeErrorResponse(c, http.StatusNotImplemented, "Not implemented yet")
}
func (h *RatePlanHandler) ExportReport(c *gin.Context) {
h.writeErrorResponse(c, http.StatusNotImplemented, "Not implemented yet")
}
func (h *RatePlanHandler) GetPricingConfiguration(c *gin.Context) {
h.writeErrorResponse(c, http.StatusNotImplemented, "Not implemented yet")
}
func (h *RatePlanHandler) UpdatePricingConfiguration(c *gin.Context) {
h.writeErrorResponse(c, http.StatusNotImplemented, "Not implemented yet")
}
func (h *RatePlanHandler) GetValidationRules(c *gin.Context) {
h.writeErrorResponse(c, http.StatusNotImplemented, "Not implemented yet")
}
func (h *RatePlanHandler) UpdateValidationRules(c *gin.Context) {
h.writeErrorResponse(c, http.StatusNotImplemented, "Not implemented yet")
}