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:
@@ -30,6 +30,15 @@ type FeedbackResponse struct {
|
||||
CompanyId *string `json:"company_id"`
|
||||
}
|
||||
|
||||
// CompanyFeedbackStatsResponse represents the response for company feedback statistics
|
||||
type CompanyFeedbackStatsResponse struct {
|
||||
CompanyID string `json:"company_id"`
|
||||
TotalLikes int64 `json:"total_likes"`
|
||||
TotalDislikes int64 `json:"total_dislikes"`
|
||||
TotalFeedback int64 `json:"total_feedback"`
|
||||
LastUpdated int64 `json:"last_updated"` // Unix timestamp
|
||||
}
|
||||
|
||||
// GetDateFromTime returns the date_from as time.Time
|
||||
func (f *ListFeedbackForm) GetDateFromTime() *time.Time {
|
||||
if f.DateFrom == nil {
|
||||
|
||||
@@ -378,3 +378,29 @@ func (h *Handler) PublicGetFeedbackByTenderID(c echo.Context) error {
|
||||
|
||||
return response.Success(c, feedback, "Feedback retrieved successfully")
|
||||
}
|
||||
|
||||
// GetCompanyFeedbackStats retrieves feedback statistics for the authenticated user's company
|
||||
// @Summary Get company feedback statistics
|
||||
// @Description Retrieve comprehensive feedback statistics for the authenticated user's company including total likes, dislikes, and percentage calculations.
|
||||
// @Tags Feedback
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} response.APIResponse{data=CompanyFeedbackStatsResponse} "Company feedback statistics retrieved successfully"
|
||||
// @Failure 400 {object} response.APIResponse{error=string,message=string} "Bad request - Missing company association"
|
||||
// @Failure 401 {object} response.APIResponse{error=string,message=string} "Unauthorized - User not authenticated"
|
||||
// @Failure 500 {object} response.APIResponse{error=string,message=string} "Internal server error"
|
||||
// @Security BearerAuth
|
||||
// @Router /api/v1/feedback/stats/company [get]
|
||||
func (h *Handler) GetCompanyFeedbackStats(c echo.Context) error {
|
||||
companyID, err := user.GetCompanyIDFromContext(c)
|
||||
if err != nil {
|
||||
return response.BadRequest(c, "Company ID required", "User must be associated with a company")
|
||||
}
|
||||
|
||||
stats, err := h.service.CompanyFeedbackStats(c.Request().Context(), companyID)
|
||||
if err != nil {
|
||||
return response.InternalServerError(c, "Failed to retrieve company feedback statistics")
|
||||
}
|
||||
|
||||
return response.Success(c, stats, "Company feedback statistics retrieved successfully")
|
||||
}
|
||||
|
||||
@@ -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{}
|
||||
|
||||
@@ -13,6 +13,7 @@ type FeedbackService interface {
|
||||
ListFeedback(ctx context.Context, criteria FeedbackSearchCriteria, limit, offset int) (*FeedbackListResponse, error)
|
||||
DeleteFeedback(ctx context.Context, id string) error
|
||||
GetFeedbackByTenderID(ctx context.Context, tenderID, companyID string) (*FeedbackResponse, error)
|
||||
CompanyFeedbackStats(ctx context.Context, companyID string) (*CompanyFeedbackStatsResponse, error)
|
||||
}
|
||||
|
||||
// feedbackService implements FeedbackService interface
|
||||
@@ -152,3 +153,27 @@ func (s *feedbackService) GetFeedbackByTenderID(ctx context.Context, tenderID, c
|
||||
}
|
||||
return feedback.ToResponse(), nil
|
||||
}
|
||||
|
||||
func (s *feedbackService) CompanyFeedbackStats(ctx context.Context, companyID string) (*CompanyFeedbackStatsResponse, error) {
|
||||
s.logger.Info("Getting company feedback statistics", map[string]interface{}{
|
||||
"company_id": companyID,
|
||||
})
|
||||
|
||||
stats, err := s.feedbackRepo.GetCompanyFeedbackStats(ctx, companyID)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get company feedback statistics", map[string]interface{}{
|
||||
"company_id": companyID,
|
||||
"error": err.Error(),
|
||||
})
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.logger.Info("Company feedback statistics retrieved successfully", map[string]interface{}{
|
||||
"company_id": companyID,
|
||||
"total_feedback": stats.TotalFeedback,
|
||||
"total_likes": stats.TotalLikes,
|
||||
"total_dislikes": stats.TotalDislikes,
|
||||
})
|
||||
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user