dsql-admin/dsql-app/.local_dist/server/pages/api/sync-table-data.js

16 lines
7.9 KiB
JavaScript
Raw Normal View History

2025-01-14 06:19:19 +00:00
"use strict";(()=>{var e={};e.id=538,e.ids=[538],e.modules={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:()=>S,default:()=>b,routeModule:()=>g});var a={};t.r(a),t.d(a,{default:()=>m});var s=t(89947),o=t(2706),i=t(96762),n=t(67619),l=t(69842),d=t(27825),u=t.n(d),c=t(48786),p=t(33170);async function _({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 i=o[0];a||(a=i.root_user_id)}return{isPriviledged:!0,rootUserId:a}}catch(e){return{isPriviledged:!1,rootUserId:a,error:e.message}}}var f=t(4820);async function m(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let t=await (0,l.A)(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let{paradigm:a,src:s,dst:o}=e.body;try{let i=await _({tbls:[{db:s.db,table:s.table},{db:o.db,table:o.table}],user:t,query:e.query});if(!i.rootUserId||!i.isPriviledged)throw Error(i.error||"Unauthorized!");let n=(0,f.A)({userId:i.rootUserId});if(!n)throw Error("User Schema Data not Found!");let l=`${process.env.DSQL_USER_DB_PREFIX}${i.rootUserId}_${o.db}`,d=`${process.env.DSQL_USER_DB_PREFIX}${i.rootUserId}_${s.db}`,m=n.find(e=>e.dbFullName==l),b=n.find(e=>e.dbFullName==d),S=m?.tables.find(e=>e.tableName==o.table),g=b?.tables.find(e=>e.tableName==s.table);if(!S||!g)throw Error("Operation Failed! Tables Schemas not present.");if(!u().isEqual(g.fields,S.fields))throw Error("Tables incompatible");switch(a){case"Push":let y=await (0,c.A)(`SELECT * FROM ${d}.${s.table}`),h=await (0,p.A)({dbName:o.db,paradigm:"import",payload:y,tableName:o.table,userId:i.rootUserId,payloadType:"object"});return await E({dbName:o.db,tableName:o.table,data:y,userId:i.rootUserId}),r.json({success:!0,payload:h});case"Pull":let $=await (0,c.A)(`SELECT * FROM ${l}.${o.table}`),v=await (0,p.A)({dbName:s.db,paradigm:"import",payload:$,tableName:s.table,userId:i.rootUserId,payloadType:"object"});return await E({dbName:s.db,tableName:s.table,data:$,userId:i.rootUserId}),r.json({success:!0,payload:v})}r.json({success:!1})}catch(e){console.log(e.message),(0,n.default)({component:"/api/sync-table-data",message:e.message,user:t}),r.json({success:!1,msg:e.message})}}async function E({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),i=`DELETE FROM ${s}.${r}`;o?.[0]&&(i+=` WHERE id NOT IN (${o.join(",")})`),await (0,c.A)(i)}let b=(0,i.M)(a,"default"),S=(0,i.M)(a,"config"),g=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:()=>i});var a=t(98e3),s=t(67619),o=t(48786);async function i({paradigm:e,userId:r,dbName:t,tableName:i,payload:n,payloadType:l}){let d={};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}\`.\`${i}\``);d.tableData=u;break;case"import":let c="base64"==l?Buffer.from(n,"base64").toString():n,p="object"===l?n: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:i,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 d}catch(e){return(0,s.default)({component:"/functions/backend/importExportTableDataFn",message:e.message}),null}}},69842:(e,r,t)=>{t.d(r,{A:()=>l});var a=t(48786),s=t(95969),o=t(29021),i=t.n(o),n=t(89159);
2024-12-08 19:56:57 +00:00
Error Message: ${r}
2025-01-13 08:00:21 +00:00
Component: ${t}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(a+=`
2024-12-08 19:56:57 +00:00
User Id: ${e?.id}
User Name: ${e?.first_name} ${e?.last_name}
2025-01-14 06:19:19 +00:00
User Email: ${e?.email}`),o?.url&&(a+=`
URL: ${o.url}`),o?.body&&(a+=`
Request Body: ${JSON.stringify(o.body,null,4)}`),n&&(a+=`
2025-01-13 08:00:21 +00:00
IP: ${n}`),a+=`
2025-01-14 06:19:19 +00:00
Date: ${i.toDateString()}
========================================`,s().existsSync("./.tmp/error.log")||s().writeFileSync("./.tmp/error.log","","utf-8");let l=s().readFileSync("./.tmp/error.log","utf-8");s().writeFileSync("./.tmp/error.log",a),s().appendFileSync("./.tmp/error.log",`
2024-12-08 19:56:57 +00:00
2025-01-14 06:19:19 +00:00
${l}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},95969:(e,r,t)=>{t.d(r,{A:()=>o});var a=t(55511),s=t(79428);function o({encryptedString:e,encryptionKey:r,encryptionSalt:t}){if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let o=r||process.env.DSQL_ENCRYPTION_PASSWORD,i=t||process.env.DSQL_ENCRYPTION_SALT,n=process.env.DSQL_ENCRYPTION_KEY_LENGTH?Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH):24;if(!o?.match(/.{8,}/))return console.log("Decrption key is invalid"),e;if(!i?.match(/.{8,}/))return console.log("Decrption salt is invalid"),e;let l=(0,a.scryptSync)(o,i,n),d=s.Buffer.alloc(16,0),u=(0,a.createDecipheriv)("aes-192-cbc",l,d);try{let r=u.update(e,"hex","utf8");return r+=u.final("utf8")}catch(r){return console.log("Error in decrypting =>",r.message),e}}}};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})();