"use strict";(()=>{var e={};e.id=861,e.ids=[861],e.modules={27825:e=>{e.exports=require("lodash")},75600:e=>{e.exports=require("next/dist/compiled/next-server/pages-api.runtime.prod.js")},79428:e=>{e.exports=require("buffer")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},33873:e=>{e.exports=require("path")},96762:(e,r)=>{Object.defineProperty(r,"M",{enumerable:!0,get:function(){return function e(r,t){return t in r?r[t]:"then"in r&&"function"==typeof r.then?r.then(r=>e(r,t)):"function"==typeof r&&"default"===t?r:void 0}}})},15679:(e,r,t)=>{t.r(r),t.d(r,{config:()=>S,default:()=>h,routeModule:()=>g});var a={};t.r(a),t.d(a,{default:()=>m});var s=t(89947),o=t(2706),n=t(96762),i=t(48786),d=t(60125),l=t(67619),u=t(69842),c=t(4820),f=t(29021),_=t.n(f),E=t(27825),b=t.n(E),p=t(76856);async function m(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let t=await (0,u.A)(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let a=e.body.schema,s=e.body.database,o=e.body.table;e.body.delegated;try{let n;let l=await (0,d.A)({request:e,user:t,databaseSlug:s,query:e.query}),u=l?.rootUserId?l.rootUserId:t.id,f=`datasquirel_user_${u}_${s}`.replace(/[^a-z\_0-9]/g,"");if(l?.delegated){let e=await (0,i.A)("SELECT * FROM user_users WHERE invited_user_id=? AND user_id=? AND user_priviledge LIKE '%Edit Tables%'",[t.id,l.rootUserId]);e&&e[0]&&(n=!0)}else n=!0;if(!n)return r.json({success:!1,msg:"Unauthorized"});let E=(0,c.A)({userId:u});if(!E)throw Error("Database schema not found!");if(!function({dbSchemaData:e,delegatedUser:r,dbFullName:t,schema:a,table:s,userId:o}){let n=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${o}/main.json`;try{if(!e)throw Error("Database schema not found!");let o=b().cloneDeep(e),i=o.find(e=>e.dbFullName==t);if(!i)throw Error("Old Database schema not found!");let d=o.findIndex(e=>e.dbFullName==t);if(d<0)throw Error("Target Database index not found!");let l=a.find(e=>e.dbFullName===t);if(!l)throw Error("New Database schema not found!");if(r?.delegated&&(r.priviledges?.match(/create.?table/i),!r.priviledges?.match(/edit.?table/i)))throw Error("Unauthorized!");let u=l.tables.find(e=>e.tableName==s.tableName);if(!u)throw Error("Table schema not found!");let c=i.tables.findIndex(e=>e.tableName==s.tableName);u&&(o[d].tables[c]=u);let f=i?.childrenDatabases;f?.forEach(e=>{let r=o.findIndex(r=>r.dbFullName===e.dbFullName);o[r]&&i&&(o[r].tables=i.tables.map(e=>{let r=b().cloneDeep(e);return delete r.childrenTables,r}))});let E=o?.[d]?.tables?.[c]?.childrenTables;return E&&E.forEach(e=>{let r=o.findIndex(r=>r.dbFullName==e.dbNameFull),t=o[r]?.tables?.findIndex(r=>r.tableName===e.tableName);o[r]?.tables[t]&&(o[r].tables[t].fields=b().cloneDeep(u.fields),o[r].tables[t].indexes=b().cloneDeep(u.indexes))}),_().writeFileSync(`${n}`,JSON.stringify(o),"utf8"),!0}catch(e){return console.log("updateSchemaData.ts ERROR: ",e.message),!1}}({dbFullName:f,dbSchemaData:E,delegatedUser:l,schema:a,table:o,userId:u}))throw Error("Update Failed!");return await (0,p.A)({userId:u,targetDatabase:f}),r.json({success:!0})}catch(e){console.log("updateDbSchemaDataForUser ERROR:",e.message),(0,l.A)({component:"/api/updateDbSchemaDataForUser/main-catch-error",message:e.message,user:t}),r.json({success:!1,msg:e.message})}}let h=(0,n.M)(a,"default"),S=(0,n.M)(a,"config"),g=new s.PagesAPIRouteModule({definition:{kind:o.A.PAGES_API,page:"/api/updateDbSchemaDataForUser",pathname:"/api/updateDbSchemaDataForUser",bundlePath:"",filename:""},userland:a})},60125:(e,r,t)=>{t.d(r,{A:()=>s});var a=t(95969);async function s({request:e,databaseSlug:r,user:t,query:s}){try{let o=t.id,n=null;if(!s?.delegated)return{dbUserId:o};let i=s.dbUserId,d=`${process.env.DSQL_USER_DB_PREFIX}${i}_${r}`,l=`${process.env.DSQL_USER_DELEGATED_DB_COOKIE_PREFIX}${d}`;try{if(!e.cookies?.[l])throw Error("Cookie not present");let t=(0,a.A)({encryptedString:e.cookies[l]});if(!t)throw Error("Invalid Token");if((n=JSON.parse(t)).databaseSlug===r)return{dbUserId:o=n.rootUserId,rootUserId:n.rootUserId,rootUserName:n.rootUserName,rootUserEmail:n.rootUserEmail,rootUserImage:n.rootUserImage,databaseFullName:n.databaseFullName,databaseSlug:n.databaseSlug,allowedTables:n.allowedTables,priviledges:n.priviledges,database:n.databaseSlug,delegated:!0}}catch(e){}return{dbUserId:o}}catch(e){return null}}},69842:(e,r,t)=>{t.d(r,{A:()=>d});var a=t(48786),s=t(95969),o=t(29021),n=t.n(o),i=t(89159);async function d(e,r,t){try{let{keyCookieName:o,csrfCookieName:d}=(0,i.A)();if(!e.cookies?.[o]?.match(/./))throw Error("Request Cookies not Found!");let l=(0,s.A)({encryptedString:e.cookies[o]});if(!l)throw Error("Couldn't decrypt auth cookie!");let u=JSON.parse(l);if(!u.csrf_k)throw Error("No `csrf_k` present in user Object!");if(t&&!Object.keys(e.headers).find(e=>e.includes(u.csrf_k)))throw console.log("req.headers",e.headers),console.log("userObject",u),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(t&&!n().existsSync(`${c}/${u.csrf_k}`))throw Error("Key not found in Auth Keys Folder!");if(0==u.verification_status&&!t){let e=await (0,a.A)(`SELECT verification_status FROM users WHERE id='${u.id}'`);e&&e[0]&&1==e[0].verification_status&&r.setHeader("Set-Cookie",["user_refresh=1"])}if(u?.date&&Date.now()-u.date>6048e5)throw Error("Key expired!");return u}catch(e){return console.log(`userAuth Error: ${e.message}`),null}}},89159:(e,r,t)=>{t.d(r,{A:()=>a});function a(e){let r=process.env.DSQL_COOKIES_PREFIX||"dsql_",t=process.env.DSQL_COOKIES_KEY_NAME||"key",a=process.env.DSQL_COOKIES_CSRF_NAME||"csrf",s=process.env.DSQL_COOKIES_ONE_TIME_CODE_NAME||"one-time-code",o=e?.database?.replace(/^datasquirel_user_\d+_/,"")||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),n=r;e?.userId&&(n+=`user_${e.userId}_`),o&&(n+=`${o}_`),n+=t;let i=r;e?.userId&&(i+=`user_${e.userId}_`),o&&(i+=`${o}_`),i+=a;let d=r;return e?.userId&&(d+=`user_${e.userId}_`),o&&(d+=`${o}_`),{keyCookieName:n,csrfCookieName:i,oneTimeCodeName:d+=s}}},4820:(e,r,t)=>{t.d(r,{A:()=>d});var a=t(67619),s=t(29021),o=t.n(s),n=t(33873),i=t.n(n);function d({userId:e}){try{let r=i().resolve(process.cwd(),`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${e}/main.json`);return JSON.parse(o().readFileSync(r,"utf-8"))}catch(e){return(0,a.A)({component:"grabUserSchemaData",message:e.message}),null}}},48786:(e,r,t)=>{t.d(r,{A:()=>a});async function a(...e){let r=global.DSQL_DB_CONN;try{if(!r)throw Error("No Connection provided to DB_HANDLER function!");let t=await r.query(...e);return JSON.parse(JSON.stringify(t))}catch(e){return console.log("DB Error =>",e),{success:!1,error:e.message}}finally{await r?.end()}}},2706:(e,r)=>{Object.defineProperty(r,"A",{enumerable:!0,get:function(){return t}});var t=function(e){return e.PAGES="PAGES",e.PAGES_API="PAGES_API",e.APP_PAGE="APP_PAGE",e.APP_ROUTE="APP_ROUTE",e.IMAGE="IMAGE",e}({})},89947:(e,r,t)=>{e.exports=t(75600)}};var r=require("../../webpack-api-runtime.js");r.C(e);var t=e=>r(r.s=e),a=r.X(0,[2282],()=>t(15679));module.exports=a})();