de30497f59
- Implemented tenant and user management features including: - Tenant creation and management with associated migrations. - User creation and management with associated migrations. - Tenant membership management with associated migrations. - Platform user roles management with associated migrations. - Quota management with associated migrations. - Article and template management with associated migrations. - Added HTTP handlers for templates and workspaces. - Created tests for protected and public routes. - Introduced a script to check tenant scope in SQL queries. - Documented task plan for backend completion and frontend foundation.
118 lines
5.4 KiB
SQL
118 lines
5.4 KiB
SQL
-- name: ListBrands :many
|
|
SELECT id, tenant_id, name, description, status, created_at, updated_at
|
|
FROM brands
|
|
WHERE tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL
|
|
ORDER BY created_at DESC;
|
|
|
|
-- name: GetBrandByID :one
|
|
SELECT id, tenant_id, name, description, status, created_at, updated_at
|
|
FROM brands
|
|
WHERE id = sqlc.arg(id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL;
|
|
|
|
-- name: CreateBrand :one
|
|
INSERT INTO brands (tenant_id, name, description, status)
|
|
VALUES (sqlc.arg(tenant_id), sqlc.arg(name), sqlc.arg(description), 'active')
|
|
RETURNING id, created_at;
|
|
|
|
-- name: UpdateBrand :exec
|
|
UPDATE brands SET name = sqlc.arg(name), description = sqlc.arg(description), updated_at = NOW()
|
|
WHERE id = sqlc.arg(id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL;
|
|
|
|
-- name: SoftDeleteBrand :exec
|
|
UPDATE brands SET deleted_at = NOW(), updated_at = NOW()
|
|
WHERE id = sqlc.arg(id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL;
|
|
|
|
-- name: ListKeywords :many
|
|
SELECT id, tenant_id, brand_id, name, status, created_at, updated_at
|
|
FROM brand_keywords
|
|
WHERE brand_id = sqlc.arg(brand_id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL
|
|
ORDER BY created_at DESC;
|
|
|
|
-- name: CreateKeyword :one
|
|
INSERT INTO brand_keywords (tenant_id, brand_id, name, status)
|
|
VALUES (sqlc.arg(tenant_id), sqlc.arg(brand_id), sqlc.arg(name), 'active')
|
|
RETURNING id, created_at;
|
|
|
|
-- name: UpdateKeyword :exec
|
|
UPDATE brand_keywords SET name = sqlc.arg(name), updated_at = NOW()
|
|
WHERE id = sqlc.arg(id) AND brand_id = sqlc.arg(brand_id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL;
|
|
|
|
-- name: SoftDeleteKeyword :exec
|
|
UPDATE brand_keywords SET deleted_at = NOW(), updated_at = NOW()
|
|
WHERE id = sqlc.arg(id) AND brand_id = sqlc.arg(brand_id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL;
|
|
|
|
-- name: SoftDeleteKeywordsByBrand :exec
|
|
UPDATE brand_keywords SET deleted_at = NOW(), updated_at = NOW()
|
|
WHERE brand_id = sqlc.arg(brand_id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL;
|
|
|
|
-- name: ListQuestions :many
|
|
SELECT q.id, q.tenant_id, q.brand_id, q.keyword_id, q.current_version_id, q.status, q.created_at,
|
|
v.question_text, v.version_no
|
|
FROM brand_questions q
|
|
LEFT JOIN brand_question_versions v ON v.id = q.current_version_id
|
|
WHERE q.brand_id = sqlc.arg(brand_id) AND q.tenant_id = sqlc.arg(tenant_id) AND q.deleted_at IS NULL
|
|
AND (sqlc.narg(keyword_id)::bigint IS NULL OR q.keyword_id = sqlc.narg(keyword_id))
|
|
ORDER BY q.created_at DESC;
|
|
|
|
-- name: CreateQuestion :one
|
|
INSERT INTO brand_questions (tenant_id, brand_id, keyword_id, status)
|
|
VALUES (sqlc.arg(tenant_id), sqlc.arg(brand_id), sqlc.arg(keyword_id), 'active')
|
|
RETURNING id;
|
|
|
|
-- name: CreateQuestionVersion :one
|
|
INSERT INTO brand_question_versions (question_id, question_text, question_hash, version_no, is_active)
|
|
VALUES (sqlc.arg(question_id), sqlc.arg(question_text), sqlc.arg(question_hash), sqlc.arg(version_no), true)
|
|
RETURNING id;
|
|
|
|
-- name: UpdateQuestionCurrentVersion :exec
|
|
UPDATE brand_questions SET current_version_id = sqlc.arg(version_id), updated_at = NOW()
|
|
WHERE id = sqlc.arg(id) AND tenant_id = sqlc.arg(tenant_id);
|
|
|
|
-- name: DeactivateQuestionVersion :exec
|
|
UPDATE brand_question_versions SET is_active = false
|
|
WHERE question_id = sqlc.arg(question_id) AND is_active = true;
|
|
|
|
-- name: GetLatestQuestionVersionNo :one
|
|
SELECT COALESCE(MAX(version_no), 0)::INT AS max_version
|
|
FROM brand_question_versions
|
|
WHERE question_id = sqlc.arg(question_id);
|
|
|
|
-- name: SoftDeleteQuestion :exec
|
|
UPDATE brand_questions SET deleted_at = NOW(), updated_at = NOW()
|
|
WHERE id = sqlc.arg(id) AND brand_id = sqlc.arg(brand_id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL;
|
|
|
|
-- name: SoftDeleteQuestionsByBrand :exec
|
|
UPDATE brand_questions SET deleted_at = NOW(), updated_at = NOW()
|
|
WHERE brand_id = sqlc.arg(brand_id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL;
|
|
|
|
-- name: ListQuestionVersions :many
|
|
SELECT v.id, v.question_id, v.question_text, v.question_hash, v.version_no, v.is_active, v.created_at
|
|
FROM brand_question_versions v
|
|
JOIN brand_questions q ON q.id = v.question_id
|
|
WHERE q.brand_id = sqlc.arg(brand_id) AND q.tenant_id = sqlc.arg(tenant_id)
|
|
ORDER BY v.created_at DESC;
|
|
|
|
-- name: ListCompetitors :many
|
|
SELECT id, tenant_id, brand_id, name, website, description, product_lines_json, created_at, updated_at
|
|
FROM competitors
|
|
WHERE brand_id = sqlc.arg(brand_id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL
|
|
ORDER BY created_at DESC;
|
|
|
|
-- name: CreateCompetitor :one
|
|
INSERT INTO competitors (tenant_id, brand_id, name, website, description, product_lines_json)
|
|
VALUES (sqlc.arg(tenant_id), sqlc.arg(brand_id), sqlc.arg(name), sqlc.arg(website), sqlc.arg(description), sqlc.arg(product_lines_json))
|
|
RETURNING id, created_at;
|
|
|
|
-- name: UpdateCompetitor :exec
|
|
UPDATE competitors SET name = sqlc.arg(name), website = sqlc.arg(website),
|
|
description = sqlc.arg(description), product_lines_json = sqlc.arg(product_lines_json), updated_at = NOW()
|
|
WHERE id = sqlc.arg(id) AND brand_id = sqlc.arg(brand_id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL;
|
|
|
|
-- name: SoftDeleteCompetitor :exec
|
|
UPDATE competitors SET deleted_at = NOW(), updated_at = NOW()
|
|
WHERE id = sqlc.arg(id) AND brand_id = sqlc.arg(brand_id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL;
|
|
|
|
-- name: SoftDeleteCompetitorsByBrand :exec
|
|
UPDATE competitors SET deleted_at = NOW(), updated_at = NOW()
|
|
WHERE brand_id = sqlc.arg(brand_id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL;
|