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.
154 lines
5.1 KiB
Go
154 lines
5.1 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/gochat/gochat/internal/repository"
|
|
applogger "github.com/gochat/gochat/pkg/logger"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// DraftMessageService implements business logic for DraftMessage operations.
|
|
// Reference: Chatwoot app/controllers/api/v1/conversations/draft_messages_controller.rb
|
|
type DraftMessageService struct {
|
|
repo *repository.DraftMessageRepo
|
|
conversationRepo *repository.ConversationRepo
|
|
}
|
|
|
|
// NewDraftMessageService creates a new DraftMessage service.
|
|
func NewDraftMessageService(repo *repository.DraftMessageRepo, conversationRepo *repository.ConversationRepo) *DraftMessageService {
|
|
return &DraftMessageService{repo: repo, conversationRepo: conversationRepo}
|
|
}
|
|
|
|
// Ready reports whether the service has its repository configured.
|
|
func (s *DraftMessageService) Ready() bool {
|
|
return s != nil && s.repo != nil
|
|
}
|
|
|
|
// List retrieves all draft messages for a conversation, optionally filtered by user.
|
|
func (s *DraftMessageService) List(ctx context.Context, accountID, conversationID, userID uint) ([]model.DraftMessage, error) {
|
|
conversation, err := s.conversationRepo.FindByAccountAndDisplayIDOrID(ctx, accountID, conversationID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return s.repo.FindByConversationID(ctx, conversation.ID, userID)
|
|
}
|
|
|
|
// ShowConversationDraft retrieves Chatwoot's conversation-scoped draft message.
|
|
func (s *DraftMessageService) ShowConversationDraft(ctx context.Context, accountID, routeID uint) (*model.DraftMessage, error) {
|
|
conversation, err := s.conversationRepo.FindByAccountAndDisplayIDOrID(ctx, accountID, routeID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
draft, err := s.repo.FindLatestByConversationID(ctx, conversation.ID)
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
return draft, err
|
|
}
|
|
|
|
// SetConversationDraft upserts Chatwoot's conversation-scoped draft message.
|
|
func (s *DraftMessageService) SetConversationDraft(ctx context.Context, accountID, routeID, userID uint, content string) error {
|
|
conversation, err := s.conversationRepo.FindByAccountAndDisplayIDOrID(ctx, accountID, routeID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
draft, err := s.repo.FindLatestByConversationID(ctx, conversation.ID)
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return s.repo.Create(ctx, &model.DraftMessage{ConversationID: conversation.ID, UserID: userID, Content: content})
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
draft.Content = content
|
|
draft.UserID = userID
|
|
return s.repo.Update(ctx, draft)
|
|
}
|
|
|
|
// DeleteConversationDraft removes Chatwoot's conversation-scoped draft message.
|
|
func (s *DraftMessageService) DeleteConversationDraft(ctx context.Context, accountID, routeID uint) error {
|
|
conversation, err := s.conversationRepo.FindByAccountAndDisplayIDOrID(ctx, accountID, routeID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return s.repo.DeleteByConversationID(ctx, conversation.ID)
|
|
}
|
|
|
|
// Create creates a new draft message.
|
|
func (s *DraftMessageService) Create(ctx context.Context, accountID, conversationID, userID uint, content string) (*model.DraftMessage, error) {
|
|
conversation, err := s.conversationRepo.FindByAccountAndDisplayIDOrID(ctx, accountID, conversationID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
draft := &model.DraftMessage{
|
|
ConversationID: conversation.ID,
|
|
UserID: userID,
|
|
Content: content,
|
|
}
|
|
|
|
if err := s.repo.Create(ctx, draft); err != nil {
|
|
applogger.L().Errorf("Failed to create draft message for conversation %d: %v", conversationID, err)
|
|
return nil, err
|
|
}
|
|
|
|
return draft, nil
|
|
}
|
|
|
|
// Get retrieves a draft message by ID.
|
|
func (s *DraftMessageService) Get(ctx context.Context, id uint) (*model.DraftMessage, error) {
|
|
return s.repo.FindByID(ctx, id)
|
|
}
|
|
|
|
// Update updates an existing draft message.
|
|
func (s *DraftMessageService) Update(ctx context.Context, id uint, content string) (*model.DraftMessage, error) {
|
|
draft, err := s.repo.FindByID(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
draft.Content = content
|
|
if err := s.repo.Update(ctx, draft); err != nil {
|
|
applogger.L().Errorf("Failed to update draft message %d: %v", id, err)
|
|
return nil, err
|
|
}
|
|
|
|
return draft, nil
|
|
}
|
|
|
|
// Delete deletes a draft message.
|
|
func (s *DraftMessageService) Delete(ctx context.Context, id uint) error {
|
|
// Verify draft exists before deleting
|
|
draft, err := s.repo.FindByID(ctx, id)
|
|
if err != nil {
|
|
applogger.L().Errorf("Draft message %d not found for deletion: %v", id, err)
|
|
return err
|
|
}
|
|
if draft == nil {
|
|
return errors.New("draft message not found")
|
|
}
|
|
if err := s.repo.Delete(ctx, id); err != nil {
|
|
applogger.L().Errorf("Failed to delete draft message %d: %v", id, err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Search searches draft messages by content for an account using LIKE query.
|
|
func (s *DraftMessageService) Search(ctx context.Context, accountID uint, query string) ([]model.DraftMessage, error) {
|
|
if query == "" {
|
|
return nil, errors.New("search query must not be empty")
|
|
}
|
|
return s.repo.SearchByContent(ctx, accountID, query)
|
|
}
|
|
|
|
// Count returns the total number of draft messages for an account.
|
|
func (s *DraftMessageService) Count(ctx context.Context, accountID uint) (int64, error) {
|
|
return s.repo.CountByAccount(ctx, accountID)
|
|
}
|