Files
machartaa_admin_dist/dist/assets/stepChallenge-2c7c9a67.js
T
2026-07-15 13:20:16 +03:30

48 lines
13 KiB
JavaScript

import{G as d,_ as A}from"./index-c3a83709.js";import{e as q}from"./convert-2ad76038.js";function C(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 v(t){return{...t,duration_minutes:Math.round((Number(t.duration_seconds)||60)/60),kodoblu_voice_url:q(t.kodoblu_voice_url)||"",audio_section_url:q(t.audio_section_url)||"",step_images:C(t.step_images).map(e=>q(e)||e)}}function k(t,e,a){var r,o,i,l,_;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:q((o=e.kodoblu_voice_url)==null?void 0:o.trim())||null,step_images:(e.step_images||[]).map(n=>q(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:((i=e.feedback_text_title)==null?void 0:i.trim())||null,feedback_text_placeholder:((l=e.feedback_text_placeholder)==null?void 0:l.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:q((_=e.audio_section_url)==null?void 0:_.trim())||null,punishment_description:e.punishment_description||"",requires_photo_upload:!!(e.enable_photo_upload||e.requires_photo_upload),updated_at:new Date().toISOString()}}function E(t,e){return e.map((a,r)=>k(t,a,r+1))}async function N(t){const{data:e,error:a}=await d.from("step_challenges").select("*").eq("id",t).single();if(a)throw a;const{data:r,error:o}=await d.from("step_challenge_steps").select("*").eq("challenge_id",t).order("step_order",{ascending:!0});if(o)throw o;return{...e,steps:(r||[]).map(i=>v(i))}}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};e&&(a.owner_id=e);const{data:r,error:o}=await d.from("step_challenges").insert([a]).select().single();if(o)throw o;const i=E(r.id,t.steps),{error:l}=await d.from("step_challenge_steps").insert(i);if(l)throw l;return r}async function F(t,e){const{error:a}=await d.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,updated_at:new Date().toISOString()}).eq("id",t);if(a)throw a;const{data:r,error:o}=await d.from("step_challenge_steps").select("id").eq("challenge_id",t);if(o)throw o;const i=new Set((r||[]).map(n=>n.id)),l=e.steps.map(n=>n.id).filter(n=>typeof n=="number"&&i.has(n)),_=[...i].filter(n=>!l.includes(n));if(_.length){const{error:n}=await d.from("user_step_challenge_steps").delete().in("step_id",_);if(n)throw n;const{error:s}=await d.from("step_challenge_steps").delete().in("id",_);if(s)throw s}for(const n of l){const{error:s}=await d.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],c=k(t,s,n+1);if(s.id&&i.has(s.id)){const{error:p}=await d.from("step_challenge_steps").update(c).eq("id",s.id);if(p)throw p;continue}const{error:u}=await d.from("step_challenge_steps").insert(c);if(u)throw u}return{id:t}}async function L(t){const{error:e}=await d.from("step_challenges").delete().eq("id",t);if(e)throw e}async function M(t,e){const{data:a,error:r}=await d.from("step_challenges").update({...e,updated_at:new Date().toISOString()}).eq("id",t).select();if(r)throw r;return a}async function U(t){const{TableEngineRequest:e}=await A(()=>import("./buildSupabaseQuery-693418f9.js"),["assets/buildSupabaseQuery-693418f9.js","assets/index-c3a83709.js","assets/index-6a736606.css","assets/supabaseTableQuery-9069679c.js"]),a={...t};!a.sort&&!a.order&&(a.order="-id");const r=await e(a,"step_challenges");try{if(Array.isArray(r==null?void 0:r.rows)){const o=r.rows,i=o.map(u=>u.id).filter(Boolean);let l={};if(i.length>0){const{data:u}=await d.from("step_challenge_steps").select("id, challenge_id, step_order, duration_seconds").in("challenge_id",i);l=(u||[]).reduce((p,f)=>(p[f.challenge_id]||(p[f.challenge_id]=[]),p[f.challenge_id].push(f),p),{})}const _=a.page||1,n=a.limit||20,s=(_-1)*n,c=o.map((u,p)=>{const f=l[u.id]||[],m=f.reduce((h,g)=>h+Math.round((g.duration_seconds||0)/60),0);return{...u,index:s+p,steps_count:f.length,total_minutes:m}});return{...r,rows:c}}}catch(o){console.log("[getStepChallengesTable] normalize error:",o)}return r}function b(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 w(t,e,a){const r=a.trim().toLowerCase();return r?t.toLowerCase().includes(r)||e.toLowerCase().includes(r):!0}function P(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 z(){const{data:t,error:e}=await d.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 d.from("user_step_challenges").select("challenge_id, user_id");if(r)throw r;const o={};for(const i of a||[]){const l=Number(i.challenge_id);o[l]||(o[l]={uniqueUsers:new Set,timesPerformed:0}),o[l].timesPerformed+=1,i.user_id&&o[l].uniqueUsers.add(String(i.user_id))}return(t||[]).map(i=>{const l=o[i.id];return{id:i.id,title:i.title,character_image_url:i.character_image_url,sort_order:i.sort_order,show:i.show,unique_users_count:l?l.uniqueUsers.size:0,times_performed:l?l.timesPerformed:0}})}async function B(t){const{data:e,error:a}=await d.from("step_challenges").select("id, title").eq("id",t).single();if(a)throw a;return e}async function T(t){const{data:e,error:a}=await d.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 o;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:(o=r.description)!=null&&o.trim()?`مرحله ${r.step_order}: ${String(r.description).trim().slice(0,28)}${String(r.description).trim().length>28?"…":""}`:`مرحله ${r.step_order}`}})}async function R(t,e="",a){const r=[];let o=d.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&&(o=o.eq("step_id",a));const{data:i,error:l}=await o;if(l)throw l;for(const n of i||[]){const s=String(n.feedback_text||"").trim();if(!s)continue;const c=Array.isArray(n.user_step_challenges)?n.user_step_challenges[0]:n.user_step_challenges,u=Array.isArray(c==null?void 0:c.users)?c.users[0]:c==null?void 0:c.users,{user_name:p,user_phone:f}=b(u);!w(p,f,e)&&!s.toLowerCase().includes(e.trim().toLowerCase())||r.push({id:`step-${n.id}`,user_name:p,user_phone:f,feedback_text:s,source:"step",step_id:n.step_id,created_at:n.created_at})}let _=!a;if(a){const{data:n}=await d.from("step_challenge_steps").select("is_final_step").eq("id",a).maybeSingle();_=!!(n!=null&&n.is_final_step)}if(_){const{data:n,error:s}=await d.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(s)throw s;for(const c of n||[]){const u=String(c.completion_feedback||"").trim();if(!u)continue;const p=Array.isArray(c.users)?c.users[0]:c.users,{user_name:f,user_phone:m}=b(p);!w(f,m,e)&&!u.toLowerCase().includes(e.trim().toLowerCase())||r.push({id:`completion-${c.id}`,user_name:f,user_phone:m,feedback_text:u,source:"completion",step_id:a||null,created_at:c.completed_at||c.created_at})}}return r.sort((n,s)=>String(s.created_at||"").localeCompare(String(n.created_at||""))),r.map((n,s)=>({...n,index:s+1}))}async function W(t,e="",a){let r=d.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:o,error:i}=await r;if(i)throw i;const l=(o||[]).map(_=>{const n=String(_.photo_url||"").trim();if(!n)return null;const s=Array.isArray(_.user_step_challenges)?_.user_step_challenges[0]:_.user_step_challenges,c=Array.isArray(s==null?void 0:s.users)?s.users[0]:s==null?void 0:s.users,{user_name:u,user_phone:p}=b(c);return w(u,p,e)?{id:_.id,step_id:_.step_id,photo_url:n,user_name:u,user_phone:p,created_at:_.created_at}:null}).filter(Boolean);return l.sort((_,n)=>String(n.created_at||"").localeCompare(String(_.created_at||""))),l}async function Q(t,e="",a){let r=d.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:o,error:i}=await r;if(i)throw i;const l=(o||[]).map(_=>{const n=String(_.voice_url||"").trim();if(!n)return null;const s=Array.isArray(_.user_step_challenges)?_.user_step_challenges[0]:_.user_step_challenges,c=Array.isArray(s==null?void 0:s.users)?s.users[0]:s==null?void 0:s.users,{user_name:u,user_phone:p}=b(c);return w(u,p,e)?{id:_.id,step_id:_.step_id,voice_url:n,user_name:u,user_phone:p,created_at:_.created_at}:null}).filter(Boolean);return l.sort((_,n)=>String(n.created_at||"").localeCompare(String(_.created_at||""))),l.map((_,n)=>({..._,index:n+1}))}async function J(t,e="",a){let r=d.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:o,error:i}=await r;if(i)throw i;let l=d.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&&(l=l.eq("step_id",a));const{data:_,error:n}=await l;if(n)throw n;return(o||[]).map(s=>{const c=P(s.mcq_options),u=(_||[]).filter(m=>m.step_id===s.id&&m.mcq_answer!==null&&m.mcq_answer!==void 0).map(m=>{const h=Array.isArray(m.user_step_challenges)?m.user_step_challenges[0]:m.user_step_challenges,g=Array.isArray(h==null?void 0:h.users)?h.users[0]:h==null?void 0:h.users,{user_name:S,user_phone:y}=b(g),x=Number(m.mcq_answer);return{id:m.id,user_name:S,user_phone:y,mcq_answer:x,answer_label:c[x]||`گزینه ${x+1}`,created_at:m.created_at}}).filter(m=>w(m.user_name,m.user_phone,e)).sort((m,h)=>String(h.created_at||"").localeCompare(String(m.created_at||""))).map((m,h)=>({...m,index:h+1})),p=u.length,f=c.map((m,h)=>{const g=u.filter(y=>y.mcq_answer===h).length,S=p>0?Math.round(g/p*1e3)/10:0;return{optionIndex:h,label:m,count:g,percent:S}});return{step_id:s.id,step_order:s.step_order,question:s.mcq_question||`سوال مرحله ${s.step_order}`,options:c,correct_index:s.mcq_correct_index,answers:u,option_stats:f,total_answers:p}})}async function V(){const{data:t,error:e}=await d.from("step_challenge_config").select("*").eq("id",1).maybeSingle();if(e)throw e;return t||{id:1,rules_aparat_embed_code:null}}async function j(t){const{data:e,error:a}=await d.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,N as b,V as c,L as d,F as e,$ as f,U as g,R as h,W as i,Q as j,J as k,B as l,T as m,j as s,M as u};