turash/bugulma/backend/internal/domain/trust_metrics.go
Damir Mukimov 000eab4740
Major repository reorganization and missing backend endpoints implementation
Repository Structure:
- Move files from cluttered root directory into organized structure
- Create archive/ for archived data and scraper results
- Create bugulma/ for the complete application (frontend + backend)
- Create data/ for sample datasets and reference materials
- Create docs/ for comprehensive documentation structure
- Create scripts/ for utility scripts and API tools

Backend Implementation:
- Implement 3 missing backend endpoints identified in gap analysis:
  * GET /api/v1/organizations/{id}/matching/direct - Direct symbiosis matches
  * GET /api/v1/users/me/organizations - User organizations
  * POST /api/v1/proposals/{id}/status - Update proposal status
- Add complete proposal domain model, repository, and service layers
- Create database migration for proposals table
- Fix CLI server command registration issue

API Documentation:
- Add comprehensive proposals.md API documentation
- Update README.md with Users and Proposals API sections
- Document all request/response formats, error codes, and business rules

Code Quality:
- Follow existing Go backend architecture patterns
- Add proper error handling and validation
- Match frontend expected response schemas
- Maintain clean separation of concerns (handler -> service -> repository)
2025-11-25 06:01:16 +01:00

131 lines
5.5 KiB
Go

package domain
import (
"context"
"time"
"gorm.io/datatypes"
)
// TrustMetrics represents trust and quality metrics for organizations and data
type TrustMetrics struct {
ID string `gorm:"primaryKey;type:text"`
OrganizationID string `gorm:"type:text;not null;index"`
MetricType string `gorm:"type:varchar(50);not null"` // organization, resource_flow, site
MetricName string `gorm:"type:varchar(100);not null"`
Value float64 `gorm:"type:decimal(5,4)"` // 0.0 to 1.0
Score string `gorm:"type:varchar(20)"` // excellent, good, fair, poor
Evidence datatypes.JSON `gorm:"type:jsonb"` // Supporting data
LastUpdated time.Time `gorm:"autoUpdateTime"`
ValidUntil time.Time `gorm:"index"`
// Associations
Organization *Organization `gorm:"foreignKey:OrganizationID"`
}
// TableName specifies the table name for GORM
func (TrustMetrics) TableName() string {
return "trust_metrics"
}
// DataQualityProfile represents a comprehensive quality profile for an organization
type DataQualityProfile struct {
OrganizationID string `json:"organization_id"`
OverallScore float64 `json:"overall_score"` // 0-1
Completeness float64 `json:"completeness"` // 0-1
Accuracy float64 `json:"accuracy"` // 0-1
Timeliness float64 `json:"timeliness"` // 0-1
Consistency float64 `json:"consistency"` // 0-1
LastAssessed time.Time `json:"last_assessed"`
AssessmentPeriod time.Duration `json:"assessment_period"`
Metrics []TrustMetrics `json:"metrics"`
Recommendations []string `json:"recommendations"`
RiskFactors []RiskFactor `json:"risk_factors"`
}
// RiskFactor represents a risk factor affecting trust
type RiskFactor struct {
Type string `json:"type"` // data_quality, verification, historical
Description string `json:"description"`
Severity float64 `json:"severity"` // 0-1
Impact string `json:"impact"` // low, medium, high
Mitigation string `json:"mitigation"`
}
// TrustMetricsRepository defines the interface for trust metrics operations
type TrustMetricsRepository interface {
Create(ctx context.Context, metric *TrustMetrics) error
GetByOrganization(ctx context.Context, orgID string) ([]*TrustMetrics, error)
GetByType(ctx context.Context, orgID, metricType string) ([]*TrustMetrics, error)
Update(ctx context.Context, metric *TrustMetrics) error
Delete(ctx context.Context, id string) error
GetLatestProfile(ctx context.Context, orgID string) (*DataQualityProfile, error)
}
// VerificationStatus represents verification status for data elements
type VerificationStatus string
const (
VerificationUnverified VerificationStatus = "unverified"
VerificationPending VerificationStatus = "pending"
VerificationVerified VerificationStatus = "verified"
VerificationRejected VerificationStatus = "rejected"
)
// VerifiedData represents verified data elements
type VerifiedData struct {
ID string `gorm:"primaryKey;type:text"`
OrganizationID string `gorm:"type:text;not null;index"`
DataType string `gorm:"type:varchar(50);not null"` // organization, resource_flow, site
DataID string `gorm:"type:text;not null"`
Status VerificationStatus `gorm:"type:varchar(20);default:'unverified'"`
VerifiedBy string `gorm:"type:text"`
VerifiedAt *time.Time `gorm:"index"`
Evidence datatypes.JSON `gorm:"type:jsonb"`
ExpiresAt *time.Time `gorm:"index"`
Notes string `gorm:"type:text"`
// Associations
Organization *Organization `gorm:"foreignKey:OrganizationID"`
}
// TableName specifies the table name for GORM
func (VerifiedData) TableName() string {
return "verified_data"
}
// HistoricalSuccess represents historical success metrics
type HistoricalSuccess struct {
ID string `gorm:"primaryKey;type:text"`
OrganizationID string `gorm:"type:text;not null;index"`
MatchID string `gorm:"type:text;index"`
MetricType string `gorm:"type:varchar(50);not null"` // completion_rate, satisfaction, volume
Value float64 `gorm:"type:decimal(5,4)"`
Period string `gorm:"type:varchar(20)"` // monthly, quarterly, yearly
RecordedAt time.Time `gorm:"not null;index"`
Data datatypes.JSON `gorm:"type:jsonb"` // Additional context
// Associations
Organization *Organization `gorm:"foreignKey:OrganizationID"`
}
// TableName specifies the table name for GORM
func (HistoricalSuccess) TableName() string {
return "historical_success"
}
// TrustScore represents an overall trust score for an organization
type TrustScore struct {
OrganizationID string `json:"organization_id"`
OverallScore float64 `json:"overall_score"` // 0-1
DataQualityScore float64 `json:"data_quality_score"` // 0-1
VerificationScore float64 `json:"verification_score"` // 0-1
HistoricalScore float64 `json:"historical_score"` // 0-1
PeerReviewScore float64 `json:"peer_review_score"` // 0-1
LastCalculated time.Time `json:"last_calculated"`
ConfidenceLevel float64 `json:"confidence_level"` // 0-1
ScoreBreakdown map[string]float64 `json:"score_breakdown"`
RiskFactors []RiskFactor `json:"risk_factors"`
Recommendations []string `json:"recommendations"`
}