"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")},33873:e=>{e.exports=require("path")},47122:(e,a,s)=>{s.r(a),s.d(a,{config:()=>S,default:()=>h,routeModule:()=>E});var t={};s.r(t),s.d(t,{default:()=>N});var r=s(89947),l=s(2706),d=s(96762),i=s(67619),n=s(69842),u=s(4820),o=s(27825),b=s.n(o),c=s(21724),_=s(48786),m=s(98e3),f=s(76856);s(29021),s(33873);let{execSync:p}=s(79646);async function N(e,a){if("POST"!==e.method)return a.json({msg:"Failed!"});let s=await (0,n.A)(e,a,!0);if(!s)return console.log("No User"),a.json({success:!1,msg:"Unauthorized"});let{payload:t}=e.body,r=e.body.dbFullName.replace(/[^a-z\_0-9]/g,""),l=e.body.dbSlug,d=e.body.dbId,o=e.body.parentDatabase,p=e.body.parentTable;try{let e=(0,u.A)({userId:s.id});if(!e)throw Error("No User Schema found!");let n=e.findIndex(e=>e.dbFullName===r),N=e[n];if(!N)throw Error("No Target Database Found!");let h=N.tables.findIndex(e=>e.tableName===t.tableName);if(h>=0?e[n].tables[h]=t:e[n].tables.push(t),t?.childTable){let a=e.find(e=>e.dbFullName===o),s=a?.tables.find(e=>{if(e.tableName===p)return!0});if(!s)throw Error("Child Table Parent Doesn't Exist!");s.childrenTables?.[0]?s.childrenTables.push({dbNameFull:r,tableName:t.tableName,tableNameFull:t.tableNameFull}):s.childrenTables=[{dbNameFull:r,tableName:t.tableName,tableNameFull:t.tableFullName}]}if(N.childrenDatabases)for(let a=0;ae.dbFullName===r.dbFullName);e[a]&&(e[a].tables=b().cloneDeep(N.tables));let l=await (0,_.A)("SELECT * FROM user_databases WHERE user_id = ? AND db_full_name = ?",[s.id,r.dbFullName]),d=await (0,_.A)("SELECT * FROM user_database_tables WHERE db_id=? AND user_id=? AND table_slug=?",[l[0].id,s.id,t.tableName]);if(d?.[0])continue;await (0,m.A)({dbFullName:"datasquirel",tableName:"user_database_tables",data:{user_id:s.id,db_id:l[0].id,db_slug:l[0].db_slug,table_name:t.tableFullName,table_slug:t.tableName}})}catch(e){(0,i.default)({component:"/api/addUserTable/lines-124-140",message:e.message,user:s})}}(0,c.A)({userId:s.id,schemaData:e});let S={user_id:s.id,db_id:d,db_slug:l,table_name:t.tableFullName,table_slug:t.tableName};t?.childTable&&(S.child_table="1",S.child_table_parent_database=o,S.child_table_parent_table=p),await (0,m.A)({dbFullName:"datasquirel",tableName:"user_database_tables",data:S}),await (0,f.A)({userId:s.id,targetDatabase:r}),a.json({success:!0})}catch(e){console.log(e.message),(0,i.default)({component:"/api/addUserTable/main-catch-error",message:e.message,user:s}),a.json({success:!1,msg:"Database Files could not be written!"})}}let h=(0,d.M)(t,"default"),S=(0,d.M)(t,"config"),E=new r.PagesAPIRouteModule({definition:{kind:l.A.PAGES_API,page:"/api/addUserTable",pathname:"/api/addUserTable",bundlePath:"",filename:""},userland:t})},69842:(e,a,s)=>{s.d(a,{A:()=>n});var t=s(48786),r=s(95969),l=s(29021),d=s.n(l),i=s(89159);async function n(e,a,s){let{keyCookieName:l,csrfCookieName:n}=(0,i.A)();if(!e.cookies?.[l]?.match(/./))return null;let u=(0,r.A)({encryptedString:e.cookies[l]});if(!u)return null;let o=JSON.parse(u);if(!o.csrf_k||s&&!Object.keys(e.headers).find(e=>e.includes(o.csrf_k)))return null;let b=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!b)return console.log("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable."),null;if(s&&!d().existsSync(`${b}/${o.csrf_k}`))return null;if(0==o.verification_status&&!s){let e=await (0,t.A)(`SELECT verification_status FROM users WHERE id='${o.id}'`);e&&e[0]&&1==e[0].verification_status&&a.setHeader("Set-Cookie",["user_refresh=1"])}return o?.date&&Date.now()-o.date>6048e5?null:o}},89159:(e,a,s)=>{s.d(a,{A:()=>t});function t(e){let a=process.env.DSQL_COOKIES_PREFIX||"dsql_",s=process.env.DSQL_COOKIES_KEY_NAME||"key",t=process.env.DSQL_COOKIES_CSRF_NAME||"csrf",r=process.env.DSQL_COOKIES_ONE_TIME_CODE_NAME||"one-time-code",l=e?.database?.replace(/^datasquirel_user_\d+_/,"")||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),d=a;e?.userId&&(d+=`user_${e.userId}_`),l&&(d+=`${l}_`),d+=s;let i=a;e?.userId&&(i+=`user_${e.userId}_`),l&&(i+=`${l}_`),i+=t;let n=a;return e?.userId&&(n+=`user_${e.userId}_`),l&&(n+=`${l}_`),{keyCookieName:d,csrfCookieName:i,oneTimeCodeName:n+=r}}},4820:(e,a,s)=>{s.d(a,{A:()=>n});var t=s(67619),r=s(29021),l=s.n(r),d=s(33873),i=s.n(d);function n({userId:e}){try{let a=i().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(0,t.default)({component:"grabUserSchemaData",message:e.message}),null}}},21724:(e,a,s)=>{s.d(a,{A:()=>n});var t=s(67619),r=s(29021),l=s.n(r),d=s(33873),i=s.n(d);function n({userId:e,schemaData:a}){try{let s=i().resolve(process.cwd(),`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${e}/main.json`);return l().writeFileSync(s,JSON.stringify(a),"utf8"),!0}catch(e){return(0,t.default)({component:"/functions/backend/setUserSchemaData",message:e.message}),!1}}}};var a=require("../../webpack-api-runtime.js");a.C(e);var s=e=>a(a.s=e),t=a.X(0,[1251,2282],()=>s(47122));module.exports=t})();