UNPKG

yekonga-server

Version:
594 lines (463 loc) 19.8 kB
/*global Yekonga */ // @ts-nocheck const H = Yekonga.Helper; const notificationKey = '__systemNotification__'; if (!Yekonga.CloudService) Yekonga.CloudService = {} if (!Yekonga.CloudService.Functions) Yekonga.CloudService.Functions = {} if (!Yekonga.CloudService.Resolvers) Yekonga.CloudService.Resolvers = {} if (!Yekonga.CloudService.Triggers) Yekonga.CloudService.Triggers = {} if (!Yekonga.CloudService.Actions) Yekonga.CloudService.Actions = {} if (!Yekonga.CloudService.Auth) Yekonga.CloudService.Auth = {} if (!Yekonga.CloudService.Middleware) Yekonga.CloudService.Middleware = [] if (!Yekonga.CloudService.initMiddleware) Yekonga.CloudService.initMiddleware = [] if (!Yekonga.CloudService.beforeAllMiddleware) Yekonga.CloudService.beforeAllMiddleware = [] if (!Yekonga.CloudService.socketListeners) Yekonga.CloudService.socketListeners = {} const CloudFunctions = {} CloudFunctions.define = (name, callback) => { Yekonga.CloudService.Functions[name] = callback; } CloudFunctions.run = async(name, payload) => { if (Yekonga.CloudService.Functions[name]) { var data = await Yekonga.CloudService.Functions[name](payload); return data; } try { throw new Yekonga.Error.Read(`Cloud function ${name} is not defined, please create this cloud function`); } catch (error) { console.error(`CloudFunction: ${name}`, error); } } CloudFunctions.exists = (name) => { if (Yekonga.CloudService.Functions[name]) { return true; } return false; } CloudFunctions.has = (name) => { return CloudFunctions.exists(name); } CloudFunctions.setMiddleware = (callback) => { Yekonga.CloudService.Middleware.push(callback); } CloudFunctions.setInitMiddleware = (callback) => { Yekonga.CloudService.initMiddleware.push(callback); } CloudFunctions.setBeforeAllMiddleware = (callback) => { Yekonga.CloudService.beforeAllMiddleware.push(callback); } /** * * @param {String} action * @param {Function} callback */ CloudFunctions.onMessage = (action, callback) => { if(!Array.isArray(Yekonga.CloudService.socketListeners[action])) { Yekonga.CloudService.socketListeners[action] = []; } Yekonga.CloudService.socketListeners[action].push(callback); } /** * * @param {String} namespace * @param {String} action * @param {Object} payload * @returns */ CloudFunctions.runOnMessage = async (namespace, action, payload) => { if (Yekonga.CloudService.socketListeners[namespace]) { var funcs = await Yekonga.CloudService.socketListeners[namespace]; if(Array.isArray(funcs)) { for (const f of funcs) { if(typeof f == 'function') { f(namespace, action, payload); } } } return; } try { throw new Yekonga.Error.Read(`Socket Listeners ${action} is not defined, please create this cloud function`); } catch (error) { console.error(`SocketListeners: ${action}`, error); } } CloudFunctions.setHeadContent = (callback) => { Yekonga.CloudService.indexHeadContent = callback; } CloudFunctions.headContent = async (req, res) => { if(typeof Yekonga.CloudService.indexHeadContent === 'function') { return await Yekonga.CloudService.indexHeadContent(req, res); } return ''; } CloudFunctions.setTemplateData = (callback) => { Yekonga.CloudService.indexTemplateData = callback; } CloudFunctions.templateData = async (req, res) => { var data = {}; if(typeof Yekonga.CloudService.indexTemplateData === 'function') { data = await Yekonga.CloudService.indexTemplateData(req, res); } return (data)? data: {}; } CloudFunctions.setSystemConfig = (callback) => { Yekonga.CloudService.indexSystemConfig = callback; } CloudFunctions.systemConfig = async (req, res) => { var data = {}; if(typeof Yekonga.CloudService.indexSystemConfig === 'function') { data = await Yekonga.CloudService.indexSystemConfig(req, res); } return (data && typeof data == 'object')? data: {}; } CloudFunctions.setSystemScript = (callback) => { Yekonga.CloudService.indexSystemScript = callback; } CloudFunctions.systemScript = async (req, res) => { if(typeof Yekonga.CloudService.indexSystemScript === 'function') { return await Yekonga.CloudService.indexSystemScript(req, res); } return null; } CloudFunctions.setProfileConfig = (callback) => { Yekonga.CloudService.indexProfileConfig = callback; } CloudFunctions.profileConfig = async (id) => { var data = {}; if(typeof Yekonga.CloudService.indexProfileConfig === 'function') { data = await Yekonga.CloudService.indexProfileConfig(id); } return (data && typeof data == 'object')? data: {}; } CloudFunctions.setAfterFileUpload = (callback) => { Yekonga.CloudService.afterFileUpload = callback; } CloudFunctions.afterFileUpload = async ({files, req, res}) => { var data = null; if (typeof Yekonga.CloudService.afterFileUpload === 'function') { data = await Yekonga.CloudService.afterFileUpload({files, req, res}); } return data; } CloudFunctions.setAction = (name, action, accessRole, route, callback) => { if (!Yekonga.CloudService.Actions[name]) { Yekonga.CloudService.Actions[name] = {} } if(typeof accessRole == 'string') { if (!Yekonga.CloudService.Actions[name][accessRole]) { Yekonga.CloudService.Actions[name][accessRole] = {} } if(typeof route == 'string') { if (!Yekonga.CloudService.Actions[name][accessRole][route]) { Yekonga.CloudService.Actions[name][accessRole][route] = {} } Yekonga.CloudService.Actions[name][accessRole][route][action] = callback; } else if(typeof route == 'function') { Yekonga.CloudService.Actions[name][accessRole][action] = route; } } else if(typeof accessRole == 'function') { Yekonga.CloudService.Actions[name][action] = accessRole; } } CloudFunctions.setResolver = (name, callback) => { Yekonga.CloudService.Resolvers[name] = callback; } CloudFunctions.setQueryResolver = (name, callback) => { Yekonga.CloudService.Resolvers[`Query.${name}`] = callback; } CloudFunctions.setMutationResolver = (name, callback) => { Yekonga.CloudService.Resolvers[`Mutation.${name}`] = callback; } CloudFunctions.getResolver = (name) => { if (Yekonga.CloudService.Resolvers[name]) { return Yekonga.CloudService.Resolvers[name]; } return null; } CloudFunctions.getQueryResolver = (name) => { if (Yekonga.CloudService.Resolvers[`Query.${name}`]) { return Yekonga.CloudService.Resolvers[`Query.${name}`]; } return null; } CloudFunctions.getMutationResolver = (name) => { if (Yekonga.CloudService.Resolvers[`Mutation.${name}`]) { return Yekonga.CloudService.Resolvers[`Mutation.${name}`]; } return null; } CloudFunctions.authorization = (callback) => { Yekonga.CloudService.Auth.authorization = callback; } CloudFunctions.customLogin = (callback) => { Yekonga.CloudService.Auth.customLogin = callback; } CloudFunctions.beforeLogin = (callback) => { Yekonga.CloudService.Auth.beforeLogin = callback; } CloudFunctions.afterLogin = (callback) => { Yekonga.CloudService.Auth.afterLogin = callback } CloudFunctions.customOtp = (callback) => { Yekonga.CloudService.Auth.customOtp = callback; } CloudFunctions.beforeOtp = (callback) => { Yekonga.CloudService.Auth.beforeOtp = callback; } CloudFunctions.afterOtp = (callback) => { Yekonga.CloudService.Auth.afterOtp = callback } CloudFunctions.customRegistration = (callback) => { Yekonga.CloudService.Auth.customRegistration = callback; } CloudFunctions.beforeRegistration = (callback) => { Yekonga.CloudService.Auth.beforeRegistration = callback; } CloudFunctions.afterRegistration = (callback) => { Yekonga.CloudService.Auth.afterRegistration = callback; } CloudFunctions.customResetPassword = (callback) => { Yekonga.CloudService.Auth.customResetPassword = callback; } CloudFunctions.beforeResetPassword = (callback) => { Yekonga.CloudService.Auth.beforeResetPassword = callback; } CloudFunctions.afterResetPassword = (callback) => { Yekonga.CloudService.Auth.afterResetPassword = callback; } CloudFunctions.customChangePassword = (callback) => { Yekonga.CloudService.Auth.customChangePassword = callback; } CloudFunctions.beforeChangePassword = (callback) => { Yekonga.CloudService.Auth.beforeChangePassword = callback; } CloudFunctions.afterChangePassword = (callback) => { Yekonga.CloudService.Auth.afterChangePassword = callback; } CloudFunctions.customLogout = (callback) => { Yekonga.CloudService.Auth.customLogout = callback; } CloudFunctions.beforeLogout = (callback) => { Yekonga.CloudService.Auth.beforeLogout = callback; } CloudFunctions.afterLogout = (callback) => { Yekonga.CloudService.Auth.afterLogout = callback; } function registerBeforeAndAfterCallback(action, name, accessRole, route, callback) { if (!Yekonga.CloudService.Triggers[name]) { Yekonga.CloudService.Triggers[name] = {} } if(typeof accessRole == 'string') { if (!Yekonga.CloudService.Triggers[name][accessRole]) { Yekonga.CloudService.Triggers[name][accessRole] = {} } if(typeof route == 'string') { if (!Yekonga.CloudService.Triggers[name][accessRole][route]) { Yekonga.CloudService.Triggers[name][accessRole][route] = {} } Yekonga.CloudService.Triggers[name][accessRole][route][action] = callback; } else if(typeof route == 'function') { Yekonga.CloudService.Triggers[name][accessRole][action] = route; } } else if(typeof accessRole == 'function') { Yekonga.CloudService.Triggers[name][action] = accessRole; } } CloudFunctions.getBeforeAndAfterCallback = function(action, name, accessRole, route, isAdmin = false) { if (!isAdmin && Yekonga.CloudService.Triggers[name]) { if(typeof accessRole == 'string') { if(typeof route == 'string') { if( Yekonga.CloudService.Triggers[name][accessRole] && Yekonga.CloudService.Triggers[name][accessRole][route] && Yekonga.CloudService.Triggers[name][accessRole][route][action] ) { return Yekonga.CloudService.Triggers[name][accessRole][route][action]; } } else if( Yekonga.CloudService.Triggers[name][accessRole] && Yekonga.CloudService.Triggers[name][accessRole][action] ) { return Yekonga.CloudService.Triggers[name][accessRole][action]; } else if( Yekonga.CloudService.Triggers[name][route] && Yekonga.CloudService.Triggers[name][route][action] ) { return Yekonga.CloudService.Triggers[name][route][action]; } } if(Yekonga.CloudService.Triggers[name][action]) { return Yekonga.CloudService.Triggers[name][action]; } } return null; } CloudFunctions.getBeforeAndAfterNotificationCallback = function(action) { return CloudFunctions.getBeforeAndAfterCallback(action, notificationKey, null, null, true); } CloudFunctions.beforeFind = (name, accessRole, route, callback) => { registerBeforeAndAfterCallback('beforeFind', name, accessRole, route, callback); } CloudFunctions.afterFind = (name, accessRole, route, callback) => { registerBeforeAndAfterCallback('afterFind', name, accessRole, route, callback); } CloudFunctions.beforeSave = (name, accessRole, route, callback) => { registerBeforeAndAfterCallback('beforeSave', name, accessRole, route, callback); } CloudFunctions.afterSave = (name, accessRole, route, callback) => { registerBeforeAndAfterCallback('afterSave', name, accessRole, route, callback); } CloudFunctions.beforeCreate = (name, accessRole, route, callback) => { registerBeforeAndAfterCallback('beforeCreate', name, accessRole, route, callback); } CloudFunctions.afterCreate = (name, accessRole, route, callback) => { registerBeforeAndAfterCallback('afterCreate', name, accessRole, route, callback); } CloudFunctions.beforeUpdate = (name, accessRole, route, callback) => { registerBeforeAndAfterCallback('beforeUpdate', name, accessRole, route, callback); } CloudFunctions.afterUpdate = (name, accessRole, route, callback) => { registerBeforeAndAfterCallback('afterUpdate', name, accessRole, route, callback); } CloudFunctions.beforeDelete = (name, accessRole, route, callback) => { registerBeforeAndAfterCallback('beforeDelete', name, accessRole, route, callback); } CloudFunctions.afterDelete = (name, accessRole, route, callback) => { registerBeforeAndAfterCallback('afterDelete', name, accessRole, route, callback); } CloudFunctions.beforeCreate('TranslatorTranslation', async({ input, context }) => { const Auth = Yekonga.Auth; if (Array.isArray(input)) { let updatedInput = []; for (let row of input) { const exists = await Yekonga.Model.TranslatorTranslation.findOne({ locale: { equalTo: row.locale }, group: { equalTo: row.group }, item: { equalTo: row.item }, }, null, true); if (!exists) { let text = Yekonga.Helper.getTitle(row.item); if (!row.descriptions || (row.descriptions && row.descriptions.trim() == '')) { row.descriptions = text; } if (!row.text || (row.text && row.text.trim() == '')) { row.text = text; } if (!row.translatorLanguageId || (row.translatorLanguageId && row.translatorLanguageId.trim() == '')) { let language = await Yekonga.Model.TranslatorLanguage.findOne({ locale: { equalTo: row.locale } }, null, true); row.translatorLanguageId = (language) ? language.translatorLanguageId : row.locale; } if (!row.unstable) row.unstable = false; if (!row.locked) row.locked = false; updatedInput.push(row); } else if (exists && (!exists.text || exists.text.trim() == '')) { let text = Yekonga.Helper.getTitle(row.item); if (!row.descriptions || (row.descriptions && row.descriptions.trim() == '')) { row.descriptions = text; } if (!row.text || (row.text && row.text.trim() == '')) { row.text = text; } await Yekonga.Model.TranslatorTranslation.update({ text: row.text, descriptions: row.descriptions, }, { translatorLanguageId: exists.translatorLanguageId }, null, true); } } return updatedInput; } }); CloudFunctions.afterCreate('TranslatorLanguage', async({ result, input, context }) => { const Auth = Yekonga.Auth; if (result.data && !Array.isArray(result.data)) result.data = [result.data]; for (const data of result.data) { Yekonga.Helper.translateProcess(true, data.locale, data.name, data.flag); } }); CloudFunctions.beforeNotification = (callback) => { registerBeforeAndAfterCallback('beforeNotification', notificationKey, null, null, callback); } CloudFunctions.afterNotification = (callback) => { registerBeforeAndAfterCallback('afterNotification', notificationKey, null, null, callback); } CloudFunctions.onReady = function(callback) { if(!Array.isArray(Yekonga.CloudService.callbacks)){ Yekonga.CloudService.callbacks = []; } Yekonga.CloudService.callbacks.push(callback); } CloudFunctions.socket = function(event, callback) { if (Yekonga.socketSystem) { Yekonga.socketSystem.on(event, callback); } } CloudFunctions.emit = function(event, data) { if (Yekonga.socketSystem) { Yekonga.socketSystem.emit(event, data); } } const MigrateFunctions = {}; MigrateFunctions.run = async function(callback) { return await MigrateFunctions.function(null, callback, 'function'); } MigrateFunctions.rename = async function(collection, newCollection, saveFile = true) { return await MigrateFunctions.function(collection, newCollection, 'renameTable', saveFile); } MigrateFunctions.empty = async function(collection, saveFile = true) { return await MigrateFunctions.function(collection, null, 'emptyTable', saveFile); } MigrateFunctions.drop = async function(collection, saveFile = true) { return await MigrateFunctions.function(collection, null, 'dropTable', saveFile); } MigrateFunctions.create = async function(collection, callback, saveFile = true) { return await MigrateFunctions.function(collection, callback, 'create', saveFile); } MigrateFunctions.update = async function(collection, callback, saveFile = true) { return await MigrateFunctions.function(collection, callback, 'alter', saveFile); } MigrateFunctions.alter = async function(collection, callback, saveFile = true) { return await MigrateFunctions.update(collection, callback, saveFile); } MigrateFunctions.collection = async function(collection, callback, saveFile = true) { return await MigrateFunctions.update(collection, callback, saveFile); } MigrateFunctions.function = async function(collection, callback, type, saveFile = true) { var result = null; if (collection) { var build = new Yekonga.MigrationBuilder(collection, type); if (H.tableActions.includes(type)) { if (type == 'renameTable') { build.renameTable(callback); } else if (type == 'emptyTable') { build.emptyTable(); } else if (type == 'dropTable') { build.dropTable(); } } if (typeof callback == 'function') { callback(build); } result = await build.run(); } else if (typeof callback == 'function') { var _response = await callback(); if (_response == false) { result = { error: { status: 'FAIL' } } } else if (_response == true || typeof _response == 'undefined') { result = { data: { status: "SUCCESS" } } } else if (_response && typeof _response == 'object') { result = _response } } if (!saveFile) return result; return async function(filename) { if (result && !result.error) { await Yekonga.MigrationDB.table('migrations').create({ 'name': filename }); } return result } } // module.exports.CloudFunctions = CloudFunctions; // module.exports.MigrateFunctions = MigrateFunctions; module.exports = { CloudFunctions: {...CloudFunctions }, MigrateFunctions: {...MigrateFunctions } };