Use coredata models in log export batch loaders
Drop export-only row types; load Identity, PersonalAPIKey, and MembershipProfile through the usual slice LoadByIDs helpers. Signed-off-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: Bryan FRIMIN <bryan@frimin.fr>
This commit is contained in:
@@ -388,17 +388,7 @@ WHERE
|
|||||||
return count, nil
|
return count, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type (
|
func (i *Identities) LoadByIDs(
|
||||||
IdentityAuditLogActorRow struct {
|
|
||||||
ID gid.GID `db:"id"`
|
|
||||||
EmailAddress mail.Addr `db:"email_address"`
|
|
||||||
FullName string `db:"full_name"`
|
|
||||||
}
|
|
||||||
|
|
||||||
IdentityAuditLogActorRows []*IdentityAuditLogActorRow
|
|
||||||
)
|
|
||||||
|
|
||||||
func (rows *IdentityAuditLogActorRows) LoadByIDs(
|
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
conn pg.Querier,
|
conn pg.Querier,
|
||||||
identityIDs []gid.GID,
|
identityIDs []gid.GID,
|
||||||
@@ -407,7 +397,13 @@ func (rows *IdentityAuditLogActorRows) LoadByIDs(
|
|||||||
SELECT
|
SELECT
|
||||||
id,
|
id,
|
||||||
email_address,
|
email_address,
|
||||||
full_name
|
full_name,
|
||||||
|
hashed_password,
|
||||||
|
email_address_verified,
|
||||||
|
saml_subject,
|
||||||
|
locale,
|
||||||
|
created_at,
|
||||||
|
updated_at
|
||||||
FROM
|
FROM
|
||||||
identities
|
identities
|
||||||
WHERE
|
WHERE
|
||||||
@@ -416,17 +412,17 @@ WHERE
|
|||||||
|
|
||||||
args := pgx.StrictNamedArgs{"identity_ids": identityIDs}
|
args := pgx.StrictNamedArgs{"identity_ids": identityIDs}
|
||||||
|
|
||||||
rowsResult, err := conn.Query(ctx, q, args)
|
rows, err := conn.Query(ctx, q, args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cannot query identity audit log actors: %w", err)
|
return fmt.Errorf("cannot query identities: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
actors, err := pgx.CollectRows(rowsResult, pgx.RowToAddrOfStructByName[IdentityAuditLogActorRow])
|
identities, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Identity])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cannot collect identity audit log actors: %w", err)
|
return fmt.Errorf("cannot collect identities: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
*rows = actors
|
*i = identities
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -495,25 +495,7 @@ WHERE
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type (
|
func (p *MembershipProfiles) LoadByOrganizationIDAndUserNames(
|
||||||
MembershipProfileByUserNameRow struct {
|
|
||||||
UserName *string `db:"user_name"`
|
|
||||||
FullName string `db:"full_name"`
|
|
||||||
FormattedName *string `db:"formatted_name"`
|
|
||||||
}
|
|
||||||
|
|
||||||
MembershipProfileByUserNameRows []*MembershipProfileByUserNameRow
|
|
||||||
)
|
|
||||||
|
|
||||||
func (p *MembershipProfileByUserNameRow) DisplayName() string {
|
|
||||||
if p.FormattedName != nil && *p.FormattedName != "" {
|
|
||||||
return *p.FormattedName
|
|
||||||
}
|
|
||||||
|
|
||||||
return p.FullName
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rows *MembershipProfileByUserNameRows) LoadByOrganizationIDAndUserNames(
|
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
conn pg.Querier,
|
conn pg.Querier,
|
||||||
scope Scoper,
|
scope Scoper,
|
||||||
@@ -522,9 +504,40 @@ func (rows *MembershipProfileByUserNameRows) LoadByOrganizationIDAndUserNames(
|
|||||||
) error {
|
) error {
|
||||||
q := `
|
q := `
|
||||||
SELECT
|
SELECT
|
||||||
p.user_name,
|
p.id,
|
||||||
|
p.identity_id,
|
||||||
|
p.organization_id,
|
||||||
|
''::citext AS email_address,
|
||||||
|
p.source,
|
||||||
|
p.state,
|
||||||
p.full_name,
|
p.full_name,
|
||||||
p.formatted_name
|
p.kind,
|
||||||
|
p.additional_email_addresses,
|
||||||
|
p.position,
|
||||||
|
p.contract_start_date,
|
||||||
|
p.contract_end_date,
|
||||||
|
'' AS organization_name,
|
||||||
|
p.user_name,
|
||||||
|
p.external_id,
|
||||||
|
p.nickname,
|
||||||
|
p.locale,
|
||||||
|
p.timezone,
|
||||||
|
p.profile_url,
|
||||||
|
p.preferred_language,
|
||||||
|
p.given_name,
|
||||||
|
p.family_name,
|
||||||
|
p.formatted_name,
|
||||||
|
p.middle_name,
|
||||||
|
p.honorific_prefix,
|
||||||
|
p.honorific_suffix,
|
||||||
|
p.employee_number,
|
||||||
|
p.department,
|
||||||
|
p.cost_center,
|
||||||
|
p.enterprise_organization,
|
||||||
|
p.division,
|
||||||
|
p.manager_value,
|
||||||
|
p.created_at,
|
||||||
|
p.updated_at
|
||||||
FROM
|
FROM
|
||||||
iam_membership_profiles p
|
iam_membership_profiles p
|
||||||
WHERE
|
WHERE
|
||||||
@@ -541,17 +554,17 @@ WHERE
|
|||||||
}
|
}
|
||||||
maps.Copy(args, scope.SQLArguments())
|
maps.Copy(args, scope.SQLArguments())
|
||||||
|
|
||||||
rowsResult, err := conn.Query(ctx, q, args)
|
rows, err := conn.Query(ctx, q, args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cannot query profiles by user names: %w", err)
|
return fmt.Errorf("cannot query profiles by user names: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
profiles, err := pgx.CollectRows(rowsResult, pgx.RowToAddrOfStructByName[MembershipProfileByUserNameRow])
|
profiles, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[MembershipProfile])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cannot collect profiles by user names: %w", err)
|
return fmt.Errorf("cannot collect profiles by user names: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
*rows = profiles
|
*p = profiles
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -302,16 +302,7 @@ WHERE
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type (
|
func (a *PersonalAPIKeys) LoadByIDs(
|
||||||
PersonalAPIKeyAuditLogActorRow struct {
|
|
||||||
ID gid.GID `db:"id"`
|
|
||||||
Name string `db:"name"`
|
|
||||||
}
|
|
||||||
|
|
||||||
PersonalAPIKeyAuditLogActorRows []*PersonalAPIKeyAuditLogActorRow
|
|
||||||
)
|
|
||||||
|
|
||||||
func (rows *PersonalAPIKeyAuditLogActorRows) LoadByIDs(
|
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
conn pg.Querier,
|
conn pg.Querier,
|
||||||
apiKeyIDs []gid.GID,
|
apiKeyIDs []gid.GID,
|
||||||
@@ -319,7 +310,13 @@ func (rows *PersonalAPIKeyAuditLogActorRows) LoadByIDs(
|
|||||||
q := `
|
q := `
|
||||||
SELECT
|
SELECT
|
||||||
id,
|
id,
|
||||||
name
|
identity_id,
|
||||||
|
name,
|
||||||
|
expires_at,
|
||||||
|
expire_reason,
|
||||||
|
last_used_at,
|
||||||
|
created_at,
|
||||||
|
updated_at
|
||||||
FROM
|
FROM
|
||||||
iam_personal_api_keys
|
iam_personal_api_keys
|
||||||
WHERE
|
WHERE
|
||||||
@@ -328,17 +325,17 @@ WHERE
|
|||||||
|
|
||||||
args := pgx.StrictNamedArgs{"api_key_ids": apiKeyIDs}
|
args := pgx.StrictNamedArgs{"api_key_ids": apiKeyIDs}
|
||||||
|
|
||||||
rowsResult, err := conn.Query(ctx, q, args)
|
rows, err := conn.Query(ctx, q, args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cannot query personal api key audit log actors: %w", err)
|
return fmt.Errorf("cannot query personal api keys: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
actors, err := pgx.CollectRows(rowsResult, pgx.RowToAddrOfStructByName[PersonalAPIKeyAuditLogActorRow])
|
apiKeys, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[PersonalAPIKey])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cannot collect personal api key audit log actors: %w", err)
|
return fmt.Errorf("cannot collect personal api keys: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
*rows = actors
|
*a = apiKeys
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -270,7 +270,7 @@ func loadAuditLogActorExportInfo(
|
|||||||
|
|
||||||
result := make(map[gid.GID]auditLogActorExportInfo)
|
result := make(map[gid.GID]auditLogActorExportInfo)
|
||||||
|
|
||||||
var identities coredata.IdentityAuditLogActorRows
|
var identities coredata.Identities
|
||||||
if err := identities.LoadByIDs(ctx, conn, identityIDs); err != nil {
|
if err := identities.LoadByIDs(ctx, conn, identityIDs); err != nil {
|
||||||
return nil, fmt.Errorf("cannot load audit log actor identities: %w", err)
|
return nil, fmt.Errorf("cannot load audit log actor identities: %w", err)
|
||||||
}
|
}
|
||||||
@@ -282,7 +282,7 @@ func loadAuditLogActorExportInfo(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var apiKeys coredata.PersonalAPIKeyAuditLogActorRows
|
var apiKeys coredata.PersonalAPIKeys
|
||||||
if err := apiKeys.LoadByIDs(ctx, conn, apiKeyIDs); err != nil {
|
if err := apiKeys.LoadByIDs(ctx, conn, apiKeyIDs); err != nil {
|
||||||
return nil, fmt.Errorf("cannot load audit log actor API keys: %w", err)
|
return nil, fmt.Errorf("cannot load audit log actor API keys: %w", err)
|
||||||
}
|
}
|
||||||
@@ -308,7 +308,7 @@ func loadSCIMProfileExportInfo(
|
|||||||
return map[string]scimProfileExportInfo{}, nil
|
return map[string]scimProfileExportInfo{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var profiles coredata.MembershipProfileByUserNameRows
|
var profiles coredata.MembershipProfiles
|
||||||
if err := profiles.LoadByOrganizationIDAndUserNames(
|
if err := profiles.LoadByOrganizationIDAndUserNames(
|
||||||
ctx,
|
ctx,
|
||||||
conn,
|
conn,
|
||||||
@@ -328,7 +328,7 @@ func loadSCIMProfileExportInfo(
|
|||||||
key := strings.ToLower(*profile.UserName)
|
key := strings.ToLower(*profile.UserName)
|
||||||
result[key] = scimProfileExportInfo{
|
result[key] = scimProfileExportInfo{
|
||||||
email: scimEmailFromUserName(*profile.UserName),
|
email: scimEmailFromUserName(*profile.UserName),
|
||||||
fullName: profile.DisplayName(),
|
fullName: profileFullName(profile),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -379,6 +379,14 @@ func scimEmailFromUserName(userName string) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func profileFullName(profile *coredata.MembershipProfile) string {
|
||||||
|
if profile.FormattedName != nil && *profile.FormattedName != "" {
|
||||||
|
return *profile.FormattedName
|
||||||
|
}
|
||||||
|
|
||||||
|
return profile.FullName
|
||||||
|
}
|
||||||
|
|
||||||
func stringPtrValue(value *string) string {
|
func stringPtrValue(value *string) string {
|
||||||
if value == nil {
|
if value == nil {
|
||||||
return ""
|
return ""
|
||||||
|
|||||||
Reference in New Issue
Block a user