forked from baron/baron-sso
chore: snapshot local state before dev merge
This commit is contained in:
@@ -69,6 +69,26 @@ func (r *userRepository) withTenantMembershipFilter(db *gorm.DB, tenantIDs []str
|
||||
clauses = append(clauses, "metadata @> ?::jsonb")
|
||||
args = append(args, string(payload))
|
||||
}
|
||||
clauses = append(clauses, `EXISTS (
|
||||
SELECT 1
|
||||
FROM tenants AS membership_tenants
|
||||
WHERE membership_tenants.id IN ?
|
||||
AND users.metadata @> jsonb_build_object(
|
||||
'additionalAppointments',
|
||||
jsonb_build_array(jsonb_build_object('tenantSlug', membership_tenants.slug))
|
||||
)
|
||||
)`)
|
||||
args = append(args, tenantIDs)
|
||||
clauses = append(clauses, `EXISTS (
|
||||
SELECT 1
|
||||
FROM tenants AS membership_tenants
|
||||
WHERE membership_tenants.id IN ?
|
||||
AND users.metadata @> jsonb_build_object(
|
||||
'additionalAppointments',
|
||||
jsonb_build_array(jsonb_build_object('tenant_slug', membership_tenants.slug))
|
||||
)
|
||||
)`)
|
||||
args = append(args, tenantIDs)
|
||||
return db.Where("("+strings.Join(clauses, " OR ")+")", args...)
|
||||
}
|
||||
|
||||
@@ -170,11 +190,63 @@ func (r *userRepository) CountByTenantIDs(ctx context.Context, tenantIDs []strin
|
||||
}
|
||||
|
||||
for _, tenantID := range tenantIDs {
|
||||
var count int64
|
||||
if err := r.withTenantMembershipFilter(r.db.WithContext(ctx).Model(&domain.User{}), []string{tenantID}).Count(&count).Error; err != nil {
|
||||
return nil, err
|
||||
tenantID = strings.TrimSpace(tenantID)
|
||||
if tenantID != "" {
|
||||
counts[tenantID] = 0
|
||||
}
|
||||
counts[tenantID] = count
|
||||
}
|
||||
|
||||
type result struct {
|
||||
TenantID string
|
||||
Count int64
|
||||
}
|
||||
var results []result
|
||||
if err := r.db.WithContext(ctx).Raw(`
|
||||
WITH requested_tenants AS (
|
||||
SELECT id, slug
|
||||
FROM tenants
|
||||
WHERE id IN ?
|
||||
),
|
||||
memberships AS (
|
||||
SELECT users.id AS user_id, users.tenant_id::text AS tenant_id
|
||||
FROM users
|
||||
WHERE users.deleted_at IS NULL
|
||||
AND users.tenant_id IN ?
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT users.id AS user_id, appointment ->> 'tenantId' AS tenant_id
|
||||
FROM users
|
||||
CROSS JOIN LATERAL jsonb_array_elements(COALESCE(users.metadata -> 'additionalAppointments', '[]'::jsonb)) AS appointment
|
||||
WHERE users.deleted_at IS NULL
|
||||
AND appointment ->> 'tenantId' IN ?
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT users.id AS user_id, requested_tenants.id::text AS tenant_id
|
||||
FROM users
|
||||
CROSS JOIN LATERAL jsonb_array_elements(COALESCE(users.metadata -> 'additionalAppointments', '[]'::jsonb)) AS appointment
|
||||
JOIN requested_tenants ON LOWER(requested_tenants.slug) = LOWER(appointment ->> 'tenantSlug')
|
||||
WHERE users.deleted_at IS NULL
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT users.id AS user_id, requested_tenants.id::text AS tenant_id
|
||||
FROM users
|
||||
CROSS JOIN LATERAL jsonb_array_elements(COALESCE(users.metadata -> 'additionalAppointments', '[]'::jsonb)) AS appointment
|
||||
JOIN requested_tenants ON LOWER(requested_tenants.slug) = LOWER(appointment ->> 'tenant_slug')
|
||||
WHERE users.deleted_at IS NULL
|
||||
)
|
||||
SELECT tenant_id, COUNT(DISTINCT user_id) AS count
|
||||
FROM memberships
|
||||
WHERE tenant_id IN ?
|
||||
GROUP BY tenant_id
|
||||
`, tenantIDs, tenantIDs, tenantIDs, tenantIDs).Scan(&results).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, result := range results {
|
||||
counts[result.TenantID] = result.Count
|
||||
}
|
||||
return counts, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user