user_attribute.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. infraerrors "github.com/Wei-Shaw/sub2api/internal/pkg/errors"
  6. )
  7. // Error definitions for user attribute operations
  8. var (
  9. ErrAttributeDefinitionNotFound = infraerrors.NotFound("ATTRIBUTE_DEFINITION_NOT_FOUND", "attribute definition not found")
  10. ErrAttributeKeyExists = infraerrors.Conflict("ATTRIBUTE_KEY_EXISTS", "attribute key already exists")
  11. ErrInvalidAttributeType = infraerrors.BadRequest("INVALID_ATTRIBUTE_TYPE", "invalid attribute type")
  12. ErrAttributeValidationFailed = infraerrors.BadRequest("ATTRIBUTE_VALIDATION_FAILED", "attribute value validation failed")
  13. )
  14. // UserAttributeType represents supported attribute types
  15. type UserAttributeType string
  16. const (
  17. AttributeTypeText UserAttributeType = "text"
  18. AttributeTypeTextarea UserAttributeType = "textarea"
  19. AttributeTypeNumber UserAttributeType = "number"
  20. AttributeTypeEmail UserAttributeType = "email"
  21. AttributeTypeURL UserAttributeType = "url"
  22. AttributeTypeDate UserAttributeType = "date"
  23. AttributeTypeSelect UserAttributeType = "select"
  24. AttributeTypeMultiSelect UserAttributeType = "multi_select"
  25. )
  26. // UserAttributeOption represents a select option for select/multi_select types
  27. type UserAttributeOption struct {
  28. Value string `json:"value"`
  29. Label string `json:"label"`
  30. }
  31. // UserAttributeValidation represents validation rules for an attribute
  32. type UserAttributeValidation struct {
  33. MinLength *int `json:"min_length,omitempty"`
  34. MaxLength *int `json:"max_length,omitempty"`
  35. Min *int `json:"min,omitempty"`
  36. Max *int `json:"max,omitempty"`
  37. Pattern *string `json:"pattern,omitempty"`
  38. Message *string `json:"message,omitempty"`
  39. }
  40. // UserAttributeDefinition represents a custom attribute definition
  41. type UserAttributeDefinition struct {
  42. ID int64
  43. Key string
  44. Name string
  45. Description string
  46. Type UserAttributeType
  47. Options []UserAttributeOption
  48. Required bool
  49. Validation UserAttributeValidation
  50. Placeholder string
  51. DisplayOrder int
  52. Enabled bool
  53. CreatedAt time.Time
  54. UpdatedAt time.Time
  55. }
  56. // UserAttributeValue represents a user's attribute value
  57. type UserAttributeValue struct {
  58. ID int64
  59. UserID int64
  60. AttributeID int64
  61. Value string
  62. CreatedAt time.Time
  63. UpdatedAt time.Time
  64. }
  65. // CreateAttributeDefinitionInput for creating new definition
  66. type CreateAttributeDefinitionInput struct {
  67. Key string
  68. Name string
  69. Description string
  70. Type UserAttributeType
  71. Options []UserAttributeOption
  72. Required bool
  73. Validation UserAttributeValidation
  74. Placeholder string
  75. Enabled bool
  76. }
  77. // UpdateAttributeDefinitionInput for updating definition
  78. type UpdateAttributeDefinitionInput struct {
  79. Name *string
  80. Description *string
  81. Type *UserAttributeType
  82. Options *[]UserAttributeOption
  83. Required *bool
  84. Validation *UserAttributeValidation
  85. Placeholder *string
  86. Enabled *bool
  87. }
  88. // UpdateUserAttributeInput for updating a single attribute value
  89. type UpdateUserAttributeInput struct {
  90. AttributeID int64
  91. Value string
  92. }
  93. // UserAttributeDefinitionRepository interface for attribute definition persistence
  94. type UserAttributeDefinitionRepository interface {
  95. Create(ctx context.Context, def *UserAttributeDefinition) error
  96. GetByID(ctx context.Context, id int64) (*UserAttributeDefinition, error)
  97. GetByKey(ctx context.Context, key string) (*UserAttributeDefinition, error)
  98. Update(ctx context.Context, def *UserAttributeDefinition) error
  99. Delete(ctx context.Context, id int64) error
  100. List(ctx context.Context, enabledOnly bool) ([]UserAttributeDefinition, error)
  101. UpdateDisplayOrders(ctx context.Context, orders map[int64]int) error
  102. ExistsByKey(ctx context.Context, key string) (bool, error)
  103. }
  104. // UserAttributeValueRepository interface for user attribute value persistence
  105. type UserAttributeValueRepository interface {
  106. GetByUserID(ctx context.Context, userID int64) ([]UserAttributeValue, error)
  107. GetByUserIDs(ctx context.Context, userIDs []int64) ([]UserAttributeValue, error)
  108. UpsertBatch(ctx context.Context, userID int64, values []UpdateUserAttributeInput) error
  109. DeleteByAttributeID(ctx context.Context, attributeID int64) error
  110. DeleteByUserID(ctx context.Context, userID int64) error
  111. }