"use strict";(()=>{var e={};e.id=8282,e.ids=[8282],e.modules={76102:e=>{e.exports=require("@moduletrace/datasquirel/dist/client")},39383:e=>{e.exports=require("@moduletrace/datasquirel/dist/package-shared/actions/users/user-auth")},65967:e=>{e.exports=require("@moduletrace/datasquirel/dist/package-shared/utils/ejson")},93726:e=>{e.exports=require("@moduletrace/datasquirel/dist/package-shared/utils/numberfy")},30889:e=>{e.exports=require("@moduletrace/datasquirel/dist/package-shared/utils/slugify")},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")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},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}}})},19991:(e,r,s)=>{s.r(r),s.d(r,{config:()=>S,default:()=>d,routeModule:()=>p});var t={};s.r(t),s.d(t,{default:()=>u});var o=s(89947),a=s(2706),n=s(96762),i=s(48786),_=s(68829),c=s(67619),l=s(69842);async function u(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let{user:s,appData:t}=await (0,l.A)(e,r,!0);if(!s)return r.json({success:!1,msg:"Unauthorized"});let o=e.body.rootUserId,a=e.body.priviledges,n=e.body.database;try{let e=await (0,i.A)("SELECT * FROM user_users WHERE user_id=? AND invited_user_id=? AND MATCH(database_access) AGAINST(?)",[o,s.id,n]);if(!e||!e[0])throw Error(`user with id ${s.id} has no access to ${n} database belonging to user ${o}`);let t=(await (0,i.A)("SELECT * FROM delegated_user_tables WHERE root_user_id=? AND delegated_user_id=? AND `database`=?",[o,s.id,n])).map(e=>e.table),c=await (0,i.A)("SELECT * FROM users WHERE id=?",[o]),l={rootUserId:o,rootUserName:`${c[0].first_name} ${c[0].last_name}`,rootUserEmail:c[0].email,rootUserImage:c[0].image_thumbnail,delegatedUserId:s.id,databaseFullName:`datasquirel_user_${o}_${n}`,databaseSlug:n,allowedTables:t,priviledges:a,dateCreated:Date.now()},u=(0,_.A)({data:JSON.stringify(l)}),d=`${process.env.DSQL_USER_DB_PREFIX}${l.rootUserId}_${n}`,S=`${process.env.DSQL_USER_DELEGATED_DB_COOKIE_PREFIX}${d}`;r.setHeader("Set-Cookie",[`${S}=${u};path=/`]),r.json({success:!0,msg:"Processed Successfully"})}catch(e){return console.log("ERROR:",e.message),(0,c.A)({component:"/api/getDelegatedDatabaseToken/main-catch-error",message:e.message,user:s}),r.json({success:!1,msg:"Unauthorized"})}}let d=(0,n.M)(t,"default"),S=(0,n.M)(t,"config"),p=new o.PagesAPIRouteModule({definition:{kind:a.A.PAGES_API,page:"/api/getDelegatedDatabaseToken",pathname:"/api/getDelegatedDatabaseToken",bundlePath:"",filename:""},userland:t})},55153:(e,r,s)=>{s.d(r,{On:()=>o.a,_n:()=>n}),s(76102);var t=s(65967),o=s.n(t);s(30889);var a=s(93726);let n=s.n(a)()},69842:(e,r,s)=>{s.d(r,{A:()=>n});var t=s(39383),o=s.n(t),a=s(55153);async function n(e,r,s,t){let n=o()({req:e,level:s?"deep":void 0}),i={DSQL_REMOTE_SQL_HOST:process.env.DSQL_REMOTE_SQL_HOST,DSQL_SU_USER_ID:process.env.DSQL_SU_USER_ID,DSQL_HOST_ENV:process.env.DSQL_HOST_ENV,DSQL_HOST:process.env.DSQL_HOST,DSQL_STATIC_HOST:process.env.DSQL_STATIC_HOST,DSQL_GOOGLE_CLIENT_ID:process.env.DSQL_GOOGLE_CLIENT_ID,DSQL_TINY_MCE_API_KEY:process.env.DSQL_TINY_MCE_API_KEY};return t&&n.payload?.id&&(0,a._n)(process.env.DSQL_SU_USER_ID)!==n.payload.id?{user:null,appData:i}:{user:n.payload,appData:i}}},67619:(e,r,s)=>{s.d(r,{A:()=>a});var t=s(29021),o=s.n(t);async function a({user:e,message:r,component:s,noMail:t,req:a}){let n=new Date,i=(()=>{if(!a)return null;try{let e=a.headers["x-forwarded-for"],r=a.headers["x-real-ip"],s=a.headers["cf-connecting-ip"],t=Array.isArray(e)?e[0]:e?.split(",")[0],o=s||t||r||a.socket.remoteAddress;if(!o)return null;return String(o)}catch(e){return null}})();try{let t=`🚀 SERVER ERROR ===========================
Error Message: ${r}
Component: ${s}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(t+=`
User Id: ${e?.id}
User Name: ${e?.first_name} ${e?.last_name}
User Email: ${e?.email}`),a?.url&&(t+=`
URL: ${a.url}`),a?.body&&(t+=`
Request Body: ${JSON.stringify(a.body,null,4)}`),i&&(t+=`
IP: ${i}`),t+=`
Date: ${n.toDateString()}
========================================`,o().existsSync("./.tmp/error.log")||o().writeFileSync("./.tmp/error.log","","utf-8");let _=o().readFileSync("./.tmp/error.log","utf-8");o().writeFileSync("./.tmp/error.log",t),o().appendFileSync("./.tmp/error.log",`




${_}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},68829:(e,r,s)=>{s.d(r,{A:()=>n});var t=s(55511),o=s(79428),a=s(31542);function n({data:e,encryptionKey:r,encryptionSalt:s}){if(!e?.match(/./))return console.log("Encryption string is invalid"),e;let{key:n,salt:i,keyLen:_,algorithm:c,bufferAllocSize:l}=(0,a.A)({encryptionKey:r});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 u=(0,t.scryptSync)(n,i,_),d=o.Buffer.alloc(l,0),S=(0,t.createCipheriv)(c,u,d);try{let r=S.update(e,"utf8","hex");return r+=S.final("hex")}catch(r){return console.log("Error in encrypting =>",r.message),e}}},48786:(e,r,s)=>{s.d(r,{A:()=>o});var t=s(7447);async function o(...e){let r=(0,t.A)();try{if(!r)throw Error("No Connection provided to DB_HANDLER function!");let s=await r.query(...e);return JSON.parse(JSON.stringify(s))}catch(e){return console.log("DB Error =>",e),{success:!1,error:e.message}}finally{await r?.end()}}},7447:(e,r,s)=>{s.d(r,{A:()=>a});var t=s(9973),o=s.n(t);function a(e){return global.DSQL_USE_LOCAL||e?.local?global.DSQL_DB_CONN||o()({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_USERNAME,password:process.env.DSQL_DB_PASSWORD,database:e?.noDb?void 0:process.env.DSQL_DB_NAME,port:process.env.DSQL_DB_PORT?Number(process.env.DSQL_DB_PORT):void 0,charset:"utf8mb4"}}):e?.ro?global.DSQL_READ_ONLY_DB_CONN||o()({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_READ_ONLY_USERNAME,password:process.env.DSQL_DB_READ_ONLY_PASSWORD,port:process.env.DSQL_DB_PORT?Number(process.env.DSQL_DB_PORT):void 0,charset:"utf8mb4"}}):e?.fa?global.DSQL_FULL_ACCESS_DB_CONN||o()({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_FULL_ACCESS_USERNAME,password:process.env.DSQL_DB_FULL_ACCESS_PASSWORD,port:process.env.DSQL_DB_PORT?Number(process.env.DSQL_DB_PORT):void 0,charset:"utf8mb4"}}):global.DSQL_DB_CONN||o()({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_USERNAME,password:process.env.DSQL_DB_PASSWORD,database:e?.noDb?void 0:process.env.DSQL_DB_NAME,port:process.env.DSQL_DB_PORT?Number(process.env.DSQL_DB_PORT):void 0,charset:"utf8mb4"}})}},31542:(e,r,s)=>{s.d(r,{A:()=>o});var t=s(82535);function o(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?(0,t.A)(process.env.DSQL_ENCRYPTION_BUFFER_ALLOCATION_SIZE):void 0)||16}}},82535:(e,r,s)=>{s.d(r,{A:()=>t});function t(e,r){try{let s=Number(e);if("number"!=typeof s||isNaN(s))return 0;if(r)return Number(s.toFixed(r));return Math.round(s)}catch(e){return console.log(`Numberfy ERROR: ${e.message}`),0}}},2706:(e,r)=>{Object.defineProperty(r,"A",{enumerable:!0,get:function(){return s}});var s=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,s)=>{e.exports=s(75600)}};var r=require("../../webpack-api-runtime.js");r.C(e);var s=r(r.s=19991);module.exports=s})();