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