"use strict";(()=>{var e={};e.id=538,e.ids=[538],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")},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")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},33873:e=>{e.exports=require("path")},69653:(e,r,a)=>{a.r(r),a.d(r,{config:()=>E,default:()=>h,routeModule:()=>y});var t={};a.r(t),a.d(t,{default:()=>g});var s=a(89947),d=a(2706),i=a(96762),o=a(67619),l=a(69842),u=a(27825),n=a.n(u),c=a(48786),b=a(33170);async function p({tbls:e,user:r,query:a}){let t;if(!a?.delegated)return{isPriviledged:!0,rootUserId:r.id};try{for(let a=0;a<e.length;a++){let s=e[a],d=await (0,c.A)(`SELECT * FROM delegated_user_tables WHERE delegated_user_id='${r.id}' AND \`database\`='${s.db}' AND \`table\`='${s.table}'`);if(!d?.[0]?.root_user_id)throw Error("Unauthorized!");let i=d[0];t||(t=i.root_user_id)}return{isPriviledged:!0,rootUserId:t}}catch(e){return{isPriviledged:!1,rootUserId:t,error:e.message}}}var m=a(4820);async function g(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let{user:a,appData:t}=await (0,l.A)(e,r,!0);if(!a)return r.json({success:!1,msg:"Unauthorized"});let{paradigm:s,src:d,dst:i}=e.body;try{let t=await p({tbls:[{db:d.db,table:d.table},{db:i.db,table:i.table}],user:a,query:e.query});if(!t.rootUserId||!t.isPriviledged)throw Error(t.error||"Unauthorized!");let o=(0,m.A)({userId:t.rootUserId});if(!o)throw Error("User Schema Data not Found!");let l=`${process.env.DSQL_USER_DB_PREFIX}${t.rootUserId}_${i.db}`,u=`${process.env.DSQL_USER_DB_PREFIX}${t.rootUserId}_${d.db}`,g=o.find(e=>e.dbFullName==l),h=o.find(e=>e.dbFullName==u),E=g?.tables.find(e=>e.tableName==i.table),y=h?.tables.find(e=>e.tableName==d.table);if(!E||!y)throw Error("Operation Failed! Tables Schemas not present.");if(!n().isEqual(y.fields,E.fields))throw Error("Tables incompatible");switch(s){case"Push":let _=await (0,c.A)(`SELECT * FROM ${u}.${d.table}`),q=await (0,b.A)({dbName:i.db,paradigm:"import",payload:_,tableName:i.table,userId:t.rootUserId,payloadType:"object"});return await f({dbName:i.db,tableName:i.table,data:_,userId:t.rootUserId}),r.json({success:!0,payload:q});case"Pull":let $=await (0,c.A)(`SELECT * FROM ${l}.${i.table}`),I=await (0,b.A)({dbName:d.db,paradigm:"import",payload:$,tableName:d.table,userId:t.rootUserId,payloadType:"object"});return await f({dbName:d.db,tableName:d.table,data:$,userId:t.rootUserId}),r.json({success:!0,payload:I})}r.json({success:!1})}catch(e){console.log(e.message),(0,o.A)({component:"/api/sync-table-data",message:e.message,user:a}),r.json({success:!1,msg:e.message})}}async function f({dbName:e,tableName:r,data:a,userId:t}){let s=`${process.env.DSQL_USER_DB_PREFIX}${t}_${e.replace(/ /g,"")}`,d=a.map(e=>e.id),i=`DELETE FROM ${s}.${r}`;d?.[0]&&(i+=` WHERE id NOT IN (${d.join(",")})`),await (0,c.A)(i)}let h=(0,i.M)(t,"default"),E=(0,i.M)(t,"config"),y=new s.PagesAPIRouteModule({definition:{kind:d.A.PAGES_API,page:"/api/sync-table-data",pathname:"/api/sync-table-data",bundlePath:"",filename:""},userland:t})},4820:(e,r,a)=>{a.d(r,{A:()=>l});var t=a(67619),s=a(29021),d=a.n(s),i=a(33873),o=a.n(i);function l({userId:e}){try{let r=o().resolve(process.cwd(),`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${e}/main.json`);return JSON.parse(d().readFileSync(r,"utf-8"))}catch(e){return(0,t.A)({component:"grabUserSchemaData",message:e.message}),null}}}};var r=require("../../webpack-api-runtime.js");r.C(e);var a=e=>r(r.s=e),t=r.X(0,[7337],()=>a(69653));module.exports=t})();