2024-11-05 11:12:42 +00:00
"use strict" ;
( ( ) => {
var exports = { } ;
exports . id = 4266 ;
exports . ids = [ 4266 ] ;
exports . modules = {
/***/ 6517 :
/***/ ( ( module ) => {
module . exports = require ( "lodash" ) ;
/***/ } ) ,
/***/ 6109 :
/***/ ( ( module ) => {
module . exports = require ( "sanitize-html" ) ;
/***/ } ) ,
/***/ 2261 :
/***/ ( ( module ) => {
module . exports = require ( "serverless-mysql" ) ;
/***/ } ) ,
/***/ 7441 :
/***/ ( ( module ) => {
module . exports = require ( "sharp" ) ;
/***/ } ) ,
/***/ 4300 :
/***/ ( ( module ) => {
module . exports = require ( "buffer" ) ;
/***/ } ) ,
/***/ 2081 :
/***/ ( ( module ) => {
module . exports = require ( "child_process" ) ;
/***/ } ) ,
/***/ 6113 :
/***/ ( ( module ) => {
module . exports = require ( "crypto" ) ;
/***/ } ) ,
/***/ 7147 :
/***/ ( ( module ) => {
module . exports = require ( "fs" ) ;
/***/ } ) ,
/***/ 3685 :
/***/ ( ( module ) => {
module . exports = require ( "http" ) ;
/***/ } ) ,
/***/ 1017 :
/***/ ( ( module ) => {
module . exports = require ( "path" ) ;
/***/ } ) ,
/***/ 6339 :
/***/ ( ( _ _unused _webpack _module , _ _webpack _exports _ _ , _ _webpack _require _ _ ) => {
// ESM COMPAT FLAG
_ _webpack _require _ _ . r ( _ _webpack _exports _ _ ) ;
// EXPORTS
_ _webpack _require _ _ . d ( _ _webpack _exports _ _ , {
"default" : ( ) => ( /* binding */ handler )
} ) ;
// EXTERNAL MODULE: ./package-shared/functions/backend/encrypt.js
var encrypt = _ _webpack _require _ _ ( 7547 ) ;
// EXTERNAL MODULE: ./functions/backend/fsWriteImageToDiskFromBase64.js
var fsWriteImageToDiskFromBase64 = _ _webpack _require _ _ ( 5910 ) ;
// EXTERNAL MODULE: ./functions/backend/serverError.js
var serverError = _ _webpack _require _ _ ( 2163 ) ;
var serverError _default = /*#__PURE__*/ _ _webpack _require _ _ . n ( serverError ) ;
// EXTERNAL MODULE: ./functions/backend/userAuth.js
var userAuth = _ _webpack _require _ _ ( 6825 ) ;
var userAuth _default = /*#__PURE__*/ _ _webpack _require _ _ . n ( userAuth ) ;
// EXTERNAL MODULE: external "lodash"
var external _lodash _ = _ _webpack _require _ _ ( 6517 ) ;
var external _lodash _default = /*#__PURE__*/ _ _webpack _require _ _ . n ( external _lodash _ ) ;
// EXTERNAL MODULE: ./package-shared/utils/backend/global-db/DB_HANDLER.js
var DB _HANDLER = _ _webpack _require _ _ ( 2224 ) ;
var DB _HANDLER _default = /*#__PURE__*/ _ _webpack _require _ _ . n ( DB _HANDLER ) ;
// EXTERNAL MODULE: ./functions/backend/importExportTableDataFn.js
var importExportTableDataFn = _ _webpack _require _ _ ( 2630 ) ;
// EXTERNAL MODULE: ./functions/backend/grabDelegatedUserFromCookie.js
var grabDelegatedUserFromCookie = _ _webpack _require _ _ ( 1352 ) ;
; // CONCATENATED MODULE: ./functions/backend/delegatedUser.js
// @ts-check
/ * *
* === === === === === === === === === === === === === === === === === === === === === === === === === ===
* Imports
* === === === === === === === === === === === === === === === === === === === === === === === === === ===
* / c o n s t h t t p = _ _ w e b p a c k _ r e q u i r e _ _ ( 3 6 8 5 ) ;
const delegatedUser _serverError = _ _webpack _require _ _ ( 2163 ) ;
const delegatedUser _DB _HANDLER = _ _webpack _require _ _ ( 2224 ) ;
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ / * *
* @ typedef { object } delegatedUserTableObject
* @ property { string } db
* @ property { string } table
* / / * *
* === === === === === === === === === === === === === === === === === === === === === === === === === ===
* Main Function
* === === === === === === === === === === === === === === === === === === === === === === === === === ===
* @ async
*
* @ param { object } params
* @ param { delegatedUserTableObject [ ] } params . tbls - Database name slug
* @ param { { id : number , first _name : string , last _name : string } } params . user - user object
* @ param { any } params . query - query params
*
* @ returns { Promise < {
* isPriviledged : boolean ,
* rootUserId ? : number ,
* error ? : string ,
* } > } new user auth object payload
* / a s y n c f u n c t i o n d e l e g a t e d U s e r ( { t b l s , u s e r , q u e r y } ) {
/ * *
* Fetch user
*
* @ description Fetch user from db
* / l e t r o o t U s e r I d ;
if ( ! query ? . delegated ) {
return {
isPriviledged : true ,
rootUserId : user . id
} ;
}
try {
for ( let i = 0 ; i < tbls . length ; i ++ ) {
const obj = tbls [ i ] ;
const check = await delegatedUser _DB _HANDLER ( ` SELECT * FROM delegated_user_tables WHERE delegated_user_id=' ${ user . id } ' AND \` database \` =' ${ obj . db } ' AND \` table \` =' ${ obj . table } ' ` ) ;
if ( ! check ? . [ 0 ] ? . root _user _id ) throw new Error ( "Unauthorized!" ) ;
const delegateObj = check [ 0 ] ;
if ( ! rootUserId ) rootUserId = delegateObj . root _user _id ;
}
return {
isPriviledged : true ,
rootUserId
} ;
} catch ( /** @type {any} */ error ) {
return {
isPriviledged : false ,
rootUserId ,
error : error . message
} ;
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
} ////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
// EXTERNAL MODULE: ./functions/backend/grabUserSchemaData.js
var grabUserSchemaData = _ _webpack _require _ _ ( 8164 ) ;
; // CONCATENATED MODULE: ./pages/api/sync-table-data.js
// @ts-check
/ * *
* === === === === === === === === === === === === === === === === === === === === === === === === === ===
* Imports
* === === === === === === === === === === === === === === === === === === === === === === === === === ===
* / c o n s t f s = _ _ w e b p a c k _ r e q u i r e _ _ ( 7 1 4 7 ) ;
const path = _ _webpack _require _ _ ( 1017 ) ;
const { execSync } = _ _webpack _require _ _ ( 2081 ) ;
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ / * *
* @ typedef { object } SyncTableBodySrcDstObject
* @ property { string } db
* @ property { string } table
* / / * *
* @ typedef { object } SyncTableBody
* @ property { "Pull" | "Push" } paradigm
* @ property { SyncTableBodySrcDstObject } src
* @ property { SyncTableBodySrcDstObject } dst
* @ property { any } [ delegated ]
* / / * *
* API handler
* === === === === === === === === === === === === === === === === === === === === === === === === === ===
* @ type { import ( "next" ) . NextApiHandler }
* / a s y n c f u n c t i o n h a n d l e r ( r e q , r e s ) {
/ * *
* Check method
*
* @ description Check request method and return if invalid
* / i f ( r e q . m e t h o d ! = = " P O S T " ) r e t u r n r e s . j s o n ( {
msg : "Failed!"
} ) ;
/ * *
* User auth
*
* @ description Authenticate user
* / c o n s t u s e r = a w a i t u s e r A u t h _ d e f a u l t ( ) ( r e q , r e s , t r u e ) ;
if ( ! user ) {
return res . json ( {
success : false ,
msg : "Unauthorized"
} ) ;
}
/ * *
* @ type { SyncTableBody }
* / c o n s t { p a r a d i g m , s r c , d s t } = r e q . b o d y ;
/ * *
* Send Response
*
* @ description Send a boolean response
* / t r y {
const delegatedUserObj = await delegatedUser ( {
tbls : [
{
db : src . db ,
table : src . table
} ,
{
db : dst . db ,
table : dst . table
} ,
] ,
user ,
query : req . query
} ) ;
if ( ! delegatedUserObj . rootUserId || ! delegatedUserObj . isPriviledged ) throw new Error ( delegatedUserObj . error || "Unauthorized!" ) ;
const userDbSchemaData = ( 0 , grabUserSchemaData [ "default" ] ) ( {
userId : delegatedUserObj . rootUserId
} ) ;
if ( ! userDbSchemaData ) throw new Error ( "User Schema Data not Found!" ) ;
const dstDbName = ` ${ process . env . DSQL _USER _DB _PREFIX } ${ delegatedUserObj . rootUserId } _ ${ dst . db } ` ;
const srcDbName = ` ${ process . env . DSQL _USER _DB _PREFIX } ${ delegatedUserObj . rootUserId } _ ${ src . db } ` ;
/** @type {import("@/package-shared/types").DSQL_DatabaseSchemaType | undefined} */ const dstDbSchema = userDbSchemaData . find ( ( db ) => db . dbFullName == dstDbName ) ;
/** @type {import("@/package-shared/types").DSQL_DatabaseSchemaType | undefined} */ const srcDbSchema = userDbSchemaData . find ( ( db ) => db . dbFullName == srcDbName ) ;
const dstTableSchema = dstDbSchema ? . tables . find ( ( tbl ) => tbl . tableName == dst . table ) ;
const srcTableSchema = srcDbSchema ? . tables . find ( ( tbl ) => tbl . tableName == src . table ) ;
if ( ! dstTableSchema || ! srcTableSchema ) throw new Error ( "Operation Failed! Tables Schemas not present." ) ;
const isEqual = external _lodash _default ( ) . isEqual ( srcTableSchema . fields , dstTableSchema . fields ) ;
if ( ! isEqual ) throw new Error ( "Tables incompatible" ) ;
switch ( paradigm ) {
case "Push" :
const srcData = await DB _HANDLER _default ( ) ( ` SELECT * FROM ${ srcDbName } . ${ src . table } ` ) ;
const pushData = await ( 0 , importExportTableDataFn /* default */ . Z ) ( {
dbName : dst . db ,
paradigm : "import" ,
payload : srcData ,
tableName : dst . table ,
userId : delegatedUserObj . rootUserId ,
payloadType : "object"
} ) ;
await pruneTable ( {
dbName : dst . db ,
tableName : dst . table ,
data : srcData ,
userId : delegatedUserObj . rootUserId
} ) ;
return res . json ( {
success : true ,
payload : pushData
} ) ;
case "Pull" :
const dstData = await DB _HANDLER _default ( ) ( ` SELECT * FROM ${ dstDbName } . ${ dst . table } ` ) ;
const pullData = await ( 0 , importExportTableDataFn /* default */ . Z ) ( {
dbName : src . db ,
paradigm : "import" ,
payload : dstData ,
tableName : src . table ,
userId : delegatedUserObj . rootUserId ,
payloadType : "object"
} ) ;
await pruneTable ( {
dbName : src . db ,
tableName : src . table ,
data : dstData ,
userId : delegatedUserObj . rootUserId
} ) ;
return res . json ( {
success : true ,
payload : pullData
} ) ;
default :
break ;
}
res . json ( {
success : false
} ) ;
////////////////////////////////////////
} catch ( /** @type {any} */ error ) {
console . log ( error . message ) ;
////////////////////////////////////////
serverError _default ( ) ( {
component : "/api/sync-table-data" ,
message : error . message ,
user : user
} ) ;
res . json ( {
success : false ,
msg : error . message
} ) ;
////////////////////////////////////////
}
}
/ * *
*
* @ param { object } param0
* @ param { string | number } param0 . userId
* @ param { string } param0 . dbName
* @ param { string } param0 . tableName
* @ param { any [ ] } param0 . data
* / a s y n c f u n c t i o n p r u n e T a b l e ( { d b N a m e , t a b l e N a m e , d a t a , u s e r I d } ) {
const dbFullName = ` ${ process . env . DSQL _USER _DB _PREFIX } ${ userId } _ ${ dbName . replace ( / /g , "" ) } ` ;
const ids = data . map ( ( dt ) => dt . id ) ;
let query = ` DELETE FROM ${ dbFullName } . ${ tableName } ` ;
if ( ids ? . [ 0 ] ) query += ` WHERE id NOT IN ( ${ ids . join ( "," ) } ) ` ;
const prune = await DB _HANDLER _default ( ) ( query ) ;
}
/***/ } )
} ;
;
// load runtime
var _ _webpack _require _ _ = require ( "../../webpack-api-runtime.js" ) ;
_ _webpack _require _ _ . C ( exports ) ;
var _ _webpack _exec _ _ = ( moduleId ) => ( _ _webpack _require _ _ ( _ _webpack _require _ _ . s = moduleId ) )
2024-11-05 14:18:40 +00:00
var _ _webpack _exports _ _ = _ _webpack _require _ _ . X ( 0 , [ 2224 , 2163 , 6825 , 3017 , 3403 , 7547 , 5886 , 5338 , 1352 , 6715 , 8164 , 5910 , 2630 ] , ( ) => ( _ _webpack _exec _ _ ( 6339 ) ) ) ;
2024-11-05 11:12:42 +00:00
module . exports = _ _webpack _exports _ _ ;
} ) ( ) ;