dsql-admin/dsql-app/.local_dist/server/pages/api/sync-table-data.js
Benjamin Toby 81cf010cb5 Updates
2025-01-14 16:27:08 +01:00

16 lines
5.7 KiB
JavaScript

"use strict";(()=>{var e={};e.id=538,e.ids=[538],e.modules={39908:e=>{e.exports=require("@moduletrace/datasquirel/dist/users/user-auth")},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,t)=>{t.r(r),t.d(r,{config:()=>E,default:()=>y,routeModule:()=>$});var a={};t.r(a),t.d(a,{default:()=>f});var s=t(89947),o=t(2706),l=t(96762),i=t(67619),d=t(69842),n=t(27825),u=t.n(n),c=t(48786),p=t(33170);async function m({tbls:e,user:r,query:t}){let a;if(!t?.delegated)return{isPriviledged:!0,rootUserId:r.id};try{for(let t=0;t<e.length;t++){let s=e[t],o=await (0,c.A)(`SELECT * FROM delegated_user_tables WHERE delegated_user_id='${r.id}' AND \`database\`='${s.db}' AND \`table\`='${s.table}'`);if(!o?.[0]?.root_user_id)throw Error("Unauthorized!");let l=o[0];a||(a=l.root_user_id)}return{isPriviledged:!0,rootUserId:a}}catch(e){return{isPriviledged:!1,rootUserId:a,error:e.message}}}var b=t(4820);async function f(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let t=await (0,d.A)(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let{paradigm:a,src:s,dst:o}=e.body;try{let l=await m({tbls:[{db:s.db,table:s.table},{db:o.db,table:o.table}],user:t,query:e.query});if(!l.rootUserId||!l.isPriviledged)throw Error(l.error||"Unauthorized!");let i=(0,b.A)({userId:l.rootUserId});if(!i)throw Error("User Schema Data not Found!");let d=`${process.env.DSQL_USER_DB_PREFIX}${l.rootUserId}_${o.db}`,n=`${process.env.DSQL_USER_DB_PREFIX}${l.rootUserId}_${s.db}`,f=i.find(e=>e.dbFullName==d),y=i.find(e=>e.dbFullName==n),E=f?.tables.find(e=>e.tableName==o.table),$=y?.tables.find(e=>e.tableName==s.table);if(!E||!$)throw Error("Operation Failed! Tables Schemas not present.");if(!u().isEqual($.fields,E.fields))throw Error("Tables incompatible");switch(a){case"Push":let S=await (0,c.A)(`SELECT * FROM ${n}.${s.table}`),h=await (0,p.A)({dbName:o.db,paradigm:"import",payload:S,tableName:o.table,userId:l.rootUserId,payloadType:"object"});return await g({dbName:o.db,tableName:o.table,data:S,userId:l.rootUserId}),r.json({success:!0,payload:h});case"Pull":let _=await (0,c.A)(`SELECT * FROM ${d}.${o.table}`),A=await (0,p.A)({dbName:s.db,paradigm:"import",payload:_,tableName:s.table,userId:l.rootUserId,payloadType:"object"});return await g({dbName:s.db,tableName:s.table,data:_,userId:l.rootUserId}),r.json({success:!0,payload:A})}r.json({success:!1})}catch(e){console.log(e.message),(0,i.A)({component:"/api/sync-table-data",message:e.message,user:t}),r.json({success:!1,msg:e.message})}}async function g({dbName:e,tableName:r,data:t,userId:a}){let s=`${process.env.DSQL_USER_DB_PREFIX}${a}_${e.replace(/ /g,"")}`,o=t.map(e=>e.id),l=`DELETE FROM ${s}.${r}`;o?.[0]&&(l+=` WHERE id NOT IN (${o.join(",")})`),await (0,c.A)(l)}let y=(0,l.M)(a,"default"),E=(0,l.M)(a,"config"),$=new s.PagesAPIRouteModule({definition:{kind:o.A.PAGES_API,page:"/api/sync-table-data",pathname:"/api/sync-table-data",bundlePath:"",filename:""},userland:a})},33170:(e,r,t)=>{t.d(r,{A:()=>l});var a=t(98e3),s=t(67619),o=t(48786);async function l({paradigm:e,userId:r,dbName:t,tableName:l,payload:i,payloadType:d}){let n={};try{let s=`${process.env.DSQL_USER_DB_PREFIX}${r}_${t.replace(/ /g,"")}`;switch(e){case"export":let u=await (0,o.A)(`SELECT * FROM \`${s}\`.\`${l}\``);n.tableData=u;break;case"import":let c="base64"==d?Buffer.from(i,"base64").toString():i,p="object"===d?i:JSON.parse(c);for(let e=0;e<p.length;e++){let r=p[e],t=await (0,a.A)({data:r,dbFullName:s,tableName:l,dbContext:"Dsql User",paradigm:"Full Access",duplicateColumnName:"id",duplicateColumnValue:r?.id,update:!0});if(t.error)throw Error(t.error)}break;default:return null}return n}catch(e){return(0,s.A)({component:"/functions/backend/importExportTableDataFn",message:e.message}),null}}},69842:(e,r,t)=>{t.d(r,{A:()=>o});var a=t(39908),s=t.n(a);async function o(e,r,t){let a=s()({req:e,level:t?"deep":void 0});return console.log(a),a.payload}},4820:(e,r,t)=>{t.d(r,{A:()=>d});var a=t(67619),s=t(29021),o=t.n(s),l=t(33873),i=t.n(l);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}}},67619:(e,r,t)=>{t.d(r,{A:()=>o});var a=t(29021),s=t.n(a);async function o({user:e,message:r,component:t,noMail:a,req:o}){let l=new Date,i=(()=>{if(!o)return null;try{let e=o.headers["x-forwarded-for"],r=o.headers["x-real-ip"],t=o.headers["cf-connecting-ip"],a=Array.isArray(e)?e[0]:e?.split(",")[0],s=t||a||r||o.socket.remoteAddress;if(!s)return null;return String(s)}catch(e){return null}})();try{let a=`🚀 SERVER ERROR ===========================
Error Message: ${r}
Component: ${t}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(a+=`
User Id: ${e?.id}
User Name: ${e?.first_name} ${e?.last_name}
User Email: ${e?.email}`),o?.url&&(a+=`
URL: ${o.url}`),o?.body&&(a+=`
Request Body: ${JSON.stringify(o.body,null,4)}`),i&&(a+=`
IP: ${i}`),a+=`
Date: ${l.toDateString()}
========================================`,s().existsSync("./.tmp/error.log")||s().writeFileSync("./.tmp/error.log","","utf-8");let d=s().readFileSync("./.tmp/error.log","utf-8");s().writeFileSync("./.tmp/error.log",a),s().appendFileSync("./.tmp/error.log",`
${d}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}}};var r=require("../../webpack-api-runtime.js");r.C(e);var t=e=>r(r.s=e),a=r.X(0,[1251],()=>t(69653));module.exports=a})();