Joe Chen 1 gadu atpakaļ
vecāks
revīzija
6345c503c8
2 mainītis faili ar 23 papildinājumiem un 2 dzēšanām
  1. 2 2
      internal/db/organizations.go
  2. 21 0
      internal/db/organizations_test.go

+ 2 - 2
internal/db/organizations.go

@@ -420,7 +420,7 @@ func (db *organizations) List(ctx context.Context, opts ListOrganizationsOptions
 	*/
 	conds := db.WithContext(ctx).Where("type = ?", UserTypeOrganization)
 
-	if opts.MemberID > 0 || opts.OwnerID > 0 || !opts.IncludePrivateMembers {
+	if opts.MemberID > 0 || opts.OwnerID > 0 {
 		conds.Joins(dbutil.Quote("JOIN org_user ON org_user.org_id = %s.id", "user"))
 	}
 	if opts.MemberID > 0 {
@@ -428,7 +428,7 @@ func (db *organizations) List(ctx context.Context, opts ListOrganizationsOptions
 	} else if opts.OwnerID > 0 {
 		conds.Where("org_user.uid = ? AND org_user.is_owner = ?", opts.OwnerID, true)
 	}
-	if !opts.IncludePrivateMembers {
+	if (opts.MemberID > 0 || opts.OwnerID > 0) && !opts.IncludePrivateMembers {
 		conds.Where("org_user.is_public = ?", true)
 	}
 

+ 21 - 0
internal/db/organizations_test.go

@@ -40,6 +40,7 @@ func TestOrganizations(t *testing.T) {
 		{"SearchByName", orgsSearchByName},
 		{"List", orgsList},
 		{"CountByUser", orgsCountByUser},
+		{"Count", orgsCount},
 	} {
 		t.Run(tc.name, func(t *testing.T) {
 			t.Cleanup(func() {
@@ -308,3 +309,23 @@ func orgsCountByUser(t *testing.T, ctx context.Context, db *organizations) {
 	require.NoError(t, err)
 	assert.Equal(t, int64(0), got)
 }
+
+func orgsCount(t *testing.T, db *organizations) {
+	ctx := context.Background()
+
+	// Has no organization initially
+	got := db.Count(ctx)
+	assert.Equal(t, int64(0), got)
+
+	tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "usersCount-tempPictureAvatarUploadPath")
+	conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
+
+	_, err := db.Create(ctx, "org1", 1, CreateOrganizationOptions{})
+	require.NoError(t, err)
+
+	// Create a user shouldn't count
+	_, err = NewUsersStore(db.DB).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
+	require.NoError(t, err)
+	got = db.Count(ctx)
+	assert.Equal(t, int64(1), got)
+}