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 {
Token string
ID 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 {
@@ -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 {
query := `
INSERT INTO authz_invitations (
tenant_id, 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
INSERT INTO
authz_invitations (
tenant_id,
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{
@@ -104,7 +120,7 @@ func (i *Invitation) Create(ctx context.Context, conn pg.Conn, scope Scoper) err
_, err := conn.Exec(ctx, query, args)
if err != nil {
return fmt.Errorf("failed to create invitation: %w", err)
return fmt.Errorf("cannot create invitation: %w", err)
}
return nil
@@ -131,8 +147,11 @@ func (i *Invitation) LoadByID(
expires_at,
accepted_at,
created_at
FROM authz_invitations
WHERE id = @id AND %s
FROM
authz_invitations
WHERE
id = @id
AND %s
`
query = fmt.Sprintf(query, scope.SQLFragment())
@@ -150,7 +169,7 @@ func (i *Invitation) LoadByID(
invitation, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Invitation])
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrInvitationNotFound{Token: id.String()}
return ErrInvitationNotFound{ID: id.String()}
}
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 {
query := `
UPDATE authz_invitations
SET accepted_at = @accepted_at
WHERE id = @id AND %s
UPDATE
authz_invitations
SET
accepted_at = @accepted_at
WHERE
id = @id
AND %s
`
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)
if err != nil {
return fmt.Errorf("failed to update invitation: %w", err)
return fmt.Errorf("cannot update invitation: %w", err)
}
if result.RowsAffected() == 0 {
return ErrInvitationNotFound{Token: i.ID.String()}
return ErrInvitationNotFound{ID: i.ID.String()}
}
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 {
query := `
DELETE FROM authz_invitations
WHERE id = @id AND %s
DELETE FROM
authz_invitations
WHERE
id = @id
AND %s
`
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)
if err != nil {
return fmt.Errorf("failed to delete invitation: %w", err)
return fmt.Errorf("cannot delete invitation: %w", err)
}
if result.RowsAffected() == 0 {
return ErrInvitationNotFound{Token: i.ID.String()}
return ErrInvitationNotFound{ID: i.ID.String()}
}
return nil
@@ -235,8 +261,10 @@ func (i *Invitations) LoadByEmail(
expires_at,
accepted_at,
created_at
FROM authz_invitations
WHERE email = @email
FROM
authz_invitations
WHERE
email = @email
AND %s
AND %s
`
@@ -285,8 +313,11 @@ func (i *Invitations) LoadByOrganizationID(
expires_at,
accepted_at,
created_at
FROM authz_invitations
WHERE organization_id = @organization_id AND %s
FROM
authz_invitations
WHERE
organization_id = @organization_id
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 {
query := `
INSERT INTO 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)
INSERT INTO
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{
@@ -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" {
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 {
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
@@ -124,9 +141,13 @@ func (m *Membership) LoadByID(
u.email_address,
m.created_at,
m.updated_at
FROM authz_memberships m
JOIN users u ON m.user_id = u.id
WHERE m.id = @membership_id AND %s
FROM
authz_memberships m
JOIN
users u ON m.user_id = u.id
WHERE
m.id = @membership_id
AND %s
`
query = fmt.Sprintf(query, scope.SQLFragment())
@@ -170,9 +191,14 @@ func (m *Membership) LoadByUserAndOrg(
u.email_address,
m.created_at,
m.updated_at
FROM authz_memberships m
JOIN users u ON m.user_id = u.id
WHERE m.user_id = @user_id AND m.organization_id = @organization_id AND %s
FROM
authz_memberships m
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())
@@ -202,9 +228,14 @@ func (m *Membership) LoadByUserAndOrg(
func (m *Membership) Update(ctx context.Context, conn pg.Conn, scope Scoper) error {
query := `
UPDATE authz_memberships
SET role = @role, updated_at = @updated_at
WHERE id = @id AND %s
UPDATE
authz_memberships
SET
role = @role,
updated_at = @updated_at
WHERE
id = @id
AND %s
`
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)
if err != nil {
return fmt.Errorf("failed to update membership: %w", err)
return fmt.Errorf("cannot update membership: %w", err)
}
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 {
query := `
DELETE FROM authz_memberships
WHERE id = @id AND %s
DELETE FROM
authz_memberships
WHERE
id = @id
AND %s
`
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)
if err != nil {
return fmt.Errorf("failed to delete membership: %w", err)
return fmt.Errorf("cannot delete membership: %w", err)
}
if result.RowsAffected() == 0 {
@@ -271,7 +305,8 @@ SELECT
m.updated_at
FROM
authz_memberships m
JOIN users u ON m.user_id = u.id
JOIN
users u ON m.user_id = u.id
WHERE
m.user_id = @user_id
AND %s
@@ -319,7 +354,8 @@ SELECT
m.updated_at
FROM
authz_memberships m
JOIN users u ON m.user_id = u.id
JOIN
users u ON m.user_id = u.id
WHERE
m.organization_id = @organization_id
AND %s
@@ -355,9 +391,13 @@ func (m *Memberships) CountByOrganizationID(
organizationID gid.GID,
) (int, error) {
query := `
SELECT COUNT(*)
FROM authz_memberships
WHERE organization_id = @organization_id AND %s
SELECT
COUNT(*)
FROM
authz_memberships
WHERE
organization_id = @organization_id
AND %s
`
query = fmt.Sprintf(query, scope.SQLFragment())
args := pgx.StrictNamedArgs{