Files
lincodex/backend/db/queries/privacy_cohort.sql
T

236 lines
9.4 KiB
SQL

-- Organization-facing anonymous candidate projection inputs. The calling
-- usecase has already authorized the active organization membership. This
-- single statement snapshot filters both revoked/unpublished C2 states and
-- candidates hidden from this organization before any profile value is read.
-- The projection reads only stack, declared role, experience dates, and
-- projects with the keys of their verified repositories. It deliberately
-- excludes names, contacts, salary, location, soft skills, employer names,
-- summaries, and imported excerpts; the repository address of a project stays
-- in the private input and never reaches the card. The only assessment data
-- is the current category (specialization and grade) of the separate current
-- category table; it carries no attempt, score, review, policy, or history.
-- name: ListVisibleCandidateViewSources :many
SELECT
effective.user_id,
effective.fields,
claim.kind,
claim.ordinal,
CASE
WHEN claim.kind = 'experience' THEN jsonb_build_object(
'employer_name', NULL::text,
'specialization', claim.value ->> 'specialization',
'started_on', claim.value ->> 'started_on',
'ended_on', claim.value -> 'ended_on',
'summary', NULL::text
)
ELSE claim.value
END AS safe_value,
claim.status,
claim.origin,
claim.trust_level,
claim.extraction_version,
claim.confirmed_at,
COALESCE(questionnaire.work_formats, '{}'::text[]) AS work_formats,
COALESCE(
(
SELECT jsonb_agg(
jsonb_build_object(
'first_index', overlap.first_index,
'second_index', overlap.second_index,
'first_period', jsonb_build_object(
'started_on', overlap.first_started_on,
'ended_on', overlap.first_ended_on
),
'second_period', jsonb_build_object(
'started_on', overlap.second_started_on,
'ended_on', overlap.second_ended_on
)
) ORDER BY overlap.first_index, overlap.second_index
)::text
FROM candidate_profile_overlap_confirmations AS overlap
WHERE overlap.user_id = effective.user_id
),
'[]'::text
) AS parallel_period_acknowledgements,
COALESCE(
(
SELECT jsonb_agg(
jsonb_build_object(
'specialization', category.specialization,
'grade', category.grade,
'source', category.source
)
ORDER BY category.specialization
)::text
FROM candidate_assessment_categories AS category
WHERE category.candidate_id = effective.user_id
),
'[]'::text
) AS confirmed_categories,
COALESCE(
(
SELECT array_agg(repository.repository_key ORDER BY repository.repository_key)
FROM candidate_project_repositories AS repository
WHERE
repository.candidate_id = effective.user_id
AND repository.state = 'verified'
),
'{}'::text []
)::text [] AS verified_repositories
FROM candidate_effective_publications AS effective
LEFT JOIN candidate_profile_claims AS claim
ON claim.user_id = effective.user_id
AND claim.kind IN ('stack', 'role', 'experience', 'project')
LEFT JOIN candidate_questionnaires AS questionnaire
ON questionnaire.user_id = effective.user_id
WHERE NOT EXISTS (
SELECT 1
FROM candidate_hidden_organizations AS hidden
WHERE hidden.user_id = effective.user_id
AND hidden.organization_id = sqlc.arg(organization_id)
)
ORDER BY effective.user_id, claim.kind, claim.ordinal;
-- Lock the account before an organization checks candidate visibility. The
-- hidden-organization insert trigger takes FOR NO KEY UPDATE on this row, so
-- a hide either commits before the later visibility recheck or waits for the
-- read transaction to finish.
-- name: LockCandidateAccountForView :one
SELECT users.id
FROM users
WHERE users.id = sqlc.arg(candidate_id)
FOR SHARE;
-- Lock the current C2 before locking the publication. C2 revocation updates
-- this row before it locks the publication, so preserving this order avoids
-- a lock inversion and makes the current consent stable through the read.
-- name: LockCurrentC2ForView :one
SELECT consent_records.id
FROM consent_records
WHERE
consent_records.subject_id = sqlc.arg(candidate_id)
AND consent_records.purpose = 'c2'
AND consent_records.revoked_at IS NULL
ORDER BY consent_records.granted_at DESC, consent_records.id DESC
LIMIT 1
FOR SHARE;
-- Lock and recheck one candidate's exact current publication only after the
-- active C2 row is share-locked. No row includes unpublished, revoked, stale
-- publication, and organization-hidden states.
-- name: LockVisiblePublicationForView :one
SELECT candidate_publications.user_id
FROM candidate_publications
INNER JOIN consent_records
ON consent_records.id = candidate_publications.consent_record_id
WHERE
candidate_publications.user_id = sqlc.arg(candidate_id)
AND candidate_publications.state = 'published'
AND candidate_publications.consent_record_id = sqlc.arg(consent_record_id)
AND consent_records.subject_id = candidate_publications.user_id
AND consent_records.purpose = 'c2'
AND consent_records.revoked_at IS NULL
AND NOT EXISTS (
SELECT 1
FROM candidate_hidden_organizations AS hidden
WHERE
hidden.user_id = candidate_publications.user_id
AND hidden.organization_id = sqlc.arg(organization_id)
)
FOR SHARE OF candidate_publications, consent_records;
-- Lock a candidate's current publication for the private preview. This has
-- no organization context and never enters the employer read quota or audit.
-- name: LockPublicationForCandidatePreview :one
SELECT candidate_publications.user_id
FROM candidate_publications
INNER JOIN consent_records
ON consent_records.id = candidate_publications.consent_record_id
WHERE
candidate_publications.user_id = sqlc.arg(candidate_id)
AND candidate_publications.state = 'published'
AND consent_records.subject_id = candidate_publications.user_id
AND consent_records.purpose = 'c2'
AND consent_records.revoked_at IS NULL
FOR SHARE OF candidate_publications, consent_records;
-- Read the candidate's current private projection inputs after the caller
-- locks its active C2 and publication. It selects only fields allowed in the
-- anonymous-card projection and is never used for employer visibility.
-- name: ReadCandidateViewSourceForPreview :many
SELECT
effective.user_id,
effective.fields,
claim.kind,
claim.ordinal,
CASE
WHEN claim.kind = 'experience' THEN jsonb_build_object(
'employer_name', NULL::text,
'specialization', claim.value ->> 'specialization',
'started_on', claim.value ->> 'started_on',
'ended_on', claim.value -> 'ended_on',
'summary', NULL::text
)
ELSE claim.value
END AS safe_value,
claim.status,
claim.origin,
claim.trust_level,
claim.extraction_version,
claim.confirmed_at,
COALESCE(questionnaire.work_formats, '{}'::text[]) AS work_formats,
COALESCE(
(
SELECT jsonb_agg(
jsonb_build_object(
'first_index', overlap.first_index,
'second_index', overlap.second_index,
'first_period', jsonb_build_object(
'started_on', overlap.first_started_on,
'ended_on', overlap.first_ended_on
),
'second_period', jsonb_build_object(
'started_on', overlap.second_started_on,
'ended_on', overlap.second_ended_on
)
) ORDER BY overlap.first_index, overlap.second_index
)::text
FROM candidate_profile_overlap_confirmations AS overlap
WHERE overlap.user_id = effective.user_id
),
'[]'::text
) AS parallel_period_acknowledgements,
COALESCE(
(
SELECT jsonb_agg(
jsonb_build_object(
'specialization', category.specialization,
'grade', category.grade,
'source', category.source
)
ORDER BY category.specialization
)::text
FROM candidate_assessment_categories AS category
WHERE category.candidate_id = effective.user_id
),
'[]'::text
) AS confirmed_categories,
COALESCE(
(
SELECT array_agg(repository.repository_key ORDER BY repository.repository_key)
FROM candidate_project_repositories AS repository
WHERE
repository.candidate_id = effective.user_id
AND repository.state = 'verified'
),
'{}'::text []
)::text [] AS verified_repositories
FROM candidate_effective_publications AS effective
LEFT JOIN candidate_profile_claims AS claim
ON claim.user_id = effective.user_id
AND claim.kind IN ('stack', 'role', 'experience', 'project')
LEFT JOIN candidate_questionnaires AS questionnaire
ON questionnaire.user_id = effective.user_id
WHERE effective.user_id = sqlc.arg(candidate_id)
ORDER BY effective.user_id, claim.kind, claim.ordinal;