Add organization_id on profiles and implement ListProfiles

Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
Émile Ré
2026-02-06 15:47:55 +04:00
parent d20b093158
commit a3b49db43e
7 changed files with 177 additions and 26 deletions

View File

@@ -32,6 +32,7 @@ type (
MembershipProfile struct {
ID gid.GID `db:"id"`
IdentityID gid.GID `db:"identity_id"`
OrganizationID gid.GID `db:"organization_id"`
MembershipID gid.GID `db:"membership_id"`
EmailAddress mail.Addr `db:"email_address"`
FullName string `db:"full_name"`
@@ -84,6 +85,7 @@ func (p *MembershipProfile) LoadByMembershipID(
SELECT
p.id,
p.identity_id,
p.organization_id,
p.membership_id,
i.email_address,
p.full_name,
@@ -138,6 +140,7 @@ func (p *MembershipProfile) LoadByID(
SELECT
p.id,
p.identity_id,
p.organization_id,
p.membership_id,
i.email_address,
p.full_name,
@@ -192,6 +195,7 @@ func (p *MembershipProfiles) LoadByIDs(
SELECT
p.id,
p.identity_id,
p.organization_id,
p.membership_id,
i.email_address,
p.full_name,
@@ -231,6 +235,75 @@ WHERE
return nil
}
func (p *MembershipProfiles) LoadByOrganizationID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
organizationID gid.GID,
cursor *page.Cursor[MembershipProfileOrderField],
filter *MembershipProfileFilter,
) error {
q := `
WITH profiles AS (
SELECT
id,
identity_id,
organization_id,
membership_id,
full_name,
kind,
additional_email_addresses,
position,
contract_start_date,
contract_end_date,
created_at,
updated_at
FROM
iam_membership_profiles
WHERE
%s
AND organization_id = @organization_id
AND %s
AND %s
)
SELECT
p.id,
p.identity_id,
p.organization_id,
p.membership_id,
i.email_address,
p.full_name,
p.kind,
p.additional_email_addresses,
p.position,
p.contract_start_date,
p.contract_end_date,
p.created_at,
p.updated_at
FROM profiles p
INNER JOIN identities i ON i.id = p.identity_id
`
q = fmt.Sprintf(q, scope.SQLFragment(), filter.SQLFragment(), cursor.SQLFragment())
args := pgx.NamedArgs{"organization_id": organizationID}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query profiles: %w", err)
}
profiles, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[MembershipProfile])
if err != nil {
return fmt.Errorf("cannot collect profiles: %w", err)
}
*p = profiles
return nil
}
func (p *MembershipProfiles) LoadByMeetingID(
ctx context.Context,
conn pg.Conn,
@@ -243,6 +316,7 @@ WITH attendees AS (
p.id,
p.tenant_id,
p.identity_id,
p.organization_id,
p.membership_id,
i.email_address,
p.full_name,
@@ -266,6 +340,7 @@ WITH attendees AS (
SELECT
id,
identity_id,
organization_id,
membership_id,
kind,
email_address,
@@ -324,6 +399,7 @@ WITH signatories AS (
SELECT
p.id,
p.identity_id,
p.organization_id,
p.membership_id,
p.kind,
p.full_name,
@@ -368,6 +444,7 @@ INSERT INTO
tenant_id,
id,
identity_id,
organization_id,
membership_id,
full_name,
kind,
@@ -382,6 +459,7 @@ VALUES (
@tenant_id,
@id,
@identity_id,
@organization_id,
@membership_id,
@full_name,
@kind,
@@ -398,6 +476,7 @@ VALUES (
"tenant_id": p.ID.TenantID().String(),
"id": p.ID,
"identity_id": p.IdentityID,
"organization_id": p.OrganizationID,
"membership_id": p.MembershipID,
"full_name": p.FullName,
"kind": p.Kind,

View File

@@ -3,6 +3,8 @@ ALTER TABLE
iam_membership_profiles
ADD
COLUMN identity_id TEXT REFERENCES identities(id),
ADD
COLUMN organization_id TEXT REFERENCES organizations(id),
ADD
COLUMN additional_email_addresses CITEXT [],
ADD
@@ -17,7 +19,8 @@ ADD
UPDATE
iam_membership_profiles mp
SET
identity_id = m.identity_id
identity_id = m.identity_id,
organization_id = m.organization_id
FROM
iam_memberships m
WHERE
@@ -27,9 +30,15 @@ ALTER TABLE
iam_membership_profiles
ALTER COLUMN
identity_id
SET
NOT NULL,
ALTER COLUMN
organization_id
SET
NOT NULL;
CREATE UNIQUE INDEX idx_profiles_identity_id_organization_id ON iam_membership_profiles(identity_id, organization_id);
-- 2. Add profile references to all table referencing peoples
-- was owner_id, NOT NULL
ALTER TABLE
@@ -193,6 +202,7 @@ FROM
WITH people_memberships AS (
SELECT
i.id AS identity_id,
m.organization_id AS organization_id,
m.id AS membership_id,
p.tenant_id AS tenant_id,
p.kind AS kind,
@@ -213,6 +223,7 @@ INSERT INTO
id,
tenant_id,
identity_id,
organization_id,
membership_id,
full_name,
kind,
@@ -227,6 +238,7 @@ SELECT
generate_gid(decode_base64_unpadded(pm.tenant_id), 51),
pm.tenant_id,
pm.identity_id,
pm.organization_id,
pm.membership_id,
pm.full_name,
pm.kind,

View File

@@ -266,12 +266,13 @@ func (s *AccountService) AcceptInvitation(
}
profile := &coredata.MembershipProfile{
ID: gid.New(tenantID, coredata.MembershipProfileEntityType),
IdentityID: identity.ID,
MembershipID: membership.ID,
FullName: identity.FullName,
CreatedAt: now,
UpdatedAt: now,
ID: gid.New(tenantID, coredata.MembershipProfileEntityType),
IdentityID: identity.ID,
OrganizationID: invitation.OrganizationID,
MembershipID: membership.ID,
FullName: identity.FullName,
CreatedAt: now,
UpdatedAt: now,
}
if err := profile.Insert(ctx, tx); err != nil {

View File

@@ -666,12 +666,13 @@ func (s *OrganizationService) CreateOrganization(
}
profile := &coredata.MembershipProfile{
ID: gid.New(tenantID, coredata.MembershipProfileEntityType),
IdentityID: identity.ID,
MembershipID: membership.ID,
FullName: identity.FullName,
CreatedAt: now,
UpdatedAt: now,
ID: gid.New(tenantID, coredata.MembershipProfileEntityType),
IdentityID: identity.ID,
OrganizationID: organization.ID,
MembershipID: membership.ID,
FullName: identity.FullName,
CreatedAt: now,
UpdatedAt: now,
}
err = profile.Insert(ctx, tx)
@@ -962,6 +963,31 @@ func (s *OrganizationService) GetProfile(ctx context.Context, profileID gid.GID)
return profile, nil
}
func (s *OrganizationService) ListProfiles(
ctx context.Context,
organizationID gid.GID,
cursor *page.Cursor[coredata.MembershipProfileOrderField],
filter *coredata.MembershipProfileFilter,
) (*page.Page[*coredata.MembershipProfile, coredata.MembershipProfileOrderField], error) {
var (
scope = coredata.NewScopeFromObjectID(organizationID)
profiles = coredata.MembershipProfiles{}
)
err := s.pg.WithConn(
ctx,
func(conn pg.Conn) error {
return profiles.LoadByOrganizationID(ctx, conn, scope, organizationID, cursor, filter)
},
)
if err != nil {
return nil, err
}
return page.NewPage(profiles, cursor), nil
}
func (s *OrganizationService) GetOrganizationForMembership(ctx context.Context, membershipID gid.GID) (*coredata.Organization, error) {
var (
scope = coredata.NewScopeFromObjectID(membershipID)

View File

@@ -317,12 +317,13 @@ func (s *Service) HandleAssertion(
}
membershipProfile := &coredata.MembershipProfile{
ID: gid.New(membership.ID.TenantID(), coredata.MembershipProfileEntityType),
IdentityID: identity.ID,
MembershipID: membership.ID,
FullName: fullname,
CreatedAt: now,
UpdatedAt: now,
ID: gid.New(membership.ID.TenantID(), coredata.MembershipProfileEntityType),
IdentityID: identity.ID,
OrganizationID: config.OrganizationID,
MembershipID: membership.ID,
FullName: fullname,
CreatedAt: now,
UpdatedAt: now,
}
err = membershipProfile.Insert(ctx, tx)

View File

@@ -196,12 +196,13 @@ func (s *Service) CreateUser(
// Create membership profile
membershipProfile := &coredata.MembershipProfile{
ID: gid.New(membership.ID.TenantID(), coredata.MembershipProfileEntityType),
IdentityID: identity.ID,
MembershipID: membership.ID,
FullName: fullName,
CreatedAt: now,
UpdatedAt: now,
ID: gid.New(membership.ID.TenantID(), coredata.MembershipProfileEntityType),
IdentityID: identity.ID,
OrganizationID: config.OrganizationID,
MembershipID: membership.ID,
FullName: fullName,
CreatedAt: now,
UpdatedAt: now,
}
err = membershipProfile.Insert(ctx, tx)

View File

@@ -24,6 +24,7 @@ import (
"go.probo.inc/probo/pkg/server/api/console/v1/schema"
"go.probo.inc/probo/pkg/server/api/console/v1/types"
"go.probo.inc/probo/pkg/server/gqlutils"
"go.probo.inc/probo/pkg/server/gqlutils/types/cursor"
)
// StateOfApplicability is the resolver for the stateOfApplicability field.
@@ -5508,7 +5509,37 @@ func (r *organizationResolver) Context(ctx context.Context, obj *types.Organizat
// Profiles is the resolver for the profiles field.
func (r *organizationResolver) Profiles(ctx context.Context, obj *types.Organization, first *int, after *page.CursorKey, last *int, before *page.CursorKey, orderBy *types.ProfileOrderBy, filter *types.ProfileFilter) (*types.ProfileConnection, error) {
panic(fmt.Errorf("not implemented: Profiles - profiles"))
if err := r.authorize(ctx, obj.ID, iam.ActionMembershipProfileList); err != nil {
return nil, err
}
if gqlutils.OnlyTotalCountSelected(ctx) {
return &types.ProfileConnection{
Resolver: r,
ParentID: obj.ID,
}, nil
}
filters := coredata.NewMembershipProfileFilter(filter.ExcludeContractEnded)
pageOrderBy := page.OrderBy[coredata.MembershipProfileOrderField]{
Field: coredata.MembershipProfileOrderFieldCreatedAt,
Direction: page.OrderDirectionDesc,
}
if orderBy != nil {
pageOrderBy.Field = coredata.MembershipProfileOrderField(orderBy.Field)
pageOrderBy.Direction = page.OrderDirection(orderBy.Direction)
}
cursor := cursor.NewCursor(first, after, last, before, pageOrderBy)
page, err := r.iam.OrganizationService.ListProfiles(ctx, obj.ID, cursor, filters)
if err != nil {
r.logger.ErrorCtx(ctx, "cannot list profiles", log.Error(err))
return nil, gqlutils.Internal(ctx)
}
return types.NewProfileConnection(page, r, obj.ID, filters), nil
}
// SlackConnections is the resolver for the slackConnections field.