Files
machartaa_admin_dist/dist/assets/stepChallenge-01d8dd91.js
T
2026-08-11 17:51:41 +03:30

49 lines
15 KiB
JavaScript

import{G as l}from"./index-f83afa83.js";import{f as C,g as A,M as v,d as E}from"./supabaseTableQuery-95762ff3.js";import{e as b}from"./convert-2ad76038.js";function N(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 M(e){return{...e,duration_minutes:Math.round((Number(e.duration_seconds)||0)/60),kodoblu_voice_url:b(e.kodoblu_voice_url)||"",audio_section_url:b(e.audio_section_url)||"",step_images:N(e.step_images).map(t=>b(t)||t)}}function k(e,t,r){var a,c,s,i,o;return{challenge_id:e,step_order:r,description:t.description||"",duration_seconds:Math.max(0,Math.round(Number(t.duration_minutes)*60)||0),aparat_embed_code:((a=t.aparat_embed_code)==null?void 0:a.trim())||null,kodoblu_voice_url:b((c=t.kodoblu_voice_url)==null?void 0:c.trim())||null,step_images:(t.step_images||[]).map(n=>b(n)||n).filter(Boolean),is_final_step:!!t.is_final_step,enable_feedback_text:!!t.enable_feedback_text,require_feedback_text:!!t.require_feedback_text,feedback_text_title:((s=t.feedback_text_title)==null?void 0:s.trim())||null,feedback_text_placeholder:((i=t.feedback_text_placeholder)==null?void 0:i.trim())||null,enable_photo_upload:!!t.enable_photo_upload,require_photo_upload:!!t.enable_photo_upload&&!!t.require_photo_upload,enable_voice_upload:!!t.enable_voice_upload,require_voice_upload:!!t.require_voice_upload,enable_mcq:!!t.enable_mcq,require_mcq:!!t.require_mcq,mcq_question:t.mcq_question||null,mcq_options:(t.mcq_options||[]).filter(n=>n.trim()),mcq_correct_index:t.enable_mcq?Number(t.mcq_correct_index??0):null,enable_audio_section:!!t.enable_audio_section,audio_section_url:b((o=t.audio_section_url)==null?void 0:o.trim())||null,punishment_description:t.punishment_description||"",requires_photo_upload:!!t.enable_photo_upload&&!!t.require_photo_upload,updated_at:new Date().toISOString()}}function P(e,t){return t.map((r,a)=>k(e,r,a+1))}async function z(e){const{data:t,error:r}=await l.from("step_challenges").select("*").eq("id",e).single();if(r)throw r;const{data:a,error:c}=await l.from("step_challenge_steps").select("*").eq("challenge_id",e).order("step_order",{ascending:!0});if(c)throw c;return{...t,steps:(a||[]).map(s=>M(s))}}async function B(e,t){const r={title:e.title,character_image_url:e.character_image_url,show:e.show??!1,access_type:e.access_type??"free",sort_order:e.sort_order??0,completion_points:Number(e.completion_points)||50,chapter_id:e.chapter_id??null};t&&(r.owner_id=t);const{data:a,error:c}=await l.from("step_challenges").insert([r]).select().single();if(c)throw c;const s=P(a.id,e.steps),{error:i}=await l.from("step_challenge_steps").insert(s);if(i)throw i;return a}async function L(e,t){const{error:r}=await l.from("step_challenges").update({title:t.title,character_image_url:t.character_image_url,show:t.show??!1,access_type:t.access_type??"free",sort_order:t.sort_order??0,completion_points:Number(t.completion_points)||50,chapter_id:t.chapter_id??null,updated_at:new Date().toISOString()}).eq("id",e);if(r)throw r;const{data:a,error:c}=await l.from("step_challenge_steps").select("id").eq("challenge_id",e);if(c)throw c;const s=new Set((a||[]).map(n=>n.id)),i=t.steps.map(n=>n.id).filter(n=>typeof n=="number"&&s.has(n)),o=[...s].filter(n=>!i.includes(n));if(o.length){const{error:n}=await l.from("user_step_challenge_steps").delete().in("step_id",o);if(n)throw n;const{error:_}=await l.from("step_challenge_steps").delete().in("id",o);if(_)throw _}for(const n of i){const{error:_}=await l.from("step_challenge_steps").update({step_order:-n,updated_at:new Date().toISOString()}).eq("id",n);if(_)throw _}for(let n=0;n<t.steps.length;n++){const _=t.steps[n],g=k(e,_,n+1);if(_.id&&s.has(_.id)){const{error:p}=await l.from("step_challenge_steps").update(g).eq("id",_.id);if(p)throw p;continue}const{error:u}=await l.from("step_challenge_steps").insert(g);if(u)throw u}return{id:e}}async function $(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(i=>i.id);if(a.length){const{error:i}=await l.from("user_step_challenge_steps").delete().in("user_challenge_id",a);if(i)throw i;const{error:o}=await l.from("user_step_challenges").delete().eq("challenge_id",e);if(o)throw o}const{error:c}=await l.from("user_step_challenge_unlocks").delete().eq("challenge_id",e);if(c)throw c;const{error:s}=await l.from("step_challenges").delete().eq("id",e);if(s)throw s}async function I(){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 W(e){var a,c;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:((c=e.cover_image_url)==null?void 0:c.trim())||null,sort_order:Number(e.sort_order)||0,show:e.show??!0}]).select().single();if(r)throw r;return t}async function Q(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 R(e){const{error:t}=await l.from("step_challenge_chapters").delete().eq("id",e);if(t)throw t}async function J(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 T(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").eq("chapter_id",e).order("sort_order",{ascending:!0}).order("id",{ascending:!0});if(r)throw r;return t||[]}function w(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 F(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 j(){const{data:e,error:t}=await l.from("step_challenges").select("id, title, character_image_url, sort_order, show, chapter_id").order("sort_order",{ascending:!0}).order("id",{ascending:!1});if(t)throw t;const{data:r,error:a}=await l.from("user_step_challenges").select("challenge_id, user_id");if(a)throw a;const c={};for(const s of r||[]){const i=Number(s.challenge_id);c[i]||(c[i]={uniqueUsers:new Set,timesPerformed:0}),c[i].timesPerformed+=1,s.user_id&&c[i].uniqueUsers.add(String(s.user_id))}return(e||[]).map(s=>{const i=c[s.id];return{id:s.id,title:s.title,character_image_url:s.character_image_url,sort_order:s.sort_order,show:s.show,chapter_id:s.chapter_id??null,unique_users_count:i?i.uniqueUsers.size:0,times_performed:i?i.timesPerformed:0}})}async function G(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 c=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&&(c=c.eq("step_id",r));const{data:s,error:i}=await c;if(i)throw i;for(const o of s||[]){const n=String(o.feedback_text||"").trim();if(!n)continue;const _=Array.isArray(o.user_step_challenges)?o.user_step_challenges[0]:o.user_step_challenges,g=Array.isArray(_==null?void 0:_.users)?_.users[0]:_==null?void 0:_.users,{user_name:u,user_phone:p}=w(g);!S(u,p,t)&&!n.toLowerCase().includes(t.trim().toLowerCase())||a.push({id:`step-${o.id}`,user_name:u,user_phone:p,feedback_text:n,source:"step",step_id:o.step_id,created_at:o.created_at})}return a.sort((o,n)=>String(n.created_at||"").localeCompare(String(o.created_at||""))),a.map((o,n)=>({...o,index:n+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 c=(r||[]).map(s=>{const i=String(s.completion_feedback||"").trim();if(!i)return null;const o=Array.isArray(s.users)?s.users[0]:s.users,{user_name:n,user_phone:_}=w(o);return!S(n,_,t)&&!i.toLowerCase().includes(t.trim().toLowerCase())?null:{id:`completion-${s.id}`,user_name:n,user_phone:_,feedback_text:i,created_at:s.completed_at||s.created_at}}).filter(Boolean);return c.sort((s,i)=>String(i.created_at||"").localeCompare(String(s.created_at||""))),c.map((s,i)=>({...s,index:i+1}))}async function K(e,t="",r,a=1,c=12){const s=Math.max(1,a||1),i=Math.min(48,Math.max(1,c||12));let o=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&&(o=o.eq("step_id",r)),t.trim()){const p=await C(l,t);if(!p.length)return{rows:[],total:0,page:s,pageSize:i};o=o.in("user_step_challenges.user_id",p.slice(0,v))}o=o.order("created_at",{ascending:!1}),o=E(o,s,i);const{data:n,error:_,count:g}=await o;if(_)throw _;return{rows:(n||[]).map(p=>{const q=String(p.photo_url||"").trim();if(!q)return null;const d=Array.isArray(p.user_step_challenges)?p.user_step_challenges[0]:p.user_step_challenges,h=Array.isArray(d==null?void 0:d.users)?d.users[0]:d==null?void 0:d.users,{user_name:m,user_phone:f}=w(h);return{id:p.id,step_id:p.step_id,photo_url:q,user_name:m,user_phone:f,created_at:p.created_at}}).filter(Boolean),total:g??0,page:s,pageSize:i}}async function Y(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:c,error:s}=await a;if(s)throw s;const i=(c||[]).map(o=>{const n=String(o.voice_url||"").trim();if(!n)return null;const _=Array.isArray(o.user_step_challenges)?o.user_step_challenges[0]:o.user_step_challenges,g=Array.isArray(_==null?void 0:_.users)?_.users[0]:_==null?void 0:_.users,{user_name:u,user_phone:p}=w(g);return S(u,p,t)?{id:o.id,step_id:o.step_id,voice_url:n,user_name:u,user_phone:p,created_at:o.created_at}:null}).filter(Boolean);return i.sort((o,n)=>String(n.created_at||"").localeCompare(String(o.created_at||""))),i.map((o,n)=>({...o,index:n+1}))}async function Z(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:c,error:s}=await a;if(s)throw s;let i=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&&(i=i.eq("step_id",r));const{data:o,error:n}=await i;if(n)throw n;return(c||[]).map(_=>{const g=F(_.mcq_options),u=(o||[]).filter(d=>d.step_id===_.id&&d.mcq_answer!==null&&d.mcq_answer!==void 0).map(d=>{const h=Array.isArray(d.user_step_challenges)?d.user_step_challenges[0]:d.user_step_challenges,m=Array.isArray(h==null?void 0:h.users)?h.users[0]:h==null?void 0:h.users,{user_name:f,user_phone:y}=w(m),x=Number(d.mcq_answer);return{id:d.id,user_name:f,user_phone:y,mcq_answer:x,answer_label:g[x]||`گزینه ${x+1}`,created_at:d.created_at}}).filter(d=>S(d.user_name,d.user_phone,t)).sort((d,h)=>String(h.created_at||"").localeCompare(String(d.created_at||""))).map((d,h)=>({...d,index:h+1})),p=u.length,q=g.map((d,h)=>{const m=u.filter(y=>y.mcq_answer===h).length,f=p>0?Math.round(m/p*1e3)/10:0;return{optionIndex:h,label:d,count:m,percent:f}});return{step_id:_.id,step_order:_.step_order,question:_.mcq_question||`سوال مرحله ${_.step_order}`,options:g,correct_index:_.mcq_correct_index,answers:u,option_stats:q,total_answers:p}})}async function ee(){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}}async function te(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,updated_at:new Date().toISOString()}).select().single();if(r)throw r;return t}async function re(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,c=a+r-1,s=((e==null?void 0:e.search)||"").trim();let i=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,c);if(s){const u=await C(l,s);i=i.or(A(["title"],s,u))}const{data:o,error:n,count:_}=await i;if(n)throw n;return{rows:(o||[]).map(u=>{const p=Array.isArray(u.users)?u.users[0]:u.users,{user_name:q,user_phone:d}=w(p),h=(Array.isArray(u.step_challenge_suggestion_steps)?u.step_challenge_suggestion_steps:[]).map(m=>({id:Number(m.id),suggestion_id:Number(m.suggestion_id),step_order:Number(m.step_order),description:String(m.description||""),created_at:m.created_at})).sort((m,f)=>m.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:q,user_phone:d,steps:h}}),total:_||0}}export{T as a,W as b,J as c,j as d,ee as e,re as f,I as g,L as h,B as i,R as j,$ as k,z as l,V as m,Y as n,Z as o,X as p,G as q,H as r,te as s,K as t,Q as u};