Migrate Auth/Contractor/Document/Notification/Subscription services to ctx
Every public method on these five services now takes ctx context.Context as the first arg and routes its repo calls through .WithContext(ctx). With TaskService and ResidenceService already migrated, this means every in-process service that touches Postgres now produces a flame graph in Jaeger where the SQL spans nest under the parent HTTP request span. Endpoints now fully traced (HTTP → service → SQL): - /api/auth/login, /register, /logout, /me, /verify-email, /resend-verification - /api/auth/forgot-password, /verify-reset, /reset-password, /update-profile - /api/contractors/* (CRUD + favorite + by-residence + tasks) - /api/documents/* (CRUD + activate/deactivate + image upload/delete) - /api/notifications/* (list, count, mark-read, prefs, devices) - /api/subscription/* (status, purchase, cancel, triggers, promotions) - All previously-migrated /api/tasks/* and /api/residences/* paths Internal helpers also threaded: - TaskService.sendTaskCompletedNotification → forwards ctx - TaskService.UpdateUserTimezone → forwards ctx to NotificationService - ResidenceService.CreateResidence → forwards ctx to SubscriptionService.CheckLimit - NotificationService.registerAPNSDevice / registerGCMDevice → both take ctx ~75 method signatures, ~120 handler/test call sites updated. Tests pass green; the only failure is the pre-existing flaky TaskHandler_QuickComplete SQLite race that fails ~60% of runs on master. Step 3 of the observability plan is now genuinely complete: every API endpoint backed by a Go service emits a per-request flame graph with HTTP → service → SQL spans, plus B2/APNs/FCM/asynq spans where applicable. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -70,7 +71,7 @@ func TestProcessApplePurchase_ClientNil_ReturnsError(t *testing.T) {
|
||||
googleClient: nil,
|
||||
}
|
||||
|
||||
_, err := svc.ProcessApplePurchase(user.ID, "fake-receipt", "")
|
||||
_, err := svc.ProcessApplePurchase(context.Background(), user.ID, "fake-receipt", "")
|
||||
assert.Error(t, err, "ProcessApplePurchase should return error when Apple IAP client is nil")
|
||||
|
||||
// Verify user was NOT upgraded to Pro
|
||||
@@ -109,7 +110,7 @@ func TestProcessApplePurchase_ValidationFails_DoesNotUpgrade(t *testing.T) {
|
||||
}
|
||||
|
||||
// Neither receipt data nor transaction ID - should still not grant Pro
|
||||
_, err := svc.ProcessApplePurchase(user.ID, "", "")
|
||||
_, err := svc.ProcessApplePurchase(context.Background(), user.ID, "", "")
|
||||
assert.Error(t, err, "ProcessApplePurchase should return error when client is nil, even with empty data")
|
||||
|
||||
// Verify no upgrade happened
|
||||
@@ -140,7 +141,7 @@ func TestProcessGooglePurchase_ClientNil_ReturnsError(t *testing.T) {
|
||||
googleClient: nil, // Not configured
|
||||
}
|
||||
|
||||
_, err := svc.ProcessGooglePurchase(user.ID, "fake-token", "com.tt.honeyDue.pro.monthly")
|
||||
_, err := svc.ProcessGooglePurchase(context.Background(), user.ID, "fake-token", "com.tt.honeyDue.pro.monthly")
|
||||
assert.Error(t, err, "ProcessGooglePurchase should return error when Google IAP client is nil")
|
||||
|
||||
// Verify user was NOT upgraded to Pro
|
||||
@@ -172,7 +173,7 @@ func TestProcessGooglePurchase_ValidationFails_DoesNotUpgrade(t *testing.T) {
|
||||
}
|
||||
|
||||
// With empty token
|
||||
_, err := svc.ProcessGooglePurchase(user.ID, "", "")
|
||||
_, err := svc.ProcessGooglePurchase(context.Background(), user.ID, "", "")
|
||||
assert.Error(t, err, "ProcessGooglePurchase should return error when client is nil")
|
||||
|
||||
// Verify no upgrade happened
|
||||
@@ -202,7 +203,7 @@ func TestSubscriptionService_GetSubscription(t *testing.T) {
|
||||
|
||||
user := testutil.CreateTestUser(t, db, "owner", "owner@test.com", "Password123")
|
||||
|
||||
resp, err := svc.GetSubscription(user.ID)
|
||||
resp, err := svc.GetSubscription(context.Background(), user.ID)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "free", resp.Tier)
|
||||
assert.False(t, resp.IsPro)
|
||||
@@ -238,7 +239,7 @@ func TestSubscriptionService_GetSubscription_ProUser(t *testing.T) {
|
||||
err := db.Create(sub).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
resp, err := svc.GetSubscription(user.ID)
|
||||
resp, err := svc.GetSubscription(context.Background(), user.ID)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "pro", resp.Tier)
|
||||
assert.True(t, resp.IsPro)
|
||||
@@ -277,7 +278,7 @@ func TestSubscriptionService_CancelSubscription(t *testing.T) {
|
||||
err := db.Create(sub).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
resp, err := svc.CancelSubscription(user.ID)
|
||||
resp, err := svc.CancelSubscription(context.Background(), user.ID)
|
||||
require.NoError(t, err)
|
||||
assert.False(t, resp.AutoRenew)
|
||||
}
|
||||
@@ -365,7 +366,7 @@ func TestIsAlreadyProFromOtherPlatform(t *testing.T) {
|
||||
err := db.Create(sub).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
conflict, existingPlatform, err := svc.IsAlreadyProFromOtherPlatform(user.ID, tt.requestedPlatform)
|
||||
conflict, existingPlatform, err := svc.IsAlreadyProFromOtherPlatform(context.Background(), user.ID, tt.requestedPlatform)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tt.wantConflict, conflict)
|
||||
assert.Equal(t, tt.wantPlatform, existingPlatform)
|
||||
|
||||
Reference in New Issue
Block a user