mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-07 13:51:36 +00:00
fix(m2-pr-d): thread ctx through Job/Notification/Audit services
Collapse CancelJobWithContext into CancelJob; eliminate 10 context.Background()
hits across the Job+Notification+Audit service cluster by threading ctx
through their handler-facing service interfaces.
Services (ctx-first):
- service/job.go: ListJobs, GetJob, CancelJob, ApproveJob, RejectJob now
accept ctx; the CancelJobWithContext wrapper is removed (handler callers
continue to invoke CancelJob, now ctx-aware).
- service/notification.go: ListNotifications, GetNotification, MarkAsRead
accept ctx.
- service/audit.go: ListAuditEvents, GetAuditEvent accept ctx.
Handlers (interface + callsites):
- handler/jobs.go, handler/notifications.go, handler/audit.go: local
service interfaces updated, r.Context() threaded at every callsite.
Tests:
- Mock services updated to match the new interfaces (ctx accepted and
ignored via '_ context.Context' first parameter; Fn closure fields
unchanged).
- job_test.go / notification_test.go callsites thread context.Background()
to match production shape.
Verification:
go build ./... ok
go vet ./... ok
go test -short ./... ok
go test -race -short ./... ok
golangci-lint run ./... 0 issues
Locked decisions from the M-2 plan:
D-1 ctx-only signatures (no dual forms)
D-4 preserve handler method names facing the router
D-5 domain types stay ctx-free
Audit complete. Commit: 1f6cf0eafa. Sections: 12. Findings: 2/7/10/4/6.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -11,8 +12,8 @@ import (
|
|||||||
|
|
||||||
// AuditService defines the service interface for audit event operations.
|
// AuditService defines the service interface for audit event operations.
|
||||||
type AuditService interface {
|
type AuditService interface {
|
||||||
ListAuditEvents(page, perPage int) ([]domain.AuditEvent, int64, error)
|
ListAuditEvents(ctx context.Context, page, perPage int) ([]domain.AuditEvent, int64, error)
|
||||||
GetAuditEvent(id string) (*domain.AuditEvent, error)
|
GetAuditEvent(ctx context.Context, id string) (*domain.AuditEvent, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AuditHandler handles HTTP requests for audit event operations.
|
// AuditHandler handles HTTP requests for audit event operations.
|
||||||
@@ -49,7 +50,7 @@ func (h AuditHandler) ListAuditEvents(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
events, total, err := h.svc.ListAuditEvents(page, perPage)
|
events, total, err := h.svc.ListAuditEvents(r.Context(), page, perPage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to list audit events", requestID)
|
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to list audit events", requestID)
|
||||||
return
|
return
|
||||||
@@ -83,7 +84,7 @@ func (h AuditHandler) GetAuditEvent(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
id = parts[0]
|
id = parts[0]
|
||||||
|
|
||||||
event, err := h.svc.GetAuditEvent(id)
|
event, err := h.svc.GetAuditEvent(r.Context(), id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrorWithRequestID(w, http.StatusNotFound, "Audit event not found", requestID)
|
ErrorWithRequestID(w, http.StatusNotFound, "Audit event not found", requestID)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -19,14 +19,14 @@ type mockAuditService struct {
|
|||||||
getFunc func(id string) (*domain.AuditEvent, error)
|
getFunc func(id string) (*domain.AuditEvent, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockAuditService) ListAuditEvents(page, perPage int) ([]domain.AuditEvent, int64, error) {
|
func (m *mockAuditService) ListAuditEvents(_ context.Context, page, perPage int) ([]domain.AuditEvent, int64, error) {
|
||||||
if m.listFunc != nil {
|
if m.listFunc != nil {
|
||||||
return m.listFunc(page, perPage)
|
return m.listFunc(page, perPage)
|
||||||
}
|
}
|
||||||
return nil, 0, nil
|
return nil, 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockAuditService) GetAuditEvent(id string) (*domain.AuditEvent, error) {
|
func (m *mockAuditService) GetAuditEvent(_ context.Context, id string) (*domain.AuditEvent, error) {
|
||||||
if m.getFunc != nil {
|
if m.getFunc != nil {
|
||||||
return m.getFunc(id)
|
return m.getFunc(id)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -21,35 +22,35 @@ type MockJobService struct {
|
|||||||
RejectJobFn func(id string, reason string) error
|
RejectJobFn func(id string, reason string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockJobService) ListJobs(status, jobType string, page, perPage int) ([]domain.Job, int64, error) {
|
func (m *MockJobService) ListJobs(_ context.Context, status, jobType string, page, perPage int) ([]domain.Job, int64, error) {
|
||||||
if m.ListJobsFn != nil {
|
if m.ListJobsFn != nil {
|
||||||
return m.ListJobsFn(status, jobType, page, perPage)
|
return m.ListJobsFn(status, jobType, page, perPage)
|
||||||
}
|
}
|
||||||
return nil, 0, nil
|
return nil, 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockJobService) GetJob(id string) (*domain.Job, error) {
|
func (m *MockJobService) GetJob(_ context.Context, id string) (*domain.Job, error) {
|
||||||
if m.GetJobFn != nil {
|
if m.GetJobFn != nil {
|
||||||
return m.GetJobFn(id)
|
return m.GetJobFn(id)
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockJobService) CancelJob(id string) error {
|
func (m *MockJobService) CancelJob(_ context.Context, id string) error {
|
||||||
if m.CancelJobFn != nil {
|
if m.CancelJobFn != nil {
|
||||||
return m.CancelJobFn(id)
|
return m.CancelJobFn(id)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockJobService) ApproveJob(id string) error {
|
func (m *MockJobService) ApproveJob(_ context.Context, id string) error {
|
||||||
if m.ApproveJobFn != nil {
|
if m.ApproveJobFn != nil {
|
||||||
return m.ApproveJobFn(id)
|
return m.ApproveJobFn(id)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockJobService) RejectJob(id string, reason string) error {
|
func (m *MockJobService) RejectJob(_ context.Context, id string, reason string) error {
|
||||||
if m.RejectJobFn != nil {
|
if m.RejectJobFn != nil {
|
||||||
return m.RejectJobFn(id, reason)
|
return m.RejectJobFn(id, reason)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -13,11 +14,11 @@ import (
|
|||||||
|
|
||||||
// JobService defines the service interface for job operations.
|
// JobService defines the service interface for job operations.
|
||||||
type JobService interface {
|
type JobService interface {
|
||||||
ListJobs(status, jobType string, page, perPage int) ([]domain.Job, int64, error)
|
ListJobs(ctx context.Context, status, jobType string, page, perPage int) ([]domain.Job, int64, error)
|
||||||
GetJob(id string) (*domain.Job, error)
|
GetJob(ctx context.Context, id string) (*domain.Job, error)
|
||||||
CancelJob(id string) error
|
CancelJob(ctx context.Context, id string) error
|
||||||
ApproveJob(id string) error
|
ApproveJob(ctx context.Context, id string) error
|
||||||
RejectJob(id string, reason string) error
|
RejectJob(ctx context.Context, id string, reason string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// JobHandler handles HTTP requests for job operations.
|
// JobHandler handles HTTP requests for job operations.
|
||||||
@@ -57,7 +58,7 @@ func (h JobHandler) ListJobs(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
jobs, total, err := h.svc.ListJobs(status, jobType, page, perPage)
|
jobs, total, err := h.svc.ListJobs(r.Context(), status, jobType, page, perPage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to list jobs", requestID)
|
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to list jobs", requestID)
|
||||||
return
|
return
|
||||||
@@ -91,7 +92,7 @@ func (h JobHandler) GetJob(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
id = parts[0]
|
id = parts[0]
|
||||||
|
|
||||||
job, err := h.svc.GetJob(id)
|
job, err := h.svc.GetJob(r.Context(), id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrorWithRequestID(w, http.StatusNotFound, "Job not found", requestID)
|
ErrorWithRequestID(w, http.StatusNotFound, "Job not found", requestID)
|
||||||
return
|
return
|
||||||
@@ -119,7 +120,7 @@ func (h JobHandler) CancelJob(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
jobID := parts[0]
|
jobID := parts[0]
|
||||||
|
|
||||||
if err := h.svc.CancelJob(jobID); err != nil {
|
if err := h.svc.CancelJob(r.Context(), jobID); err != nil {
|
||||||
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to cancel job", requestID)
|
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to cancel job", requestID)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -149,7 +150,7 @@ func (h JobHandler) ApproveJob(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
jobID := parts[0]
|
jobID := parts[0]
|
||||||
|
|
||||||
if err := h.svc.ApproveJob(jobID); err != nil {
|
if err := h.svc.ApproveJob(r.Context(), jobID); err != nil {
|
||||||
if strings.Contains(err.Error(), "not found") {
|
if strings.Contains(err.Error(), "not found") {
|
||||||
ErrorWithRequestID(w, http.StatusNotFound, "Job not found", requestID)
|
ErrorWithRequestID(w, http.StatusNotFound, "Job not found", requestID)
|
||||||
return
|
return
|
||||||
@@ -193,7 +194,7 @@ func (h JobHandler) RejectJob(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := h.svc.RejectJob(jobID, body.Reason); err != nil {
|
if err := h.svc.RejectJob(r.Context(), jobID, body.Reason); err != nil {
|
||||||
if strings.Contains(err.Error(), "not found") {
|
if strings.Contains(err.Error(), "not found") {
|
||||||
ErrorWithRequestID(w, http.StatusNotFound, "Job not found", requestID)
|
ErrorWithRequestID(w, http.StatusNotFound, "Job not found", requestID)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
@@ -17,21 +18,21 @@ type MockNotificationService struct {
|
|||||||
MarkAsReadFn func(id string) error
|
MarkAsReadFn func(id string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockNotificationService) ListNotifications(page, perPage int) ([]domain.NotificationEvent, int64, error) {
|
func (m *MockNotificationService) ListNotifications(_ context.Context, page, perPage int) ([]domain.NotificationEvent, int64, error) {
|
||||||
if m.ListNotificationsFn != nil {
|
if m.ListNotificationsFn != nil {
|
||||||
return m.ListNotificationsFn(page, perPage)
|
return m.ListNotificationsFn(page, perPage)
|
||||||
}
|
}
|
||||||
return nil, 0, nil
|
return nil, 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockNotificationService) GetNotification(id string) (*domain.NotificationEvent, error) {
|
func (m *MockNotificationService) GetNotification(_ context.Context, id string) (*domain.NotificationEvent, error) {
|
||||||
if m.GetNotificationFn != nil {
|
if m.GetNotificationFn != nil {
|
||||||
return m.GetNotificationFn(id)
|
return m.GetNotificationFn(id)
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockNotificationService) MarkAsRead(id string) error {
|
func (m *MockNotificationService) MarkAsRead(_ context.Context, id string) error {
|
||||||
if m.MarkAsReadFn != nil {
|
if m.MarkAsReadFn != nil {
|
||||||
return m.MarkAsReadFn(id)
|
return m.MarkAsReadFn(id)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -11,9 +12,9 @@ import (
|
|||||||
|
|
||||||
// NotificationService defines the service interface for notification operations.
|
// NotificationService defines the service interface for notification operations.
|
||||||
type NotificationService interface {
|
type NotificationService interface {
|
||||||
ListNotifications(page, perPage int) ([]domain.NotificationEvent, int64, error)
|
ListNotifications(ctx context.Context, page, perPage int) ([]domain.NotificationEvent, int64, error)
|
||||||
GetNotification(id string) (*domain.NotificationEvent, error)
|
GetNotification(ctx context.Context, id string) (*domain.NotificationEvent, error)
|
||||||
MarkAsRead(id string) error
|
MarkAsRead(ctx context.Context, id string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// NotificationHandler handles HTTP requests for notification operations.
|
// NotificationHandler handles HTTP requests for notification operations.
|
||||||
@@ -50,7 +51,7 @@ func (h NotificationHandler) ListNotifications(w http.ResponseWriter, r *http.Re
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
notifications, total, err := h.svc.ListNotifications(page, perPage)
|
notifications, total, err := h.svc.ListNotifications(r.Context(), page, perPage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to list notifications", requestID)
|
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to list notifications", requestID)
|
||||||
return
|
return
|
||||||
@@ -84,7 +85,7 @@ func (h NotificationHandler) GetNotification(w http.ResponseWriter, r *http.Requ
|
|||||||
}
|
}
|
||||||
id = parts[0]
|
id = parts[0]
|
||||||
|
|
||||||
notification, err := h.svc.GetNotification(id)
|
notification, err := h.svc.GetNotification(r.Context(), id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrorWithRequestID(w, http.StatusNotFound, "Notification not found", requestID)
|
ErrorWithRequestID(w, http.StatusNotFound, "Notification not found", requestID)
|
||||||
return
|
return
|
||||||
@@ -112,7 +113,7 @@ func (h NotificationHandler) MarkAsRead(w http.ResponseWriter, r *http.Request)
|
|||||||
}
|
}
|
||||||
notificationID := parts[0]
|
notificationID := parts[0]
|
||||||
|
|
||||||
if err := h.svc.MarkAsRead(notificationID); err != nil {
|
if err := h.svc.MarkAsRead(r.Context(), notificationID); err != nil {
|
||||||
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to mark notification as read", requestID)
|
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to mark notification as read", requestID)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ func (s *AuditService) ListByAction(ctx context.Context, action string, from, to
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListAuditEvents returns paginated audit events (handler interface method).
|
// ListAuditEvents returns paginated audit events (handler interface method).
|
||||||
func (s *AuditService) ListAuditEvents(page, perPage int) ([]domain.AuditEvent, int64, error) {
|
func (s *AuditService) ListAuditEvents(ctx context.Context, page, perPage int) ([]domain.AuditEvent, int64, error) {
|
||||||
if page < 1 {
|
if page < 1 {
|
||||||
page = 1
|
page = 1
|
||||||
}
|
}
|
||||||
@@ -123,7 +123,7 @@ func (s *AuditService) ListAuditEvents(page, perPage int) ([]domain.AuditEvent,
|
|||||||
PerPage: perPage,
|
PerPage: perPage,
|
||||||
}
|
}
|
||||||
|
|
||||||
events, err := s.auditRepo.List(context.Background(), filter)
|
events, err := s.auditRepo.List(ctx, filter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, fmt.Errorf("failed to list audit events: %w", err)
|
return nil, 0, fmt.Errorf("failed to list audit events: %w", err)
|
||||||
}
|
}
|
||||||
@@ -143,13 +143,13 @@ func (s *AuditService) ListAuditEvents(page, perPage int) ([]domain.AuditEvent,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetAuditEvent returns a single audit event (handler interface method).
|
// GetAuditEvent returns a single audit event (handler interface method).
|
||||||
func (s *AuditService) GetAuditEvent(id string) (*domain.AuditEvent, error) {
|
func (s *AuditService) GetAuditEvent(ctx context.Context, id string) (*domain.AuditEvent, error) {
|
||||||
filter := &repository.AuditFilter{
|
filter := &repository.AuditFilter{
|
||||||
ResourceID: id,
|
ResourceID: id,
|
||||||
PerPage: 1,
|
PerPage: 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
events, err := s.auditRepo.List(context.Background(), filter)
|
events, err := s.auditRepo.List(ctx, filter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get audit event: %w", err)
|
return nil, fmt.Errorf("failed to get audit event: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-15
@@ -189,8 +189,8 @@ func (s *JobService) GetJobStatus(ctx context.Context, jobID string) (*domain.Jo
|
|||||||
return job, nil
|
return job, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CancelJobWithContext cancels a pending or running job.
|
// CancelJob cancels a pending or running job (handler interface method).
|
||||||
func (s *JobService) CancelJobWithContext(ctx context.Context, jobID string) error {
|
func (s *JobService) CancelJob(ctx context.Context, jobID string) error {
|
||||||
job, err := s.jobRepo.Get(ctx, jobID)
|
job, err := s.jobRepo.Get(ctx, jobID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to fetch job: %w", err)
|
return fmt.Errorf("failed to fetch job: %w", err)
|
||||||
@@ -208,13 +208,8 @@ func (s *JobService) CancelJobWithContext(ctx context.Context, jobID string) err
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CancelJob cancels a job (handler interface method).
|
|
||||||
func (s *JobService) CancelJob(id string) error {
|
|
||||||
return s.CancelJobWithContext(context.Background(), id)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListJobs returns paginated jobs with optional filtering (handler interface method).
|
// ListJobs returns paginated jobs with optional filtering (handler interface method).
|
||||||
func (s *JobService) ListJobs(status, jobType string, page, perPage int) ([]domain.Job, int64, error) {
|
func (s *JobService) ListJobs(ctx context.Context, status, jobType string, page, perPage int) ([]domain.Job, int64, error) {
|
||||||
if page < 1 {
|
if page < 1 {
|
||||||
page = 1
|
page = 1
|
||||||
}
|
}
|
||||||
@@ -222,7 +217,7 @@ func (s *JobService) ListJobs(status, jobType string, page, perPage int) ([]doma
|
|||||||
perPage = 50
|
perPage = 50
|
||||||
}
|
}
|
||||||
|
|
||||||
allJobs, err := s.jobRepo.List(context.Background())
|
allJobs, err := s.jobRepo.List(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, fmt.Errorf("failed to list jobs: %w", err)
|
return nil, 0, fmt.Errorf("failed to list jobs: %w", err)
|
||||||
}
|
}
|
||||||
@@ -263,14 +258,13 @@ func (s *JobService) ListJobs(status, jobType string, page, perPage int) ([]doma
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetJob returns a single job (handler interface method).
|
// GetJob returns a single job (handler interface method).
|
||||||
func (s *JobService) GetJob(id string) (*domain.Job, error) {
|
func (s *JobService) GetJob(ctx context.Context, id string) (*domain.Job, error) {
|
||||||
return s.jobRepo.Get(context.Background(), id)
|
return s.jobRepo.Get(ctx, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApproveJob approves a renewal job that is awaiting approval.
|
// ApproveJob approves a renewal job that is awaiting approval.
|
||||||
// Transitions the job from AwaitingApproval to Pending so the scheduler picks it up.
|
// Transitions the job from AwaitingApproval to Pending so the scheduler picks it up.
|
||||||
func (s *JobService) ApproveJob(id string) error {
|
func (s *JobService) ApproveJob(ctx context.Context, id string) error {
|
||||||
ctx := context.Background()
|
|
||||||
job, err := s.jobRepo.Get(ctx, id)
|
job, err := s.jobRepo.Get(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("job not found: %w", err)
|
return fmt.Errorf("job not found: %w", err)
|
||||||
@@ -290,8 +284,7 @@ func (s *JobService) ApproveJob(id string) error {
|
|||||||
|
|
||||||
// RejectJob rejects a renewal job that is awaiting approval.
|
// RejectJob rejects a renewal job that is awaiting approval.
|
||||||
// Transitions the job to Cancelled with a rejection reason.
|
// Transitions the job to Cancelled with a rejection reason.
|
||||||
func (s *JobService) RejectJob(id string, reason string) error {
|
func (s *JobService) RejectJob(ctx context.Context, id string, reason string) error {
|
||||||
ctx := context.Background()
|
|
||||||
job, err := s.jobRepo.Get(ctx, id)
|
job, err := s.jobRepo.Get(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("job not found: %w", err)
|
return fmt.Errorf("job not found: %w", err)
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ func TestCancelJob(t *testing.T) {
|
|||||||
|
|
||||||
jobService := newTestJobService(jobRepo)
|
jobService := newTestJobService(jobRepo)
|
||||||
|
|
||||||
err := jobService.CancelJobWithContext(ctx, "job-001")
|
err := jobService.CancelJob(ctx, "job-001")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("CancelJob failed: %v", err)
|
t.Fatalf("CancelJob failed: %v", err)
|
||||||
}
|
}
|
||||||
@@ -129,13 +129,15 @@ func TestCancelJob_AlreadyCompleted(t *testing.T) {
|
|||||||
|
|
||||||
jobService := newTestJobService(jobRepo)
|
jobService := newTestJobService(jobRepo)
|
||||||
|
|
||||||
err := jobService.CancelJobWithContext(ctx, "job-001")
|
err := jobService.CancelJob(ctx, "job-001")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("expected error for completed job")
|
t.Fatal("expected error for completed job")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetJob(t *testing.T) {
|
func TestGetJob(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
job := &domain.Job{
|
job := &domain.Job{
|
||||||
ID: "job-001",
|
ID: "job-001",
|
||||||
@@ -153,7 +155,7 @@ func TestGetJob(t *testing.T) {
|
|||||||
|
|
||||||
jobService := newTestJobService(jobRepo)
|
jobService := newTestJobService(jobRepo)
|
||||||
|
|
||||||
retrieved, err := jobService.GetJob("job-001")
|
retrieved, err := jobService.GetJob(ctx, "job-001")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("GetJob failed: %v", err)
|
t.Fatalf("GetJob failed: %v", err)
|
||||||
}
|
}
|
||||||
@@ -167,6 +169,8 @@ func TestGetJob(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestListJobs(t *testing.T) {
|
func TestListJobs(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
job1 := &domain.Job{
|
job1 := &domain.Job{
|
||||||
ID: "job-001",
|
ID: "job-001",
|
||||||
@@ -192,7 +196,7 @@ func TestListJobs(t *testing.T) {
|
|||||||
|
|
||||||
jobService := newTestJobService(jobRepo)
|
jobService := newTestJobService(jobRepo)
|
||||||
|
|
||||||
jobs, total, err := jobService.ListJobs("", "", 1, 50)
|
jobs, total, err := jobService.ListJobs(ctx, "", "", 1, 50)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("ListJobs failed: %v", err)
|
t.Fatalf("ListJobs failed: %v", err)
|
||||||
}
|
}
|
||||||
@@ -206,6 +210,8 @@ func TestListJobs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestListJobs_FilterByStatus(t *testing.T) {
|
func TestListJobs_FilterByStatus(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
job1 := &domain.Job{
|
job1 := &domain.Job{
|
||||||
ID: "job-001",
|
ID: "job-001",
|
||||||
@@ -231,7 +237,7 @@ func TestListJobs_FilterByStatus(t *testing.T) {
|
|||||||
|
|
||||||
jobService := newTestJobService(jobRepo)
|
jobService := newTestJobService(jobRepo)
|
||||||
|
|
||||||
jobs, total, err := jobService.ListJobs(string(domain.JobStatusPending), "", 1, 50)
|
jobs, total, err := jobService.ListJobs(ctx, string(domain.JobStatusPending), "", 1, 50)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("ListJobs failed: %v", err)
|
t.Fatalf("ListJobs failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -319,7 +319,7 @@ func (s *NotificationService) GetNotificationHistory(ctx context.Context, certID
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListNotifications returns paginated notifications (handler interface method).
|
// ListNotifications returns paginated notifications (handler interface method).
|
||||||
func (s *NotificationService) ListNotifications(page, perPage int) ([]domain.NotificationEvent, int64, error) {
|
func (s *NotificationService) ListNotifications(ctx context.Context, page, perPage int) ([]domain.NotificationEvent, int64, error) {
|
||||||
if page < 1 {
|
if page < 1 {
|
||||||
page = 1
|
page = 1
|
||||||
}
|
}
|
||||||
@@ -332,7 +332,7 @@ func (s *NotificationService) ListNotifications(page, perPage int) ([]domain.Not
|
|||||||
PerPage: perPage,
|
PerPage: perPage,
|
||||||
}
|
}
|
||||||
|
|
||||||
notifications, err := s.notifRepo.List(context.Background(), filter)
|
notifications, err := s.notifRepo.List(ctx, filter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, fmt.Errorf("failed to list notifications: %w", err)
|
return nil, 0, fmt.Errorf("failed to list notifications: %w", err)
|
||||||
}
|
}
|
||||||
@@ -349,12 +349,12 @@ func (s *NotificationService) ListNotifications(page, perPage int) ([]domain.Not
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetNotification returns a single notification (handler interface method).
|
// GetNotification returns a single notification (handler interface method).
|
||||||
func (s *NotificationService) GetNotification(id string) (*domain.NotificationEvent, error) {
|
func (s *NotificationService) GetNotification(ctx context.Context, id string) (*domain.NotificationEvent, error) {
|
||||||
filter := &repository.NotificationFilter{
|
filter := &repository.NotificationFilter{
|
||||||
PerPage: 1,
|
PerPage: 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
notifications, err := s.notifRepo.List(context.Background(), filter)
|
notifications, err := s.notifRepo.List(ctx, filter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get notification: %w", err)
|
return nil, fmt.Errorf("failed to get notification: %w", err)
|
||||||
}
|
}
|
||||||
@@ -370,6 +370,6 @@ func (s *NotificationService) GetNotification(id string) (*domain.NotificationEv
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MarkAsRead marks a notification as read (handler interface method).
|
// MarkAsRead marks a notification as read (handler interface method).
|
||||||
func (s *NotificationService) MarkAsRead(id string) error {
|
func (s *NotificationService) MarkAsRead(ctx context.Context, id string) error {
|
||||||
return s.notifRepo.UpdateStatus(context.Background(), id, "read", time.Now())
|
return s.notifRepo.UpdateStatus(ctx, id, "read", time.Now())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -370,7 +370,7 @@ func TestListNotifications(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// List with pagination
|
// List with pagination
|
||||||
notifs, total, err := svc.ListNotifications(1, 3)
|
notifs, total, err := svc.ListNotifications(context.Background(), 1, 3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("ListNotifications failed: %v", err)
|
t.Fatalf("ListNotifications failed: %v", err)
|
||||||
}
|
}
|
||||||
@@ -404,7 +404,7 @@ func TestMarkAsRead(t *testing.T) {
|
|||||||
notifRepo.AddNotification(notif)
|
notifRepo.AddNotification(notif)
|
||||||
|
|
||||||
// Mark as read
|
// Mark as read
|
||||||
err := svc.MarkAsRead(notif.ID)
|
err := svc.MarkAsRead(context.Background(), notif.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("MarkAsRead failed: %v", err)
|
t.Fatalf("MarkAsRead failed: %v", err)
|
||||||
}
|
}
|
||||||
@@ -434,7 +434,7 @@ func TestGetNotification(t *testing.T) {
|
|||||||
notifRepo.AddNotification(notif)
|
notifRepo.AddNotification(notif)
|
||||||
|
|
||||||
// Get the notification
|
// Get the notification
|
||||||
retrieved, err := svc.GetNotification(notif.ID)
|
retrieved, err := svc.GetNotification(context.Background(), notif.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("GetNotification failed: %v", err)
|
t.Fatalf("GetNotification failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user