Add status to invitation

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Sacha Al Himdani
2025-10-14 09:35:40 +02:00
parent 165eb267f2
commit 4a24219500
14 changed files with 324 additions and 63 deletions

View File

@@ -29,14 +29,15 @@ import (
type (
Invitation struct {
ID gid.GID `db:"id"`
OrganizationID gid.GID `db:"organization_id"`
Email string `db:"email"`
FullName string `db:"full_name"`
Role string `db:"role"`
ExpiresAt time.Time `db:"expires_at"`
AcceptedAt *time.Time `db:"accepted_at"`
CreatedAt time.Time `db:"created_at"`
ID gid.GID `db:"id"`
OrganizationID gid.GID `db:"organization_id"`
Email string `db:"email"`
FullName string `db:"full_name"`
Role string `db:"role"`
Status InvitationStatus `db:"status"`
ExpiresAt time.Time `db:"expires_at"`
AcceptedAt *time.Time `db:"accepted_at"`
CreatedAt time.Time `db:"created_at"`
}
Invitations []*Invitation
@@ -116,7 +117,20 @@ func (i *Invitation) LoadByID(
id gid.GID,
) error {
query := `
SELECT id, organization_id, email, full_name, role, expires_at, accepted_at, created_at
SELECT
id,
organization_id,
email,
full_name,
role,
CASE
WHEN accepted_at IS NOT NULL THEN 'ACCEPTED'
WHEN expires_at < NOW() THEN 'EXPIRED'
ELSE 'PENDING'
END as status,
expires_at,
accepted_at,
created_at
FROM authz_invitations
WHERE id = @id AND %s
`
@@ -207,7 +221,20 @@ func (i *Invitations) LoadByEmail(
filter *InvitationFilter,
) error {
query := `
SELECT id, organization_id, email, full_name, role, expires_at, accepted_at, created_at
SELECT
id,
organization_id,
email,
full_name,
role,
CASE
WHEN accepted_at IS NOT NULL THEN 'ACCEPTED'
WHEN expires_at < NOW() THEN 'EXPIRED'
ELSE 'PENDING'
END as status,
expires_at,
accepted_at,
created_at
FROM authz_invitations
WHERE email = @email
AND %s
@@ -244,7 +271,20 @@ func (i *Invitations) LoadByOrganizationID(
cursor *page.Cursor[InvitationOrderField],
) error {
query := `
SELECT id, organization_id, email, full_name, role, expires_at, accepted_at, created_at
SELECT
id,
organization_id,
email,
full_name,
role,
CASE
WHEN accepted_at IS NOT NULL THEN 'ACCEPTED'
WHEN expires_at < NOW() THEN 'EXPIRED'
ELSE 'PENDING'
END as status,
expires_at,
accepted_at,
created_at
FROM authz_invitations
WHERE organization_id = @organization_id AND %s
AND %s

View File

@@ -20,19 +20,19 @@ import (
type (
InvitationFilter struct {
onlyPending *bool
status *InvitationStatus
}
)
func NewInvitationFilter(onlyPending *bool) *InvitationFilter {
func NewInvitationFilter(status *InvitationStatus) *InvitationFilter {
return &InvitationFilter{
onlyPending: onlyPending,
status: status,
}
}
func (f *InvitationFilter) SQLArguments() pgx.NamedArgs {
return pgx.NamedArgs{
"only_pending": f.onlyPending,
"status": f.status,
}
}
@@ -40,10 +40,13 @@ func (f *InvitationFilter) SQLFragment() string {
return `
(
CASE
WHEN @only_pending::boolean IS NOT NULL AND @only_pending::boolean = true THEN
(accepted_at IS NULL AND expires_at > NOW())
WHEN @status::text IS NOT NULL THEN
(CASE
WHEN accepted_at IS NOT NULL THEN 'ACCEPTED'
WHEN expires_at < NOW() THEN 'EXPIRED'
ELSE 'PENDING'
END) = @status::text
ELSE TRUE
END
)`
}

View File

@@ -0,0 +1,60 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
import (
"database/sql/driver"
"fmt"
)
type InvitationStatus string
const (
InvitationStatusPending InvitationStatus = "PENDING"
InvitationStatusAccepted InvitationStatus = "ACCEPTED"
InvitationStatusExpired InvitationStatus = "EXPIRED"
)
func (tcv InvitationStatus) String() string {
return string(tcv)
}
func (tcv *InvitationStatus) Scan(value any) error {
var s string
switch v := value.(type) {
case string:
s = v
case []byte:
s = string(v)
default:
return fmt.Errorf("unsupported type for TrustCenterVisibility: %T", value)
}
switch s {
case "PENDING":
*tcv = InvitationStatusPending
case "ACCEPTED":
*tcv = InvitationStatusAccepted
case "EXPIRED":
*tcv = InvitationStatusExpired
default:
return fmt.Errorf("invalid InvitationStatus value: %q", s)
}
return nil
}
func (tcv InvitationStatus) Value() (driver.Value, error) {
return tcv.String(), nil
}