fix: port assignment bulk update

This commit is contained in:
HasanMu 2025-07-16 08:58:19 +07:00
parent 689505fa6a
commit 0c25a3a941
7 changed files with 1465 additions and 1351 deletions

View File

@ -13,9 +13,9 @@ import (
type Server struct {
ucManager manager.UsecaseManager
engine *gin.Engine
host string
cfg *config.Config
engine *gin.Engine
host string
cfg *config.Config
}
func NewServer() *Server {
@ -41,66 +41,62 @@ func NewServer() *Server {
return &Server{
ucManager: ucManager,
engine: engine,
host: host,
cfg: cfg,
engine: engine,
host: host,
cfg: cfg,
}
}
func (s *Server) setupController() {
s.engine.Static("/uploads", "./uploads")
rg := s.engine.Group("/api/v1")
// Add bypass controller FIRST - before any auth middleware
controller.NewBypassController(s.ucManager.NewUserUsecase(), rg).Route()
// Add auth controller (public endpoints)
controller.NewAuthController(s.ucManager.NewAuthUsecase(), rg).Route()
publicRegistration := rg.Group("/user-registration")
{
publicRegistration.POST("/register", controller.NewUserRegistrationController(s.ucManager.NewUserUsecase(), rg).RegisterUser)
}
s.engine.Static("/uploads", "./uploads")
rg := s.engine.Group("/api/v1")
// Add bypass controller FIRST - before any auth middleware
controller.NewBypassController(s.ucManager.NewUserUsecase(), rg).Route()
// Add auth controller (public endpoints)
controller.NewAuthController(s.ucManager.NewAuthUsecase(), rg).Route()
publicRegistration := rg.Group("/user-registration")
{
publicRegistration.POST("/register", controller.NewUserRegistrationController(s.ucManager.NewUserUsecase(), rg).RegisterUser)
}
// Add user registration controller (public endpoints)
// controller.NewUserRegistrationController(s.ucManager.NewUserUsecase(), rg).Route()
// Add user registration controller (public endpoints)
// controller.NewUserRegistrationController(s.ucManager.NewUserUsecase(), rg).Route()
// Now apply auth middleware to protected routes
protected := rg.Use(middleware.ConditionalAuthMiddleware(s.ucManager.NewUserUsecase(), s.ucManager.NewAuthUsecase(), s.cfg)).(*gin.RouterGroup)
{
protectedRegistration := protected.Group("/user-registration")
{
protectedRegistration.GET("/pending", middleware.RequireAdminRole(), controller.NewUserRegistrationController(s.ucManager.NewUserUsecase(), protected).GetPendingUsers)
protectedRegistration.PUT("/approve/:id", middleware.RequireAdminRole(), controller.NewUserRegistrationController(s.ucManager.NewUserUsecase(), protected).ApproveUser)
protectedRegistration.PUT("/reject/:id", middleware.RequireAdminRole(), controller.NewUserRegistrationController(s.ucManager.NewUserUsecase(), protected).RejectUser)
}
protectedRegistration := protected.Group("/user-registration")
{
protectedRegistration.GET("/pending", middleware.RequireAdminRole(), controller.NewUserRegistrationController(s.ucManager.NewUserUsecase(), protected).GetPendingUsers)
protectedRegistration.PUT("/approve/:id", middleware.RequireAdminRole(), controller.NewUserRegistrationController(s.ucManager.NewUserUsecase(), protected).ApproveUser)
protectedRegistration.PUT("/reject/:id", middleware.RequireAdminRole(), controller.NewUserRegistrationController(s.ucManager.NewUserUsecase(), protected).RejectUser)
}
controller.NewUserRoleManagementController(s.ucManager.NewUserUsecase(), protected).Route()
controller.NewUsersController(s.ucManager.NewUserUsecase(), s.ucManager.NewAuthUsecase(), protected).Route()
controller.NewCountAssetsController(s.ucManager.NewCountAssetsUsecase(), protected).Route()
controller.NewDeviceController(s.ucManager.NewDeviceUsecase(), protected, s.cfg).Route()
controller.NewBackboneController(s.ucManager.NewBackboneUsecase(), protected, s.cfg).Route()
controller.NewFishboneController(s.ucManager.NewFishboneUsecase(), protected, s.cfg).Route()
controller.NewTowerController(s.ucManager.NewTowerUsecase(), protected, s.cfg).Route()
controller.NewDevicePortController(s.ucManager.NewDevicePortUsecase(), protected).Route()
controller.NewActivityLogController(s.ucManager.NewActivityLogUsecase(), protected, s.cfg).Route()
controller.NewDeviceInspectionController(s.ucManager.NewDeviceInspectionUsecase(), protected).Route()
controller.NewNearestDeviceController(s.ucManager.NewNearestDeviceUsecase(), protected, s.cfg).Route()
controller.NewDeviceDetailsController(s.ucManager.NewDeviceDetailsUsecase(), protected, s.cfg).Route()
controller.NewCountAssetsController(s.ucManager.NewCountAssetsUsecase(), protected).Route()
controller.NewDeviceController(s.ucManager.NewDeviceUsecase(), protected, s.cfg).Route()
controller.NewBackboneController(s.ucManager.NewBackboneUsecase(), protected, s.cfg).Route()
controller.NewFishboneController(s.ucManager.NewFishboneUsecase(), protected, s.cfg).Route()
controller.NewTowerController(s.ucManager.NewTowerUsecase(), protected, s.cfg).Route()
controller.NewDevicePortController(s.ucManager.NewDevicePortUsecase(), protected).Route()
controller.NewActivityLogController(s.ucManager.NewActivityLogUsecase(), protected, s.cfg).Route()
controller.NewDeviceInspectionController(s.ucManager.NewDeviceInspectionUsecase(), protected).Route()
controller.NewNearestDeviceController(s.ucManager.NewNearestDeviceUsecase(), protected, s.cfg).Route()
controller.NewDeviceDetailsController(s.ucManager.NewDeviceDetailsUsecase(), protected, s.cfg).Route()
controller.NewOLTController(s.ucManager.NewOLTUsecase(), protected).Route()
}
}
}
func (s *Server) Run() {
s.setupController()
if err := s.engine.Run(s.host); err != nil {
panic(err)
}
}
}

View File

@ -15,40 +15,44 @@ type InfraManager interface {
}
type infraManager struct {
db *gorm.DB
db *gorm.DB
cfg *config.Config
}
func (im *infraManager) openConn() error {
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s TimeZone=Asia/Shanghai",
im.cfg.DBHost, im.cfg.DBUser, im.cfg.DBPass, im.cfg.DBName, im.cfg.DBPort)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
return err
}
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s TimeZone=Asia/Shanghai",
im.cfg.DBHost, im.cfg.DBUser, im.cfg.DBPass, im.cfg.DBName, im.cfg.DBPort)
err = im.autoMigrate(db)
if err != nil {
return fmt.Errorf("failed to migrate database schema: %w", err)
}
im.db = db
return nil
// Configure GORM to disable foreign key constraint during migration
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
})
if err != nil {
return err
}
err = im.autoMigrate(db)
if err != nil {
return fmt.Errorf("failed to migrate database schema: %w", err)
}
im.db = db
return nil
}
func (im *infraManager) autoMigrate(db *gorm.DB) error {
return db.AutoMigrate(
return db.AutoMigrate(
&entity.Role{},
&entity.User{},
&entity.Device{},
&entity.Backbone{},
&entity.Fishbone{},
&entity.Tower{},
&entity.DevicePort{},
&entity.CountAssets{},
&entity.ActivityLog{},
&entity.OLT{},
)
&entity.User{},
&entity.Device{},
&entity.Tower{},
&entity.OLT{},
&entity.DevicePort{},
&entity.Backbone{},
&entity.Fishbone{},
&entity.CountAssets{},
&entity.ActivityLog{},
)
}
func NewInfraManager(cfg *config.Config) (InfraManager, error) {

View File

@ -1,66 +1,67 @@
package middleware
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
origin := c.Request.Header.Get("Origin")
// Define allowed origins (add your React app's URL)
allowedOrigins := []string{
"http://localhost:3000",
"http://localhost:3001",
"http://127.0.0.1:3000",
"http://127.0.0.1:5173",
"http://103.110.8.103:80",// Add production URL
"http://103.110.8.103",
return func(c *gin.Context) {
origin := c.Request.Header.Get("Origin")
"http://nam.winteraccess.id",
// Define allowed origins (add your React app's URL)
allowedOrigins := []string{
"http://localhost:3000",
"http://localhost:3001",
"http://localhost:5173", // Vite development server
"http://127.0.0.1:3000",
"http://127.0.0.1:5173",
"http://103.110.8.103:80", // Add production URL
"http://103.110.8.103",
"http://nam.winteraccess.id",
"https://nam.winteraccess.id",
}
}
// Check if origin is in allowed list
isAllowed := false
for _, allowed := range allowedOrigins {
if origin == allowed {
isAllowed = true
break
}
}
// For development, also allow localhost variations
if strings.Contains(origin, "localhost") || strings.Contains(origin, "127.0.0.1") {
isAllowed = true
}
if origin != "" && isAllowed {
// For allowed origins, set specific origin and enable credentials
c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
} else if origin == "" {
// For requests without origin (direct API calls, mobile apps, etc.)
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
// Don't set credentials for wildcard
} else {
// For disallowed origins, still set basic CORS but no credentials
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
}
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE, PATCH")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length, Content-Type")
c.Writer.Header().Set("Access-Control-Max-Age", "86400")
// Handle preflight requests
if c.Request.Method == http.MethodOptions {
c.AbortWithStatus(http.StatusNoContent)
return
}
// Check if origin is in allowed list
isAllowed := false
for _, allowed := range allowedOrigins {
if origin == allowed {
isAllowed = true
break
}
}
c.Next()
}
// For development, also allow localhost variations
if strings.Contains(origin, "localhost") || strings.Contains(origin, "127.0.0.1") {
isAllowed = true
}
if origin != "" && isAllowed {
// For allowed origins, set specific origin and enable credentials
c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
} else if origin == "" {
// For requests without origin (direct API calls, mobile apps, etc.)
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
// Don't set credentials for wildcard
} else {
// For disallowed origins, still set basic CORS but no credentials
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
}
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE, PATCH")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length, Content-Type")
c.Writer.Header().Set("Access-Control-Max-Age", "86400")
// Handle preflight requests
if c.Request.Method == http.MethodOptions {
c.AbortWithStatus(http.StatusNoContent)
return
}
c.Next()
}
}

View File

@ -7,47 +7,49 @@ import (
)
type DeviceDetailsDTO struct {
DeviceCode string `json:"device_code" validate:"required"`
DeviceType string `json:"device_type" validate:"required,oneof=OTB ODP"`
Longitude float64 `json:"longitude" validate:"required"`
Latitude float64 `json:"latitude" validate:"required"`
PortAmount int `json:"port_amount" validate:"required,min=1,max=100"`
Status string `json:"status" validate:"required,oneof=active inactive maintenance"`
Region *string `json:"region,omitempty" validate:"omitempty,min=3"`
Province *string `json:"province,omitempty" validate:"omitempty,min=3"`
City *string `json:"city,omitempty" validate:"omitempty,min=3"`
District *string `json:"district,omitempty" validate:"omitempty,min=3"`
DeviceCode string `json:"device_code" validate:"required"`
DeviceType string `json:"device_type" validate:"required,oneof=OTB ODP"`
Longitude float64 `json:"longitude" validate:"required"`
Latitude float64 `json:"latitude" validate:"required"`
PortAmount int `json:"port_amount" validate:"required,min=1,max=100"`
Status string `json:"status" validate:"required,oneof=active inactive maintenance"`
Region *string `json:"region,omitempty" validate:"omitempty,min=3"`
Province *string `json:"province,omitempty" validate:"omitempty,min=3"`
City *string `json:"city,omitempty" validate:"omitempty,min=3"`
District *string `json:"district,omitempty" validate:"omitempty,min=3"`
TowerID *uuid.UUID `json:"tower_id,omitempty"`
OLTID *uuid.UUID `json:"olt_id,omitempty"`
OLTID *uuid.UUID `json:"olt_id,omitempty"`
}
type UpdateDeviceDetailsDTO struct {
DeviceCode *string `json:"device_code,omitempty" validate:"omitempty,min=3"`
DeviceType *string `json:"device_type,omitempty" validate:"omitempty,oneof=OTB ODP CLOSURE"`
Longitude *float64 `json:"longitude,omitempty" validate:"omitempty,longitude"`
Latitude *float64 `json:"latitude,omitempty" validate:"omitempty,latitude"`
PortAmount *int `json:"port_amount,omitempty" validate:"omitempty,min=0,max=100"`
Status *string `json:"status,omitempty" validate:"omitempty,oneof=active inactive maintenance"`
Region *string `json:"region,omitempty" validate:"omitempty,min=3"`
Province *string `json:"province,omitempty" validate:"omitempty,min=3"`
City *string `json:"city,omitempty" validate:"omitempty,min=3"`
District *string `json:"district,omitempty" validate:"omitempty,min=3"`
TowerID *uuid.UUID `json:"tower_id,omitempty"`
OLTID *uuid.UUID `json:"olt_id,omitempty"`
DeviceCode *string `json:"device_code,omitempty" validate:"omitempty,min=3"`
DeviceType *string `json:"device_type,omitempty" validate:"omitempty,oneof=OTB ODP CLOSURE"`
Longitude *float64 `json:"longitude,omitempty" validate:"omitempty,longitude"`
Latitude *float64 `json:"latitude,omitempty" validate:"omitempty,latitude"`
PortAmount *int `json:"port_amount,omitempty" validate:"omitempty,min=0,max=100"`
Status *string `json:"status,omitempty" validate:"omitempty,oneof=active inactive maintenance"`
Region *string `json:"region,omitempty" validate:"omitempty,min=3"`
Province *string `json:"province,omitempty" validate:"omitempty,min=3"`
City *string `json:"city,omitempty" validate:"omitempty,min=3"`
District *string `json:"district,omitempty" validate:"omitempty,min=3"`
TowerID *uuid.UUID `json:"tower_id,omitempty"`
OLTID *uuid.UUID `json:"olt_id,omitempty"`
}
type AssignMultipleCustomersDTO struct {
CustomerName string `json:"customer_name" binding:"required"`
PortNumber *int `json:"port_number,omitempty"`
IsOccupied *bool `json:"is_occupied,omitempty"`
Status *entity.PortStatus `json:"status,omitempty"` // Add status field
Bandwidth *string `json:"bandwidth,omitempty"` // Add bandwidth field
}
type UpdateCustomerByPortDTO struct {
PortNumber int `json:"port_number,omitempty"`
NewCustomerName *string `json:"new_customer_name,omitempty"`
Status *entity.PortStatus `json:"status,omitempty"` // Add status field
Bandwidth *string `json:"bandwidth,omitempty"` // Add bandwidth field
PortNumber int `json:"port_number,omitempty"`
NewCustomerName *string `json:"new_customer_name,omitempty"`
IsOccupied *bool `json:"is_occupied,omitempty"`
Status *entity.PortStatus `json:"status,omitempty"` // Add status field
Bandwidth *string `json:"bandwidth,omitempty"` // Add bandwidth field
}
type BulkUpdateCustomersByPortDTO struct {
@ -60,4 +62,4 @@ type RemoveCustomerByPortDTO struct {
type DeleteImageDTO struct {
Filename string `json:"filename" validate:"required"`
}
}

View File

@ -7,34 +7,34 @@ import (
)
type DevicePort struct {
DeviceID uuid.UUID `json:"device_id"`
PortNumber int `json:"port_number"`
DeviceID uuid.UUID `json:"device_id"`
PortNumber int `json:"port_number"`
}
type UpdateDevicePort struct {
DeviceID *uuid.UUID `json:"device_id,omitempty" validate:"omitempty,min=3"`
PortNumber *int `json:"port_number,omitempty" validate:"omitempty,min=1"`
DeviceID *uuid.UUID `json:"device_id,omitempty" validate:"omitempty,min=3"`
PortNumber *int `json:"port_number,omitempty" validate:"omitempty,min=1"`
}
type AssignCustomerToPortDTO struct {
CustomerName string `json:"customer_name" binding:"required"`
PortNumber *int `json:"port_number,omitempty"` // Optional, will auto-assign if not provided
Status *entity.PortStatus `json:"status,omitempty"` // Add status field
Bandwidth *string `json:"bandwidth,omitempty"` // Add bandwidth field
CustomerName string `json:"customer_name" binding:"required"`
PortNumber *int `json:"port_number,omitempty"` // Optional, will auto-assign if not provided
Status *entity.PortStatus `json:"status,omitempty"` // Add status field
Bandwidth *string `json:"bandwidth,omitempty"` // Add bandwidth field
}
type UpdatePortUsageDTO struct {
PortUsed int `json:"port_used" binding:"required,min=0"`
PortUsed int `json:"port_used" binding:"required,min=0"`
}
type PortAssignmentDTO struct {
PortNumber int `json:"port_number" binding:"required,min=1"`
CustomerName *string `json:"customer_name,omitempty"`
Status *entity.PortStatus `json:"status,omitempty" validate:"omitempty,portstatus"`
Bandwidth *string `json:"bandwidth,omitempty" validate:"omitempty,min=1"`
PortNumber int `json:"port_number" binding:"required,min=1"`
CustomerName *string `json:"customer_name,omitempty"`
IsOccupied *bool `json:"is_occupied,omitempty"`
Status *entity.PortStatus `json:"status,omitempty" validate:"omitempty,portstatus"`
Bandwidth *string `json:"bandwidth,omitempty" validate:"omitempty,min=1"`
}
type UpdatePortAssignmentsDTO struct {
PortAssignments []PortAssignmentDTO `json:"port_assignments" binding:"required"`
}
PortAssignments []PortAssignmentDTO `json:"port_assignments" binding:"required"`
}

View File

@ -13,287 +13,306 @@ import (
type PortStatus string
const (
PortStatusOn PortStatus = "on"
PortStatusOff PortStatus = "off"
PortStatusDyingGasp PortStatus = "dyingGasp"
PortStatusLOS PortStatus = "los"
PortStatusOn PortStatus = "on"
PortStatusOff PortStatus = "off"
PortStatusDyingGasp PortStatus = "dyingGasp"
PortStatusLOS PortStatus = "los"
)
type StringSlice []string
func (s StringSlice) Value() (driver.Value, error) {
if len(s) == 0 {
return "[]", nil
}
return json.Marshal(s)
if len(s) == 0 {
return "[]", nil
}
return json.Marshal(s)
}
func (s *StringSlice) Scan(value interface{}) error {
if value == nil {
*s = StringSlice{}
return nil
}
var bytes []byte
switch v := value.(type) {
case []byte: // []byte and []uint8 are the same type in Go
bytes = v
case string:
bytes = []byte(v)
default:
return fmt.Errorf("cannot scan %T into StringSlice", value)
}
// Handle empty string case
if len(bytes) == 0 {
*s = StringSlice{}
return nil
}
return json.Unmarshal(bytes, s)
if value == nil {
*s = StringSlice{}
return nil
}
var bytes []byte
switch v := value.(type) {
case []byte: // []byte and []uint8 are the same type in Go
bytes = v
case string:
bytes = []byte(v)
default:
return fmt.Errorf("cannot scan %T into StringSlice", value)
}
// Handle empty string case
if len(bytes) == 0 {
*s = StringSlice{}
return nil
}
return json.Unmarshal(bytes, s)
}
type PortAssignment struct {
PortNumber int `json:"port_number"`
CustomerName *string `json:"customer_name"` // Nullable for empty ports
Status PortStatus `json:"status"` // Add status field
Bandwidth *string `json:"bandwidth"` // Add bandwidth field (nullable)
PortNumber int `json:"port_number"`
CustomerName *string `json:"customer_name"` // Nullable for empty ports
IsOccupied bool `json:"is_occupied"` // Explicitly store occupation status
Status PortStatus `json:"status"` // Add status field
Bandwidth *string `json:"bandwidth"` // Add bandwidth field (nullable)
}
type PortAssignmentResponse struct {
PortNumber int `json:"port_number"`
CustomerName *string `json:"customer_name"`
IsOccupied bool `json:"is_occupied"`
Status PortStatus `json:"status"` // Add status field
Bandwidth *string `json:"bandwidth"` // Add bandwidth field
PortNumber int `json:"port_number"`
CustomerName *string `json:"customer_name"`
IsOccupied bool `json:"is_occupied"`
Status PortStatus `json:"status"` // Add status field
Bandwidth *string `json:"bandwidth"` // Add bandwidth field
}
type PortAssignments []PortAssignment
func (p PortAssignments) Value() (driver.Value, error) {
if len(p) == 0 {
return "[]", nil
}
return json.Marshal(p)
if len(p) == 0 {
return "[]", nil
}
return json.Marshal(p)
}
func (p *PortAssignments) Scan(value interface{}) error {
if value == nil {
*p = PortAssignments{}
return nil
}
var bytes []byte
switch v := value.(type) {
case []byte:
bytes = v
case string:
bytes = []byte(v)
default:
return fmt.Errorf("cannot scan %T into PortAssignments", value)
}
if len(bytes) == 0 {
*p = PortAssignments{}
return nil
}
return json.Unmarshal(bytes, p)
if value == nil {
*p = PortAssignments{}
return nil
}
var bytes []byte
switch v := value.(type) {
case []byte:
bytes = v
case string:
bytes = []byte(v)
default:
return fmt.Errorf("cannot scan %T into PortAssignments", value)
}
if len(bytes) == 0 {
*p = PortAssignments{}
return nil
}
return json.Unmarshal(bytes, p)
}
type DevicePort struct {
ID uuid.UUID `json:"id" gorm:"type:uuid;primaryKey"`
DeviceID uuid.UUID `json:"device_id" gorm:"type:uuid;not null"`
PortUsed int `json:"port_used"`
PortAvailable int `json:"port_available"`
CustomerCount int `json:"customer_count"`
CustomerNames StringSlice `json:"customer_names" gorm:"type:jsonb"`
PortAssignments PortAssignments `json:"port_assignments" gorm:"type:jsonb"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// Relationships
Device Device `json:"device" gorm:"foreignKey:DeviceID"`
ID uuid.UUID `json:"id" gorm:"type:uuid;primaryKey"`
DeviceID uuid.UUID `json:"device_id" gorm:"type:uuid;not null"`
PortUsed int `json:"port_used"`
PortAvailable int `json:"port_available"`
CustomerCount int `json:"customer_count"`
CustomerNames StringSlice `json:"customer_names" gorm:"type:jsonb"`
PortAssignments PortAssignments `json:"port_assignments" gorm:"type:jsonb"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// Relationships
Device Device `json:"device" gorm:"foreignKey:DeviceID"`
}
// Helper method to get customer names with port numbers
func (dp *DevicePort) GetCustomerNamesWithPorts() []string {
if dp.Device.DeviceType == "OTB" {
return []string{} // OTB devices do not have port assignments
}
var result []string
for _, assignment := range dp.PortAssignments {
if assignment.CustomerName != nil && *assignment.CustomerName != "" {
result = append(result, fmt.Sprintf("%d: %s", assignment.PortNumber, *assignment.CustomerName))
}
}
// If no port assignments, fall back to customer names
if len(result) == 0 {
for i, name := range dp.CustomerNames {
if name != "" {
result = append(result, fmt.Sprintf("%d: %s", i+1, name))
}
}
}
return result
if dp.Device.DeviceType == "OTB" {
return []string{} // OTB devices do not have port assignments
}
var result []string
for _, assignment := range dp.PortAssignments {
if assignment.CustomerName != nil && *assignment.CustomerName != "" {
result = append(result, fmt.Sprintf("%d: %s", assignment.PortNumber, *assignment.CustomerName))
}
}
// If no port assignments, fall back to customer names
if len(result) == 0 {
for i, name := range dp.CustomerNames {
if name != "" {
result = append(result, fmt.Sprintf("%d: %s", i+1, name))
}
}
}
return result
}
// Helper method to get only customer names
func (dp *DevicePort) GetCustomerNames() []string {
if dp.Device.DeviceType == "OTB" {
return []string{} // OTB devices do not have port assignments
}
var result []string
for _, assignment := range dp.PortAssignments {
if assignment.CustomerName != nil && *assignment.CustomerName != "" {
result = append(result, *assignment.CustomerName)
}
}
// If no port assignments, fall back to customer names
if len(result) == 0 {
for _, name := range dp.CustomerNames {
if name != "" {
result = append(result, name)
}
}
}
return result
if dp.Device.DeviceType == "OTB" {
return []string{} // OTB devices do not have port assignments
}
var result []string
for _, assignment := range dp.PortAssignments {
if assignment.CustomerName != nil && *assignment.CustomerName != "" {
result = append(result, *assignment.CustomerName)
}
}
// If no port assignments, fall back to customer names
if len(result) == 0 {
for _, name := range dp.CustomerNames {
if name != "" {
result = append(result, name)
}
}
}
return result
}
func (dp *DevicePort) GetPortAssignmentsWithDetails() []PortAssignmentResponse {
// If port_used is 0, return empty array
// If port_used is 0, return empty array
if dp.Device.DeviceType == "OTB" {
return []PortAssignmentResponse{}
}
if dp.Device.DeviceType == "OTB" {
return []PortAssignmentResponse{}
}
if dp.PortUsed == 0 {
return []PortAssignmentResponse{}
}
var result []PortAssignmentResponse
// Step 1: Create a map of all port assignments
portMap := make(map[int]*PortAssignment)
maxPort := 0
if len(dp.PortAssignments) > 0 {
for _, assignment := range dp.PortAssignments {
portMap[assignment.PortNumber] = &assignment
if assignment.PortNumber > maxPort {
maxPort = assignment.PortNumber
}
}
} else {
// Fallback: use CustomerNames array if PortAssignments is empty
for i, name := range dp.CustomerNames {
portNum := i + 1
if name != "" || portNum <= dp.PortUsed {
var customerName *string
if name != "" {
customerName = &name
}
assignment := PortAssignment{
PortNumber: portNum,
CustomerName: customerName,
Status: PortStatusOn, // Default status
Bandwidth: nil, // Default bandwidth
}
portMap[portNum] = &assignment
if portNum > maxPort {
maxPort = portNum
}
}
}
}
// Ensure maxPort is at least equal to port_used
if maxPort < dp.PortUsed {
maxPort = dp.PortUsed
}
// Step 2: Determine which ports should be occupied
portsWithCustomers := make([]int, 0)
for portNum, assignment := range portMap {
if assignment.CustomerName != nil && *assignment.CustomerName != "" {
portsWithCustomers = append(portsWithCustomers, portNum)
}
}
// Sort to get them in order
sort.Ints(portsWithCustomers)
// Step 3: Determine occupied ports
occupiedPorts := make(map[int]bool)
// All ports with customers are occupied
for _, portNum := range portsWithCustomers {
occupiedPorts[portNum] = true
}
// Fill remaining slots to reach port_used
occupiedCount := len(portsWithCustomers)
if occupiedCount < dp.PortUsed {
// Need to occupy more ports (ports without customer names but still occupied)
for i := 1; i <= maxPort && occupiedCount < dp.PortUsed; i++ {
if !occupiedPorts[i] {
occupiedPorts[i] = true
occupiedCount++
}
}
}
// Step 4: Create the result for ALL ports up to maxPort
for i := 1; i <= maxPort; i++ {
var customerName *string
var status PortStatus = PortStatusOff // Default status for unoccupied ports
var bandwidth *string
if assignment := portMap[i]; assignment != nil {
customerName = assignment.CustomerName
status = assignment.Status
bandwidth = assignment.Bandwidth
} else if occupiedPorts[i] {
// Port is occupied but no assignment data - use defaults
status = PortStatusOn
}
result = append(result, PortAssignmentResponse{
PortNumber: i,
CustomerName: customerName,
IsOccupied: occupiedPorts[i],
Status: status,
Bandwidth: bandwidth,
})
}
if dp.PortUsed == 0 {
return []PortAssignmentResponse{}
}
return result
var result []PortAssignmentResponse
// Step 1: Create a map of all port assignments
portMap := make(map[int]*PortAssignment)
maxPort := 0
if len(dp.PortAssignments) > 0 {
for _, assignment := range dp.PortAssignments {
portMap[assignment.PortNumber] = &assignment
if assignment.PortNumber > maxPort {
maxPort = assignment.PortNumber
}
}
} else {
// Fallback: use CustomerNames array if PortAssignments is empty
for i, name := range dp.CustomerNames {
portNum := i + 1
if name != "" || portNum <= dp.PortUsed {
var customerName *string
if name != "" {
customerName = &name
}
assignment := PortAssignment{
PortNumber: portNum,
CustomerName: customerName,
Status: PortStatusOn, // Default status
Bandwidth: nil, // Default bandwidth
}
portMap[portNum] = &assignment
if portNum > maxPort {
maxPort = portNum
}
}
}
}
// Ensure maxPort is at least equal to port_used
if maxPort < dp.PortUsed {
maxPort = dp.PortUsed
}
// Step 2: Determine which ports should be occupied
portsWithCustomers := make([]int, 0)
for portNum, assignment := range portMap {
if assignment.CustomerName != nil && *assignment.CustomerName != "" {
portsWithCustomers = append(portsWithCustomers, portNum)
}
}
// Sort to get them in order
sort.Ints(portsWithCustomers)
// Step 3: Determine occupied ports - USE EXPLICIT IsOccupied FIELD
occupiedPorts := make(map[int]bool)
// For modern approach: use the explicitly stored IsOccupied field
for portNum, assignment := range portMap {
// Use the explicitly stored IsOccupied value
// If not set, fallback to checking customer name presence
isOccupied := assignment.IsOccupied
if !isOccupied && assignment.CustomerName != nil && *assignment.CustomerName != "" {
isOccupied = true
}
occupiedPorts[portNum] = isOccupied
}
// Legacy fallback: if we don't have enough explicit port assignments,
// use the old logic only for ports that don't have explicit assignment data
currentOccupiedCount := 0
for _, isOccupied := range occupiedPorts {
if isOccupied {
currentOccupiedCount++
}
}
// Only apply legacy logic if we have fewer occupied ports than port_used
// and only for ports that don't have explicit assignment data
if currentOccupiedCount < dp.PortUsed {
for i := 1; i <= maxPort && currentOccupiedCount < dp.PortUsed; i++ {
if _, hasExplicitData := portMap[i]; !hasExplicitData {
if !occupiedPorts[i] {
occupiedPorts[i] = true
currentOccupiedCount++
}
}
}
}
// Step 4: Create the result for ALL ports up to maxPort
for i := 1; i <= maxPort; i++ {
var customerName *string
var status PortStatus = PortStatusOff // Default status for unoccupied ports
var bandwidth *string
if assignment := portMap[i]; assignment != nil {
customerName = assignment.CustomerName
status = assignment.Status
bandwidth = assignment.Bandwidth
}
// Override status for legacy occupied ports (those without explicit assignment)
if occupiedPorts[i] && portMap[i] == nil {
status = PortStatusOn
}
result = append(result, PortAssignmentResponse{
PortNumber: i,
CustomerName: customerName,
IsOccupied: occupiedPorts[i],
Status: status,
Bandwidth: bandwidth,
})
}
return result
}
func (dp *DevicePort) GetCustomerNamesOnly() []string {
if dp.Device.DeviceType == "OTB" {
return []string{} // OTB devices do not have port assignments
}
var result []string
// If we have PortAssignments data, use it
if len(dp.PortAssignments) > 0 {
for _, assignment := range dp.PortAssignments {
if assignment.CustomerName != nil && *assignment.CustomerName != "" {
result = append(result, *assignment.CustomerName)
}
}
} else {
// Fallback: use CustomerNames directly
for _, name := range dp.CustomerNames {
if name != "" {
result = append(result, name)
}
}
}
return result
if dp.Device.DeviceType == "OTB" {
return []string{} // OTB devices do not have port assignments
}
var result []string
// If we have PortAssignments data, use it
if len(dp.PortAssignments) > 0 {
for _, assignment := range dp.PortAssignments {
if assignment.CustomerName != nil && *assignment.CustomerName != "" {
result = append(result, *assignment.CustomerName)
}
}
} else {
// Fallback: use CustomerNames directly
for _, name := range dp.CustomerNames {
if name != "" {
result = append(result, name)
}
}
}
return result
}
func (DevicePort) TableName() string {
return "device_ports"
}
return "device_ports"
}

File diff suppressed because it is too large Load Diff