"use strict";(()=>{var e={};e.id=4105,e.ids=[4105],e.modules={60818:e=>{e.exports=require("dotenv")},27825:e=>{e.exports=require("lodash")},75600:e=>{e.exports=require("next/dist/compiled/next-server/pages-api.runtime.prod.js")},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")},81630:e=>{e.exports=require("http")},33873:e=>{e.exports=require("path")},96762:(e,r)=>{Object.defineProperty(r,"M",{enumerable:!0,get:function(){return function e(r,s){return s in r?r[s]:"then"in r&&"function"==typeof r.then?r.then(r=>e(r,s)):"function"==typeof r&&"default"===s?r:void 0}}})},46127:(e,r,s)=>{s.r(r),s.d(r,{config:()=>v,default:()=>m,routeModule:()=>D});var t={};s.r(t),s.d(t,{default:()=>b});var a=s(89947),n=s(2706),i=s(96762),o=s(6420),u=s.n(o),c=s(48225),l=s.n(c);s(27825);var d=s(51348),_=s.n(d),p=s(33873),f=s.n(p);s(79646);var S=s(19261),E=s.n(S);let h=s(29021);async function b(e,r){if("POST"!==e.method)return r.json({user:null,msg:"Registration Failed!"});let s=await u()(e,r,!0);if(!s)return r.json({success:!1,msg:"Unauthorized"});let t=e.body.schema,a=e.body.dbId;try{let e=await _()("SELECT * FROM user_databases WHERE user_id = ? AND id = ?",[s.id,a]);if(!e?.[0])throw Error("Invalid Database");let n=e[0],i=f().resolve(process.cwd(),`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${s.id}/main.json`),o=JSON.parse(h.readFileSync(i,"utf-8")),u=o.find(e=>e.dbFullName==n.db_full_name);if(!u)throw Error("This Database doesn't exist");if(u.tables=t.tables,u.childrenDatabases)for(let e=0;ee.dbFullName===r);if(!s||!s.tables){u.childrenDatabases?.splice(e,1);continue}s.tables=t.tables}h.writeFileSync(`${i}`,JSON.stringify(o),"utf8"),await E()({userId:s.id,targetDatabase:n.db_full_name}),r.json({success:!0})}catch(t){l()({component:"/api/acceptUserInvitation/catch-error",message:t.message,user:s,req:e}),r.json({success:!1})}}let m=(0,i.M)(t,"default"),v=(0,i.M)(t,"config"),D=new a.PagesAPIRouteModule({definition:{kind:n.A.PAGES_API,page:"/api/user-schema/replace-database-schema",pathname:"/api/user-schema/replace-database-schema",bundlePath:"",filename:""},userland:t})},6420:(e,r,s)=>{s(81630);let t=s(51348),a=s(72835),n=s(29021),i=s(31341);async function o(e,r,s,o){let{keyCookieName:u,csrfCookieName:c}=i();if(!e.cookies?.[u]?.match(/./))return null;let l=a({encryptedString:e.cookies[u]});if(!l)return null;let d=JSON.parse(l);if(!d.csrf_k||s&&!e.headers["x-csrf-auth"]?.match(RegExp(`${d.csrf_k}`)))return null;let _=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!_)return console.log("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable."),null;if(s&&!n.existsSync(`${_}/${d.csrf_k}`))return null;if(0==d.verification_status&&!s){let e=await t(`SELECT verification_status FROM users WHERE id='${d.id}'`);e&&e[0]&&1==e[0].verification_status&&r.setHeader("Set-Cookie",["user_refresh=1"])}return d?.date&&Date.now()-d.date>6048e5?null:d}e.exports=o},31341:e=>{e.exports=function(e){let r=process.env.DSQL_COOKIES_PREFIX||"dsql_",s=process.env.DSQL_COOKIES_KEY_NAME||"key",t=process.env.DSQL_COOKIES_CSRF_NAME||"csrf",a=e?.database||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),n=r;e?.userId&&(n+=`user_${e.userId}_`),a&&(n+=`${a}_`),n+=s;let i=r;return e?.userId&&(i+=`user_${e.userId}_`),a&&(i+=`${a}_`),{keyCookieName:n,csrfCookieName:i+=t}}},51348:(e,r,s)=>{let t=s(9973),a=s(21580),n=t({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_USERNAME,password:process.env.DSQL_DB_PASSWORD,database:process.env.DSQL_DB_NAME,port:process.env.DSQL_DB_PORT?Number(process.env.DSQL_DB_PORT):void 0,charset:"utf8mb4",ssl:a()}});async function i(...e){try{let r=await n.query(...e);return await n.end(),JSON.parse(JSON.stringify(r))}catch(e){return console.log("DB Error =>",e),{success:!1,error:e.message}}}e.exports=i},21580:(e,r,s)=>{let t=s(29021);e.exports=function(){let e=process.env.DSQL_SSL_DIR;if(!e?.match(/./))return;let r=`${e}/ca-cert.pem`;if(!t.existsSync(r)){console.log(`${r} does not exist`);return}return{ca:t.readFileSync(`${e}/ca-cert.pem`)}}},2706:(e,r)=>{var s;Object.defineProperty(r,"A",{enumerable:!0,get:function(){return s}}),function(e){e.PAGES="PAGES",e.PAGES_API="PAGES_API",e.APP_PAGE="APP_PAGE",e.APP_ROUTE="APP_ROUTE",e.IMAGE="IMAGE"}(s||(s={}))},89947:(e,r,s)=>{e.exports=s(75600)}};var r=require("../../../webpack-api-runtime.js");r.C(e);var s=e=>r(r.s=e),t=r.X(0,[2451],()=>s(46127));module.exports=t})();