orgsCount

This commit is contained in:
Joe Chen
2023-11-12 21:42:14 -05:00
parent cbd3c7a40b
commit 6345c503c8
2 changed files with 23 additions and 2 deletions

View File

@@ -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)
}

View File

@@ -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)
}