2 lines
6.5 KiB
JavaScript
2 lines
6.5 KiB
JavaScript
import{_ as N,I as f}from"./index-69112d2d.js";async function k(){const{data:e,error:r}=await f.from("matchs").select("*");if(r)throw r;return e}async function z(e,r){const{data:i,error:n}=await f.from("matchs").update(r).eq("id",e).select();if(n)throw n;return i}async function V(e){const{data:r,error:i}=await f.from("matchs").select("*").eq("id",e).single();if(i)throw i;const{data:n,error:s}=await f.from("questions").select("*").eq("match_id",e);if(s)throw s;return{...r,questions:n||[]}}async function P(e){console.log("addMatchWithQuestions payload:",e);const{data:r,error:i}=await f.from("matchs").insert([e.match]).select().single();if(console.log("Match insert result:",{matchData:r,matchError:i}),i)throw console.error("Match insert error:",i),i;for(const n of e.questions){const{data:s,error:m}=await f.from("questions").insert([{match_id:r.id,question:n.question,options:n.options,correct_answer:n.correct_answer}]).select().single();if(m)throw m;console.log("Question created:",s)}return r}async function R(e,r){const{error:i}=await f.from("matchs").update(r.match).eq("id",e);if(i)throw i;const{data:n}=await f.from("questions").select("id").eq("match_id",e);if(n)for(const s of n)await f.from("questions").delete().eq("id",s.id);for(const s of r.questions){const{data:m,error:a}=await f.from("questions").insert([{match_id:e,question:s.question,options:s.options,correct_answer:s.correct_answer}]).select().single();if(a)throw a;console.log("Question updated:",m)}return{id:e}}async function O(e){await f.from("questions").delete().eq("match_id",e);const{error:r}=await f.from("matchs").delete().eq("id",e);if(r)throw r}async function j(e){const{TableEngineRequest:r}=await N(()=>import("./buildSupabaseQuery-d5395bbc.js"),["assets/buildSupabaseQuery-d5395bbc.js","assets/index-69112d2d.js","assets/index-44d4db91.css"]),i={...e};!i.sort&&!i.order&&(i.order="-id");const n=await r(i,"matchs");try{if(Array.isArray(n==null?void 0:n.rows)){const s=i.page||1,m=i.limit||200,a=(s-1)*m,h=n.rows.map((o,_)=>({id:o.id,title:o.title??"",description:o.description??"",created_at:o.created_at??"",index:a+_,...o}));return{...n,rows:h}}}catch(s){console.log("[getMatchTable] normalize error:",s)}return n}async function I(e){var r,i,n,s,m;try{const{limit:a,filters:h,page:o,sort:_,order:M,search:T}=e;let p=f.from("answers").select("*, matchs!match_id(id, title, description, minimum_score), users!user_id(id, first_name, last_name, phone, name, email, username)",{count:"exact"});h&&Array.isArray(h)&&h.forEach(t=>{const{field:c,op:u,value:d,hidden:l}=t;l||(c==="match_id"&&u==="eq"?p=p.eq("match_id",d):c==="match_id"&&u==="in"&&(p=p.in("match_id",Array.isArray(d)?d:[d])))});const{data:y,error:S,count:A}=await p;if(S)throw console.error("[getWinnersTable] Supabase query error:",S),S;if(!y||y.length===0)return{rows:[],total:0,query:{...e,total:0}};let g=y.map(t=>{const u=(Array.isArray(t.matchs)?t.matchs[0]:t.matchs)||{},l=(Array.isArray(t.users)?t.users[0]:t.users)||{},w=u.minimum_score?Number(u.minimum_score):0,C=t.score?Number(t.score):0;let q="نامشخص";return l&&Object.keys(l).length>0&&(l.name?q=l.name:l.first_name||l.last_name?q=`${l.first_name||""} ${l.last_name||""}`.trim():l.phone?q=l.phone:l.email?q=l.email:l.username&&(q=l.username)),{id:t.id,match_id:t.match_id,user_id:t.user_id,score:t.score,match_title:u.title||"",match_description:u.description||"",match_minimum_score:u.minimum_score||null,user_name:q,user_mobile:(l==null?void 0:l.phone)||"",created_at:t.created_at||"",_match:u,_user:l,_userScore:C,_minimumScore:w}}).filter(t=>t._userScore>=t._minimumScore).map(t=>{const{_match:c,_user:u,_userScore:d,_minimumScore:l,...w}=t;return w});if(e.search){const t=e.search.toLowerCase();g=g.filter(c=>(c.match_title||"").toLowerCase().includes(t)||(c.match_description||"").toLowerCase().includes(t)||(c.user_name||"").toLowerCase().includes(t)||(c.user_mobile||"").toLowerCase().includes(t)||String(c.score||"").includes(t))}e.filters&&e.filters.length>0&&(g=g.filter(t=>e.filters.every(c=>{if(!c.field||c.hidden)return!0;const u=t[c.field],d=c.value;if(d=
|