feat(platform): add the notification inbox and deterministic match search
This commit is contained in:
1 parent
5edff21d2d
commit
0b11021b16
169 files changed
+17083
-443
No files matched your search
@@ -12,7 +12,9 @@ import (
|
||||
var (
|
||||
evidenceTableName = regexp.MustCompile(`(?i)\bskill_evidence_(facts|skills)\b`)
|
||||
// evidenceReaders are the only queries that may name the evidence tables.
|
||||
evidenceReaders = []string{"ListCandidateEvidenceFacts", "ListCandidatePreviewEvidence", "ListOrganizationVisibleEvidence"}
|
||||
evidenceReaders = []string{
|
||||
"ListCandidateEvidenceFacts", "ListCandidatePreviewEvidence", "ListOrganizationVisibleEvidence", "ListOrganizationVisibleEvidenceBatch",
|
||||
}
|
||||
// evidenceForbiddenSources are tables whose content must never reach the
|
||||
// evidence reads: raw answers and items, results with their scores,
|
||||
// attempts, signals, and the anti-cheat review band.
|
||||
@@ -123,3 +125,41 @@ func TestOrganizationEvidenceReadUsesTheVisibilityBarrier(t *testing.T) {
|
||||
t.Errorf("organization evidence query reads the publication table directly: %s", statement)
|
||||
}
|
||||
}
|
||||
|
||||
// The batch read repeats every barrier of the single organization read, and
|
||||
// the match-search queries name no evidence table.
|
||||
func TestOrganizationEvidenceBatchReadRepeatsTheVisibilityBarrier(t *testing.T) {
|
||||
statements := evidenceStatements(t)
|
||||
statement := statements["ListOrganizationVisibleEvidenceBatch"]
|
||||
if statement == "" {
|
||||
t.Fatal("the batch organization evidence query is missing")
|
||||
}
|
||||
for _, required := range []string{
|
||||
"inner join candidate_effective_publications as effective",
|
||||
"from candidate_hidden_organizations as hidden",
|
||||
"not exists",
|
||||
"hidden.organization_id = sqlc.arg(organization_id)",
|
||||
"'categories' = any(effective.fields)",
|
||||
"fact.visibility = 'visible'",
|
||||
"fact.kind = 'measurement'",
|
||||
"(skill.skill_code || '/' || fact.grade) = wanted.category",
|
||||
} {
|
||||
if !strings.Contains(statement, required) {
|
||||
t.Errorf("batch organization evidence query lacks %q: %s", required, statement)
|
||||
}
|
||||
}
|
||||
if strings.Contains(statement, "candidate_publications") && !strings.Contains(statement, "candidate_effective_publications") {
|
||||
t.Errorf("batch organization evidence query reads the publication table directly: %s", statement)
|
||||
}
|
||||
for _, private := range []string{"fact.id,", "event_id", "result_id", "fact.visibility,", "occurred_at,"} {
|
||||
selected := statement[len("select"):strings.Index(statement, " from (")]
|
||||
if strings.Contains(selected, private) {
|
||||
t.Errorf("batch organization evidence query selects %q: %s", private, selected)
|
||||
}
|
||||
}
|
||||
for name, search := range statements {
|
||||
if strings.HasPrefix(name, "CountReal") && evidenceTableName.MatchString(search) {
|
||||
t.Errorf("%s names an evidence table: %s", name, search)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
-- The inbox allowlist gains a synthetic event type. It carries no product
|
||||
-- meaning: it lets the consumer, the read-time visibility check, the list,
|
||||
-- and the read state run end to end before real producers exist. Each event
|
||||
-- type is bound to the one source type its content is resolved from.
|
||||
-- +goose Up
|
||||
ALTER TABLE notification_inbox_events
|
||||
DROP CONSTRAINT notification_inbox_events_type_known;
|
||||
|
||||
ALTER TABLE notification_inbox_events
|
||||
ADD CONSTRAINT notification_inbox_events_type_known CHECK (
|
||||
schema_version = 1
|
||||
AND (
|
||||
(event_type = 'invitation.delivered' AND source_type = 'invitation')
|
||||
OR (
|
||||
event_type = 'notification.synthetic_delivered'
|
||||
AND source_type = 'organization'
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
-- A rollback must not silently lose or orphan recipient-visible history.
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM notification_inbox_events
|
||||
WHERE event_type <> 'invitation.delivered'
|
||||
) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'integrity_constraint_violation',
|
||||
CONSTRAINT = 'notification_inbox_events_type_known',
|
||||
TABLE = 'notification_inbox_events',
|
||||
MESSAGE = 'synthetic notification inbox records must be resolved before rollback';
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
ALTER TABLE notification_inbox_events
|
||||
DROP CONSTRAINT notification_inbox_events_type_known;
|
||||
|
||||
ALTER TABLE notification_inbox_events
|
||||
ADD CONSTRAINT notification_inbox_events_type_known CHECK (
|
||||
event_type = 'invitation.delivered'
|
||||
AND schema_version = 1
|
||||
AND source_type = 'invitation'
|
||||
);
|
||||
@@ -0,0 +1,18 @@
|
||||
-- Reads that only the match search needs. The visibility of the candidates and
|
||||
-- their permitted evidence come from the card and evidence queries, which are
|
||||
-- the single copy of those barriers; this file adds no candidate-facing read.
|
||||
|
||||
-- The number of candidates who hold each given category from a source that is
|
||||
-- not a synthetic demonstration, which is the size of the comparable real
|
||||
-- cohort that a percentile needs. It returns counts only, for the pairs the
|
||||
-- caller passes as "specialization/grade", and no row for a pair nobody holds.
|
||||
-- name: CountRealComparableCategories :many
|
||||
SELECT
|
||||
category.specialization,
|
||||
category.grade,
|
||||
count(*)::bigint AS candidates
|
||||
FROM candidate_assessment_categories AS category
|
||||
WHERE
|
||||
category.source <> 'synthetic_demo'
|
||||
AND (category.specialization || '/' || category.grade) = any(sqlc.arg(categories)::text [])
|
||||
GROUP BY category.specialization, category.grade;
|
||||
@@ -2,6 +2,20 @@
|
||||
-- finally usage rows. The organization lock serializes quota decisions for
|
||||
-- the tenant and makes the subsequent count and insert atomic.
|
||||
|
||||
-- Takes the organization lock ahead of every share lock of a reader that will
|
||||
-- reserve usage later. FOR NO KEY UPDATE conflicts with itself and with FOR
|
||||
-- SHARE, so readers of one organization queue up instead of deadlocking on a
|
||||
-- share lock that each then tries to raise, but it does not conflict with the
|
||||
-- FOR KEY SHARE that a foreign key takes, such as the hidden-organization
|
||||
-- insert of a candidate, which holds the candidate's row and then needs this
|
||||
-- one. The reservation itself still takes FOR UPDATE inside the same
|
||||
-- transaction.
|
||||
-- name: LockCompanyQuotaOrganizationFirst :one
|
||||
SELECT id
|
||||
FROM organizations
|
||||
WHERE id = sqlc.arg(organization_id)
|
||||
FOR NO KEY UPDATE;
|
||||
|
||||
-- name: LockCompanyQuotaOrganization :one
|
||||
SELECT
|
||||
id,
|
||||
|
||||
@@ -103,3 +103,48 @@ WHERE
|
||||
AND fact.kind = 'measurement'
|
||||
AND fact.visibility = 'visible'
|
||||
ORDER BY skill.skill_code, fact.occurred_at, fact.id;
|
||||
|
||||
-- The same organization read for a whole candidate set, so a search reads the
|
||||
-- evidence of every listed candidate in one statement. The pairs come from the
|
||||
-- caller as two parallel arrays, one row per candidate and category of the card
|
||||
-- that was just projected. The statement repeats every barrier of the single
|
||||
-- read: the effective publication view, NOT EXISTS over the candidates that
|
||||
-- hide the organization, the categories field, and only visible measurements.
|
||||
-- The candidate identifier is returned for the caller to group by and never
|
||||
-- leaves the server; the other columns are the same allowlist.
|
||||
-- name: ListOrganizationVisibleEvidenceBatch :many
|
||||
SELECT DISTINCT ON (wanted.candidate_id, skill.skill_code)
|
||||
wanted.candidate_id::uuid AS candidate_id,
|
||||
skill.skill_code AS skill,
|
||||
fact.grade,
|
||||
fact.trust,
|
||||
fact.basis,
|
||||
fact.calibration_status,
|
||||
extract(YEAR FROM fact.occurred_at AT TIME ZONE 'UTC')::integer AS observed_year
|
||||
FROM (
|
||||
SELECT
|
||||
candidate_ids.candidate_id,
|
||||
wanted_categories.category
|
||||
FROM unnest(sqlc.arg(candidate_ids)::uuid []) WITH ORDINALITY AS candidate_ids (candidate_id, position)
|
||||
INNER JOIN unnest(sqlc.arg(categories)::text []) WITH ORDINALITY AS wanted_categories (category, position)
|
||||
ON candidate_ids.position = wanted_categories.position
|
||||
) AS wanted
|
||||
INNER JOIN candidate_effective_publications AS effective
|
||||
ON wanted.candidate_id = effective.user_id
|
||||
INNER JOIN skill_evidence_facts AS fact
|
||||
ON effective.user_id = fact.candidate_id
|
||||
INNER JOIN skill_evidence_skills AS skill
|
||||
ON fact.id = skill.fact_id
|
||||
WHERE
|
||||
'categories' = any(effective.fields)
|
||||
AND (skill.skill_code || '/' || fact.grade) = wanted.category
|
||||
AND fact.kind = 'measurement'
|
||||
AND fact.visibility = 'visible'
|
||||
AND 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 wanted.candidate_id, skill.skill_code, fact.occurred_at, fact.id;
|
||||
@@ -69,13 +69,19 @@ WHERE recipient_user_id = sqlc.arg(recipient_user_id)
|
||||
ORDER BY occurred_at DESC, event_id DESC
|
||||
LIMIT sqlc.arg(page_limit);
|
||||
|
||||
-- Persist the first read time under exact recipient and organization scope.
|
||||
-- Repeated calls preserve the original read timestamp.
|
||||
-- Persist the first read time for the recipient, optionally under one exact
|
||||
-- organization context. A missing organization is the candidate cabinet,
|
||||
-- which spans the organizations that sent to it; the employer cabinet always
|
||||
-- names its organization. Repeated calls preserve the original read
|
||||
-- timestamp.
|
||||
-- name: MarkNotificationInboxEventRead :one
|
||||
UPDATE notification_inbox_events
|
||||
SET read_at = COALESCE(read_at, sqlc.arg(read_at)::timestamptz)
|
||||
WHERE recipient_user_id = sqlc.arg(recipient_user_id)
|
||||
AND organization_id = sqlc.arg(organization_id)
|
||||
AND (
|
||||
sqlc.narg(organization_id)::uuid IS NULL
|
||||
OR organization_id = sqlc.narg(organization_id)::uuid
|
||||
)
|
||||
AND event_id = sqlc.arg(event_id)
|
||||
RETURNING
|
||||
recipient_user_id,
|
||||
@@ -87,3 +93,33 @@ RETURNING
|
||||
source_id,
|
||||
occurred_at,
|
||||
read_at;
|
||||
|
||||
-- Resolve the sender organization of a synthetic notice under the recipient's
|
||||
-- current standing, so the notice never outlives that standing. A candidate
|
||||
-- sees the name only while the organization is not hidden from them; an
|
||||
-- employer sees it only while they are an active member. No row means the
|
||||
-- content is no longer visible to this recipient.
|
||||
-- name: GetNotificationOrganizationNameForRecipient :one
|
||||
SELECT organizations.name
|
||||
FROM organizations
|
||||
INNER JOIN users ON users.id = sqlc.arg(recipient_user_id)
|
||||
WHERE organizations.id = sqlc.arg(organization_id)
|
||||
AND (
|
||||
(
|
||||
users.scenario = 'candidate'
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM candidate_hidden_organizations
|
||||
WHERE candidate_hidden_organizations.user_id = users.id
|
||||
AND candidate_hidden_organizations.organization_id = organizations.id
|
||||
)
|
||||
)
|
||||
OR (
|
||||
users.scenario = 'employer'
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM memberships
|
||||
WHERE memberships.user_id = users.id
|
||||
AND memberships.organization_id = organizations.id
|
||||
AND memberships.state = 'active'
|
||||
)
|
||||
)
|
||||
);
|
||||
Reference in new issue
Block a user