File size: 4,567 Bytes
7be37dc
 
 
 
 
 
c557ee8
7be37dc
8c33d0f
e9eb3ca
8c33d0f
c557ee8
8c33d0f
 
c557ee8
7be37dc
 
 
 
 
 
 
5b933bc
7be37dc
 
5b933bc
c557ee8
7be37dc
 
019355b
7be37dc
 
5b933bc
c557ee8
 
 
 
 
 
 
 
 
5a58792
7be37dc
 
5a58792
 
c557ee8
7be37dc
 
 
5a58792
 
 
 
 
 
 
c557ee8
 
 
7be37dc
5a58792
7be37dc
 
 
 
 
 
 
5a58792
 
c557ee8
7be37dc
5a58792
5b933bc
c557ee8
 
 
5b933bc
7be37dc
5b933bc
7be37dc
5b933bc
c557ee8
 
 
 
 
 
5a58792
1b21de5
7be37dc
 
 
 
c557ee8
7be37dc
 
1b21de5
 
c557ee8
5a58792
019355b
c557ee8
019355b
6b84275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7be37dc
c557ee8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from supabase import create_client, Client
import os
import re
import json

# ---------------------------------------------
# تحميل مفاتيح Supabase من Secrets
# ---------------------------------------------
SUPABASE_URL = os.environ.get("SUPABASE_URL")
SUPABASE_KEY = os.environ.get("SUPABASE_SERVICE_ROLE_KEY")

if not SUPABASE_URL or not SUPABASE_KEY:
    raise ValueError("❌ لم يتم العثور على مفاتيح Supabase في Secrets")

# إنشاء العميل
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)


# ---------------------------------------------
# حفظ / استبدال القانون في قاعدة البيانات
# ---------------------------------------------
def save_law_to_supabase(law_json: dict):
    """
    law_json: dict يحتوي على المفاتيح
        title, preamble, number, year, sections
    """

    title      = law_json.get("title", "عنوان غير معروف")
    preamble   = law_json.get("preamble", "")
    law_number = law_json.get("number")
    law_year   = law_json.get("year")
    sections   = law_json.get("sections", [])

    # ---------------------------------------------------------
    # 1) التحقق من وجود القانون سابقًا
    # ---------------------------------------------------------
    existing = (
        supabase.table("laws")
        .select("id")
        .eq("title", title)
        .execute()
    )

    if existing.data:
        law_id = existing.data[0]["id"]
        print(f"⚠ القانون موجود مسبقاً، يتم حذفه: {title}")

        # حذف المواد التابعة للأقسام
        secs = supabase.table("sections").select("id").eq("law_id", law_id).execute()
        for s in secs.data:
            supabase.table("articles").delete().eq("chapter_id", s["id"]).execute()

        # حذف الأقسام
        supabase.table("sections").delete().eq("law_id", law_id).execute()

        # حذف القانون نفسه
        supabase.table("laws").delete().eq("id", law_id).execute()

    # ---------------------------------------------------------
    # 2) إضافة القانون الجديد
    # ---------------------------------------------------------
    res_law = (
        supabase.table("laws")
        .insert({
            "title":      title or "",
            "number_law": law_number,
            "year_law":   law_year,
            "preamble":   preamble or "",
            "law_type":   "def",
        })
        .execute()
    )

    law_id = res_law.data[0]["id"]
    print(f"✔ تم إدراج القانون الجديد: {title}")

    # ---------------------------------------------------------
    # 3) إضافة الأقسام والمواد
    # ---------------------------------------------------------
    for sec in sections:
        sec_title   = sec.get("title", "")
        sec_content = sec.get("content", "")
        sec_tables  = sec.get("tables", [])

        # استخراج رقم القسم من العنوان
        chapter_number = 0
        match = re.search(r"\d+", sec_title or "")
        if match:
            chapter_number = int(match.group(0))

        res_sec = (
            supabase.table("sections")
            .insert({
                "law_id":         law_id,
                "title":          sec_title or "",
                "content":        sec_content or "",
                "chapter_number": chapter_number,
                "tables":         sec_tables or [],
            })
            .execute()
        )

        section_id = res_sec.data[0]["id"]

        # ----------------------- إضافة المواد -----------------------
        for art in sec.get("articles", []):
            article_number = int(art["number"]) if art.get("number") else None
            article_content = (art.get("text") or "").strip()
            article_tag = (art.get("tag") or "").strip()
            article_tables = art.get("tables", [])

            # ❌ تجاهل المادة إذا كانت فارغة تمامًا
            if not article_content and not article_tag:
                continue

            supabase.table("articles").insert({
                "chapter_id": section_id,
                "article_number": article_number,
                "content": article_content,
                "tag": article_tag,
                "lowid": law_id,
                "tables": article_tables or [],
            }).execute()

    print(f"✔ تم حفظ جميع الأقسام والمواد للقانون: {title}")