|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "strconv" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/Financial-Partner/server/internal/contextutil" |
| 10 | + "github.com/Financial-Partner/server/internal/entities" |
| 11 | + "github.com/Financial-Partner/server/internal/interfaces/http/dto" |
| 12 | + httperror "github.com/Financial-Partner/server/internal/interfaces/http/error" |
| 13 | + responde "github.com/Financial-Partner/server/internal/interfaces/http/respond" |
| 14 | +) |
| 15 | + |
| 16 | +//go:generate mockgen -source=report.go -destination=report_mock.go -package=handler |
| 17 | + |
| 18 | +type ReportService interface { |
| 19 | + GetReport(ctx context.Context, userID string, startTime time.Time, endTime time.Time, reportType string) (*entities.Report, error) |
| 20 | + GetReportSummary(ctx context.Context, userID string) (*entities.ReportSummary, error) |
| 21 | +} |
| 22 | + |
| 23 | +// @Summary Get report |
| 24 | +// @Description Get report for a user |
| 25 | +// @Tags reports |
| 26 | +// @Accept json |
| 27 | +// @Produce json |
| 28 | +// @Param Authorization header string true "Bearer {token}" default |
| 29 | +// @Param type query string true "Type of report (summary or detailed)" |
| 30 | +// @Param start query int64 false "Start time as Unix timestamp (seconds since epoch)" |
| 31 | +// @Param end query int64 false "End time as Unix timestamp (seconds since epoch)" |
| 32 | +// @Success 200 {object} dto.ReportResponse |
| 33 | +// @Failure 400 {object} dto.ErrorResponse |
| 34 | +// @Failure 401 {object} dto.ErrorResponse |
| 35 | +// @Failure 500 {object} dto.ErrorResponse |
| 36 | +// @Router /reports/finance [get] |
| 37 | +func (h *Handler) GetReport(w http.ResponseWriter, r *http.Request) { |
| 38 | + // Parse query parameters |
| 39 | + reportType := r.URL.Query().Get("type") |
| 40 | + start := r.URL.Query().Get("start") |
| 41 | + end := r.URL.Query().Get("end") |
| 42 | + |
| 43 | + var startDate, endDate time.Time |
| 44 | + var err error |
| 45 | + if start != "" { |
| 46 | + startTimestamp, err := strconv.ParseInt(start, 10, 64) // Parse Unix timestamp |
| 47 | + if err != nil { |
| 48 | + h.log.Warnf("Invalid start timestamp format. Use a valid Unix timestamp.") |
| 49 | + responde.WithError(w, r, h.log, err, httperror.ErrInvalidParameter, http.StatusBadRequest) |
| 50 | + return |
| 51 | + } |
| 52 | + startDate = time.Unix(startTimestamp, 0).UTC() // Convert to time.Time in UTC |
| 53 | + } |
| 54 | + |
| 55 | + if end != "" { |
| 56 | + endTimestamp, err := strconv.ParseInt(end, 10, 64) // Parse Unix timestamp |
| 57 | + if err != nil { |
| 58 | + h.log.Warnf("Invalid end timestamp format. Use a valid Unix timestamp.") |
| 59 | + responde.WithError(w, r, h.log, err, httperror.ErrInvalidParameter, http.StatusBadRequest) |
| 60 | + return |
| 61 | + } |
| 62 | + endDate = time.Unix(endTimestamp, 0).UTC() // Convert to time.Time in UTC |
| 63 | + } |
| 64 | + |
| 65 | + userID, ok := contextutil.GetUserID(r.Context()) |
| 66 | + if !ok { |
| 67 | + h.log.Warnf("failed to get user ID from context") |
| 68 | + responde.WithError(w, r, h.log, nil, httperror.ErrUnauthorized, http.StatusUnauthorized) |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + report, err := h.reportService.GetReport(r.Context(), userID, startDate, endDate, reportType) |
| 73 | + if err != nil { |
| 74 | + h.log.Errorf("failed to get report") |
| 75 | + responde.WithError(w, r, h.log, err, httperror.ErrFailedToGetReport, http.StatusInternalServerError) |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + resp := dto.ReportResponse{ |
| 80 | + Revenue: report.Revenue, |
| 81 | + Expenses: report.Expenses, |
| 82 | + NetProfit: report.NetProfit, |
| 83 | + Categories: report.Categories, |
| 84 | + Amounts: report.Amounts, |
| 85 | + Percentages: report.Percentages, |
| 86 | + } |
| 87 | + |
| 88 | + responde.WithJSON(w, r, resp, http.StatusOK) |
| 89 | +} |
| 90 | + |
| 91 | +// @Summary Get report summary |
| 92 | +// @Description Get report summary generated by AI for a user |
| 93 | +// @Tags reports |
| 94 | +// @Accept json |
| 95 | +// @Produce json |
| 96 | +// @Param Authorization header string true "Bearer {token}" default |
| 97 | +// @Success 200 {object} dto.ReportSummaryResponse |
| 98 | +// @Failure 401 {object} dto.ErrorResponse |
| 99 | +// @Failure 500 {object} dto.ErrorResponse |
| 100 | +// @Router /reports/analysis [get] |
| 101 | +func (h *Handler) GetReportSummary(w http.ResponseWriter, r *http.Request) { |
| 102 | + userID, ok := contextutil.GetUserID(r.Context()) |
| 103 | + if !ok { |
| 104 | + h.log.Warnf("failed to get user ID from context") |
| 105 | + responde.WithError(w, r, h.log, nil, httperror.ErrUnauthorized, http.StatusUnauthorized) |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + reportSummary, err := h.reportService.GetReportSummary(r.Context(), userID) |
| 110 | + if err != nil { |
| 111 | + h.log.Errorf("failed to get report summary") |
| 112 | + responde.WithError(w, r, h.log, err, httperror.ErrFailedToGetReportSummary, http.StatusInternalServerError) |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + resp := dto.ReportSummaryResponse{ |
| 117 | + Summary: reportSummary.Summary, |
| 118 | + } |
| 119 | + |
| 120 | + responde.WithJSON(w, r, resp, http.StatusOK) |
| 121 | +} |
0 commit comments