49 lines
12 KiB
JavaScript
49 lines
12 KiB
JavaScript
import{adminDb as l}from"./adminDb-aa2cb7bf.js";import{R as A}from"./index-c678e119.js";import{fetchMatchingUserIds as k,applySupabasePagination as x}from"./supabaseTableQuery-e9feb810.js";import{e as C}from"./convert-2ad76038.js";function v(e){if(Array.isArray(e))return e.map(String).filter(Boolean);if(typeof e=="string")try{const t=JSON.parse(e);if(Array.isArray(t))return t.map(String).filter(Boolean)}catch{return e.trim()?[e]:[]}return[]}function E(e){return{...e,duration_minutes:Math.round((Number(e.duration_seconds)||0)/60),kodoblu_voice_url:C(e.kodoblu_voice_url)||"",audio_section_url:C(e.audio_section_url)||"",step_images:v(e.step_images).map(t=>C(t)||t)}}async function D(e){const{data:t,error:r}=await l.from("step_challenges").select("*").eq("id",e).single();if(r)throw r;const{data:a,error:d}=await l.from("step_challenge_steps").select("*").eq("challenge_id",e).order("step_order",{ascending:!0});if(d)throw d;return{...t,steps:(a||[]).map(n=>E(n))}}async function L(e,t){return(await A.post("/admin/step-challenges/with-steps",{...e,owner_id:t??null})).data}async function O(e,t){return(await A.put(`/admin/step-challenges/${e}/with-steps`,t)).data}async function U(e){const{data:t,error:r}=await l.from("user_step_challenges").select("id").eq("challenge_id",e);if(r)throw r;const a=(t||[]).map(o=>o.id);if(a.length){const{error:o}=await l.from("user_step_challenge_steps").delete().in("user_challenge_id",a);if(o)throw o;const{error:s}=await l.from("user_step_challenges").delete().eq("challenge_id",e);if(s)throw s}const{error:d}=await l.from("user_step_challenge_unlocks").delete().eq("challenge_id",e);if(d)throw d;const{error:n}=await l.from("step_challenges").delete().eq("id",e);if(n)throw n}async function B(){const{data:e,error:t}=await l.from("step_challenge_chapters").select("*").order("sort_order",{ascending:!0}).order("id",{ascending:!0});if(t)throw t;return e||[]}async function P(e){var a,d;const{data:t,error:r}=await l.from("step_challenge_chapters").insert([{title:e.title,subtitle:((a=e.subtitle)==null?void 0:a.trim())||null,theme_key:e.theme_key||"meadow",cover_image_url:((d=e.cover_image_url)==null?void 0:d.trim())||null,sort_order:Number(e.sort_order)||0,show:e.show??!0}]).select().single();if(r)throw r;return t}async function R(e,t){const{data:r,error:a}=await l.from("step_challenge_chapters").update({...t,updated_at:new Date().toISOString()}).eq("id",e).select().single();if(a)throw a;return r}async function W(e){const{error:t}=await l.from("step_challenge_chapters").delete().eq("id",e);if(t)throw t}async function Q(e){const{count:t,error:r}=await l.from("step_challenges").select("id",{count:"exact",head:!0}).eq("chapter_id",e);if(r)throw r;return t||0}async function J(e){const{data:t,error:r}=await l.from("step_challenges").select("id, title, show, sort_order, chapter_id, character_image_url, access_type, completion_points, publish_date, publish_time").eq("chapter_id",e).order("sort_order",{ascending:!0}).order("id",{ascending:!0});if(r)throw r;return t||[]}function b(e){if(!e)return{user_name:"نامشخص",user_phone:""};let t="نامشخص";return e!=null&&e.name?t=e.name:e!=null&&e.first_name||e!=null&&e.last_name?t=`${e.first_name||""} ${e.last_name||""}`.trim():e!=null&&e.phone&&(t=e.phone),{user_name:t,user_phone:(e==null?void 0:e.phone)||""}}function S(e,t,r){const a=r.trim().toLowerCase();return a?e.toLowerCase().includes(a)||t.toLowerCase().includes(a):!0}function M(e){if(Array.isArray(e))return e.map(String);if(typeof e=="string")try{const t=JSON.parse(e);if(Array.isArray(t))return t.map(String)}catch{return[]}return[]}async function T(){return(await A.get("/admin/step-challenges/progress-overview")).data}async function j(e){const{data:t,error:r}=await l.from("step_challenges").select("id, title").eq("id",e).single();if(r)throw r;return t}async function H(e){const{data:t,error:r}=await l.from("step_challenge_steps").select(`
|
|
id,
|
|
step_order,
|
|
description,
|
|
is_final_step,
|
|
enable_feedback_text,
|
|
enable_photo_upload,
|
|
enable_voice_upload,
|
|
enable_mcq,
|
|
requires_photo_upload
|
|
`).eq("challenge_id",e).order("step_order",{ascending:!0});if(r)throw r;return(t||[]).map(a=>({id:a.id,step_order:a.step_order,description:(a.description||"").trim(),is_final_step:!!a.is_final_step,enable_feedback_text:!!a.enable_feedback_text,enable_photo_upload:!!(a.enable_photo_upload||a.requires_photo_upload),enable_voice_upload:!!a.enable_voice_upload,enable_mcq:!!a.enable_mcq,label:`مرحله ${a.step_order}`}))}async function V(e,t="",r){const a=[];let d=l.from("user_step_challenge_steps").select(`
|
|
id,
|
|
step_id,
|
|
feedback_text,
|
|
created_at,
|
|
user_step_challenges!inner (
|
|
challenge_id,
|
|
users (name, phone, first_name, last_name)
|
|
)
|
|
`).eq("user_step_challenges.challenge_id",e).not("feedback_text","is",null);r&&(d=d.eq("step_id",r));const{data:n,error:o}=await d;if(o)throw o;for(const s of n||[]){const _=String(s.feedback_text||"").trim();if(!_)continue;const i=Array.isArray(s.user_step_challenges)?s.user_step_challenges[0]:s.user_step_challenges,m=Array.isArray(i==null?void 0:i.users)?i.users[0]:i==null?void 0:i.users,{user_name:u,user_phone:p}=b(m);!S(u,p,t)&&!_.toLowerCase().includes(t.trim().toLowerCase())||a.push({id:`step-${s.id}`,user_name:u,user_phone:p,feedback_text:_,source:"step",step_id:s.step_id,created_at:s.created_at})}return a.sort((s,_)=>String(_.created_at||"").localeCompare(String(s.created_at||""))),a.map((s,_)=>({...s,index:_+1}))}async function X(e,t=""){const{data:r,error:a}=await l.from("user_step_challenges").select("id, completion_feedback, completed_at, created_at, users(name, phone, first_name, last_name)").eq("challenge_id",e).not("completion_feedback","is",null);if(a)throw a;const d=(r||[]).map(n=>{const o=String(n.completion_feedback||"").trim();if(!o)return null;const s=Array.isArray(n.users)?n.users[0]:n.users,{user_name:_,user_phone:i}=b(s);return!S(_,i,t)&&!o.toLowerCase().includes(t.trim().toLowerCase())?null:{id:`completion-${n.id}`,user_name:_,user_phone:i,feedback_text:o,created_at:n.completed_at||n.created_at}}).filter(Boolean);return d.sort((n,o)=>String(o.created_at||"").localeCompare(String(n.created_at||""))),d.map((n,o)=>({...n,index:o+1}))}async function G(e,t="",r,a=1,d=12){const n=Math.max(1,a||1),o=Math.min(48,Math.max(1,d||12));let s=l.from("user_step_challenge_steps").select(`
|
|
id,
|
|
step_id,
|
|
photo_url,
|
|
created_at,
|
|
user_step_challenges!inner (
|
|
challenge_id,
|
|
user_id,
|
|
users (name, phone, first_name, last_name)
|
|
)
|
|
`,{count:"exact"}).eq("user_step_challenges.challenge_id",e).not("photo_url","is",null).neq("photo_url","");if(r&&(s=s.eq("step_id",r)),t.trim()){const p=await k(l,t);if(!p.length)return{rows:[],total:0,page:n,pageSize:o};s=s.in("user_step_challenges.user_id",p.slice(0,MAX_USER_IDS_IN_SEARCH))}s=s.order("created_at",{ascending:!1}),s=x(s,n,o);const{data:_,error:i,count:m}=await s;if(i)throw i;return{rows:(_||[]).map(p=>{const y=String(p.photo_url||"").trim();if(!y)return null;const c=Array.isArray(p.user_step_challenges)?p.user_step_challenges[0]:p.user_step_challenges,h=Array.isArray(c==null?void 0:c.users)?c.users[0]:c==null?void 0:c.users,{user_name:g,user_phone:f}=b(h);return{id:p.id,step_id:p.step_id,photo_url:y,user_name:g,user_phone:f,created_at:p.created_at}}).filter(Boolean),total:m??0,page:n,pageSize:o}}async function I(e,t="",r){let a=l.from("user_step_challenge_steps").select(`
|
|
id,
|
|
step_id,
|
|
voice_url,
|
|
created_at,
|
|
user_step_challenges!inner (
|
|
challenge_id,
|
|
users (name, phone, first_name, last_name)
|
|
)
|
|
`).eq("user_step_challenges.challenge_id",e).not("voice_url","is",null);r&&(a=a.eq("step_id",r));const{data:d,error:n}=await a;if(n)throw n;const o=(d||[]).map(s=>{const _=String(s.voice_url||"").trim();if(!_)return null;const i=Array.isArray(s.user_step_challenges)?s.user_step_challenges[0]:s.user_step_challenges,m=Array.isArray(i==null?void 0:i.users)?i.users[0]:i==null?void 0:i.users,{user_name:u,user_phone:p}=b(m);return S(u,p,t)?{id:s.id,step_id:s.step_id,voice_url:_,user_name:u,user_phone:p,created_at:s.created_at}:null}).filter(Boolean);return o.sort((s,_)=>String(_.created_at||"").localeCompare(String(s.created_at||""))),o.map((s,_)=>({...s,index:_+1}))}async function K(e,t="",r){let a=l.from("step_challenge_steps").select("id, step_order, mcq_question, mcq_options, mcq_correct_index, enable_mcq").eq("challenge_id",e).eq("enable_mcq",!0).order("step_order",{ascending:!0});r&&(a=a.eq("id",r));const{data:d,error:n}=await a;if(n)throw n;let o=l.from("user_step_challenge_steps").select(`
|
|
id,
|
|
step_id,
|
|
mcq_answer,
|
|
created_at,
|
|
user_step_challenges!inner (
|
|
challenge_id,
|
|
users (name, phone, first_name, last_name)
|
|
)
|
|
`).eq("user_step_challenges.challenge_id",e).not("mcq_answer","is",null);r&&(o=o.eq("step_id",r));const{data:s,error:_}=await o;if(_)throw _;return(d||[]).map(i=>{const m=M(i.mcq_options),u=(s||[]).filter(c=>c.step_id===i.id&&c.mcq_answer!==null&&c.mcq_answer!==void 0).map(c=>{const h=Array.isArray(c.user_step_challenges)?c.user_step_challenges[0]:c.user_step_challenges,g=Array.isArray(h==null?void 0:h.users)?h.users[0]:h==null?void 0:h.users,{user_name:f,user_phone:w}=b(g),q=Number(c.mcq_answer);return{id:c.id,user_name:f,user_phone:w,mcq_answer:q,answer_label:m[q]||`گزینه ${q+1}`,created_at:c.created_at}}).filter(c=>S(c.user_name,c.user_phone,t)).sort((c,h)=>String(h.created_at||"").localeCompare(String(c.created_at||""))).map((c,h)=>({...c,index:h+1})),p=u.length,y=m.map((c,h)=>{const g=u.filter(w=>w.mcq_answer===h).length,f=p>0?Math.round(g/p*1e3)/10:0;return{optionIndex:h,label:c,count:g,percent:f}});return{step_id:i.id,step_order:i.step_order,question:i.mcq_question||`سوال مرحله ${i.step_order}`,options:m,correct_index:i.mcq_correct_index,answers:u,option_stats:y,total_answers:p}})}async function Y(){const{data:e,error:t}=await l.from("step_challenge_config").select("*").eq("id",1).maybeSingle();if(t)throw t;return e||{id:1,rules_aparat_embed_code:null,show_completion_feedback:!0}}async function Z(e){const{data:t,error:r}=await l.from("step_challenge_config").upsert({id:1,rules_aparat_embed_code:e.rules_aparat_embed_code||null,show_completion_feedback:e.show_completion_feedback!==!1,updated_at:new Date().toISOString()}).select().single();if(r)throw r;return t}async function ee(e){const t=Math.max(1,(e==null?void 0:e.page)||1),r=Math.max(1,(e==null?void 0:e.pageSize)||20),a=(t-1)*r,d=a+r-1,n=((e==null?void 0:e.search)||"").trim();let o=l.from("step_challenge_suggestions").select("id, user_id, title, created_at, users(name, phone, first_name, last_name), step_challenge_suggestion_steps(id, suggestion_id, step_order, description, created_at)",{count:"exact"}).order("created_at",{ascending:!1}).range(a,d);if(n){const u=await k(l,n);o=o.or(buildIlikeOrFilter(["title"],n,u))}const{data:s,error:_,count:i}=await o;if(_)throw _;return{rows:(s||[]).map(u=>{const p=Array.isArray(u.users)?u.users[0]:u.users,{user_name:y,user_phone:c}=b(p),h=(Array.isArray(u.step_challenge_suggestion_steps)?u.step_challenge_suggestion_steps:[]).map(g=>({id:Number(g.id),suggestion_id:Number(g.suggestion_id),step_order:Number(g.step_order),description:String(g.description||""),created_at:g.created_at})).sort((g,f)=>g.step_order-f.step_order);return{id:Number(u.id),user_id:u.user_id,title:String(u.title||""),created_at:u.created_at,user_name:y,user_phone:c,steps:h}}),total:i||0}}export{J as a,P as b,Q as c,T as d,Y as e,ee as f,B as g,O as h,L as i,W as j,U as k,D as l,V as m,I as n,K as o,X as p,j as q,H as r,Z as s,G as t,R as u};
|