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 {
|
||||
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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user