diff --git a/apps/users/src/handlers/reviews.rs b/apps/users/src/handlers/reviews.rs index bd67f63..0337a63 100644 --- a/apps/users/src/handlers/reviews.rs +++ b/apps/users/src/handlers/reviews.rs @@ -140,7 +140,7 @@ async fn admin_create_review( r#" INSERT INTO reviews (lead_request_id, customer_id, professional_id, rating, comment, is_published) SELECT $1, - (SELECT id FROM customer_profiles WHERE user_id = (SELECT user_id FROM lead_requests WHERE id = $1)), + (SELECT id FROM customer_profiles WHERE user_id = (SELECT customer_user_id FROM lead_requests WHERE id = $1)), (SELECT user_role_profile_id FROM lead_requests WHERE id = $1), $2, $3, true RETURNING id, lead_request_id, customer_id, professional_id, rating, comment, is_published, created_at diff --git a/crates/db/migrations/20260721050000_create_audit_logs.down.sql b/crates/db/migrations/20260721050000_create_audit_logs.down.sql new file mode 100644 index 0000000..2b0bd41 --- /dev/null +++ b/crates/db/migrations/20260721050000_create_audit_logs.down.sql @@ -0,0 +1,2 @@ +DROP TABLE IF EXISTS audit_log_changes; +DROP TABLE IF EXISTS audit_logs; diff --git a/crates/db/migrations/20260721050000_create_audit_logs.up.sql b/crates/db/migrations/20260721050000_create_audit_logs.up.sql new file mode 100644 index 0000000..8ec982d --- /dev/null +++ b/crates/db/migrations/20260721050000_create_audit_logs.up.sql @@ -0,0 +1,34 @@ +-- Backs wallet::audit() (crates/wallet/src/lib.rs), called from every admin +-- Tracecoin balance adjustment (apps/payments/src/admin.rs::adjust_credits). +-- Never created by any active migration — only in scripts/init-db.sql (see +-- 20260718200000_create_verifications_and_approvals for why that file is +-- never actually applied). actor_type/request_id/ip_address/user_agent are +-- nullable/defaulted here since the only current caller doesn't supply them. +CREATE TABLE IF NOT EXISTS audit_logs ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + actor_user_id UUID NOT NULL, + actor_type VARCHAR(20) NOT NULL DEFAULT 'USER', + action VARCHAR(100) NOT NULL, + entity_type VARCHAR(50) NOT NULL, + entity_id UUID NOT NULL, + module_key VARCHAR(100), + request_id UUID, + ip_address VARCHAR(45), + user_agent TEXT, + status VARCHAR(20) NOT NULL DEFAULT 'SUCCESS', + summary TEXT, + metadata_json JSONB, + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() +); +CREATE INDEX IF NOT EXISTS idx_audit_logs_entity ON audit_logs(entity_type, entity_id); +CREATE INDEX IF NOT EXISTS idx_audit_logs_actor ON audit_logs(actor_type, actor_user_id); +CREATE INDEX IF NOT EXISTS idx_audit_logs_created_at ON audit_logs(created_at DESC); + +CREATE TABLE IF NOT EXISTS audit_log_changes ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + audit_log_id UUID NOT NULL REFERENCES audit_logs(id) ON DELETE CASCADE, + field_name VARCHAR(255) NOT NULL, + old_value TEXT, + new_value TEXT, + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() +); diff --git a/crates/db/migrations/20260721060000_create_tax_rules_and_reviews.down.sql b/crates/db/migrations/20260721060000_create_tax_rules_and_reviews.down.sql new file mode 100644 index 0000000..121cdbb --- /dev/null +++ b/crates/db/migrations/20260721060000_create_tax_rules_and_reviews.down.sql @@ -0,0 +1,2 @@ +DROP TABLE IF EXISTS reviews; +DROP TABLE IF EXISTS tax_rules; diff --git a/crates/db/migrations/20260721060000_create_tax_rules_and_reviews.up.sql b/crates/db/migrations/20260721060000_create_tax_rules_and_reviews.up.sql new file mode 100644 index 0000000..c351f50 --- /dev/null +++ b/crates/db/migrations/20260721060000_create_tax_rules_and_reviews.up.sql @@ -0,0 +1,31 @@ +-- tax_rules backs apps/payments/src/admin.rs's admin tax-rule CRUD. Never +-- created by any active migration; init-db.sql has a differently-named +-- version (title/percentage/applicable_to) that doesn't match what the live +-- code reads/writes (name/tax_rate/applies_to) — schema below matches the +-- Rust code exactly. +CREATE TABLE IF NOT EXISTS tax_rules ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + name VARCHAR(255) NOT NULL, + tax_type VARCHAR(50) NOT NULL, + tax_rate NUMERIC(5,2) NOT NULL, + applies_to VARCHAR(50), + is_active BOOLEAN NOT NULL DEFAULT true, + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() +); + +-- reviews backs apps/users/src/handlers/reviews.rs (professional review/ +-- rating system, sourced from completed lead_requests). Same story — only +-- ever existed in scripts/init-db.sql, never applied. +CREATE TABLE IF NOT EXISTS reviews ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + lead_request_id UUID REFERENCES lead_requests(id) ON DELETE SET NULL, + customer_id UUID, + professional_id UUID, + rating SMALLINT NOT NULL, + comment TEXT, + is_published BOOLEAN NOT NULL DEFAULT true, + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), + updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() +); +CREATE INDEX IF NOT EXISTS idx_reviews_professional_id ON reviews(professional_id); +CREATE INDEX IF NOT EXISTS idx_reviews_customer_id ON reviews(customer_id);