Agenda, Agendador, Configurações
This commit is contained in:
61
migrations/professional_pricing.sql
Normal file
61
migrations/professional_pricing.sql
Normal file
@@ -0,0 +1,61 @@
|
||||
-- migrations/professional_pricing.sql
|
||||
-- Fase 1: Precificação — tabela de preços padrão por profissional
|
||||
-- Execute no Supabase SQL Editor
|
||||
|
||||
-- ─────────────────────────────────────────────────────────────
|
||||
-- 1. Campo price em agenda_eventos
|
||||
-- ─────────────────────────────────────────────────────────────
|
||||
ALTER TABLE agenda_eventos
|
||||
ADD COLUMN IF NOT EXISTS price numeric(10,2);
|
||||
|
||||
-- ─────────────────────────────────────────────────────────────
|
||||
-- 2. Tabela de preços por profissional
|
||||
-- Chave: owner_id + determined_commitment_id (NULL = padrão)
|
||||
-- ─────────────────────────────────────────────────────────────
|
||||
CREATE TABLE IF NOT EXISTS professional_pricing (
|
||||
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
|
||||
owner_id uuid NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
|
||||
tenant_id uuid NOT NULL,
|
||||
determined_commitment_id uuid REFERENCES determined_commitments(id) ON DELETE SET NULL,
|
||||
-- NULL = preço padrão (fallback quando não há match por tipo)
|
||||
price numeric(10,2) NOT NULL,
|
||||
notes text,
|
||||
created_at timestamptz DEFAULT now(),
|
||||
updated_at timestamptz DEFAULT now(),
|
||||
|
||||
CONSTRAINT professional_pricing_owner_commitment_key
|
||||
UNIQUE (owner_id, determined_commitment_id)
|
||||
);
|
||||
|
||||
-- Índice por tenant (listagens do admin)
|
||||
CREATE INDEX IF NOT EXISTS professional_pricing_tenant_idx
|
||||
ON professional_pricing (tenant_id);
|
||||
|
||||
-- ─────────────────────────────────────────────────────────────
|
||||
-- 3. RLS
|
||||
-- ─────────────────────────────────────────────────────────────
|
||||
ALTER TABLE professional_pricing ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Terapeuta lê e escreve seus próprios preços
|
||||
DROP POLICY IF EXISTS "professional_pricing: owner full access" ON professional_pricing;
|
||||
CREATE POLICY "professional_pricing: owner full access"
|
||||
ON professional_pricing
|
||||
FOR ALL
|
||||
USING (owner_id = auth.uid())
|
||||
WITH CHECK (owner_id = auth.uid());
|
||||
|
||||
-- ─────────────────────────────────────────────────────────────
|
||||
-- 4. updated_at automático
|
||||
-- ─────────────────────────────────────────────────────────────
|
||||
CREATE OR REPLACE FUNCTION update_professional_pricing_updated_at()
|
||||
RETURNS TRIGGER LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
NEW.updated_at = now();
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$;
|
||||
|
||||
DROP TRIGGER IF EXISTS trg_professional_pricing_updated_at ON professional_pricing;
|
||||
CREATE TRIGGER trg_professional_pricing_updated_at
|
||||
BEFORE UPDATE ON professional_pricing
|
||||
FOR EACH ROW EXECUTE FUNCTION update_professional_pricing_updated_at();
|
||||
Reference in New Issue
Block a user