50 lines
2.4 KiB
Go
50 lines
2.4 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// SummaryReportRepo provides read-only data access for summary reports.
|
|
// It queries the existing reporting_events_rollups table — no separate summary_reports table exists.
|
|
// Reference: Chatwoot SummaryReport controller — queries ReportingEventsRollup filtered by dimension_type.
|
|
type SummaryReportRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewSummaryReportRepo creates a new SummaryReportRepo.
|
|
func NewSummaryReportRepo(db *gorm.DB) *SummaryReportRepo {
|
|
return &SummaryReportRepo{db: db}
|
|
}
|
|
|
|
// GetAgentSummary retrieves agent-level summary metrics for an account within a date range.
|
|
func (r *SummaryReportRepo) GetAgentSummary(ctx context.Context, accountID uint, since, until time.Time) ([]model.ReportingEventsRollup, error) {
|
|
return r.aggregateByDimensionType(ctx, accountID, model.DimensionAgent, since, until)
|
|
}
|
|
|
|
// GetTeamSummary retrieves team-level summary metrics for an account within a date range.
|
|
func (r *SummaryReportRepo) GetTeamSummary(ctx context.Context, accountID uint, since, until time.Time) ([]model.ReportingEventsRollup, error) {
|
|
return r.aggregateByDimensionType(ctx, accountID, model.DimensionTeam, since, until)
|
|
}
|
|
|
|
// GetInboxSummary retrieves inbox-level summary metrics for an account within a date range.
|
|
func (r *SummaryReportRepo) GetInboxSummary(ctx context.Context, accountID uint, since, until time.Time) ([]model.ReportingEventsRollup, error) {
|
|
return r.aggregateByDimensionType(ctx, accountID, model.DimensionInbox, since, until)
|
|
}
|
|
|
|
// GetLabelSummary retrieves label-level summary metrics for an account within a date range.
|
|
func (r *SummaryReportRepo) GetLabelSummary(ctx context.Context, accountID uint, since, until time.Time) ([]model.ReportingEventsRollup, error) {
|
|
return r.aggregateByDimensionType(ctx, accountID, model.DimensionLabel, since, until)
|
|
}
|
|
|
|
// aggregateByDimensionType queries ReportingEventsRollup filtered by the given dimension type and date range.
|
|
func (r *SummaryReportRepo) aggregateByDimensionType(ctx context.Context, accountID uint, dimType model.DimensionType, since, until time.Time) ([]model.ReportingEventsRollup, error) {
|
|
var rollups []model.ReportingEventsRollup
|
|
err := r.db.WithContext(ctx).
|
|
Where("account_id = ? AND dimension_type = ? AND date >= ? AND date <= ?", accountID, dimType, since, until).
|
|
Find(&rollups).Error
|
|
return rollups, err
|
|
} |