Files
machartaa_admin_dist/dist/assets/competition-13c588d8.js
T
2026-06-23 17:53:22 +03:30

2 lines
4.1 KiB
JavaScript

import{_,H as c}from"./index-eebfe383.js";async function p(){const{data:e,error:s}=await c.from("matchs").select("*");if(s)throw s;return e}async function M(e,s){const{data:t,error:r}=await c.from("matchs").update(s).eq("id",e).select();if(r)throw r;return t}async function E(e){const{data:s,error:t}=await c.from("matchs").select("*").eq("id",e).single();if(t)throw t;const{data:r,error:i}=await c.from("questions").select("*").eq("match_id",e);if(i)throw i;return{...s,questions:r||[]}}async function y(e){console.log("addMatchWithQuestions payload:",e);const{data:s,error:t}=await c.from("matchs").insert([e.match]).select().single();if(console.log("Match insert result:",{matchData:s,matchError:t}),t)throw console.error("Match insert error:",t),t;for(const r of e.questions){const{data:i,error:d}=await c.from("questions").insert([{match_id:s.id,question:r.question,options:r.options,correct_answer:r.correct_answer}]).select().single();if(d)throw d;console.log("Question created:",i)}return s}async function b(e,s){const{error:t}=await c.from("matchs").update(s.match).eq("id",e);if(t)throw t;const{data:r}=await c.from("questions").select("id").eq("match_id",e);if(r)for(const i of r)await c.from("questions").delete().eq("id",i.id);for(const i of s.questions){const{data:d,error:l}=await c.from("questions").insert([{match_id:e,question:i.question,options:i.options,correct_answer:i.correct_answer}]).select().single();if(l)throw l;console.log("Question updated:",d)}return{id:e}}async function S(e){await c.from("questions").delete().eq("match_id",e);const{error:s}=await c.from("matchs").delete().eq("id",e);if(s)throw s}async function D(e){const{TableEngineRequest:s}=await _(()=>import("./buildSupabaseQuery-ca1b6752.js"),["assets/buildSupabaseQuery-ca1b6752.js","assets/index-eebfe383.js","assets/index-f59f1398.css","assets/supabaseTableQuery-95762ff3.js"]),t={...e};!t.sort&&!t.order&&(t.order="-id");const r=await s(t,"matchs");try{if(Array.isArray(r==null?void 0:r.rows)){const i=t.page||1,d=t.limit||200,l=(i-1)*d,h=r.rows.map((u,f)=>({id:u.id,title:u.title??"",description:u.description??"",created_at:u.created_at??"",index:l+f,...u}));return{...r,rows:h}}}catch(i){console.log("[getMatchTable] normalize error:",i)}return r}async function W(e){const{TableEngineRequest:s}=await _(()=>import("./buildSupabaseQuery-ca1b6752.js"),["assets/buildSupabaseQuery-ca1b6752.js","assets/index-eebfe383.js","assets/index-f59f1398.css","assets/supabaseTableQuery-95762ff3.js"]),t={...e};return!t.sort&&!t.order&&(t.order="-created_at"),s(t,"competition_winners")}async function Q(e){const s=e.map(i=>{const d={match_id:i.match_id,rank:i.rank};return i.user_id!==void 0?d.user_id=i.user_id:i.winner_id!==void 0&&(d.user_id=i.winner_id),d}),{data:t,error:r}=await c.from("winners_match").insert(s).select();if(r)throw r;return t}async function R(e){const{data:s,error:t}=await c.from("winners_match").select("*").eq("match_id",e).order("rank",{ascending:!0});if(t)throw t;if(!s||s.length===0)return[];const r=[...new Set(s.map(o=>o.user_id||o.winner_id).filter(Boolean))];if(r.length===0)return[];const[i,d]=await Promise.all([c.from("users").select("*").in("id",r),c.from("answers").select("user_id, score").eq("match_id",e).in("user_id",r)]),l=i.data||[],h=d.data||[],u=new Map;l.forEach(o=>{const a=o.id;if(u.set(a,o),u.set(String(a),o),typeof a=="string"){const n=a.toLowerCase().replace(/-/g,"");u.set(n,o),a.includes("-")&&(u.set(a.replace(/-/g,""),o),u.set(a.replace(/-/g,"").toLowerCase(),o))}});const f=new Map;return h.forEach(o=>{const a=o.user_id;f.set(a,o.score||0),f.set(String(a),o.score||0)}),s.map(o=>{const a=o.user_id||o.winner_id,n=u.get(a)||u.get(String(a))||l.find(g=>String(g.id)===String(a))||{},w=f.get(a)||f.get(String(a))||0;let m="نامشخص";return n&&Object.keys(n).length>0&&(n.name?m=n.name:n.first_name||n.last_name?m=`${n.first_name||""} ${n.last_name||""}`.trim():n.phone?m=n.phone:n.email?m=n.email:n.username&&(m=n.username)),{...o,user_name:m,user_mobile:(n==null?void 0:n.phone)||"",user_email:(n==null?void 0:n.email)||"",score:Number(w)||0}})}export{p as a,D as b,y as c,E as d,M as e,S as f,W as g,R as h,Q as s,b as u};