"use strict";(()=>{var e={};e.id=8275,e.ids=[8275],e.modules={39908:e=>{e.exports=require("@moduletrace/datasquirel/dist/users/user-auth")},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")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},33873:e=>{e.exports=require("path")},47122:(e,a,t)=>{t.r(a),t.d(a,{config:()=>h,default:()=>f,routeModule:()=>g});var l={};t.r(l),t.d(l,{default:()=>N});var s=t(89947),r=t(2706),d=t(96762),i=t(67619),n=t(69842),u=t(4820),b=t(27825),o=t.n(b),c=t(21724),m=t(48786),p=t(98e3),_=t(76856);async function N(e,a){if("POST"!==e.method)return a.json({msg:"Failed!"});let t=await (0,n.A)(e,a,!0);if(!t)return console.log("No User"),a.json({success:!1,msg:"Unauthorized"});let{payload:l}=e.body,s=e.body.dbFullName.replace(/[^a-z\_0-9]/g,""),r=e.body.dbSlug,d=e.body.dbId,b=e.body.parentDatabase,N=e.body.parentTable;try{let e=(0,u.A)({userId:t.id});if(!e)throw Error("No User Schema found!");let n=e.findIndex(e=>e.dbFullName===s),f=e[n];if(!f)throw Error("No Target Database Found!");let h=f.tables.findIndex(e=>e.tableName===l.tableName);if(h>=0?e[n].tables[h]=l:e[n].tables.push(l),l?.childTable){let a=e.find(e=>e.dbFullName===b),t=a?.tables.find(e=>{if(e.tableName===N)return!0});if(!t)throw Error("Child Table Parent Doesn't Exist!");t.childrenTables?.[0]?t.childrenTables.push({dbNameFull:s,tableName:l.tableName,tableNameFull:l.tableNameFull}):t.childrenTables=[{dbNameFull:s,tableName:l.tableName,tableNameFull:l.tableFullName}]}if(f.childrenDatabases)for(let a=0;ae.dbFullName===s.dbFullName);e[a]&&(e[a].tables=o().cloneDeep(f.tables));let r=await (0,m.A)("SELECT * FROM user_databases WHERE user_id = ? AND db_full_name = ?",[t.id,s.dbFullName]),d=await (0,m.A)("SELECT * FROM user_database_tables WHERE db_id=? AND user_id=? AND table_slug=?",[r[0].id,t.id,l.tableName]);if(d?.[0])continue;await (0,p.A)({dbFullName:"datasquirel",tableName:"user_database_tables",data:{user_id:t.id,db_id:r[0].id,db_slug:r[0].db_slug,table_name:l.tableFullName,table_slug:l.tableName}})}catch(e){(0,i.A)({component:"/api/addUserTable/lines-124-140",message:e.message,user:t})}}(0,c.A)({userId:t.id,schemaData:e});let g={user_id:t.id,db_id:d,db_slug:r,table_name:l.tableFullName,table_slug:l.tableName};l?.childTable&&(g.child_table="1",g.child_table_parent_database=b,g.child_table_parent_table=N),await (0,p.A)({dbFullName:"datasquirel",tableName:"user_database_tables",data:g}),await (0,_.A)({userId:t.id,targetDatabase:s}),a.json({success:!0})}catch(e){console.log(e.message),(0,i.A)({component:"/api/addUserTable/main-catch-error",message:e.message,user:t}),a.json({success:!1,msg:"Database Files could not be written!"})}}let f=(0,d.M)(l,"default"),h=(0,d.M)(l,"config"),g=new s.PagesAPIRouteModule({definition:{kind:r.A.PAGES_API,page:"/api/addUserTable",pathname:"/api/addUserTable",bundlePath:"",filename:""},userland:l})},69842:(e,a,t)=>{t.d(a,{A:()=>r});var l=t(39908),s=t.n(l);async function r(e,a,t){let l=s()({req:e,level:t?"deep":void 0});return console.log(l),l.payload}},4820:(e,a,t)=>{t.d(a,{A:()=>n});var l=t(67619),s=t(29021),r=t.n(s),d=t(33873),i=t.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(r().readFileSync(a,"utf-8"))}catch(e){return(0,l.A)({component:"grabUserSchemaData",message:e.message}),null}}},21724:(e,a,t)=>{t.d(a,{A:()=>n});var l=t(67619),s=t(29021),r=t.n(s),d=t(33873),i=t.n(d);function n({userId:e,schemaData:a}){try{let t=i().resolve(process.cwd(),`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${e}/main.json`);return r().writeFileSync(t,JSON.stringify(a),"utf8"),!0}catch(e){return(0,l.A)({component:"/functions/backend/setUserSchemaData",message:e.message}),!1}}}};var a=require("../../webpack-api-runtime.js");a.C(e);var t=e=>a(a.s=e),l=a.X(0,[1251,9814],()=>t(47122));module.exports=l})();