feat(profile): persist candidate edits and enforce company access

This commit is contained in:
TakuyaYagam1 committed 2026-10-07 14:15:16 +03:00
1 parent 3ab6bd91a3
commit 73c7caa225
67 files changed
+5286 -229

No files matched your search

@@ -0,0 +1,74 @@
-- +goose Up
-- Each row in candidate_profile_overlap_confirmations represents the
-- candidate's acknowledgement of one exact pair of periods in the current
-- profile. Replacing a profile replaces these rows in the same transaction.
CREATE TABLE candidate_profile_overlap_confirmations (
user_id uuid NOT NULL,
first_index smallint NOT NULL,
second_index smallint NOT NULL,
first_started_on text NOT NULL,
first_ended_on text,
second_started_on text NOT NULL,
second_ended_on text,
CONSTRAINT candidate_profile_overlap_confirmations_pkey PRIMARY KEY (
user_id, first_index, second_index
),
CONSTRAINT candidate_profile_overlap_confirmations_profile_fkey FOREIGN KEY (
user_id
) REFERENCES candidate_profiles (user_id) ON DELETE CASCADE,
CONSTRAINT candidate_profile_overlap_confirmations_indices CHECK (
first_index BETWEEN 0 AND 18
AND second_index > first_index
AND second_index BETWEEN 1 AND 19
),
CONSTRAINT candidate_profile_overlap_confirmations_months CHECK (
first_started_on ~ '^[0-9]{4}-(0[1-9]|1[0-2])$'
AND (first_ended_on IS NULL OR first_ended_on ~ '^[0-9]{4}-(0[1-9]|1[0-2])$')
AND second_started_on ~ '^[0-9]{4}-(0[1-9]|1[0-2])$'
AND (second_ended_on IS NULL OR second_ended_on ~ '^[0-9]{4}-(0[1-9]|1[0-2])$')
AND (first_ended_on IS NULL OR first_ended_on >= first_started_on)
AND (second_ended_on IS NULL OR second_ended_on >= second_started_on)
AND (first_ended_on IS NULL OR second_started_on <= first_ended_on)
AND (second_ended_on IS NULL OR first_started_on <= second_ended_on)
)
);
-- The snapshots are private, append-only application records of every
-- changed profile version. They go with the owning account and have no
-- automatic retention limit. Each individual snapshot is bounded by the
-- complete public profile schema.
CREATE TABLE candidate_profile_history (
user_id uuid NOT NULL,
version bigint NOT NULL,
changed_at timestamptz NOT NULL,
snapshot jsonb NOT NULL,
CONSTRAINT candidate_profile_history_pkey PRIMARY KEY (user_id, version),
CONSTRAINT candidate_profile_history_profile_fkey FOREIGN KEY (
user_id
) REFERENCES candidate_profiles (user_id) ON DELETE CASCADE,
CONSTRAINT candidate_profile_history_version_positive CHECK (version > 0),
CONSTRAINT candidate_profile_history_snapshot_bounded CHECK (
jsonb_typeof(snapshot) = 'object'
AND octet_length(snapshot::text) <= 262144
)
);
-- +goose StatementBegin
CREATE FUNCTION reject_candidate_profile_history_update() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
RAISE EXCEPTION 'candidate profile history does not allow updates'
USING ERRCODE = 'integrity_constraint_violation';
END;
$$;
-- +goose StatementEnd
CREATE TRIGGER candidate_profile_history_no_update
BEFORE UPDATE ON candidate_profile_history
FOR EACH ROW EXECUTE FUNCTION reject_candidate_profile_history_update();
-- +goose Down
DROP TRIGGER candidate_profile_history_no_update ON candidate_profile_history;
DROP FUNCTION reject_candidate_profile_history_update();
DROP TABLE candidate_profile_history;
DROP TABLE candidate_profile_overlap_confirmations;
+76 -1
View File
@@ -60,7 +60,28 @@ SELECT
candidate_profile_claims.trust_level,
candidate_profile_claims.quote,
candidate_profile_claims.extraction_version,
candidate_profile_claims.confirmed_at
candidate_profile_claims.confirmed_at,
COALESCE(
(
SELECT jsonb_agg(
jsonb_build_object(
'first_index', confirmations.first_index,
'second_index', confirmations.second_index,
'first_period', jsonb_build_object(
'started_on', confirmations.first_started_on,
'ended_on', confirmations.first_ended_on
),
'second_period', jsonb_build_object(
'started_on', confirmations.second_started_on,
'ended_on', confirmations.second_ended_on
)
) ORDER BY confirmations.first_index, confirmations.second_index
)::text
FROM candidate_profile_overlap_confirmations AS confirmations
WHERE confirmations.user_id = candidate_profiles.user_id
),
'[]'::text
)::text AS parallel_period_acknowledgements
FROM candidate_profiles
LEFT JOIN candidate_profile_claims
ON candidate_profile_claims.user_id = candidate_profiles.user_id
@@ -100,3 +121,57 @@ INSERT INTO candidate_profile_claims (
sqlc.narg(extraction_version),
sqlc.narg(confirmed_at)
);
-- Replaces the current explicit decisions with those in the new profile.
-- The versioned profile row is advanced first in this same transaction.
-- name: DeleteCandidateProfileOverlapConfirmations :exec
DELETE FROM candidate_profile_overlap_confirmations
WHERE user_id = sqlc.arg(user_id);
-- name: InsertCandidateProfileOverlapConfirmation :exec
INSERT INTO candidate_profile_overlap_confirmations (
user_id,
first_index,
second_index,
first_started_on,
first_ended_on,
second_started_on,
second_ended_on
) VALUES (
sqlc.arg(user_id),
sqlc.arg(first_index),
sqlc.arg(second_index),
sqlc.arg(first_started_on),
sqlc.narg(first_ended_on),
sqlc.arg(second_started_on),
sqlc.narg(second_ended_on)
);
-- Every changed version appends exactly one private snapshot. The unique
-- key prevents a second snapshot for the same profile version.
-- name: AppendCandidateProfileHistory :exec
INSERT INTO candidate_profile_history (
user_id,
version,
changed_at,
snapshot
) VALUES (
sqlc.arg(user_id),
sqlc.arg(version),
sqlc.arg(changed_at),
sqlc.arg(snapshot)::jsonb
);
-- Saving a profile advances onboarding only from that exact step. A retry or
-- a save after a later step is already stored never regresses onboarding.
-- name: AdvanceCandidateOnboardingAfterProfile :exec
UPDATE users
SET
onboarding_step = 'candidate_questionnaire',
version = users.version + 1,
updated_at = GREATEST(users.updated_at, sqlc.arg(at)::timestamptz)
WHERE
users.id = sqlc.arg(user_id)
AND users.scenario = 'candidate'
AND users.email_verified_at IS NOT NULL
AND users.onboarding_step = 'candidate_profile';