Add Company Feedback Statistics Endpoint and Update API Documentation
- Introduced a new endpoint to retrieve comprehensive feedback statistics for the authenticated user's company, including total likes, dislikes, and percentage calculations. - Implemented the GetCompanyFeedbackStats method in the feedback handler to handle requests, ensuring proper authentication and error handling. - Updated Swagger JSON, YAML, and Go documentation to accurately reflect the new endpoint, including detailed descriptions, parameters, and response formats for the CompanyFeedbackStatsResponse. - Enhanced the feedback repository and service layers to support the new statistics functionality, improving the overall data handling capabilities of the tender management system. - These changes enhance the usability and functionality of the API, providing valuable insights into company feedback statistics.
This commit is contained in:
@@ -23,6 +23,7 @@ type Repository interface {
|
||||
GetFeedbackSummary(ctx context.Context, tenderID string) (*FeedbackSummary, error)
|
||||
GetFeedbackCountByType(ctx context.Context) (map[FeedbackType]int64, error)
|
||||
GetByTenderID(ctx context.Context, tenderID, companyID string) (*Feedback, error)
|
||||
GetCompanyFeedbackStats(ctx context.Context, companyID string) (*CompanyFeedbackStatsResponse, error)
|
||||
}
|
||||
|
||||
// feedbackRepository implements FeedbackRepository interface using MongoDB ORM
|
||||
@@ -412,6 +413,79 @@ func (r *feedbackRepository) GetByTenderID(ctx context.Context, tenderID, compan
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetCompanyFeedbackStats calculates feedback statistics for a company
|
||||
func (r *feedbackRepository) GetCompanyFeedbackStats(ctx context.Context, companyID string) (*CompanyFeedbackStatsResponse, error) {
|
||||
pipeline := driver.Pipeline{
|
||||
{
|
||||
{Key: "$match", Value: bson.M{
|
||||
"company_id": companyID,
|
||||
}},
|
||||
},
|
||||
{
|
||||
{Key: "$group", Value: bson.M{
|
||||
"_id": nil,
|
||||
"total_feedback": bson.M{
|
||||
"$sum": 1,
|
||||
},
|
||||
"total_likes": bson.M{
|
||||
"$sum": bson.M{
|
||||
"$cond": []interface{}{
|
||||
bson.M{"$eq": []string{"$feedback_type", string(FeedbackTypeLike)}},
|
||||
1,
|
||||
0,
|
||||
},
|
||||
},
|
||||
},
|
||||
"total_dislikes": bson.M{
|
||||
"$sum": bson.M{
|
||||
"$cond": []interface{}{
|
||||
bson.M{"$eq": []string{"$feedback_type", string(FeedbackTypeDislike)}},
|
||||
1,
|
||||
0,
|
||||
},
|
||||
},
|
||||
},
|
||||
"last_updated": bson.M{"$max": "$updated_at"},
|
||||
}},
|
||||
},
|
||||
}
|
||||
|
||||
results, err := r.repo.Aggregate(ctx, pipeline)
|
||||
if err != nil {
|
||||
r.logger.Error("Failed to get company feedback stats", map[string]interface{}{
|
||||
"company_id": companyID,
|
||||
"error": err.Error(),
|
||||
})
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stats := &CompanyFeedbackStatsResponse{
|
||||
CompanyID: companyID,
|
||||
TotalFeedback: 0,
|
||||
TotalLikes: 0,
|
||||
TotalDislikes: 0,
|
||||
LastUpdated: time.Now().Unix(),
|
||||
}
|
||||
|
||||
if len(results) > 0 {
|
||||
result := results[0]
|
||||
if totalFeedback, ok := result["total_feedback"].(int32); ok {
|
||||
stats.TotalFeedback = int64(totalFeedback)
|
||||
}
|
||||
if totalLikes, ok := result["total_likes"].(int32); ok {
|
||||
stats.TotalLikes = int64(totalLikes)
|
||||
}
|
||||
if totalDislikes, ok := result["total_dislikes"].(int32); ok {
|
||||
stats.TotalDislikes = int64(totalDislikes)
|
||||
}
|
||||
if lastUpdated, ok := result["last_updated"].(int64); ok {
|
||||
stats.LastUpdated = lastUpdated
|
||||
}
|
||||
}
|
||||
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
// buildSearchFilter builds MongoDB filter from search criteria
|
||||
func (r *feedbackRepository) buildSearchFilter(criteria FeedbackSearchCriteria) bson.M {
|
||||
filter := bson.M{}
|
||||
|
||||
Reference in New Issue
Block a user