Hide accepted invitations
Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
@@ -297,6 +297,7 @@ func (i *Invitations) LoadByOrganizationID(
|
||||
scope Scoper,
|
||||
orgID gid.GID,
|
||||
cursor *page.Cursor[InvitationOrderField],
|
||||
filter *InvitationFilter,
|
||||
) error {
|
||||
query := `
|
||||
SELECT
|
||||
@@ -319,14 +320,16 @@ 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(), filter.SQLFragment(), cursor.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"organization_id": orgID,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
maps.Copy(args, filter.SQLArguments())
|
||||
maps.Copy(args, cursor.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, query, args)
|
||||
@@ -348,6 +351,7 @@ func (i *Invitations) CountByOrganizationID(
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
orgID gid.GID,
|
||||
filter *InvitationFilter,
|
||||
) (int, error) {
|
||||
q := `
|
||||
SELECT
|
||||
@@ -355,15 +359,16 @@ SELECT
|
||||
FROM
|
||||
authz_invitations
|
||||
WHERE
|
||||
organization_id = @organization_id AND %s
|
||||
organization_id = @organization_id AND %s AND %s
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), filter.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"organization_id": orgID,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
maps.Copy(args, filter.SQLArguments())
|
||||
|
||||
row := conn.QueryRow(ctx, q, args)
|
||||
|
||||
|
||||
@@ -20,19 +20,19 @@ import (
|
||||
|
||||
type (
|
||||
InvitationFilter struct {
|
||||
status *InvitationStatus
|
||||
statuses InvitationStatuses
|
||||
}
|
||||
)
|
||||
|
||||
func NewInvitationFilter(status *InvitationStatus) *InvitationFilter {
|
||||
func NewInvitationFilter(statuses []InvitationStatus) *InvitationFilter {
|
||||
return &InvitationFilter{
|
||||
status: status,
|
||||
statuses: InvitationStatuses(statuses),
|
||||
}
|
||||
}
|
||||
|
||||
func (f *InvitationFilter) SQLArguments() pgx.NamedArgs {
|
||||
return pgx.NamedArgs{
|
||||
"status": f.status,
|
||||
"statuses": f.statuses,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,12 +40,12 @@ func (f *InvitationFilter) SQLFragment() string {
|
||||
return `
|
||||
(
|
||||
CASE
|
||||
WHEN @status::text IS NOT NULL THEN
|
||||
WHEN @statuses::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
|
||||
END) = ANY(@statuses::text[])
|
||||
ELSE TRUE
|
||||
END
|
||||
)`
|
||||
|
||||
@@ -17,9 +17,13 @@ package coredata
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type InvitationStatus string
|
||||
type (
|
||||
InvitationStatus string
|
||||
InvitationStatuses []InvitationStatus
|
||||
)
|
||||
|
||||
const (
|
||||
InvitationStatusPending InvitationStatus = "PENDING"
|
||||
@@ -58,3 +62,20 @@ func (tcv *InvitationStatus) Scan(value any) error {
|
||||
func (tcv InvitationStatus) Value() (driver.Value, error) {
|
||||
return tcv.String(), nil
|
||||
}
|
||||
|
||||
func (statuses InvitationStatuses) Value() (driver.Value, error) {
|
||||
if len(statuses) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var result strings.Builder
|
||||
result.WriteString("{")
|
||||
for i, status := range statuses {
|
||||
if i > 0 {
|
||||
result.WriteString(",")
|
||||
}
|
||||
result.WriteString(fmt.Sprintf("%q", status.String()))
|
||||
}
|
||||
result.WriteString("}")
|
||||
return result.String(), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user