feat(invitations): accept invitations with C4 consent and contact grants; share the E0 quota across holdings

This commit is contained in:
shira committed 2026-10-09 18:03:35 +03:00
1 parent a5990d3fc6
commit 676b3c6e24
59 files changed
+4926 -194

No files matched your search

@@ -11,6 +11,51 @@ import (
"github.com/jackc/pgx/v5/pgtype"
)
const acceptInvitation = `-- name: AcceptInvitation :one
UPDATE invitations
SET
state = 'accepted',
version = $1,
responded_at = $2,
updated_at = $2
WHERE
organization_id = $3
AND id = $4
AND state IN ('delivered', 'viewed')
AND version = $5
AND terms_version = $6
AND response_deadline_at > $2
AND $1::bigint = $5::bigint + 1
RETURNING id
`
type AcceptInvitationParams struct {
NextVersion int64
AcceptedAt pgtype.Timestamptz
OrganizationID pgtype.UUID
InvitationID pgtype.UUID
ExpectedVersion int64
TermsVersion int64
}
// The compare-and-swap of an acceptance: it applies only to a delivered or
// viewed invitation at the version and the terms version that the candidate
// read, and before its deadline. The answer is the acceptance time, and the
// deadline is kept.
func (q *Queries) AcceptInvitation(ctx context.Context, arg AcceptInvitationParams) (pgtype.UUID, error) {
row := q.db.QueryRow(ctx, acceptInvitation,
arg.NextVersion,
arg.AcceptedAt,
arg.OrganizationID,
arg.InvitationID,
arg.ExpectedVersion,
arg.TermsVersion,
)
var id pgtype.UUID
err := row.Scan(&id)
return id, err
}
const declineInvitation = `-- name: DeclineInvitation :one
UPDATE invitations
SET
@@ -51,6 +96,26 @@ func (q *Queries) DeclineInvitation(ctx context.Context, arg DeclineInvitationPa
return id, err
}
const getInvitationAcceptResult = `-- name: GetInvitationAcceptResult :one
SELECT
invitation_id,
outcome
FROM invitation_accept_results
WHERE scope_hash = $1
`
type GetInvitationAcceptResultRow struct {
InvitationID pgtype.UUID
Outcome string
}
func (q *Queries) GetInvitationAcceptResult(ctx context.Context, scopeHash []byte) (GetInvitationAcceptResultRow, error) {
row := q.db.QueryRow(ctx, getInvitationAcceptResult, scopeHash)
var i GetInvitationAcceptResultRow
err := row.Scan(&i.InvitationID, &i.Outcome)
return i, err
}
const getInvitationDeclineResult = `-- name: GetInvitationDeclineResult :one
SELECT
invitation_id,
@@ -71,6 +136,29 @@ func (q *Queries) GetInvitationDeclineResult(ctx context.Context, scopeHash []by
return i, err
}
const insertInvitationAcceptResult = `-- name: InsertInvitationAcceptResult :exec
INSERT INTO invitation_accept_results (
scope_hash,
invitation_id,
outcome
) VALUES (
$1,
$2,
$3
)
`
type InsertInvitationAcceptResultParams struct {
ScopeHash []byte
InvitationID pgtype.UUID
Outcome string
}
func (q *Queries) InsertInvitationAcceptResult(ctx context.Context, arg InsertInvitationAcceptResultParams) error {
_, err := q.db.Exec(ctx, insertInvitationAcceptResult, arg.ScopeHash, arg.InvitationID, arg.Outcome)
return err
}
const insertInvitationDecline = `-- name: InsertInvitationDecline :exec
INSERT INTO invitation_declines (
invitation_id,