Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Simplify storage warning candidate lookup

Neeraj Guptacb78d8e28197
diff --git a/server/pkg/repo/usage.go b/server/pkg/repo/usage.go
index 6434615656..76e7eb5aac 100644
--- a/server/pkg/repo/usage.go
+++ b/server/pkg/repo/usage.go
@@ -67,34 +67,15 @@ func (repo *UsageRepository) GetCombinedUsage(ctx context.Context, userIDs []int
func (repo *UsageRepository) GetStorageWarningCandidates(ctx context.Context, usageThreshold int64) ([]StorageWarningCandidate, error) {
rows, err := repo.DB.QueryContext(ctx, `
- SELECT recipient_id, is_family_plan
- FROM (
- SELECT
- f.admin_id AS recipient_id,
- TRUE AS is_family_plan
- FROM users u
- INNER JOIN usage us
- ON us.user_id = u.user_id
- INNER JOIN families f
- ON f.member_id = u.user_id
- AND f.admin_id = u.family_admin_id
- AND f.status IN ('SELF', 'ACCEPTED')
- WHERE
- us.storage_consumed > $1
- AND u.encrypted_email IS NOT NULL
- AND u.family_admin_id IS NOT NULL
- UNION ALL
- SELECT
- u.user_id AS recipient_id,
- FALSE AS is_family_plan
- FROM users u
- INNER JOIN usage us
- ON us.user_id = u.user_id
- WHERE
- us.storage_consumed > $1
- AND u.encrypted_email IS NOT NULL
- AND u.family_admin_id IS NULL
- ) candidates
+ SELECT
+ COALESCE(u.family_admin_id, u.user_id) AS recipient_id,
+ u.family_admin_id IS NOT NULL AS is_family_plan
+ FROM users u
+ INNER JOIN usage us
+ ON us.user_id = u.user_id
+ WHERE
+ us.storage_consumed > $1
+ AND u.encrypted_email IS NOT NULL
ORDER BY recipient_id
`, usageThreshold)
if err != nil {
diff --git a/server/pkg/repo/usage_storage_warning_test.go b/server/pkg/repo/usage_storage_warning_test.go
index 18d74f358f..e4e3c37393 100644
--- a/server/pkg/repo/usage_storage_warning_test.go
+++ b/server/pkg/repo/usage_storage_warning_test.go
@@ -205,7 +205,7 @@ func TestGetStorageWarningCandidatesExcludesFamilyTotalBelowPerMemberThreshold(t
}
}
-func TestGetStorageWarningCandidatesExcludesInactiveFamilyMemberships(t *testing.T) {
+func TestGetStorageWarningCandidatesUsesFamilyAdminIDForMembershipAwareness(t *testing.T) {
db := getStorageWarningRepoTestDB(t)
repo := &UsageRepository{DB: db}
ctx := context.Background()
@@ -214,7 +214,7 @@ func TestGetStorageWarningCandidatesExcludesInactiveFamilyMemberships(t *testing
memberID := int64(252)
insertStorageWarningTestUser(t, db, adminID, &adminID)
- insertStorageWarningTestUser(t, db, memberID, &adminID)
+ insertStorageWarningTestUser(t, db, memberID, nil)
insertStorageWarningTestFamilyMember(t, db, adminID, memberID, "INVITED")
insertStorageWarningTestUsage(t, db, memberID, 26*testGiB)
@@ -222,8 +222,14 @@ func TestGetStorageWarningCandidatesExcludesInactiveFamilyMemberships(t *testing
if err != nil {
t.Fatalf("GetStorageWarningCandidates() error = %v", err)
}
- if len(candidates) != 0 {
- t.Fatalf("expected no candidates for inactive family membership, got %+v", candidates)
+ if len(candidates) != 1 {
+ t.Fatalf("expected invited member without family_admin_id to be treated as an individual candidate, got %+v", candidates)
+ }
+ if candidates[0].RecipientID != memberID {
+ t.Fatalf("unexpected recipient id: got %d want %d", candidates[0].RecipientID, memberID)
+ }
+ if candidates[0].IsFamilyPlan {
+ t.Fatal("expected invited member without family_admin_id to not be marked as family plan")
}
}