2026-07-16 18:05:28 +03:30
import { G as l } from "./index-413b1e47.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
2026-07-16 16:44:06 +03:30
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};