diff --git a/pkg/coredata/membership_profile.go b/pkg/coredata/membership_profile.go index 6f4cdbe9e..3be047356 100644 --- a/pkg/coredata/membership_profile.go +++ b/pkg/coredata/membership_profile.go @@ -498,6 +498,41 @@ INNER JOIN signatories ON p.id = signatories.signed_by_profile_id return nil } +func (p *MembershipProfiles) CountByOrganizationID( + ctx context.Context, + conn pg.Conn, + scope Scoper, + organizationID gid.GID, +) (int, error) { + q := ` +SELECT + COUNT(*) +FROM + iam_membership_profiles +WHERE + %s + AND organization_id = @organization_id +` + + q = fmt.Sprintf(q, scope.SQLFragment()) + + args := pgx.StrictNamedArgs{"organization_id": organizationID} + maps.Copy(args, scope.SQLArguments()) + + rows, err := conn.Query(ctx, q, args) + if err != nil { + return 0, fmt.Errorf("cannot query iam_membership_profiles: %w", err) + } + + var count int + err = rows.Scan(&count) + if err != nil { + return 0, fmt.Errorf("cannot collect count: %w", err) + } + + return count, nil +} + func (p *MembershipProfile) Insert( ctx context.Context, conn pg.Conn, diff --git a/pkg/iam/organization_service.go b/pkg/iam/organization_service.go index e6f068466..e5ef70c6a 100644 --- a/pkg/iam/organization_service.go +++ b/pkg/iam/organization_service.go @@ -1069,6 +1069,31 @@ func (s *OrganizationService) ListProfiles( return page.NewPage(profiles, cursor), nil } +func (s OrganizationService) CountProfiles( + ctx context.Context, + organizationID gid.GID, +) (int, error) { + var ( + scope = coredata.NewScopeFromObjectID(organizationID) + count int + ) + + err := s.pg.WithConn( + ctx, + func(conn pg.Conn) (err error) { + profiles := coredata.MembershipProfiles{} + count, err = profiles.CountByOrganizationID(ctx, conn, scope, organizationID) + if err != nil { + return fmt.Errorf("cannot count profiles: %w", err) + } + + return nil + }, + ) + + return count, err +} + func (s *OrganizationService) GetOrganizationForMembership(ctx context.Context, membershipID gid.GID) (*coredata.Organization, error) { var ( scope = coredata.NewScopeFromObjectID(membershipID) diff --git a/pkg/server/api/console/v1/v1_resolver.go b/pkg/server/api/console/v1/v1_resolver.go index a1cec88d3..f94bd2936 100644 --- a/pkg/server/api/console/v1/v1_resolver.go +++ b/pkg/server/api/console/v1/v1_resolver.go @@ -5520,7 +5520,10 @@ func (r *organizationResolver) Profiles(ctx context.Context, obj *types.Organiza }, nil } - filters := coredata.NewMembershipProfileFilter(filter.ExcludeContractEnded) + filters := coredata.NewMembershipProfileFilter(nil) + if filter != nil { + filters = coredata.NewMembershipProfileFilter(filter.ExcludeContractEnded) + } pageOrderBy := page.OrderBy[coredata.MembershipProfileOrderField]{ Field: coredata.MembershipProfileOrderFieldCreatedAt, @@ -6456,12 +6459,25 @@ func (r *processingActivityConnectionResolver) TotalCount(ctx context.Context, o // Permission is the resolver for the permission field. func (r *profileResolver) Permission(ctx context.Context, obj *types.Profile, action string) (bool, error) { - panic(fmt.Errorf("not implemented: Permission - permission")) + return r.Resolver.Permission(ctx, obj, action) } // TotalCount is the resolver for the totalCount field. func (r *profileConnectionResolver) TotalCount(ctx context.Context, obj *types.ProfileConnection) (int, error) { - panic(fmt.Errorf("not implemented: TotalCount - totalCount")) + if err := r.authorize(ctx, obj.ParentID, iam.ActionMembershipProfileList); err != nil { + return 0, err + } + + switch obj.Resolver.(type) { + case *stateOfApplicabilityResolver: + count, err := r.iam.OrganizationService.CountProfiles(ctx, obj.ParentID) + if err != nil { + panic(fmt.Errorf("cannot count profiles: %w", err)) + } + return count, nil + } + + panic(fmt.Errorf("not implemented: TotalCount for parent type %T", obj.Resolver)) } // Node is the resolver for the node field.