"use strict";(()=>{var e={};e.id=8275,e.ids=[8275],e.modules={27825:e=>{e.exports=require("lodash")},75600:e=>{e.exports=require("next/dist/compiled/next-server/pages-api.runtime.prod.js")},72583:e=>{e.exports=require("sanitize-html")},9973:e=>{e.exports=require("serverless-mysql")},79428:e=>{e.exports=require("buffer")},79646:e=>{e.exports=require("child_process")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},81630:e=>{e.exports=require("http")},33873:e=>{e.exports=require("path")},13441:(e,a,t)=>{t.r(a),t.d(a,{config:()=>F,default:()=>E,routeModule:()=>D});var r={};t.r(r),t.d(r,{default:()=>y});var l=t(89947),s=t(2706),n=t(96762),i=t(77538),d=t.n(i),u=t(6420),o=t.n(u),c=t(49827),b=t(27825),m=t.n(b),f=t(19857),p=t(51348),_=t.n(p),h=t(47670),g=t.n(h);t(29021);let N=t(33873),{execSync:S}=t(79646);async function y(e,a){if("POST"!==e.method)return a.json({msg:"Failed!"});let t=await o()(e,a,!0);if(!t)return console.log("No User"),a.json({success:!1,msg:"Unauthorized"});let{payload:r}=e.body,l=e.body.dbFullName.replace(/[^a-z\_0-9]/g,""),s=e.body.dbSlug,n=e.body.dbId,i=e.body.parentDatabase,u=e.body.parentTable;try{let e=(0,c.default)({userId:t.id});if(!e)throw Error("No User Schema found!");let o=e.findIndex(e=>e.dbFullName===l),b=e[o];if(!b)throw Error("No Target Database Found!");let p=b.tables.findIndex(e=>e.tableName===r.tableName);if(p>=0?e[o].tables[p]=r:e[o].tables.push(r),r?.childTable){let a=e.find(e=>e.dbFullName===i),t=a?.tables.find(e=>{if(e.tableName===u)return!0});if(!t)throw Error("Child Table Parent Doesn't Exist!");t.childrenTables?.[0]?t.childrenTables.push({dbNameFull:l,tableName:r.tableName,tableNameFull:r.tableNameFull}):t.childrenTables=[{dbNameFull:l,tableName:r.tableName,tableNameFull:r.tableFullName}]}if(b.childrenDatabases)for(let a=0;ae.dbFullName===l.dbFullName);e[a]&&(e[a].tables=m().cloneDeep(b.tables));let s=await _()("SELECT * FROM user_databases WHERE user_id = ? AND db_full_name = ?",[t.id,l.dbFullName]),n=await _()("SELECT * FROM user_database_tables WHERE db_id=? AND user_id=? AND table_slug=?",[s[0].id,t.id,r.tableName]);if(n?.[0])continue;await g()({dbFullName:"datasquirel",tableName:"user_database_tables",data:{user_id:t.id,db_id:s[0].id,db_slug:s[0].db_slug,table_name:r.tableFullName,table_slug:r.tableName}})}catch(e){d()({component:"/api/addUserTable/lines-124-140",message:e.message,user:t})}}(0,f.default)({userId:t.id,schemaData:e});let h={user_id:t.id,db_id:n,db_slug:s,table_name:r.tableFullName,table_slug:r.tableName};r?.childTable&&(h.child_table="1",h.child_table_parent_database=i,h.child_table_parent_table=u),await g()({dbFullName:"datasquirel",tableName:"user_database_tables",data:h});let y=N.join(process.cwd(),"/shell");S(`node createDbFromSchema.js --user ${t.id} --database ${l}${r?.childTable?" --clone":""}`,{cwd:y}),a.json({success:!0})}catch(e){console.log(e.message),d()({component:"/api/addUserTable/main-catch-error",message:e.message,user:t}),a.json({success:!1,msg:"Database Files could not be written!"})}}let E=(0,n.M)(r,"default"),F=(0,n.M)(r,"config"),D=new l.PagesAPIRouteModule({definition:{kind:s.A.PAGES_API,page:"/api/addUserTable",pathname:"/api/addUserTable",bundlePath:"",filename:""},userland:r})},49827:(e,a,t)=>{t.r(a),t.d(a,{default:()=>n});let r=t(77538),l=t(29021),s=t(33873);function n({userId:e}){try{let a=s.resolve(process.cwd(),`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${e}/main.json`);return JSON.parse(l.readFileSync(a,"utf-8"))}catch(e){return r({component:"/functions/backend/grabUserSchemaData",message:e.message}),null}}},77538:(e,a,t)=>{let r=t(29021),{IncomingMessage:l}=t(81630);e.exports=async function({user:e,message:a,component:t,noMail:l,req:s}){let n=new Date,i=(()=>{if(!s)return null;try{let e=s.headers["x-forwarded-for"],a=s.headers["x-real-ip"],t=s.headers["cf-connecting-ip"],r=Array.isArray(e)?e[0]:e?.split(",")[0],l=t||r||a||s.socket.remoteAddress;if(!l)return null;return String(l)}catch(e){return null}})();try{let l=`🚀 SERVER ERROR =========================== Error Message: ${a} Component: ${t}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(l+=` User Id: ${e?.id} User Name: ${e?.first_name} ${e?.last_name} User Email: ${e?.email}`),s?.url&&(l+=` URL: ${s.url}`),s?.body&&(l+=` Request Body: ${JSON.stringify(s.body,null,4)}`),i&&(l+=` IP: ${i}`),l+=` Date: ${n.toDateString()} ========================================`,r.existsSync("./.tmp/error.log")||r.writeFileSync("./.tmp/error.log","","utf-8");let d=r.readFileSync("./.tmp/error.log","utf-8");r.writeFileSync("./.tmp/error.log",l),r.appendFileSync("./.tmp/error.log",` ${d}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},19857:(e,a,t)=>{t.r(a),t.d(a,{default:()=>n});let r=t(77538),l=t(29021),s=t(33873);function n({userId:e,schemaData:a}){try{let t=s.resolve(process.cwd(),`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${e}/main.json`);return l.writeFileSync(t,JSON.stringify(a),"utf8"),!0}catch(e){return r({component:"/functions/backend/setUserSchemaData",message:e.message}),!1}}},6420:(e,a,t)=>{t(81630);let r=t(51348),l=t(72907),s=t(29021);async function n(e,a,t,n){if(!e.cookies?.datasquirelAuthKey?.match(/./))return null;let i=l(e.cookies.datasquirelAuthKey);if(!i)return null;let d=JSON.parse(i);if(!d.csrf_k||t&&!e.headers["x-csrf-auth"]?.match(RegExp(`${d.csrf_k}`)))return null;let u=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!u)return console.log("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable."),null;if(t&&!s.existsSync(`${u}/${d.csrf_k}`))return null;if(0==d.verification_status&&!t){let e=await r(`SELECT verification_status FROM users WHERE id='${d.id}'`);e&&e[0]&&1==e[0].verification_status&&a.setHeader("Set-Cookie",["user_refresh=1"])}return d?.date&&Date.now()-d.date>6048e5?null:d}e.exports=n},72907:(e,a,t)=>{let{scryptSync:r,createDecipheriv:l}=t(55511),{Buffer:s}=t(79428);e.exports=e=>{let a=l("aes-192-cbc",r(process.env.DSQL_ENCRYPTION_PASSWORD||"",process.env.DSQL_ENCRYPTION_SALT||"",24),s.alloc(16,0));try{let t=a.update(e,"hex","utf8");return t+=a.final("utf8")}catch(e){return null}}}};var a=require("../../webpack-api-runtime.js");a.C(e);var t=e=>a(a.s=e),r=a.X(0,[4050],()=>t(13441));module.exports=r})();