Files
gochat/internal/service/inbox_limit_service.go
T
2026-06-04 15:44:48 +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
}