Files
gochat/backend/internal/service/inbox_limit_service.go
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

100 lines
3.0 KiB
Go

package service
import (
"context"
"errors"
"gorm.io/gorm"
"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"
)
// InboxLimitService implements business logic for inbox limit management.
// Reference: Chatwoot app/controllers/api/v1/accounts/inboxes/inbox_limits_controller.rb
// Inbox limits are create/update/destroy only (no index or show in Chatwoot).
type InboxLimitService struct {
repo *repository.InboxLimitRepo
}
// NewInboxLimitService creates a new InboxLimit service.
func NewInboxLimitService(repo *repository.InboxLimitRepo) *InboxLimitService {
return &InboxLimitService{repo: repo}
}
// CreateInboxLimitRequest is the DTO for creating an inbox limit.
// Reference: Chatwoot InboxLimitsController#create — params: {type, value}
type CreateInboxLimitRequest struct {
Type string `json:"type" validate:"required,min=1"`
Value int `json:"value" validate:"required,min=1"`
}
// UpdateInboxLimitRequest is the DTO for updating an inbox limit.
// Reference: Chatwoot InboxLimitsController#update — params: {type, value}
type UpdateInboxLimitRequest struct {
Type string `json:"type,omitempty" validate:"omitempty,min=1"`
Value int `json:"value,omitempty" validate:"omitempty,min=1"`
}
// Create creates a new inbox limit for an inbox.
// Reference: Chatwoot InboxLimitsController#create
func (s *InboxLimitService) Create(ctx context.Context, inboxID uint, req CreateInboxLimitRequest) (*model.InboxLimit, error) {
if err := pkgvalidator.ValidateStruct(req); err != nil {
return nil, err
}
limit := &model.InboxLimit{
InboxID: inboxID,
Type: req.Type,
Value: req.Value,
}
if err := s.repo.Create(ctx, limit); err != nil {
return nil, err
}
applogger.L().Infof("Inbox limit created for inbox %d, type=%s, value=%d", inboxID, req.Type, req.Value)
return limit, nil
}
// Update modifies an existing inbox limit.
// Reference: Chatwoot InboxLimitsController#update
func (s *InboxLimitService) Update(ctx context.Context, id uint, req UpdateInboxLimitRequest) (*model.InboxLimit, error) {
existing, err := s.repo.FindByID(ctx, id)
if err != nil {
return nil, err
}
if req.Type != "" {
existing.Type = req.Type
}
if req.Value != 0 {
existing.Value = req.Value
}
if err := s.repo.Update(ctx, existing); err != nil {
return nil, err
}
applogger.L().Infof("Inbox limit updated, id=%d", id)
return existing, nil
}
// Delete removes an inbox limit by ID.
// Reference: Chatwoot InboxLimitsController#destroy
func (s *InboxLimitService) Delete(ctx context.Context, id uint) error {
// Verify existence before delete
existing, err := s.repo.FindByID(ctx, id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return errors.New("inbox limit not found")
}
return err
}
_ = existing // confirmed exists
if err := s.repo.Delete(ctx, id); err != nil {
return err
}
applogger.L().Infof("Inbox limit deleted, id=%d", id)
return nil
}