feat(accounts): add consent settings and organization management

This commit is contained in:
TakuyaYagam1 committed 2026-10-07 13:00:39 +03:00
1 parent 24638d46d2
commit 391af7a1ef
83 files changed
+10198 -613

No files matched your search

@@ -11,6 +11,82 @@ import (
"github.com/jackc/pgx/v5/pgtype"
)
const createAccountDeletionRequest = `-- name: CreateAccountDeletionRequest :one
INSERT INTO account_deletion_requests (
id,
subject_id,
status,
requested_at
) VALUES (
$1,
$2,
'requested',
$3
)
ON CONFLICT (subject_id)
WHERE subject_id IS NOT NULL AND status IN ('requested', 'processing')
DO NOTHING
RETURNING id, subject_id, status, requested_at
`
type CreateAccountDeletionRequestParams struct {
ID pgtype.UUID
SubjectID pgtype.UUID
RequestedAt pgtype.Timestamptz
}
type CreateAccountDeletionRequestRow struct {
ID pgtype.UUID
SubjectID pgtype.UUID
Status string
RequestedAt pgtype.Timestamptz
}
// Creates one durable deletion request per subject until a worker completes it.
// The caller appends the matching outbox intent in the same transaction only
// when this insert returns a row.
func (q *Queries) CreateAccountDeletionRequest(ctx context.Context, arg CreateAccountDeletionRequestParams) (CreateAccountDeletionRequestRow, error) {
row := q.db.QueryRow(ctx, createAccountDeletionRequest, arg.ID, arg.SubjectID, arg.RequestedAt)
var i CreateAccountDeletionRequestRow
err := row.Scan(
&i.ID,
&i.SubjectID,
&i.Status,
&i.RequestedAt,
)
return i, err
}
const getActiveAccountDeletionRequest = `-- name: GetActiveAccountDeletionRequest :one
SELECT id, subject_id, status, requested_at
FROM account_deletion_requests
WHERE subject_id = $1
AND status IN ('requested', 'processing')
ORDER BY requested_at, id
LIMIT 1
`
type GetActiveAccountDeletionRequestRow struct {
ID pgtype.UUID
SubjectID pgtype.UUID
Status string
RequestedAt pgtype.Timestamptz
}
// Reads the durable active deletion request when an earlier request already
// exists. A completed request does not suppress a later explicit request.
func (q *Queries) GetActiveAccountDeletionRequest(ctx context.Context, subjectID pgtype.UUID) (GetActiveAccountDeletionRequestRow, error) {
row := q.db.QueryRow(ctx, getActiveAccountDeletionRequest, subjectID)
var i GetActiveAccountDeletionRequestRow
err := row.Scan(
&i.ID,
&i.SubjectID,
&i.Status,
&i.RequestedAt,
)
return i, err
}
const getConsentText = `-- name: GetConsentText :one
SELECT
purpose,
@@ -8,6 +8,14 @@ import (
"github.com/jackc/pgx/v5/pgtype"
)
type AccountDeletionRequest struct {
ID pgtype.UUID
SubjectID pgtype.UUID
Status string
RequestedAt pgtype.Timestamptz
CompletedAt pgtype.Timestamptz
}
type AssessmentCategoryOpening struct {
ID pgtype.UUID
Category string
@@ -315,6 +323,11 @@ type Organization struct {
UpdatedAt pgtype.Timestamptz
}
type OrganizationCreationResult struct {
ScopeHash []byte
Snapshot []byte
}
type OrganizationRepresentative struct {
OrganizationID pgtype.UUID
Ordinal int16
@@ -57,6 +57,21 @@ func (q *Queries) GetMembership(ctx context.Context, arg GetMembershipParams) (M
return i, err
}
const getOrganizationCreationResult = `-- name: GetOrganizationCreationResult :one
SELECT snapshot
FROM organization_creation_results
WHERE scope_hash = $1
`
// Reads only the historical result. It intentionally does not join the live
// organization or membership, whose profile and state may have since changed.
func (q *Queries) GetOrganizationCreationResult(ctx context.Context, scopeHash []byte) ([]byte, error) {
row := q.db.QueryRow(ctx, getOrganizationCreationResult, scopeHash)
var snapshot []byte
err := row.Scan(&snapshot)
return snapshot, err
}
const getOrganizationForMember = `-- name: GetOrganizationForMember :one
SELECT
organization.id,
@@ -390,6 +405,27 @@ func (q *Queries) ListOrganizationsForUser(ctx context.Context, arg ListOrganiza
return items, nil
}
const saveOrganizationCreationResult = `-- name: SaveOrganizationCreationResult :exec
INSERT INTO organization_creation_results (scope_hash, snapshot)
VALUES ($1, $2)
ON CONFLICT (scope_hash) DO UPDATE
SET snapshot = excluded.snapshot
`
type SaveOrganizationCreationResultParams struct {
ScopeHash []byte
Snapshot []byte
}
// Stores the immutable domain snapshot of a successful create command. The
// generic idempotency row and this result are committed with the organization
// and owner membership. A reused expired scope replaces the snapshot in the
// same transaction as the new organization.
func (q *Queries) SaveOrganizationCreationResult(ctx context.Context, arg SaveOrganizationCreationResultParams) error {
_, err := q.db.Exec(ctx, saveOrganizationCreationResult, arg.ScopeHash, arg.Snapshot)
return err
}
const updateOrganizationProfile = `-- name: UpdateOrganizationProfile :one
WITH target AS (
SELECT id FROM organizations
@@ -421,6 +421,38 @@ func (q *Queries) ListCandidatePrivacyEvents(ctx context.Context, arg ListCandid
return items, nil
}
const lockCandidatePublicationForConsentRevocation = `-- name: LockCandidatePublicationForConsentRevocation :one
SELECT greatest(candidate_publications.published_at, consent_records.revoked_at)::timestamptz AS closed_at
FROM candidate_publications
INNER JOIN consent_records
ON consent_records.id = candidate_publications.consent_record_id
WHERE
candidate_publications.user_id = $1
AND candidate_publications.state = 'published'
AND consent_records.id = ANY($2::uuid [])
AND consent_records.subject_id = candidate_publications.user_id
AND consent_records.purpose = 'c2'
AND consent_records.revoked_at IS NOT NULL
FOR UPDATE OF candidate_publications
`
type LockCandidatePublicationForConsentRevocationParams struct {
UserID pgtype.UUID
ConsentRecordIds []pgtype.UUID
}
// Locks the candidate's published row only when it refers to a C2 record
// revoked by this transaction. The setting remains linked to that immutable
// record; ReadCandidatePublication computes the effective closed state and
// keeps the consent-revoked reason after later grants. The returned time is
// the effective close time used by the privacy history event.
func (q *Queries) LockCandidatePublicationForConsentRevocation(ctx context.Context, arg LockCandidatePublicationForConsentRevocationParams) (pgtype.Timestamptz, error) {
row := q.db.QueryRow(ctx, lockCandidatePublicationForConsentRevocation, arg.UserID, arg.ConsentRecordIds)
var closed_at pgtype.Timestamptz
err := row.Scan(&closed_at)
return closed_at, err
}
const lockCurrentC2Record = `-- name: LockCurrentC2Record :one
SELECT id
FROM consent_records
@@ -107,6 +107,10 @@ type Querier interface {
// Counts the families of a category that have at least one published
// version, however many versions each has.
CountPublishedAssessmentFamilies(ctx context.Context, category string) (int32, error)
// Creates one durable deletion request per subject until a worker completes it.
// The caller appends the matching outbox intent in the same transaction only
// when this insert returns a row.
CreateAccountDeletionRequest(ctx context.Context, arg CreateAccountDeletionRequestParams) (CreateAccountDeletionRequestRow, error)
// Lifts one hiding. No row returns when the organization was not hidden, so
// the caller writes the history event only for a real deletion and answers
// 204 either way.
@@ -156,6 +160,9 @@ type Querier interface {
// made at or after reserved_since. A retry of the delivery inside the
// provider's idempotency window reuses it instead of counting again.
FindOpenMailQuotaReservation(ctx context.Context, arg FindOpenMailQuotaReservationParams) (FindOpenMailQuotaReservationRow, error)
// Reads the durable active deletion request when an earlier request already
// exists. A completed request does not suppress a later explicit request.
GetActiveAccountDeletionRequest(ctx context.Context, subjectID pgtype.UUID) (GetActiveAccountDeletionRequestRow, error)
GetActiveSessionByTokenHash(ctx context.Context, arg GetActiveSessionByTokenHashParams) (Session, error)
GetAssessmentFamilyByID(ctx context.Context, id pgtype.UUID) (AssessmentFamily, error)
GetAssessmentFamilyVersion(ctx context.Context, arg GetAssessmentFamilyVersionParams) (AssessmentFamily, error)
@@ -207,6 +214,9 @@ type Querier interface {
GetMailQuotaUsage(ctx context.Context, arg GetMailQuotaUsageParams) ([]GetMailQuotaUsageRow, error)
// Reads the membership of one account in one organization, in any state.
GetMembership(ctx context.Context, arg GetMembershipParams) (Membership, error)
// Reads only the historical result. It intentionally does not join the live
// organization or membership, whose profile and state may have since changed.
GetOrganizationCreationResult(ctx context.Context, scopeHash []byte) ([]byte, error)
// Reads an organization together with the membership of one account in it
// and its representatives in list order, as one statement, so the profile and
// the list come from one snapshot. Only an active membership matches, so an
@@ -377,6 +387,12 @@ type Querier interface {
// published or open the category, until the transaction ends. The key is
// namespaced, so it cannot collide with another use of advisory locks.
LockAssessmentCategory(ctx context.Context, category string) error
// Locks the candidate's published row only when it refers to a C2 record
// revoked by this transaction. The setting remains linked to that immutable
// record; ReadCandidatePublication computes the effective closed state and
// keeps the consent-revoked reason after later grants. The returned time is
// the effective close time used by the privacy history event.
LockCandidatePublicationForConsentRevocation(ctx context.Context, arg LockCandidatePublicationForConsentRevocationParams) (pgtype.Timestamptz, error)
// Locks the delivery row of the lease holder until the transaction ends, so
// every path that settles a send takes the delivery lock before the quota
// rows, the same order as the claim. No row means that owner no longer holds
@@ -542,6 +558,11 @@ type Querier interface {
RevokeContactConsentRecords(ctx context.Context, arg RevokeContactConsentRecordsParams) ([]RevokeContactConsentRecordsRow, error)
RevokeSession(ctx context.Context, arg RevokeSessionParams) (int64, error)
RevokeUserSessions(ctx context.Context, arg RevokeUserSessionsParams) (int64, error)
// Stores the immutable domain snapshot of a successful create command. The
// generic idempotency row and this result are committed with the organization
// and owner membership. A reused expired scope replaces the snapshot in the
// same transaction as the new organization.
SaveOrganizationCreationResult(ctx context.Context, arg SaveOrganizationCreationResultParams) error
// Bounds a consumer transaction on the server, local to the transaction.
// statement_timeout stops a slow statement; idle_in_transaction_session_timeout
// ends the session of a process that froze or lost the network inside the