dsql-admin/dsql-app/.local_dist/server/pages/api/addUserTable.js

1 line
5.7 KiB
JavaScript
Raw Normal View History

2025-01-14 06:19:19 +00:00
"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,r)=>{r.r(a),r.d(a,{config:()=>N,default:()=>E,routeModule:()=>S});var t={};r.r(t),r.d(t,{default:()=>h});var s=r(89947),l=r(2706),d=r(96762),i=r(67619),n=r(69842),o=r(4820),u=r(27825),c=r.n(u),b=r(21724),_=r(48786),m=r(98e3),f=r(76856);r(29021),r(33873);let{execSync:p}=r(79646);async function h(e,a){if("POST"!==e.method)return a.json({msg:"Failed!"});let r=await (0,n.A)(e,a,!0);if(!r)return console.log("No User"),a.json({success:!1,msg:"Unauthorized"});let{payload:t}=e.body,s=e.body.dbFullName.replace(/[^a-z\_0-9]/g,""),l=e.body.dbSlug,d=e.body.dbId,u=e.body.parentDatabase,p=e.body.parentTable;try{let e=(0,o.A)({userId:r.id});if(!e)throw Error("No User Schema found!");let n=e.findIndex(e=>e.dbFullName===s),h=e[n];if(!h)throw Error("No Target Database Found!");let E=h.tables.findIndex(e=>e.tableName===t.tableName);if(E>=0?e[n].tables[E]=t:e[n].tables.push(t),t?.childTable){let a=e.find(e=>e.dbFullName===u),r=a?.tables.find(e=>{if(e.tableName===p)return!0});if(!r)throw Error("Child Table Parent Doesn't Exist!");r.childrenTables?.[0]?r.childrenTables.push({dbNameFull:s,tableName:t.tableName,tableNameFull:t.tableNameFull}):r.childrenTables=[{dbNameFull:s,tableName:t.tableName,tableNameFull:t.tableFullName}]}if(h.childrenDatabases)for(let a=0;a<h.childrenDatabases.length;a++){let s=h.childrenDatabases[a];try{let a=e.findIndex(e=>e.dbFullName===s.dbFullName);e[a]&&(e[a].tables=c().cloneDeep(h.tables));let l=await (0,_.A)("SELECT * FROM user_databases WHERE user_id = ? AND db_full_name = ?",[r.id,s.dbFullName]),d=await (0,_.A)("SELECT * FROM user_database_tables WHERE db_id=? AND user_id=? AND table_slug=?",[l[0].id,r.id,t.tableName]);if(d?.[0])continue;await (0,m.A)({dbFullName:"datasquirel",tableName:"user_database_tables",data:{user_id:r.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:r})}}(0,b.A)({userId:r.id,schemaData:e});let N={user_id:r.id,db_id:d,db_slug:l,table_name:t.tableFullName,table_slug:t.tableName};t?.childTable&&(N.child_table="1",N.child_table_parent_database=u,N.child_table_parent_table=p),await (0,m.A)({dbFullName:"datasquirel",tableName:"user_database_tables",data:N}),await (0,f.A)({userId:r.id,targetDatabase:s}),a.json({success:!0})}catch(e){console.log(e.message),(0,i.default)({component:"/api/addUserTable/main-catch-error",message:e.message,user:r}),a.json({success:!1,msg:"Database Files could not be written!"})}}let E=(0,d.M)(t,"default"),N=(0,d.M)(t,"config"),S=new s.PagesAPIRouteModule({definition:{kind:l.A.PAGES_API,page:"/api/addUserTable",pathname:"/api/addUserTable",bundlePath:"",filename:""},userland:t})},69842:(e,a,r)=>{r.d(a,{A:()=>n});var t=r(48786),s=r(95969),l=r(29021),d=r.n(l),i=r(89159);async function n(e,a,r){try{let{keyCookieName:l,csrfCookieName:n}=(0,i.A)();if(!e.cookies?.[l]?.match(/./))throw Error("Request Cookies not Found!");let o=(0,s.A)({encryptedString:e.cookies[l]});if(!o)throw Error("Couldn't decrypt auth cookie!");let u=JSON.parse(o);if(!u.csrf_k)throw Error("No `csrf_k` present in user Object!");if(r&&!Object.keys(e.headers).find(e=>e.includes(u.csrf_k)))throw Error("Deep Auth Failed. `csrf` key not found in request headers!");let c=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!c)throw Error("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable.");if(r&&!d().existsSync(`${c}/${u.csrf_k}`))throw Error("Key not found in Auth Keys Folder!");if(0==u.verification_status&&!r){let e=await (0,t.A)(`SELECT verification_status FROM users WHERE id='${u.