"use strict";(()=>{var e={};e.id=4371,e.ids=[4371],e.modules={39383:e=>{e.exports=require("@moduletrace/datasquirel/dist/package-shared/actions/users/user-auth")},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")},81147:(e,t,r)=>{r.r(t),r.d(t,{config:()=>h,default:()=>_,routeModule:()=>N});var a={};r.r(a),r.d(a,{default:()=>m});var s=r(89947),l=r(2706),n=r(96762),i=r(29021),o=r.n(i),u=r(48786),c=r(31495),d=r(60125),g=r(67619),p=r(69842),f=r(30802),b=r(76856);async function m(e,t){if("POST"!==e.method)return t.json({msg:"Failed!"});let r=await (0,p.A)(e,t,!0);if(!r)return t.json({success:!1,msg:"Unauthorized"});let{dbFullName:a,dbSlug:s,dbId:l,tableNameOld:n,payload:i,delegated:m}=e.body,{tableName:_,tableFullName:h,tableDescription:N}=i;if(!_?.match(/./)||_.match(/[^a-zA-Z0-9-_]/))return t.json({success:!1,msg:"Bad Input"});try{let i=await (0,d.A)({databaseSlug:s,request:e,user:r,query:e.query});if(!await (0,c.A)({user:r,confirmedDelegetedUser:i,database:s,table:n,dbId:l,priviledgeRegex:/Edit Table/i}))return console.log("User Unauthorized"),t.json({success:!1,msg:"Unauthorized"});let p=i?.rootUserId?i.rootUserId:r.id,y=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${p}/main.json`,E=JSON.parse(o().readFileSync(y,"utf8")),S=E.filter(e=>e.dbFullName===a)[0].tables.filter((e,t)=>{if(e.tableName===n)return!0});if(S&&S[0])S[0].tableName=_,S[0].tableNameOld=n,S[0].tableFullName=h,N&&(S[0].tableDescription=N),S[0].childrenTables&&S[0].childrenTables.forEach(e=>{try{E.filter(t=>t.dbFullName===e.dbNameFull)[0].tables.filter(t=>{if(t.tableName===e.tableName)return!0})[0].childTableName=_}catch(e){(0,g.A)({component:"/api/updateUserTable/lines-131-138",message:e.message,user:r})}});else throw Error("Table Does not exist");o().writeFileSync(`${y}`,JSON.stringify(E),"utf8"),await (0,f.A)({dbFullName:"datasquirel",tableName:"user_database_tables",identifierColumnName:"table_slug",identifierValue:n,data:{table_name:h,table_slug:_,table_description:N}}),m&&i&&await (0,u.A)("UPDATE delegated_user_tables SET `table`=? WHERE delegated_user_id=? AND root_user_id=? AND `table`=? AND `database`=?",[_,r.id,i.rootUserId,n,s]);let A=a.replace(/[^a-z\_0-9]/g,"");await (0,b.A)({userId:p,targetDatabase:A}),t.json({success:!0})}catch(e){(0,g.A)({component:"/api/updateUserTable/main-catch-error",message:e.message,user:r}),t.json({success:!1,msg:"Database Files could not be written!"})}}let _=(0,n.M)(a,"default"),h=(0,n.M)(a,"config"),N=new s.PagesAPIRouteModule({definition:{kind:l.A.PAGES_API,page:"/api/updateUserTable",pathname:"/api/updateUserTable",bundlePath:"",filename:""},userland:a})},31495:(e,t,r)=>{r.d(t,{A:()=>l});var a=r(48786),s=r(67619);async function l({user:e,confirmedDelegetedUser:t,database:r,table:l,priviledgeRegex:n,dbId:i}){let o,u;try{t?.delegated?(o=await (0,a.A)("SELECT priviledge FROM delegated_user_tables WHERE root_user_id=? AND delegated_user_id=? AND `database`=? AND `table`=?",[t.rootUserId,e.id,r,l]),u=o[0]?.priviledge?.match(n)):(o=await (0,a.A)("SELECT table_slug FROM user_database_tables WHERE user_id=? AND table_slug=? AND db_id=?",[e.id,l,i]),u=!0)}catch(t){(0,s.A)({component:"checkUserRights",message:t.message,user:e})}return!!o&&!!o[0]&&!!u}},60125:(e,t,r)=>{r.d(t,{A:()=>s});var a=r(95969);async function s({request:e,databaseSlug:t,user:r,query:s}){try{let l=r.id,n=null;if(!s?.delegated)return{dbUserId:l};let i=s.dbUserId,o=`${process.env.DSQL_USER_DB_PREFIX}${i}_${t}`,u=`${process.env.DSQL_USER_DELEGATED_DB_COOKIE_PREFIX}${o}`;try{if(!e.cookies?.[u])throw Error("Cookie not present");let r=(0,a.A)({encryptedString:e.cookies[u]});if(!r)throw Error("Invalid Token");if((n=JSON.parse(r)).databaseSlug===t)return{dbUserId:l=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:l}}catch(e){return null}}},69842:(e,t,r)=>{r.d(t,{A:()=>l});var a=r(39383),s=r.n(a);async function l(e,t,r){return s()({req:e,level:r?"deep":void 0}).payload}},30802:(e,t,r)=>{r.d(t,{A:()=>u});var a=r(72583),s=r.n(a),l=r(92652),n=r(68829),i=r(52939),o=r(64292);async function u({dbContext:e,dbFullName:t,tableName:r,data:a,tableSchema:u,identifierColumnName:c,identifierValue:d,encryptionKey:g,encryptionSalt:p}){if(!a||!Object.keys(a).length)return null;let f=(0,i.A)({dbContext:e,dbFullName:t}),b=f?global.DSQL_DB_CONN:global.DSQL_FULL_ACCESS_DB_CONN||global.DSQL_DB_CONN;f?global.DSQL_DB_CONN:global.DSQL_READ_ONLY_DB_CONN||global.DSQL_DB_CONN;let m=Object.keys(a),_=[],h=[];for(let e=0;ee.fieldName===t):null,o=i&&i[0]?i[0]:null;if(null==r||void 0==r)continue;let c=/<[^>]+>/g;(o?.richText||String(r).match(c))&&(r=s()(r,l.A)),o?.encrypted&&(r=(0,n.A)({data:r,encryptionKey:g,encryptionSalt:p})),"object"==typeof r&&(r=JSON.stringify(r)),o?.pattern&&!new RegExp(o.pattern,o.patternFlags||"").test(r)&&(console.log("DSQL: Pattern not matched =>",r),r=""),"string"==typeof r&&r.match(/^null$/i)&&(r={toSqlString:function(){return"NULL"}}),"string"!=typeof r||r.match(/./i)||(r={toSqlString:function(){return"NULL"}}),_.push(`\`${t}\`=?`),"number"==typeof r?h.push(String(r)):h.push(r)}catch(e){console.log("DSQL: Error in parsing data keys in update function =>",e.message);continue}_.push(`date_updated='${Date()}'`),_.push(`date_updated_code='${Date.now()}'`);let N=`UPDATE ${f?"":`\`${t}\`.`}\`${r}\` SET ${_.join(",")} WHERE \`${c}\`=?`;return h.push(d),await (0,o.A)(b,N,h)}},92652:(e,t,r)=>{r.d(t,{A:()=>a});let a={allowedTags:["b","i","em","strong","a","p","span","ul","ol","li","h1","h2","h3","h4","h5","h6","img","div","button","pre","code","br"],allowedAttributes:{a:["href"],img:["src","alt","width","height","class","style"],"*":["style","class"]}}},95969:(e,t,r)=>{r.d(t,{A:()=>n});var a=r(55511),s=r(79428),l=r(32443);function n({encryptedString:e,encryptionKey:t,encryptionSalt:r}){if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let{key:n,salt:i,keyLen:o,algorithm:u,bufferAllocSize:c}=(0,l.A)({encryptionKey:t});if(!n?.match(/.{8,}/))return console.log("Decrption key is invalid"),e;if(!i?.match(/.{8,}/))return console.log("Decrption salt is invalid"),e;let d=(0,a.scryptSync)(n,i,o),g=s.Buffer.alloc(c,0),p=(0,a.createDecipheriv)(u,d,g);try{let t=p.update(e,"hex","utf8");return t+=p.final("utf8")}catch(t){return console.log("Error in decrypting =>",t.message),e}}},68829:(e,t,r)=>{r.d(t,{A:()=>n});var a=r(55511),s=r(79428),l=r(32443);function n({data:e,encryptionKey:t,encryptionSalt:r}){if(!e?.match(/./))return console.log("Encryption string is invalid"),e;let{key:n,salt:i,keyLen:o,algorithm:u,bufferAllocSize:c}=(0,l.A)({encryptionKey:t});if(!n?.match(/.{8,}/))return console.log("Encryption key is invalid"),e;if(!i?.match(/.{8,}/))return console.log("Encryption salt is invalid"),e;let d=(0,a.scryptSync)(n,i,o),g=s.Buffer.alloc(c,0),p=(0,a.createCipheriv)(u,d,g);try{let t=p.update(e,"utf8","hex");return t+=p.final("hex")}catch(t){return console.log("Error in encrypting =>",t.message),e}}},48786:(e,t,r)=>{r.d(t,{A:()=>s});var a=r(7447);async function s(...e){let t=(0,a.A)();try{if(!t)throw Error("No Connection provided to DB_HANDLER function!");let r=await t.query(...e);return JSON.parse(JSON.stringify(r))}catch(e){return console.log("DB Error =>",e),{success:!1,error:e.message}}finally{await t?.end()}}},52939:(e,t,r)=>{r.d(t,{A:()=>a});function a({dbContext:e,dbFullName:t}){return!e?.match(/dsql.user/i)&&(!!global.DSQL_USE_LOCAL||!t||!!t.match(/^datasquirel$/))}},64292:(e,t,r)=>{async function a(e,t,r){try{if(!e)throw Error("No Connection Found!");if(!t)throw Error("Query String Required!");if("string"==typeof t){let a=await e.query(s(t),r);return JSON.parse(JSON.stringify(a))}if("object"!=typeof t)return null;{let r=[];for(let a=0;aa})},32443:(e,t,r)=>{r.d(t,{A:()=>a});function a(e){return{key:e?.encryptionKey||process.env.DSQL_ENCRYPTION_PASSWORD,keyLen:process.env.DSQL_ENCRYPTION_KEY_LENGTH?Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH):24,salt:e?.encryptionSalt||process.env.DSQL_ENCRYPTION_SALT,apiKey:e?.apiKey||process.env.DSQL_API_KEY,algorithm:e?.algorithm||process.env.DSQL_ENCRYPTION_ALGORITHM||"aes-192-cbc",bufferAllocSize:e?.bufferAllocSize||(process.env.DSQL_ENCRYPTION_BUFFER_ALLOCATION_SIZE?function(e,t){try{let t=Number(e);if("number"!=typeof t||isNaN(t))return 0;return Math.round(t)}catch(e){return console.log(`Numberfy ERROR: ${e.message}`),0}}(process.env.DSQL_ENCRYPTION_BUFFER_ALLOCATION_SIZE):void 0)||16}}}};var t=require("../../webpack-api-runtime.js");t.C(e);var r=e=>t(t.s=e),a=t.X(0,[6934],()=>r(81147));module.exports=a})();