refactor: shorten model and table prefixes
This commit is contained in:
@@ -116,7 +116,7 @@ func currentInboxUser(c *gin.Context) (uint, bool) {
|
||||
return userID, true
|
||||
}
|
||||
|
||||
func inboxItemDTO(item models.SenlinAgentInboxItem) InboxItemDTO {
|
||||
func inboxItemDTO(item models.SaInboxItem) InboxItemDTO {
|
||||
return InboxItemDTO{
|
||||
ID: item.Identity, ProjectID: item.ProjectIdentity, SourceType: item.SourceType,
|
||||
Title: item.Title, Body: item.Body, Status: item.Status,
|
||||
|
||||
@@ -40,14 +40,14 @@ type ConfirmResult struct {
|
||||
}
|
||||
|
||||
type Analyzer interface {
|
||||
Analyze(item models.SenlinAgentInboxItem, userID uint) ([]Suggestion, error)
|
||||
Analyze(item models.SaInboxItem, userID uint) ([]Suggestion, error)
|
||||
}
|
||||
|
||||
type StaticAnalyzer struct {
|
||||
Suggestions []Suggestion
|
||||
}
|
||||
|
||||
func (a StaticAnalyzer) Analyze(item models.SenlinAgentInboxItem, userID uint) ([]Suggestion, error) {
|
||||
func (a StaticAnalyzer) Analyze(item models.SaInboxItem, userID uint) ([]Suggestion, error) {
|
||||
if len(a.Suggestions) > 0 {
|
||||
return a.Suggestions, nil
|
||||
}
|
||||
@@ -85,7 +85,7 @@ func (s *Service) database() *gorm.DB {
|
||||
}
|
||||
|
||||
// Capture 先以 owner + project identity 校验项目边界,再创建归属于当前用户的原始 Inbox 条目。
|
||||
func (s *Service) Capture(input CaptureInput) (*models.SenlinAgentInboxItem, error) {
|
||||
func (s *Service) Capture(input CaptureInput) (*models.SaInboxItem, error) {
|
||||
if strings.TrimSpace(input.ProjectIdentity) == "" || input.UserID == 0 {
|
||||
return nil, ErrProjectAndUserRequired
|
||||
}
|
||||
@@ -93,13 +93,13 @@ func (s *Service) Capture(input CaptureInput) (*models.SenlinAgentInboxItem, err
|
||||
if sourceType == "" {
|
||||
return nil, ErrSourceTypeRequired
|
||||
}
|
||||
var item models.SenlinAgentInboxItem
|
||||
var item models.SaInboxItem
|
||||
err := s.database().Transaction(func(tx *gorm.DB) error {
|
||||
var project models.SenlinAgentProject
|
||||
var project models.SaProject
|
||||
if err := tx.Where("owner_id = ? AND identity = ?", input.UserID, input.ProjectIdentity).First(&project).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
item = models.SenlinAgentInboxItem{
|
||||
item = models.SaInboxItem{
|
||||
ProjectID: project.ID, ProjectIdentity: project.Identity, CreatedBy: input.UserID,
|
||||
SourceType: sourceType, Title: strings.TrimSpace(input.Title), Body: strings.TrimSpace(input.Body), Status: "open",
|
||||
}
|
||||
@@ -138,11 +138,11 @@ func (s *Service) Analyze(itemIdentity string, userID uint) ([]Suggestion, error
|
||||
if locked.Status != "open" {
|
||||
return ErrInboxAlreadyConfirmed
|
||||
}
|
||||
if err := tx.Where("inbox_item_id = ?", locked.ID).Delete(&models.SenlinAgentInboxSuggestion{}).Error; err != nil {
|
||||
if err := tx.Where("inbox_item_id = ?", locked.ID).Delete(&models.SaInboxSuggestion{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
for _, suggestion := range normalized {
|
||||
draft := models.SenlinAgentInboxSuggestion{
|
||||
draft := models.SaInboxSuggestion{
|
||||
InboxItemID: locked.ID, InboxItemIdentity: locked.Identity, CreatedBy: userID,
|
||||
Kind: suggestion.Kind, Title: suggestion.Title, Body: suggestion.Body,
|
||||
}
|
||||
@@ -174,14 +174,14 @@ func (s *Service) Confirm(itemIdentity string, userID uint, selectedSuggestionId
|
||||
return err
|
||||
}
|
||||
|
||||
var saved []models.SenlinAgentInboxSuggestion
|
||||
var saved []models.SaInboxSuggestion
|
||||
if err := tx.Where("inbox_item_id = ? AND created_by = ? AND identity IN ?", item.ID, userID, selected).Find(&saved).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if len(saved) != len(selected) {
|
||||
return ErrSuggestionNotFound
|
||||
}
|
||||
byIdentity := make(map[string]models.SenlinAgentInboxSuggestion, len(saved))
|
||||
byIdentity := make(map[string]models.SaInboxSuggestion, len(saved))
|
||||
for _, suggestion := range saved {
|
||||
byIdentity[suggestion.Identity] = suggestion
|
||||
}
|
||||
@@ -203,11 +203,11 @@ func (s *Service) Confirm(itemIdentity string, userID uint, selectedSuggestionId
|
||||
return result, err
|
||||
}
|
||||
|
||||
func findOwnedInboxItem(tx *gorm.DB, identity string, userID uint, lock bool) (*models.SenlinAgentInboxItem, error) {
|
||||
var item models.SenlinAgentInboxItem
|
||||
query := tx.Model(&models.SenlinAgentInboxItem{}).
|
||||
Joins("JOIN senlin_agent_projects ON senlin_agent_projects.id = senlin_agent_inbox_items.project_id").
|
||||
Where("senlin_agent_inbox_items.identity = ? AND senlin_agent_inbox_items.created_by = ? AND senlin_agent_projects.owner_id = ?", identity, userID, userID)
|
||||
func findOwnedInboxItem(tx *gorm.DB, identity string, userID uint, lock bool) (*models.SaInboxItem, error) {
|
||||
var item models.SaInboxItem
|
||||
query := tx.Model(&models.SaInboxItem{}).
|
||||
Joins("JOIN sa_projects ON sa_projects.id = sa_inbox_items.project_id").
|
||||
Where("sa_inbox_items.identity = ? AND sa_inbox_items.created_by = ? AND sa_projects.owner_id = ?", identity, userID, userID)
|
||||
if lock {
|
||||
query = query.Clauses(clause.Locking{Strength: "UPDATE"})
|
||||
}
|
||||
@@ -230,7 +230,7 @@ func normalizeSuggestions(suggestions []Suggestion) ([]Suggestion, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func suggestionSubject(item models.SenlinAgentInboxItem) string {
|
||||
func suggestionSubject(item models.SaInboxItem) string {
|
||||
value := strings.TrimSpace(item.Title)
|
||||
if value == "" {
|
||||
value = strings.TrimSpace(item.Body)
|
||||
@@ -266,22 +266,22 @@ func normalizeSelectedIdentities(values []string) ([]string, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func createConfirmedObject(tx *gorm.DB, item models.SenlinAgentInboxItem, suggestion models.SenlinAgentInboxSuggestion) error {
|
||||
func createConfirmedObject(tx *gorm.DB, item models.SaInboxItem, suggestion models.SaInboxSuggestion) error {
|
||||
sourceInboxItemID := item.ID
|
||||
switch suggestion.Kind {
|
||||
case "task":
|
||||
return tx.Create(&models.SenlinAgentTask{
|
||||
return tx.Create(&models.SaTask{
|
||||
ProjectID: item.ProjectID, ProjectIdentity: item.ProjectIdentity, CreatedBy: item.CreatedBy,
|
||||
SourceInboxItemID: &sourceInboxItemID, Title: suggestion.Title, Description: suggestion.Body, Status: "open",
|
||||
}).Error
|
||||
case "note":
|
||||
return tx.Create(&models.SenlinAgentNote{
|
||||
return tx.Create(&models.SaNote{
|
||||
ProjectID: item.ProjectID, ProjectIdentity: item.ProjectIdentity, CreatedBy: item.CreatedBy,
|
||||
SourceInboxItemID: &sourceInboxItemID, Title: suggestion.Title, Markdown: suggestion.Body,
|
||||
}).Error
|
||||
case "source":
|
||||
// 文本型 Inbox 资料不是上传文件,不构造或伪造任何本地路径。
|
||||
return tx.Create(&models.SenlinAgentSource{
|
||||
return tx.Create(&models.SaSource{
|
||||
ProjectID: item.ProjectID, ProjectIdentity: item.ProjectIdentity, CreatedBy: item.CreatedBy,
|
||||
SourceInboxItemID: &sourceInboxItemID, Kind: "text", Title: suggestion.Title, ContentText: suggestion.Body,
|
||||
}).Error
|
||||
|
||||
@@ -21,9 +21,9 @@ import (
|
||||
|
||||
type inboxTestFixture struct {
|
||||
database *gorm.DB
|
||||
owner models.SenlinAgentUser
|
||||
other models.SenlinAgentUser
|
||||
project models.SenlinAgentProject
|
||||
owner models.SaUser
|
||||
other models.SaUser
|
||||
project models.SaProject
|
||||
}
|
||||
|
||||
type inboxSuggestionResponse struct {
|
||||
@@ -38,7 +38,7 @@ type inboxAnalyzeResponse struct {
|
||||
}
|
||||
|
||||
func TestStaticAnalyzerBuildsReviewableDraftsFromInboxContent(t *testing.T) {
|
||||
item := models.SenlinAgentInboxItem{Title: "客户访谈", Body: "客户希望下周确认交付计划。"}
|
||||
item := models.SaInboxItem{Title: "客户访谈", Body: "客户希望下周确认交付计划。"}
|
||||
|
||||
suggestions, err := (StaticAnalyzer{}).Analyze(item, 7)
|
||||
|
||||
@@ -70,7 +70,7 @@ func TestInboxCaptureUsesOwnedUUIDv7AndCamelCaseDTO(t *testing.T) {
|
||||
require.Equal(t, "open", payload["status"])
|
||||
requireUUIDv7(t, payload["id"].(string))
|
||||
|
||||
var item models.SenlinAgentInboxItem
|
||||
var item models.SaInboxItem
|
||||
require.NoError(t, fixture.database.Where("identity = ?", payload["id"]).First(&item).Error)
|
||||
require.Equal(t, fixture.owner.ID, item.CreatedBy)
|
||||
require.Equal(t, fixture.owner.Identity, item.CreatedByIdentity)
|
||||
@@ -87,7 +87,7 @@ func TestInboxCaptureRejectsNumericAndUnownedProjectIdentities(t *testing.T) {
|
||||
})
|
||||
require.Equal(t, http.StatusBadRequest, numeric.Code)
|
||||
|
||||
otherProject := models.SenlinAgentProject{OwnerID: fixture.other.ID, Name: "Other", Identifier: "OTHER"}
|
||||
otherProject := models.SaProject{OwnerID: fixture.other.ID, Name: "Other", Identifier: "OTHER"}
|
||||
require.NoError(t, fixture.database.Create(&otherProject).Error)
|
||||
unowned := performInboxJSON(t, router, http.MethodPost, "/api/v1/projects/"+otherProject.Identity+"/inbox", gin.H{
|
||||
"sourceType": "text", "body": "not mine",
|
||||
@@ -95,7 +95,7 @@ func TestInboxCaptureRejectsNumericAndUnownedProjectIdentities(t *testing.T) {
|
||||
require.Equal(t, http.StatusNotFound, unowned.Code)
|
||||
|
||||
var count int64
|
||||
require.NoError(t, fixture.database.Model(&models.SenlinAgentInboxItem{}).Count(&count).Error)
|
||||
require.NoError(t, fixture.database.Model(&models.SaInboxItem{}).Count(&count).Error)
|
||||
require.Zero(t, count)
|
||||
}
|
||||
|
||||
@@ -116,7 +116,7 @@ func TestAnalyzeUsesInboxIdentityAndCreatesNoFormalObjects(t *testing.T) {
|
||||
requireUUIDv7(t, analysis.Suggestions[0].ID)
|
||||
requireFormalObjectCounts(t, fixture.database, 0, 0, 0)
|
||||
|
||||
var reloaded models.SenlinAgentInboxItem
|
||||
var reloaded models.SaInboxItem
|
||||
require.NoError(t, fixture.database.First(&reloaded, item.ID).Error)
|
||||
require.Equal(t, "open", reloaded.Status)
|
||||
}
|
||||
@@ -148,21 +148,21 @@ func TestConfirmCreatesOnlySelectedSavedSuggestionsWithInboxIdentity(t *testing.
|
||||
require.Equal(t, 3, result.CreatedCount)
|
||||
requireFormalObjectCounts(t, fixture.database, 1, 1, 1)
|
||||
|
||||
var task models.SenlinAgentTask
|
||||
var task models.SaTask
|
||||
require.NoError(t, fixture.database.First(&task).Error)
|
||||
require.Equal(t, "跟进报价", task.Title)
|
||||
require.Equal(t, fixture.project.ID, task.ProjectID)
|
||||
require.Equal(t, fixture.owner.ID, task.CreatedBy)
|
||||
requireSourceInboxIdentity(t, item, task.SourceInboxItemID, task.SourceInboxItemIdentity)
|
||||
|
||||
var note models.SenlinAgentNote
|
||||
var note models.SaNote
|
||||
require.NoError(t, fixture.database.First(¬e).Error)
|
||||
require.Equal(t, "会议纪要", note.Title)
|
||||
require.Equal(t, fixture.project.ID, note.ProjectID)
|
||||
require.Equal(t, fixture.owner.ID, note.CreatedBy)
|
||||
requireSourceInboxIdentity(t, item, note.SourceInboxItemID, note.SourceInboxItemIdentity)
|
||||
|
||||
var source models.SenlinAgentSource
|
||||
var source models.SaSource
|
||||
require.NoError(t, fixture.database.First(&source).Error)
|
||||
require.Equal(t, "背景资料", source.Title)
|
||||
require.Equal(t, "text", source.Kind)
|
||||
@@ -173,9 +173,9 @@ func TestConfirmCreatesOnlySelectedSavedSuggestionsWithInboxIdentity(t *testing.
|
||||
requireSourceInboxIdentity(t, item, source.SourceInboxItemID, source.SourceInboxItemIdentity)
|
||||
|
||||
var skipped int64
|
||||
require.NoError(t, fixture.database.Model(&models.SenlinAgentTask{}).Where("title = ?", "不创建的任务").Count(&skipped).Error)
|
||||
require.NoError(t, fixture.database.Model(&models.SaTask{}).Where("title = ?", "不创建的任务").Count(&skipped).Error)
|
||||
require.Zero(t, skipped)
|
||||
var reloaded models.SenlinAgentInboxItem
|
||||
var reloaded models.SaInboxItem
|
||||
require.NoError(t, fixture.database.First(&reloaded, item.ID).Error)
|
||||
require.Equal(t, "processed", reloaded.Status)
|
||||
}
|
||||
@@ -234,7 +234,7 @@ func TestConfirmIsTransactionalWhenASelectedWriteFails(t *testing.T) {
|
||||
|
||||
const callbackName = "test:fail_inbox_source_create"
|
||||
require.NoError(t, fixture.database.Callback().Create().Before("gorm:create").Register(callbackName, func(tx *gorm.DB) {
|
||||
if tx.Statement.Schema != nil && tx.Statement.Schema.Name == "SenlinAgentSource" {
|
||||
if tx.Statement.Schema != nil && tx.Statement.Schema.Name == "SaSource" {
|
||||
tx.AddError(errors.New("simulated source write failure"))
|
||||
}
|
||||
}))
|
||||
@@ -246,7 +246,7 @@ func TestConfirmIsTransactionalWhenASelectedWriteFails(t *testing.T) {
|
||||
|
||||
require.Equal(t, http.StatusInternalServerError, response.Code)
|
||||
requireFormalObjectCounts(t, fixture.database, 0, 0, 0)
|
||||
var reloaded models.SenlinAgentInboxItem
|
||||
var reloaded models.SaInboxItem
|
||||
require.NoError(t, fixture.database.First(&reloaded, item.ID).Error)
|
||||
require.Equal(t, "open", reloaded.Status)
|
||||
}
|
||||
@@ -268,7 +268,7 @@ func TestRepeatedConfirmReturnsPersistedResultAfterRelatedObjectsChange(t *testi
|
||||
requireFormalObjectCounts(t, fixture.database, 1, 1, 0)
|
||||
|
||||
sourceInboxItemID := item.ID
|
||||
extraSource := models.SenlinAgentSource{
|
||||
extraSource := models.SaSource{
|
||||
ProjectID: item.ProjectID, CreatedBy: fixture.owner.ID, SourceInboxItemID: &sourceInboxItemID,
|
||||
Kind: "text", Title: "后续关联资料", ContentText: "不得改变历史确认结果",
|
||||
}
|
||||
@@ -277,7 +277,7 @@ func TestRepeatedConfirmReturnsPersistedResultAfterRelatedObjectsChange(t *testi
|
||||
require.Equal(t, http.StatusOK, second.Code, second.Body.String())
|
||||
requireConfirmCreatedCount(t, second, 2)
|
||||
|
||||
var createdTask models.SenlinAgentTask
|
||||
var createdTask models.SaTask
|
||||
require.NoError(t, fixture.database.Where("source_inbox_item_id = ?", item.ID).First(&createdTask).Error)
|
||||
require.NoError(t, fixture.database.Delete(&createdTask).Error)
|
||||
require.NoError(t, fixture.database.Delete(&extraSource).Error)
|
||||
@@ -297,9 +297,9 @@ func (fixture inboxTestFixture) router(userID uint, analyzer Analyzer) http.Hand
|
||||
)
|
||||
}
|
||||
|
||||
func (fixture inboxTestFixture) createInbox(t *testing.T, userID, projectID uint) models.SenlinAgentInboxItem {
|
||||
func (fixture inboxTestFixture) createInbox(t *testing.T, userID, projectID uint) models.SaInboxItem {
|
||||
t.Helper()
|
||||
item := models.SenlinAgentInboxItem{
|
||||
item := models.SaInboxItem{
|
||||
ProjectID: projectID, CreatedBy: userID, SourceType: "text", Title: "待整理", Body: "需要整理的原始内容", Status: "open",
|
||||
}
|
||||
require.NoError(t, fixture.database.Create(&item).Error)
|
||||
@@ -309,11 +309,11 @@ func (fixture inboxTestFixture) createInbox(t *testing.T, userID, projectID uint
|
||||
func newInboxTestFixture(t *testing.T) inboxTestFixture {
|
||||
t.Helper()
|
||||
database := newTestDB(t)
|
||||
owner := models.SenlinAgentUser{Email: t.Name() + "-owner@example.com", DisplayName: "Owner", PasswordHash: "hash"}
|
||||
other := models.SenlinAgentUser{Email: t.Name() + "-other@example.com", DisplayName: "Other", PasswordHash: "hash"}
|
||||
owner := models.SaUser{Email: t.Name() + "-owner@example.com", DisplayName: "Owner", PasswordHash: "hash"}
|
||||
other := models.SaUser{Email: t.Name() + "-other@example.com", DisplayName: "Other", PasswordHash: "hash"}
|
||||
require.NoError(t, database.Create(&owner).Error)
|
||||
require.NoError(t, database.Create(&other).Error)
|
||||
project := models.SenlinAgentProject{OwnerID: owner.ID, Name: "Inbox Project", Identifier: "INBOX"}
|
||||
project := models.SaProject{OwnerID: owner.ID, Name: "Inbox Project", Identifier: "INBOX"}
|
||||
require.NoError(t, database.Create(&project).Error)
|
||||
return inboxTestFixture{database: database, owner: owner, other: other, project: project}
|
||||
}
|
||||
@@ -348,9 +348,9 @@ func requireFormalObjectCounts(t *testing.T, database *gorm.DB, tasks, notes, so
|
||||
model any
|
||||
want int64
|
||||
}{
|
||||
{model: &models.SenlinAgentTask{}, want: tasks},
|
||||
{model: &models.SenlinAgentNote{}, want: notes},
|
||||
{model: &models.SenlinAgentSource{}, want: sources},
|
||||
{model: &models.SaTask{}, want: tasks},
|
||||
{model: &models.SaNote{}, want: notes},
|
||||
{model: &models.SaSource{}, want: sources},
|
||||
} {
|
||||
var count int64
|
||||
require.NoError(t, database.Model(check.model).Count(&count).Error)
|
||||
@@ -365,7 +365,7 @@ func requireConfirmCreatedCount(t *testing.T, response *httptest.ResponseRecorde
|
||||
require.Equal(t, want, result.CreatedCount)
|
||||
}
|
||||
|
||||
func requireSourceInboxIdentity(t *testing.T, item models.SenlinAgentInboxItem, sourceID *uint, sourceIdentity *string) {
|
||||
func requireSourceInboxIdentity(t *testing.T, item models.SaInboxItem, sourceID *uint, sourceIdentity *string) {
|
||||
t.Helper()
|
||||
require.NotNil(t, sourceID)
|
||||
require.Equal(t, item.ID, *sourceID)
|
||||
|
||||
Reference in New Issue
Block a user