nxtgauge-backend-rust/crates/db/migrations/20260425000000_ai_usage.up.sql
Tracewebstudio Dev aa71ccdf36 Add AI endpoints and gateway route fix
- Fix gateway: add /api/ai route to users_url
- Add AI job field generation endpoints (generate-job-field, generate-cover-letter, tailor-resume, auto-apply)
- Add AI usage tracking and rate limiting
- Add professional auto-respond-to-lead endpoint (30 tracecoins)
- Add DB migrations for AI usage tracking tables
- Update leads service with AI auto-respond functionality
2026-05-01 02:54:42 +02:00

38 lines
No EOL
1.6 KiB
PL/PgSQL

-- AI usage tracking for companies and job seekers
-- Supports per-day rate limiting for AI generation features
BEGIN;
-- Track AI usage per company per day
CREATE TABLE IF NOT EXISTS company_ai_usage (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
company_id UUID NOT NULL REFERENCES company_profiles(id) ON DELETE CASCADE,
usage_date DATE NOT NULL DEFAULT CURRENT_DATE,
generations_used INTEGER NOT NULL DEFAULT 0,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
UNIQUE(company_id, usage_date)
);
-- Track AI usage per job seeker per day
CREATE TABLE IF NOT EXISTS job_seeker_ai_usage (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
job_seeker_id UUID NOT NULL REFERENCES job_seeker_profiles(id) ON DELETE CASCADE,
usage_date DATE NOT NULL DEFAULT CURRENT_DATE,
generations_used INTEGER NOT NULL DEFAULT 0,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
UNIQUE(job_seeker_id, usage_date)
);
-- Indexes for fast lookups
CREATE INDEX IF NOT EXISTS idx_company_ai_usage_company_date ON company_ai_usage(company_id, usage_date);
CREATE INDEX IF NOT EXISTS idx_job_seeker_ai_usage_seeker_date ON job_seeker_ai_usage(job_seeker_id, usage_date);
-- Add applied_via_ai flag to job_applications for AI auto-apply tracking
ALTER TABLE job_applications ADD COLUMN IF NOT EXISTS applied_via_ai BOOLEAN DEFAULT false;
-- Add ai_pack field to job_seeker_profiles for quick lookup (cached from pricing_packages)
ALTER TABLE job_seeker_profiles ADD COLUMN IF NOT EXISTS has_ai_pack BOOLEAN DEFAULT false;
COMMIT;