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,12 +84,28 @@ 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{
"tenant_id": scope.GetTenantID(), "tenant_id": scope.GetTenantID(),
@@ -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
@@ -117,7 +133,7 @@ func (i *Invitation) LoadByID(
id gid.GID, id gid.GID,
) error { ) error {
query := ` query := `
SELECT SELECT
id, id,
organization_id, organization_id,
email, email,
@@ -131,9 +147,12 @@ 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,10 +180,14 @@ 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,9 +211,12 @@ 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
@@ -221,7 +247,7 @@ func (i *Invitations) LoadByEmail(
filter *InvitationFilter, filter *InvitationFilter,
) error { ) error {
query := ` query := `
SELECT SELECT
id, id,
organization_id, organization_id,
email, email,
@@ -235,11 +261,13 @@ 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
` `
query = fmt.Sprintf(query, filter.SQLFragment(), cursor.SQLFragment()) query = fmt.Sprintf(query, filter.SQLFragment(), cursor.SQLFragment())
@@ -271,7 +299,7 @@ func (i *Invitations) LoadByOrganizationID(
cursor *page.Cursor[InvitationOrderField], cursor *page.Cursor[InvitationOrderField],
) error { ) error {
query := ` query := `
SELECT SELECT
id, id,
organization_id, organization_id,
email, email,
@@ -285,10 +313,13 @@ 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
`
query = fmt.Sprintf(query, scope.SQLFragment(), cursor.SQLFragment()) query = fmt.Sprintf(query, scope.SQLFragment(), cursor.SQLFragment())

View File

@@ -78,9 +78,26 @@ 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{
"tenant_id": scope.GetTenantID(), "tenant_id": scope.GetTenantID(),
@@ -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
@@ -115,7 +132,7 @@ func (m *Membership) LoadByID(
membershipID gid.GID, membershipID gid.GID,
) error { ) error {
query := ` query := `
SELECT SELECT
m.id, m.id,
m.user_id, m.user_id,
m.organization_id, m.organization_id,
@@ -124,10 +141,14 @@ 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())
@@ -161,7 +182,7 @@ func (m *Membership) LoadByUserAndOrg(
orgID gid.GID, orgID gid.GID,
) error { ) error {
query := ` query := `
SELECT SELECT
m.id, m.id,
m.user_id, m.user_id,
m.organization_id, m.organization_id,
@@ -170,10 +191,15 @@ 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,10 +228,15 @@ 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,9 +261,12 @@ 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,13 +305,14 @@ 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
ORDER BY ORDER BY
m.created_at DESC m.created_at DESC
` `
query = fmt.Sprintf(query, scope.SQLFragment()) query = fmt.Sprintf(query, scope.SQLFragment())
@@ -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,10 +391,14 @@ 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{
"organization_id": organizationID, "organization_id": organizationID,