- Removed detailed event routes from the API. - Added new report request routes for creating and fetching report requests. - Introduced new domain models for report requests, including metadata and status handling. - Implemented report request processing logic, including CSV generation for event interval reports. - Enhanced company statistics handling with new domain models and service methods. - Updated repository interfaces and implementations to support new report functionalities. - Added error handling and logging for report file operations and notifications.
104 lines
3.2 KiB
Go
104 lines
3.2 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
dbgen "github.com/SamuelTariku/FortuneBet-Backend/gen/db"
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
func (s *Store) CreateReportRequest(ctx context.Context, report domain.CreateReportRequest) (domain.ReportRequest, error) {
|
|
|
|
reportMetadata, err := report.Metadata.ToPG()
|
|
if err != nil {
|
|
return domain.ReportRequest{}, err
|
|
}
|
|
|
|
dbReportRequest, err := s.queries.CreateReportRequest(ctx, dbgen.CreateReportRequestParams{
|
|
CompanyID: report.CompanyID.ToPG(),
|
|
RequestedBy: report.RequestedBy.ToPG(),
|
|
Type: string(report.Type),
|
|
Metadata: reportMetadata,
|
|
})
|
|
|
|
if err != nil {
|
|
return domain.ReportRequest{}, fmt.Errorf("failed to create report request: %w", err)
|
|
}
|
|
|
|
return domain.ConvertDBReportRequest(dbReportRequest)
|
|
}
|
|
|
|
func (s *Store) GetAllReportRequests(ctx context.Context, filter domain.ReportRequestFilter) ([]domain.ReportRequestDetail, int64, error) {
|
|
dbReportRequests, err := s.queries.GetAllReportRequests(ctx, dbgen.GetAllReportRequestsParams{
|
|
CompanyID: filter.CompanyID.ToPG(),
|
|
Type: filter.Type.ToPG(),
|
|
Status: filter.Status.ToPG(),
|
|
Limit: filter.Limit.ToPG(),
|
|
Offset: pgtype.Int4{
|
|
Int32: int32(filter.Offset.Value * filter.Limit.Value),
|
|
Valid: filter.Offset.Valid,
|
|
},
|
|
RequestedBy: filter.RequestedBy.ToPG(),
|
|
})
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
total, err := s.queries.GetTotalReportRequests(ctx, dbgen.GetTotalReportRequestsParams{
|
|
CompanyID: filter.CompanyID.ToPG(),
|
|
Type: filter.Type.ToPG(),
|
|
Status: filter.Status.ToPG(),
|
|
RequestedBy: filter.RequestedBy.ToPG(),
|
|
})
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
result, err := domain.ConvertDBReportRequestDetailList(dbReportRequests)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return result, total, nil
|
|
}
|
|
func (s *Store) GetReportRequestByRequestedByID(ctx context.Context, requestedBy int64, filter domain.ReportRequestFilter) ([]domain.ReportRequestDetail, error) {
|
|
dbReportRequests, err := s.queries.GetReportRequestByRequestedByID(ctx, dbgen.GetReportRequestByRequestedByIDParams{
|
|
RequestedBy: pgtype.Int8{
|
|
Int64: requestedBy,
|
|
Valid: true,
|
|
},
|
|
Type: filter.Type.ToPG(),
|
|
Status: filter.Status.ToPG(),
|
|
Limit: filter.Limit.ToPG(),
|
|
Offset: pgtype.Int4{
|
|
Int32: int32(filter.Offset.Value * filter.Limit.Value),
|
|
Valid: filter.Offset.Valid,
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return domain.ConvertDBReportRequestDetailList(dbReportRequests)
|
|
}
|
|
|
|
func (s *Store) GetReportRequestByID(ctx context.Context, ID int64) (domain.ReportRequestDetail, error) {
|
|
dbReportRequest, err := s.queries.GetReportRequestByID(ctx, ID)
|
|
if err != nil {
|
|
return domain.ReportRequestDetail{}, err
|
|
}
|
|
return domain.ConvertDBReportRequestDetail(dbReportRequest)
|
|
}
|
|
|
|
func (s *Store) UpdateReportRequest(ctx context.Context, report domain.UpdateRequestRequest) error {
|
|
err := s.queries.UpdateReportRequest(ctx, dbgen.UpdateReportRequestParams{
|
|
ID: report.ID,
|
|
FilePath: report.FilePath.ToPG(),
|
|
RejectReason: report.RejectReason.ToPG(),
|
|
Status: report.Status.ToPG(),
|
|
})
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("failed to update report request: %w", err)
|
|
}
|
|
return nil
|
|
}
|