Fix style of invitation and membership files

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Sacha Al Himdani
2025-10-14 09:55:01 +02:00
parent 4a24219500
commit feeaad777c
2 changed files with 217 additions and 146 deletions

View File

@@ -51,12 +51,12 @@ type (
} }
ErrInvitationNotFound struct { ErrInvitationNotFound struct {
Token string ID string
} }
) )
func (e ErrInvitationNotFound) Error() string { func (e ErrInvitationNotFound) Error() string {
return fmt.Sprintf("invitation not found: %s", e.Token) return fmt.Sprintf("invitation not found: %s", e.ID)
} }
func (i Invitation) CursorKey(orderBy InvitationOrderField) page.CursorKey { func (i Invitation) CursorKey(orderBy InvitationOrderField) page.CursorKey {
@@ -84,11 +84,27 @@ func (i Invitation) CursorKey(orderBy InvitationOrderField) page.CursorKey {
func (i *Invitation) Create(ctx context.Context, conn pg.Conn, scope Scoper) error { func (i *Invitation) Create(ctx context.Context, conn pg.Conn, scope Scoper) error {
query := ` query := `
INSERT INTO authz_invitations ( INSERT INTO
tenant_id, id, organization_id, email, full_name, role, expires_at, created_at authz_invitations (
) VALUES ( tenant_id,
@tenant_id, @id, @organization_id, @email, @full_name, @role, @expires_at, @created_at id,
organization_id,
email,
full_name,
role,
expires_at,
created_at
) )
VALUES (
@tenant_id,
@id,
@organization_id,
@email,
@full_name,
@role,
@expires_at,
@created_at
);
` `
args := pgx.StrictNamedArgs{ args := pgx.StrictNamedArgs{
@@ -104,7 +120,7 @@ func (i *Invitation) Create(ctx context.Context, conn pg.Conn, scope Scoper) err
_, err := conn.Exec(ctx, query, args) _, err := conn.Exec(ctx, query, args)
if err != nil { if err != nil {
return fmt.Errorf("failed to create invitation: %w", err) return fmt.Errorf("cannot create invitation: %w", err)
} }
return nil return nil
@@ -131,8 +147,11 @@ func (i *Invitation) LoadByID(
expires_at, expires_at,
accepted_at, accepted_at,
created_at created_at
FROM authz_invitations FROM
WHERE id = @id AND %s authz_invitations
WHERE
id = @id
AND %s
` `
query = fmt.Sprintf(query, scope.SQLFragment()) query = fmt.Sprintf(query, scope.SQLFragment())
@@ -150,7 +169,7 @@ func (i *Invitation) LoadByID(
invitation, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Invitation]) invitation, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Invitation])
if err != nil { if err != nil {
if errors.Is(err, pgx.ErrNoRows) { if errors.Is(err, pgx.ErrNoRows) {
return ErrInvitationNotFound{Token: id.String()} return ErrInvitationNotFound{ID: id.String()}
} }
return fmt.Errorf("cannot collect invitation: %w", err) return fmt.Errorf("cannot collect invitation: %w", err)
} }
@@ -161,9 +180,13 @@ func (i *Invitation) LoadByID(
func (i *Invitation) Update(ctx context.Context, conn pg.Conn, scope Scoper) error { func (i *Invitation) Update(ctx context.Context, conn pg.Conn, scope Scoper) error {
query := ` query := `
UPDATE authz_invitations UPDATE
SET accepted_at = @accepted_at authz_invitations
WHERE id = @id AND %s SET
accepted_at = @accepted_at
WHERE
id = @id
AND %s
` `
query = fmt.Sprintf(query, scope.SQLFragment()) query = fmt.Sprintf(query, scope.SQLFragment())
@@ -176,11 +199,11 @@ func (i *Invitation) Update(ctx context.Context, conn pg.Conn, scope Scoper) err
result, err := conn.Exec(ctx, query, args) result, err := conn.Exec(ctx, query, args)
if err != nil { if err != nil {
return fmt.Errorf("failed to update invitation: %w", err) return fmt.Errorf("cannot update invitation: %w", err)
} }
if result.RowsAffected() == 0 { if result.RowsAffected() == 0 {
return ErrInvitationNotFound{Token: i.ID.String()} return ErrInvitationNotFound{ID: i.ID.String()}
} }
return nil return nil
@@ -188,8 +211,11 @@ func (i *Invitation) Update(ctx context.Context, conn pg.Conn, scope Scoper) err
func (i *Invitation) Delete(ctx context.Context, conn pg.Conn, scope Scoper) error { func (i *Invitation) Delete(ctx context.Context, conn pg.Conn, scope Scoper) error {
query := ` query := `
DELETE FROM authz_invitations DELETE FROM
WHERE id = @id AND %s authz_invitations
WHERE
id = @id
AND %s
` `
query = fmt.Sprintf(query, scope.SQLFragment()) query = fmt.Sprintf(query, scope.SQLFragment())
@@ -201,11 +227,11 @@ func (i *Invitation) Delete(ctx context.Context, conn pg.Conn, scope Scoper) err
result, err := conn.Exec(ctx, query, args) result, err := conn.Exec(ctx, query, args)
if err != nil { if err != nil {
return fmt.Errorf("failed to delete invitation: %w", err) return fmt.Errorf("cannot delete invitation: %w", err)
} }
if result.RowsAffected() == 0 { if result.RowsAffected() == 0 {
return ErrInvitationNotFound{Token: i.ID.String()} return ErrInvitationNotFound{ID: i.ID.String()}
} }
return nil return nil
@@ -235,8 +261,10 @@ func (i *Invitations) LoadByEmail(
expires_at, expires_at,
accepted_at, accepted_at,
created_at created_at
FROM authz_invitations FROM
WHERE email = @email authz_invitations
WHERE
email = @email
AND %s AND %s
AND %s AND %s
` `
@@ -285,8 +313,11 @@ func (i *Invitations) LoadByOrganizationID(
expires_at, expires_at,
accepted_at, accepted_at,
created_at created_at
FROM authz_invitations FROM
WHERE organization_id = @organization_id AND %s authz_invitations
WHERE
organization_id = @organization_id
AND %s
AND %s AND %s
` `

View File

@@ -78,8 +78,25 @@ func (m Membership) CursorKey(orderBy MembershipOrderField) page.CursorKey {
func (m *Membership) Create(ctx context.Context, conn pg.Conn, scope Scoper) error { func (m *Membership) Create(ctx context.Context, conn pg.Conn, scope Scoper) error {
query := ` query := `
INSERT INTO authz_memberships (tenant_id, id, user_id, organization_id, role, created_at, updated_at) INSERT INTO
VALUES (@tenant_id, @id, @user_id, @organization_id, @role, @created_at, @updated_at) authz_memberships (
tenant_id,
id,
user_id,
organization_id,
role,
created_at,
updated_at
)
VALUES (
@tenant_id,
@id,
@user_id,
@organization_id,
@role,
@created_at,
@updated_at
);
` `
args := pgx.StrictNamedArgs{ args := pgx.StrictNamedArgs{
@@ -98,11 +115,11 @@ func (m *Membership) Create(ctx context.Context, conn pg.Conn, scope Scoper) err
if errors.As(err, &pgErr) && pgErr.Code == "23505" { if errors.As(err, &pgErr) && pgErr.Code == "23505" {
return ErrMembershipAlreadyExists{UserID: m.UserID, OrgID: m.OrganizationID} return ErrMembershipAlreadyExists{UserID: m.UserID, OrgID: m.OrganizationID}
} }
return fmt.Errorf("failed to create membership: %w", err) return fmt.Errorf("cannot create membership: %w", err)
} }
if result.RowsAffected() == 0 { if result.RowsAffected() == 0 {
return fmt.Errorf("failed to create membership: organization %s not found", m.OrganizationID) return fmt.Errorf("cannot create membership: organization %s not found", m.OrganizationID)
} }
return nil return nil
@@ -124,9 +141,13 @@ func (m *Membership) LoadByID(
u.email_address, u.email_address,
m.created_at, m.created_at,
m.updated_at m.updated_at
FROM authz_memberships m FROM
JOIN users u ON m.user_id = u.id authz_memberships m
WHERE m.id = @membership_id AND %s JOIN
users u ON m.user_id = u.id
WHERE
m.id = @membership_id
AND %s
` `
query = fmt.Sprintf(query, scope.SQLFragment()) query = fmt.Sprintf(query, scope.SQLFragment())
@@ -170,9 +191,14 @@ func (m *Membership) LoadByUserAndOrg(
u.email_address, u.email_address,
m.created_at, m.created_at,
m.updated_at m.updated_at
FROM authz_memberships m FROM
JOIN users u ON m.user_id = u.id authz_memberships m
WHERE m.user_id = @user_id AND m.organization_id = @organization_id AND %s JOIN
users u ON m.user_id = u.id
WHERE
m.user_id = @user_id
AND m.organization_id = @organization_id
AND %s
` `
query = fmt.Sprintf(query, scope.SQLFragment()) query = fmt.Sprintf(query, scope.SQLFragment())
@@ -202,9 +228,14 @@ func (m *Membership) LoadByUserAndOrg(
func (m *Membership) Update(ctx context.Context, conn pg.Conn, scope Scoper) error { func (m *Membership) Update(ctx context.Context, conn pg.Conn, scope Scoper) error {
query := ` query := `
UPDATE authz_memberships UPDATE
SET role = @role, updated_at = @updated_at authz_memberships
WHERE id = @id AND %s SET
role = @role,
updated_at = @updated_at
WHERE
id = @id
AND %s
` `
query = fmt.Sprintf(query, scope.SQLFragment()) query = fmt.Sprintf(query, scope.SQLFragment())
@@ -218,7 +249,7 @@ func (m *Membership) Update(ctx context.Context, conn pg.Conn, scope Scoper) err
result, err := conn.Exec(ctx, query, args) result, err := conn.Exec(ctx, query, args)
if err != nil { if err != nil {
return fmt.Errorf("failed to update membership: %w", err) return fmt.Errorf("cannot update membership: %w", err)
} }
if result.RowsAffected() == 0 { if result.RowsAffected() == 0 {
@@ -230,8 +261,11 @@ func (m *Membership) Update(ctx context.Context, conn pg.Conn, scope Scoper) err
func (m *Membership) Delete(ctx context.Context, conn pg.Conn, scope Scoper) error { func (m *Membership) Delete(ctx context.Context, conn pg.Conn, scope Scoper) error {
query := ` query := `
DELETE FROM authz_memberships DELETE FROM
WHERE id = @id AND %s authz_memberships
WHERE
id = @id
AND %s
` `
query = fmt.Sprintf(query, scope.SQLFragment()) query = fmt.Sprintf(query, scope.SQLFragment())
@@ -243,7 +277,7 @@ func (m *Membership) Delete(ctx context.Context, conn pg.Conn, scope Scoper) err
result, err := conn.Exec(ctx, query, args) result, err := conn.Exec(ctx, query, args)
if err != nil { if err != nil {
return fmt.Errorf("failed to delete membership: %w", err) return fmt.Errorf("cannot delete membership: %w", err)
} }
if result.RowsAffected() == 0 { if result.RowsAffected() == 0 {
@@ -271,7 +305,8 @@ SELECT
m.updated_at m.updated_at
FROM FROM
authz_memberships m authz_memberships m
JOIN users u ON m.user_id = u.id JOIN
users u ON m.user_id = u.id
WHERE WHERE
m.user_id = @user_id m.user_id = @user_id
AND %s AND %s
@@ -319,7 +354,8 @@ SELECT
m.updated_at m.updated_at
FROM FROM
authz_memberships m authz_memberships m
JOIN users u ON m.user_id = u.id JOIN
users u ON m.user_id = u.id
WHERE WHERE
m.organization_id = @organization_id m.organization_id = @organization_id
AND %s AND %s
@@ -355,9 +391,13 @@ func (m *Memberships) CountByOrganizationID(
organizationID gid.GID, organizationID gid.GID,
) (int, error) { ) (int, error) {
query := ` query := `
SELECT COUNT(*) SELECT
FROM authz_memberships COUNT(*)
WHERE organization_id = @organization_id AND %s FROM
authz_memberships
WHERE
organization_id = @organization_id
AND %s
` `
query = fmt.Sprintf(query, scope.SQLFragment()) query = fmt.Sprintf(query, scope.SQLFragment())
args := pgx.StrictNamedArgs{ args := pgx.StrictNamedArgs{