48 lines
13 KiB
JavaScript
48 lines
13 KiB
JavaScript
import{G as l}from"./index-02897754.js";import{e as f}from"./convert-2ad76038.js";function k(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 A(e){return{...e,duration_minutes:Math.round((Number(e.duration_seconds)||60)/60),kodoblu_voice_url:f(e.kodoblu_voice_url)||"",audio_section_url:f(e.audio_section_url)||"",step_images:k(e.step_images).map(t=>f(t)||t)}}function C(e,t,a){var r,c,n,_,o;return{challenge_id:e,step_order:a,description:t.description||"",duration_seconds:Math.max(1,Math.round(Number(t.duration_minutes)*60)),aparat_embed_code:((r=t.aparat_embed_code)==null?void 0:r.trim())||null,kodoblu_voice_url:f((c=t.kodoblu_voice_url)==null?void 0:c.trim())||null,step_images:(t.step_images||[]).map(s=>f(s)||s).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:((n=t.feedback_text_title)==null?void 0:n.trim())||null,feedback_text_placeholder:((_=t.feedback_text_placeholder)==null?void 0:_.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(s=>s.trim()),mcq_correct_index:t.enable_mcq?Number(t.mcq_correct_index??0):null,enable_audio_section:!!t.enable_audio_section,audio_section_url:f((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 v(e,t){return t.map((a,r)=>C(e,a,r+1))}async function O(e){const{data:t,error:a}=await l.from("step_challenges").select("*").eq("id",e).single();if(a)throw a;const{data:r,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:(r||[]).map(n=>A(n))}}async function D(e,t){const a={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&&(a.owner_id=t);const{data:r,error:c}=await l.from("step_challenges").insert([a]).select().single();if(c)throw c;const n=v(r.id,e.steps),{error:_}=await l.from("step_challenge_steps").insert(n);if(_)throw _;return r}async function P(e,t){const{error:a}=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(a)throw a;const{data:r,error:c}=await l.from("step_challenge_steps").select("id").eq("challenge_id",e);if(c)throw c;const n=new Set((r||[]).map(s=>s.id)),_=t.steps.map(s=>s.id).filter(s=>typeof s=="number"&&n.has(s)),o=[...n].filter(s=>!_.includes(s));if(o.length){const{error:s}=await l.from("user_step_challenge_steps").delete().in("step_id",o);if(s)throw s;const{error:i}=await l.from("step_challenge_steps").delete().in("id",o);if(i)throw i}for(const s of _){const{error:i}=await l.from("step_challenge_steps").update({step_order:-s,updated_at:new Date().toISOString()}).eq("id",s);if(i)throw i}for(let s=0;s<t.steps.length;s++){const i=t.steps[s],m=C(e,i,s+1);if(i.id&&n.has(i.id)){const{error:p}=await l.from("step_challenge_steps").update(m).eq("id",i.id);if(p)throw p;continue}const{error:u}=await l.from("step_challenge_steps").insert(m);if(u)throw u}return{id:e}}async function B(e){const{data:t,error:a}=await l.from("user_step_challenges").select("id").eq("challenge_id",e);if(a)throw a;const r=(t||[]).map(_=>_.id);if(r.length){const{error:_}=await l.from("user_step_challenge_steps").delete().in("user_challenge_id",r);if(_)throw _;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:n}=await l.from("step_challenges").delete().eq("id",e);if(n)throw n}async function L(){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 U(e){var r,c;const{data:t,error:a}=await l.from("step_challenge_chapters").insert([{title:e.title,subtitle:((r=e.subtitle)==null?void 0:r.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(a)throw a;return t}async function $(e,t){const{data:a,error:r}=await l.from("step_challenge_chapters").update({...t,updated_at:new Date().toISOString()}).eq("id",e).select().single();if(r)throw r;return a}async function M(e){const{error:t}=await l.from("step_challenge_chapters").delete().eq("id",e);if(t)throw t}async function W(e){const{count:t,error:a}=await l.from("step_challenges").select("id",{count:"exact",head:!0}).eq("chapter_id",e);if(a)throw a;return t||0}async function z(e){const{data:t,error:a}=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(a)throw a;return t||[]}function g(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 q(e,t,a){const r=a.trim().toLowerCase();return r?e.toLowerCase().includes(r)||t.toLowerCase().includes(r):!0}function E(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 Q(){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:a,error:r}=await l.from("user_step_challenges").select("challenge_id, user_id");if(r)throw r;const c={};for(const n of a||[]){const _=Number(n.challenge_id);c[_]||(c[_]={uniqueUsers:new Set,timesPerformed:0}),c[_].timesPerformed+=1,n.user_id&&c[_].uniqueUsers.add(String(n.user_id))}return(e||[]).map(n=>{const _=c[n.id];return{id:n.id,title:n.title,character_image_url:n.character_image_url,sort_order:n.sort_order,show:n.show,chapter_id:n.chapter_id??null,unique_users_count:_?_.uniqueUsers.size:0,times_performed:_?_.timesPerformed:0}})}async function J(e){const{data:t,error:a}=await l.from("step_challenges").select("id, title").eq("id",e).single();if(a)throw a;return t}async function T(e){const{data:t,error:a}=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(a)throw a;return(t||[]).map(r=>({id:r.id,step_order:r.step_order,description:(r.description||"").trim(),is_final_step:!!r.is_final_step,enable_feedback_text:!!r.enable_feedback_text,enable_photo_upload:!!(r.enable_photo_upload||r.requires_photo_upload),enable_voice_upload:!!r.enable_voice_upload,enable_mcq:!!r.enable_mcq,label:`مرحله ${r.step_order}`}))}async function j(e,t="",a){const r=[];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);a&&(c=c.eq("step_id",a));const{data:n,error:_}=await c;if(_)throw _;for(const o of n||[]){const s=String(o.feedback_text||"").trim();if(!s)continue;const i=Array.isArray(o.user_step_challenges)?o.user_step_challenges[0]:o.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}=g(m);!q(u,p,t)&&!s.toLowerCase().includes(t.trim().toLowerCase())||r.push({id:`step-${o.id}`,user_name:u,user_phone:p,feedback_text:s,source:"step",step_id:o.step_id,created_at:o.created_at})}return r.sort((o,s)=>String(s.created_at||"").localeCompare(String(o.created_at||""))),r.map((o,s)=>({...o,index:s+1}))}async function G(e,t=""){const{data:a,error:r}=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(r)throw r;const c=(a||[]).map(n=>{const _=String(n.completion_feedback||"").trim();if(!_)return null;const o=Array.isArray(n.users)?n.users[0]:n.users,{user_name:s,user_phone:i}=g(o);return!q(s,i,t)&&!_.toLowerCase().includes(t.trim().toLowerCase())?null:{id:`completion-${n.id}`,user_name:s,user_phone:i,feedback_text:_,created_at:n.completed_at||n.created_at}}).filter(Boolean);return c.sort((n,_)=>String(_.created_at||"").localeCompare(String(n.created_at||""))),c.map((n,_)=>({...n,index:_+1}))}async function R(e,t="",a){let r=l.from("user_step_challenge_steps").select(`
|
|
id,
|
|
step_id,
|
|
photo_url,
|
|
created_at,
|
|
user_step_challenges!inner (
|
|
challenge_id,
|
|
users (name, phone, first_name, last_name)
|
|
)
|
|
`).eq("user_step_challenges.challenge_id",e).not("photo_url","is",null);a&&(r=r.eq("step_id",a));const{data:c,error:n}=await r;if(n)throw n;const _=(c||[]).map(o=>{const s=String(o.photo_url||"").trim();if(!s)return null;const i=Array.isArray(o.user_step_challenges)?o.user_step_challenges[0]:o.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}=g(m);return q(u,p,t)?{id:o.id,step_id:o.step_id,photo_url:s,user_name:u,user_phone:p,created_at:o.created_at}:null}).filter(Boolean);return _.sort((o,s)=>String(s.created_at||"").localeCompare(String(o.created_at||""))),_}async function V(e,t="",a){let r=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);a&&(r=r.eq("step_id",a));const{data:c,error:n}=await r;if(n)throw n;const _=(c||[]).map(o=>{const s=String(o.voice_url||"").trim();if(!s)return null;const i=Array.isArray(o.user_step_challenges)?o.user_step_challenges[0]:o.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}=g(m);return q(u,p,t)?{id:o.id,step_id:o.step_id,voice_url:s,user_name:u,user_phone:p,created_at:o.created_at}:null}).filter(Boolean);return _.sort((o,s)=>String(s.created_at||"").localeCompare(String(o.created_at||""))),_.map((o,s)=>({...o,index:s+1}))}async function H(e,t="",a){let r=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});a&&(r=r.eq("id",a));const{data:c,error:n}=await r;if(n)throw n;let _=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);a&&(_=_.eq("step_id",a));const{data:o,error:s}=await _;if(s)throw s;return(c||[]).map(i=>{const m=E(i.mcq_options),u=(o||[]).filter(d=>d.step_id===i.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,w=Array.isArray(h==null?void 0:h.users)?h.users[0]:h==null?void 0:h.users,{user_name:b,user_phone:S}=g(w),y=Number(d.mcq_answer);return{id:d.id,user_name:b,user_phone:S,mcq_answer:y,answer_label:m[y]||`گزینه ${y+1}`,created_at:d.created_at}}).filter(d=>q(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,x=m.map((d,h)=>{const w=u.filter(S=>S.mcq_answer===h).length,b=p>0?Math.round(w/p*1e3)/10:0;return{optionIndex:h,label:d,count:w,percent:b}});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:x,total_answers:p}})}async function K(){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 X(e){const{data:t,error:a}=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(a)throw a;return t}export{z as a,U as b,W as c,Q as d,K as e,P as f,L as g,D as h,M as i,B as j,O as k,j as l,R as m,V as n,H as o,G as p,J as q,T as r,X as s,$ as u};
|