models.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package models
  2. import (
  3. "time"
  4. )
  5. // User is an agent‑or‑human account that can call ERP tools.
  6. type User struct {
  7. ID uint `gorm:"primaryKey"`
  8. Email string `gorm:"size:255;not null;uniqueIndex"`
  9. Password string `gorm:"size:255;not null"` // hashed password or credential reference
  10. Roles []Role `gorm:"many2many:user_roles;constraint:OnDelete:CASCADE;"`
  11. CreatedAt time.Time
  12. UpdatedAt time.Time
  13. }
  14. // Note represents a note belonging to a user and is associated with a Service.
  15. type Note struct {
  16. ID uint `gorm:"primaryKey"`
  17. Title string `gorm:"size:200;not null"` // title of the note
  18. Content string `gorm:"type:text;not null"` // content body
  19. // Associations
  20. UserID uint
  21. User User `gorm:"foreignKey:UserID"`
  22. ServiceID uint
  23. Service Service `gorm:"foreignKey:ServiceID"`
  24. CreatedAt time.Time
  25. UpdatedAt time.Time
  26. }
  27. // Role groups permissions that can be assigned to users or agents.
  28. type Role struct {
  29. ID uint `gorm:"primaryKey"`
  30. Name string `gorm:"size:100;not null;uniqueIndex"`
  31. Description string `gorm:"size:255"`
  32. Permissions []Permission `gorm:"many2many:role_permissions;constraint:OnDelete:CASCADE;"`
  33. }
  34. // Permission is a fine‑grained action that can be allowed/denied.
  35. type Permission struct {
  36. ID uint `gorm:"primaryKey"`
  37. Code string `gorm:"size:100;not null;uniqueIndex"` // e.g. "invoice:create"
  38. Description string `gorm:"size:255"`
  39. }
  40. // Service & related entities (agents coordinate work around Services)
  41. type Service struct {
  42. ID uint `gorm:"primaryKey"`
  43. Name string `gorm:"size:200;not null"` // human‑readable Service name
  44. Description string `gorm:"type:text"`
  45. // Owner/creator of the Service
  46. CreatedByID uint
  47. CreatedBy User `gorm:"foreignKey:CreatedByID"`
  48. // Participants – many‑to‑many (agents & humans)
  49. Participants []User `gorm:"many2many:Service_participants;"`
  50. // Service tasks
  51. Tasks []Task `gorm:"foreignKey:ServiceID"`
  52. CreatedAt time.Time
  53. UpdatedAt time.Time
  54. }
  55. // Task management (assignable work items)
  56. type Task struct {
  57. ID uint `gorm:"primaryKey"`
  58. Title string `gorm:"size:200;not null"`
  59. Content string `gorm:"type:text;not null"`
  60. // Which service the task belongs to
  61. ServiceID *uint
  62. Service *Service `gorm:"foreignKey:ServiceID"`
  63. // Who created the task
  64. CreatedByID uint
  65. CreatedBy User `gorm:"foreignKey:CreatedByID"`
  66. // Assignment – can be nil (unassigned) or point to a user/agent
  67. AssigneeID *uint
  68. Assignee *User `gorm:"foreignKey:AssigneeID"`
  69. StatusID uint // FK to TaskStatus
  70. Status TaskStatus `gorm:"foreignKey:StatusID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
  71. DueDate *time.Time
  72. Priority string `gorm:"size:20"` // e.g. low/medium/high
  73. CreatedAt time.Time
  74. UpdatedAt time.Time
  75. }
  76. // Separate table for task status – makes it easy to add custom workflow steps.
  77. type TaskStatus struct {
  78. ID uint `gorm:"primaryKey"`
  79. Code string `gorm:"size:50;not null;uniqueIndex"` // e.g. "open", "in_progress", "done"
  80. Label string `gorm:"size:100"` // human‑readable label
  81. // Reverse relation (optional)
  82. Tasks []Task `gorm:"foreignKey:StatusID"`
  83. CreatedAt time.Time
  84. UpdatedAt time.Time
  85. }
  86. // Simple chat / messaging between users/agents
  87. type Channel struct {
  88. ID uint `gorm:"primaryKey"`
  89. // Participants – many‑to‑many (GORM will create the join table automatically)
  90. Participants []User `gorm:"many2many:conversation_participants;"`
  91. CreatedAt time.Time
  92. UpdatedAt time.Time
  93. }
  94. // Message sent inside a conversation.
  95. type Message struct {
  96. ID uint `gorm:"primaryKey"`
  97. ConversationID uint `gorm:"index;not null"` // refers to Channel.ID
  98. SenderID uint `gorm:"index;not null"` // user/agent that authored the message
  99. Sender User `gorm:"foreignKey:SenderID"`
  100. Content string `gorm:"type:text;not null"`
  101. SentAt time.Time `gorm:"autoCreateTime"`
  102. CreatedAt time.Time
  103. UpdatedAt time.Time
  104. }