mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-11-22 11:58:48 +00:00
- Add RatePlanHandler struct with service and logger fields - Add CreateRatePlan with JSON binding, draft status initialization, and comprehensive field mapping - Add GetRatePlan with ID path parameter extraction and not found handling - Add UpdateRatePlan with selective field updates and existing plan retrieval - Add DeleteRatePlan with active subscription conflict detection - Add ListRatePlans with carrier
176 lines
5.2 KiB
Go
176 lines
5.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/nutcas3/telecom-platform/apps/carrier-connector/internal/rateplan"
|
|
)
|
|
|
|
// RecordUsageRequest represents the request to record usage
|
|
type RecordUsageRequest struct {
|
|
ProfileID string `json:"profile_id" binding:"required"`
|
|
DataUsed int64 `json:"data_used" binding:"required,min=0"`
|
|
VoiceUsed int64 `json:"voice_used" binding:"required,min=0"`
|
|
SMSUsed int64 `json:"sms_used" binding:"required,min=0"`
|
|
}
|
|
|
|
// UsageResponse represents the response for usage operations
|
|
type UsageResponse struct {
|
|
Success bool `json:"success"`
|
|
Message string `json:"message,omitempty"`
|
|
Data *rateplan.RatePlanUsage `json:"data,omitempty"`
|
|
}
|
|
|
|
// UsageHistoryResponse represents the response for usage history
|
|
type UsageHistoryResponse struct {
|
|
Success bool `json:"success"`
|
|
Message string `json:"message,omitempty"`
|
|
Data []*rateplan.RatePlanUsage `json:"data,omitempty"`
|
|
}
|
|
|
|
// RecordUsage handles recording usage for a subscription
|
|
func (h *RatePlanHandler) RecordUsage(c *gin.Context) {
|
|
var req RecordUsageRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
h.writeErrorResponse(c, http.StatusBadRequest, "Invalid request body: "+err.Error())
|
|
return
|
|
}
|
|
|
|
recordReq := &rateplan.RecordUsageRequest{
|
|
ProfileID: req.ProfileID,
|
|
DataUsed: req.DataUsed,
|
|
VoiceUsed: req.VoiceUsed,
|
|
SMSUsed: req.SMSUsed,
|
|
}
|
|
|
|
usage, err := h.service.RecordUsage(c.Request.Context(), recordReq)
|
|
if err != nil {
|
|
h.logger.WithError(err).Error("Failed to record usage")
|
|
h.writeErrorResponse(c, http.StatusInternalServerError, "Failed to record usage: "+err.Error())
|
|
return
|
|
}
|
|
|
|
response := UsageResponse{
|
|
Success: true,
|
|
Message: "Usage recorded successfully",
|
|
Data: usage,
|
|
}
|
|
|
|
h.writeJSONResponse(c, http.StatusCreated, response)
|
|
}
|
|
|
|
// GetUsage handles retrieving current usage for a profile
|
|
func (h *RatePlanHandler) GetUsage(c *gin.Context) {
|
|
profileID := c.Query("profile_id")
|
|
if profileID == "" {
|
|
h.writeErrorResponse(c, http.StatusBadRequest, "Profile ID is required")
|
|
return
|
|
}
|
|
|
|
usage, err := h.service.GetUsage(c.Request.Context(), profileID)
|
|
if err != nil {
|
|
h.logger.WithError(err).Error("Failed to get usage")
|
|
h.writeErrorResponse(c, http.StatusInternalServerError, "Failed to get usage")
|
|
return
|
|
}
|
|
|
|
if usage == nil {
|
|
response := struct {
|
|
Success bool `json:"success"`
|
|
Message string `json:"message"`
|
|
}{
|
|
Success: false,
|
|
Message: "No usage data found",
|
|
}
|
|
h.writeJSONResponse(c, http.StatusOK, response)
|
|
return
|
|
}
|
|
|
|
response := UsageResponse{
|
|
Success: true,
|
|
Data: usage,
|
|
}
|
|
|
|
h.writeJSONResponse(c, http.StatusOK, response)
|
|
}
|
|
|
|
// GetUsageHistory handles retrieving usage history for a profile
|
|
func (h *RatePlanHandler) GetUsageHistory(c *gin.Context) {
|
|
profileID := c.Query("profile_id")
|
|
if profileID == "" {
|
|
h.writeErrorResponse(c, http.StatusBadRequest, "Profile ID is required")
|
|
return
|
|
}
|
|
|
|
limit := 10 // default limit
|
|
if limitStr := c.Query("limit"); limitStr != "" {
|
|
if parsedLimit, err := strconv.Atoi(limitStr); err == nil && parsedLimit > 0 {
|
|
limit = parsedLimit
|
|
}
|
|
}
|
|
|
|
usageHistory, err := h.service.GetUsageHistory(c.Request.Context(), profileID, limit)
|
|
if err != nil {
|
|
h.logger.WithError(err).Error("Failed to get usage history")
|
|
h.writeErrorResponse(c, http.StatusInternalServerError, "Failed to get usage history")
|
|
return
|
|
}
|
|
|
|
response := UsageHistoryResponse{
|
|
Success: true,
|
|
Data: usageHistory,
|
|
}
|
|
|
|
h.writeJSONResponse(c, http.StatusOK, response)
|
|
}
|
|
|
|
// CalculateCostRequest represents the request to calculate cost
|
|
type CalculateCostRequest struct {
|
|
RatePlanID string `json:"rate_plan_id" binding:"required"`
|
|
DataUsed int64 `json:"data_used" binding:"required,min=0"`
|
|
VoiceUsed int64 `json:"voice_used" binding:"required,min=0"`
|
|
SMSUsed int64 `json:"sms_used" binding:"required,min=0"`
|
|
AppliedDiscounts []string `json:"applied_discounts,omitempty"`
|
|
}
|
|
|
|
// CostCalculationResponse represents the response for cost calculation
|
|
type CostCalculationResponse struct {
|
|
Success bool `json:"success"`
|
|
Message string `json:"message,omitempty"`
|
|
Data *rateplan.RatePlanCostCalculation `json:"data,omitempty"`
|
|
}
|
|
|
|
// CalculateCost handles calculating cost for a rate plan based on usage
|
|
func (h *RatePlanHandler) CalculateCost(c *gin.Context) {
|
|
var req CalculateCostRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
h.writeErrorResponse(c, http.StatusBadRequest, "Invalid request body: "+err.Error())
|
|
return
|
|
}
|
|
|
|
calcReq := &rateplan.CalculateCostRequest{
|
|
RatePlanID: req.RatePlanID,
|
|
DataUsed: req.DataUsed,
|
|
VoiceUsed: req.VoiceUsed,
|
|
SMSUsed: req.SMSUsed,
|
|
AppliedDiscounts: req.AppliedDiscounts,
|
|
}
|
|
|
|
calculation, err := h.service.CalculateCost(c.Request.Context(), calcReq)
|
|
if err != nil {
|
|
h.logger.WithError(err).Error("Failed to calculate cost")
|
|
h.writeErrorResponse(c, http.StatusInternalServerError, "Failed to calculate cost: "+err.Error())
|
|
return
|
|
}
|
|
|
|
response := CostCalculationResponse{
|
|
Success: true,
|
|
Data: calculation,
|
|
}
|
|
|
|
h.writeJSONResponse(c, http.StatusOK, response)
|
|
}
|