package service import ( "context" "errors" "github.com/gochat/gochat/internal/model" "github.com/gochat/gochat/internal/repository" applogger "github.com/gochat/gochat/pkg/logger" pkgvalidator "github.com/gochat/gochat/pkg/validator" ) // InstallationConfigService implements business logic for InstallationConfig CRUD. // Reference: Chatwoot app/controllers/api/v1/platform/installation_configs_controller.rb type InstallationConfigService struct { repo *repository.InstallationConfigRepo } // NewInstallationConfigService creates a new InstallationConfig service. func NewInstallationConfigService(repo *repository.InstallationConfigRepo) *InstallationConfigService { return &InstallationConfigService{repo: repo} } // --- DTOs for InstallationConfig CRUD --- // CreateInstallationConfigRequest is the DTO for creating an installation config. type CreateInstallationConfigRequest struct { Name string `json:"name" validate:"required,min=1,max=100"` Value string `json:"value" validate:"required"` } // UpdateInstallationConfigRequest is the DTO for updating an installation config. type UpdateInstallationConfigRequest struct { Name string `json:"name,omitempty" validate:"omitempty,min=1,max=100"` Value string `json:"value,omitempty" validate:"omitempty,min=1"` } // --- Service methods --- // Get retrieves an InstallationConfig by ID. func (s *InstallationConfigService) Get(ctx context.Context, id uint) (*model.InstallationConfig, error) { cfg, err := s.repo.FindByID(ctx, id) if err != nil { applogger.L().Errorf("InstallationConfigService.Get error: %v", err) return nil, err } return cfg, nil } // GetByName retrieves an InstallationConfig by name. func (s *InstallationConfigService) GetByName(ctx context.Context, name string) (*model.InstallationConfig, error) { cfg, err := s.repo.FindByName(ctx, name) if err != nil { applogger.L().Errorf("InstallationConfigService.GetByName error: %v", err) return nil, err } return cfg, nil } // List retrieves all installation configs with pagination. func (s *InstallationConfigService) List(ctx context.Context, offset, limit int) ([]model.InstallationConfig, int64, error) { configs, total, err := s.repo.List(ctx, offset, limit) if err != nil { applogger.L().Errorf("InstallationConfigService.List error: %v", err) return nil, 0, err } return configs, total, nil } // Create creates a new installation config. func (s *InstallationConfigService) Create(ctx context.Context, req *CreateInstallationConfigRequest) (*model.InstallationConfig, error) { if err := pkgvalidator.ValidateStruct(req); err != nil { return nil, errors.New("validation error: " + err.Error()) } // Check for duplicate name existing, err := s.repo.FindByName(ctx, req.Name) if err == nil && existing != nil { return nil, errors.New("installation config with this name already exists") } cfg := &model.InstallationConfig{ Name: req.Name, Value: req.Value, } if err := s.repo.Create(ctx, cfg); err != nil { applogger.L().Errorf("InstallationConfigService.Create error: %v", err) return nil, err } return cfg, nil } // Update modifies an existing installation config. func (s *InstallationConfigService) Update(ctx context.Context, id uint, req *UpdateInstallationConfigRequest) (*model.InstallationConfig, error) { if err := pkgvalidator.ValidateStruct(req); err != nil { return nil, errors.New("validation error: " + err.Error()) } cfg, err := s.repo.FindByID(ctx, id) if err != nil { return nil, err } if req.Name != "" { cfg.Name = req.Name } if req.Value != "" { cfg.Value = req.Value } if err := s.repo.Update(ctx, cfg); err != nil { applogger.L().Errorf("InstallationConfigService.Update error: %v", err) return nil, err } return cfg, nil } // Delete removes an installation config by ID. func (s *InstallationConfigService) Delete(ctx context.Context, id uint) error { if err := s.repo.Delete(ctx, id); err != nil { applogger.L().Errorf("InstallationConfigService.Delete error: %v", err) return err } return nil }