Add organization_id on profiles and implement ListProfiles
Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
@@ -32,6 +32,7 @@ type (
|
|||||||
MembershipProfile struct {
|
MembershipProfile struct {
|
||||||
ID gid.GID `db:"id"`
|
ID gid.GID `db:"id"`
|
||||||
IdentityID gid.GID `db:"identity_id"`
|
IdentityID gid.GID `db:"identity_id"`
|
||||||
|
OrganizationID gid.GID `db:"organization_id"`
|
||||||
MembershipID gid.GID `db:"membership_id"`
|
MembershipID gid.GID `db:"membership_id"`
|
||||||
EmailAddress mail.Addr `db:"email_address"`
|
EmailAddress mail.Addr `db:"email_address"`
|
||||||
FullName string `db:"full_name"`
|
FullName string `db:"full_name"`
|
||||||
@@ -84,6 +85,7 @@ func (p *MembershipProfile) LoadByMembershipID(
|
|||||||
SELECT
|
SELECT
|
||||||
p.id,
|
p.id,
|
||||||
p.identity_id,
|
p.identity_id,
|
||||||
|
p.organization_id,
|
||||||
p.membership_id,
|
p.membership_id,
|
||||||
i.email_address,
|
i.email_address,
|
||||||
p.full_name,
|
p.full_name,
|
||||||
@@ -138,6 +140,7 @@ func (p *MembershipProfile) LoadByID(
|
|||||||
SELECT
|
SELECT
|
||||||
p.id,
|
p.id,
|
||||||
p.identity_id,
|
p.identity_id,
|
||||||
|
p.organization_id,
|
||||||
p.membership_id,
|
p.membership_id,
|
||||||
i.email_address,
|
i.email_address,
|
||||||
p.full_name,
|
p.full_name,
|
||||||
@@ -192,6 +195,7 @@ func (p *MembershipProfiles) LoadByIDs(
|
|||||||
SELECT
|
SELECT
|
||||||
p.id,
|
p.id,
|
||||||
p.identity_id,
|
p.identity_id,
|
||||||
|
p.organization_id,
|
||||||
p.membership_id,
|
p.membership_id,
|
||||||
i.email_address,
|
i.email_address,
|
||||||
p.full_name,
|
p.full_name,
|
||||||
@@ -231,6 +235,75 @@ WHERE
|
|||||||
return nil
|
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(
|
func (p *MembershipProfiles) LoadByMeetingID(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
conn pg.Conn,
|
conn pg.Conn,
|
||||||
@@ -243,6 +316,7 @@ WITH attendees AS (
|
|||||||
p.id,
|
p.id,
|
||||||
p.tenant_id,
|
p.tenant_id,
|
||||||
p.identity_id,
|
p.identity_id,
|
||||||
|
p.organization_id,
|
||||||
p.membership_id,
|
p.membership_id,
|
||||||
i.email_address,
|
i.email_address,
|
||||||
p.full_name,
|
p.full_name,
|
||||||
@@ -266,6 +340,7 @@ WITH attendees AS (
|
|||||||
SELECT
|
SELECT
|
||||||
id,
|
id,
|
||||||
identity_id,
|
identity_id,
|
||||||
|
organization_id,
|
||||||
membership_id,
|
membership_id,
|
||||||
kind,
|
kind,
|
||||||
email_address,
|
email_address,
|
||||||
@@ -324,6 +399,7 @@ WITH signatories AS (
|
|||||||
SELECT
|
SELECT
|
||||||
p.id,
|
p.id,
|
||||||
p.identity_id,
|
p.identity_id,
|
||||||
|
p.organization_id,
|
||||||
p.membership_id,
|
p.membership_id,
|
||||||
p.kind,
|
p.kind,
|
||||||
p.full_name,
|
p.full_name,
|
||||||
@@ -368,6 +444,7 @@ INSERT INTO
|
|||||||
tenant_id,
|
tenant_id,
|
||||||
id,
|
id,
|
||||||
identity_id,
|
identity_id,
|
||||||
|
organization_id,
|
||||||
membership_id,
|
membership_id,
|
||||||
full_name,
|
full_name,
|
||||||
kind,
|
kind,
|
||||||
@@ -382,6 +459,7 @@ VALUES (
|
|||||||
@tenant_id,
|
@tenant_id,
|
||||||
@id,
|
@id,
|
||||||
@identity_id,
|
@identity_id,
|
||||||
|
@organization_id,
|
||||||
@membership_id,
|
@membership_id,
|
||||||
@full_name,
|
@full_name,
|
||||||
@kind,
|
@kind,
|
||||||
@@ -398,6 +476,7 @@ VALUES (
|
|||||||
"tenant_id": p.ID.TenantID().String(),
|
"tenant_id": p.ID.TenantID().String(),
|
||||||
"id": p.ID,
|
"id": p.ID,
|
||||||
"identity_id": p.IdentityID,
|
"identity_id": p.IdentityID,
|
||||||
|
"organization_id": p.OrganizationID,
|
||||||
"membership_id": p.MembershipID,
|
"membership_id": p.MembershipID,
|
||||||
"full_name": p.FullName,
|
"full_name": p.FullName,
|
||||||
"kind": p.Kind,
|
"kind": p.Kind,
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ ALTER TABLE
|
|||||||
iam_membership_profiles
|
iam_membership_profiles
|
||||||
ADD
|
ADD
|
||||||
COLUMN identity_id TEXT REFERENCES identities(id),
|
COLUMN identity_id TEXT REFERENCES identities(id),
|
||||||
|
ADD
|
||||||
|
COLUMN organization_id TEXT REFERENCES organizations(id),
|
||||||
ADD
|
ADD
|
||||||
COLUMN additional_email_addresses CITEXT [],
|
COLUMN additional_email_addresses CITEXT [],
|
||||||
ADD
|
ADD
|
||||||
@@ -17,7 +19,8 @@ ADD
|
|||||||
UPDATE
|
UPDATE
|
||||||
iam_membership_profiles mp
|
iam_membership_profiles mp
|
||||||
SET
|
SET
|
||||||
identity_id = m.identity_id
|
identity_id = m.identity_id,
|
||||||
|
organization_id = m.organization_id
|
||||||
FROM
|
FROM
|
||||||
iam_memberships m
|
iam_memberships m
|
||||||
WHERE
|
WHERE
|
||||||
@@ -27,9 +30,15 @@ ALTER TABLE
|
|||||||
iam_membership_profiles
|
iam_membership_profiles
|
||||||
ALTER COLUMN
|
ALTER COLUMN
|
||||||
identity_id
|
identity_id
|
||||||
|
SET
|
||||||
|
NOT NULL,
|
||||||
|
ALTER COLUMN
|
||||||
|
organization_id
|
||||||
SET
|
SET
|
||||||
NOT NULL;
|
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
|
-- 2. Add profile references to all table referencing peoples
|
||||||
-- was owner_id, NOT NULL
|
-- was owner_id, NOT NULL
|
||||||
ALTER TABLE
|
ALTER TABLE
|
||||||
@@ -193,6 +202,7 @@ FROM
|
|||||||
WITH people_memberships AS (
|
WITH people_memberships AS (
|
||||||
SELECT
|
SELECT
|
||||||
i.id AS identity_id,
|
i.id AS identity_id,
|
||||||
|
m.organization_id AS organization_id,
|
||||||
m.id AS membership_id,
|
m.id AS membership_id,
|
||||||
p.tenant_id AS tenant_id,
|
p.tenant_id AS tenant_id,
|
||||||
p.kind AS kind,
|
p.kind AS kind,
|
||||||
@@ -213,6 +223,7 @@ INSERT INTO
|
|||||||
id,
|
id,
|
||||||
tenant_id,
|
tenant_id,
|
||||||
identity_id,
|
identity_id,
|
||||||
|
organization_id,
|
||||||
membership_id,
|
membership_id,
|
||||||
full_name,
|
full_name,
|
||||||
kind,
|
kind,
|
||||||
@@ -227,6 +238,7 @@ SELECT
|
|||||||
generate_gid(decode_base64_unpadded(pm.tenant_id), 51),
|
generate_gid(decode_base64_unpadded(pm.tenant_id), 51),
|
||||||
pm.tenant_id,
|
pm.tenant_id,
|
||||||
pm.identity_id,
|
pm.identity_id,
|
||||||
|
pm.organization_id,
|
||||||
pm.membership_id,
|
pm.membership_id,
|
||||||
pm.full_name,
|
pm.full_name,
|
||||||
pm.kind,
|
pm.kind,
|
||||||
|
|||||||
@@ -268,6 +268,7 @@ func (s *AccountService) AcceptInvitation(
|
|||||||
profile := &coredata.MembershipProfile{
|
profile := &coredata.MembershipProfile{
|
||||||
ID: gid.New(tenantID, coredata.MembershipProfileEntityType),
|
ID: gid.New(tenantID, coredata.MembershipProfileEntityType),
|
||||||
IdentityID: identity.ID,
|
IdentityID: identity.ID,
|
||||||
|
OrganizationID: invitation.OrganizationID,
|
||||||
MembershipID: membership.ID,
|
MembershipID: membership.ID,
|
||||||
FullName: identity.FullName,
|
FullName: identity.FullName,
|
||||||
CreatedAt: now,
|
CreatedAt: now,
|
||||||
|
|||||||
@@ -668,6 +668,7 @@ func (s *OrganizationService) CreateOrganization(
|
|||||||
profile := &coredata.MembershipProfile{
|
profile := &coredata.MembershipProfile{
|
||||||
ID: gid.New(tenantID, coredata.MembershipProfileEntityType),
|
ID: gid.New(tenantID, coredata.MembershipProfileEntityType),
|
||||||
IdentityID: identity.ID,
|
IdentityID: identity.ID,
|
||||||
|
OrganizationID: organization.ID,
|
||||||
MembershipID: membership.ID,
|
MembershipID: membership.ID,
|
||||||
FullName: identity.FullName,
|
FullName: identity.FullName,
|
||||||
CreatedAt: now,
|
CreatedAt: now,
|
||||||
@@ -962,6 +963,31 @@ func (s *OrganizationService) GetProfile(ctx context.Context, profileID gid.GID)
|
|||||||
return profile, nil
|
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) {
|
func (s *OrganizationService) GetOrganizationForMembership(ctx context.Context, membershipID gid.GID) (*coredata.Organization, error) {
|
||||||
var (
|
var (
|
||||||
scope = coredata.NewScopeFromObjectID(membershipID)
|
scope = coredata.NewScopeFromObjectID(membershipID)
|
||||||
|
|||||||
@@ -319,6 +319,7 @@ func (s *Service) HandleAssertion(
|
|||||||
membershipProfile := &coredata.MembershipProfile{
|
membershipProfile := &coredata.MembershipProfile{
|
||||||
ID: gid.New(membership.ID.TenantID(), coredata.MembershipProfileEntityType),
|
ID: gid.New(membership.ID.TenantID(), coredata.MembershipProfileEntityType),
|
||||||
IdentityID: identity.ID,
|
IdentityID: identity.ID,
|
||||||
|
OrganizationID: config.OrganizationID,
|
||||||
MembershipID: membership.ID,
|
MembershipID: membership.ID,
|
||||||
FullName: fullname,
|
FullName: fullname,
|
||||||
CreatedAt: now,
|
CreatedAt: now,
|
||||||
|
|||||||
@@ -198,6 +198,7 @@ func (s *Service) CreateUser(
|
|||||||
membershipProfile := &coredata.MembershipProfile{
|
membershipProfile := &coredata.MembershipProfile{
|
||||||
ID: gid.New(membership.ID.TenantID(), coredata.MembershipProfileEntityType),
|
ID: gid.New(membership.ID.TenantID(), coredata.MembershipProfileEntityType),
|
||||||
IdentityID: identity.ID,
|
IdentityID: identity.ID,
|
||||||
|
OrganizationID: config.OrganizationID,
|
||||||
MembershipID: membership.ID,
|
MembershipID: membership.ID,
|
||||||
FullName: fullName,
|
FullName: fullName,
|
||||||
CreatedAt: now,
|
CreatedAt: now,
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import (
|
|||||||
"go.probo.inc/probo/pkg/server/api/console/v1/schema"
|
"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/api/console/v1/types"
|
||||||
"go.probo.inc/probo/pkg/server/gqlutils"
|
"go.probo.inc/probo/pkg/server/gqlutils"
|
||||||
|
"go.probo.inc/probo/pkg/server/gqlutils/types/cursor"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StateOfApplicability is the resolver for the stateOfApplicability field.
|
// 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.
|
// 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) {
|
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.
|
// SlackConnections is the resolver for the slackConnections field.
|
||||||
|
|||||||
Reference in New Issue
Block a user