yekonga-server
Version:
Yekonga Server
289 lines (242 loc) • 10.9 kB
JavaScript
// @ts-nocheck
/*global Yekonga, serverLibrary */
const H = Yekonga.Helper;
const fs = serverLibrary.fs;
const path = serverLibrary.path;
const DB = Yekonga.DB;
const Devices = ['MOBILE', 'WEB', 'DESKTOP'];
const PageTypes = ['CREATE', 'EDIT', 'FORM', 'LIST', 'VIEW', 'DETAIL', 'CUSTOM', 'IMPORT'];
const ViewComponent = null;
const TemplateConfig = require(path.join(__dirname, '../../', 'core/frontendTemplate'));
const resolvers = {
Query: {
_languages: async function(parent, params, context) {
return await Yekonga.DB.table('languages').find();
},
_translations: async function(parent, params, context) {
return await Yekonga.DB.table('translations').find();
},
_databaseStructure: async function(parent, params, context) {
const { search, limit, page } = (params) ? params: {};
var result = [];
for (const row of Yekonga.Schema) {
var item = {
name: row._id.collection,
secondaryKey: null,
fields: [],
}
item.class = H.getClass(item.name);
item.variableKey = H.getVariable(item.class);
for (const key in row) {
if (row.hasOwnProperty(key)) {
var field = row[key];
field.name = key;
if (field.source && !field.reference) {
field.reference = field.source;
}
item.fields.push(field);
if (H.getClass(field.name) == item.class) {
item.secondaryKey = field.name;
}
}
}
if (search) {
if (row._id.collection.toLowerCase().includes(search.toLowerCase())) {
result.push(item);
}
} else {
result.push(item);
}
}
return result;
},
_configurations: async function(_, params, context) {
const configTypes = serverLibrary.config;
function convertData(data, types) {
const config = [];
for (const key in types) {
if (Object.hasOwnProperty.call(types, key)) {
const type = types[key];
const value = data[key];
if (typeof type == 'string') {
config.push({
key: key,
title: H.getTitle(key),
type: type,
value: Array.isArray(value) ? value.join(', ') : value,
});
} else if (typeof value == 'object') {
config.push({
key: key,
title: H.getTitle(key),
type: 'Object',
value: convertData(value, type),
});
}
}
}
return config;
}
const config = convertData(Yekonga.Config, configTypes);
return config;
},
_structure: async function(_, params, context) {
const { type } = (params) ? params: {};
var result = { type: type, data: [] }
const templateConfig = TemplateConfig();
if (type == 'sidebar') {
result.data = templateConfig.sidebarMenu;
} else if (type == 'routes') {
result.data = templateConfig.routes;
} else if (type == 'defaultRoutes') {
result.data = templateConfig.defaultRoutes;
} else if (type == 'profileMenu') {
result.data = templateConfig.profileMenu;
} else if (type == 'quickMenu') {
result.data = templateConfig.quickMenu;
} else if (type == 'report') {
result.data = templateConfig.reportConfig;
}
return result;
},
},
Mutation: {
_createLanguage: async function(_, params, context) {
const { input } = params;
H.translateProcess(true, input.local, input.name, input.flag);
return { status: true, message: 'Success' };
},
_resetPermission: async function(_, params, context) {
const { input, } = params;
const baseActions = {
'create': 'Create',
'import': 'Import',
'edit': 'Edit',
'information': 'View information of',
'delete': 'Delete',
'list': 'List of',
'download': 'Download',
'summary': 'Summary report',
'action': 'Action for'
};
for (const row of Yekonga.Schema) {
for (const name in baseActions) {
if (baseActions.hasOwnProperty(name)) {
const collection = row._id.collection;
const group = H.getVariable(collection, true);
const description = `${baseActions[name]} ${H.getTitle(collection).toLowerCase()}`;
await H.savePermission(Yekonga.DB, {
namespace: null,
group: group,
name: name,
description: description,
});
}
}
}
return { status: true, message: 'Success' };
},
_insertData: async function(_, params, context) {
const { input, collection, _id } = params;
var result = await Yekonga.DB.table(collection).create(input);
var status = (result.affectedCount > 0);
return {
success: status,
status: status,
message: (result.error) ? result.error : `${result.affectedCount} document inserted`,
};
},
_updateData: async function(_, params, context) {
const { input, collection, _id } = params;
const where = { _id: { "$eq": H.ObjectId(_id) } };
var result = await Yekonga.DB.table(collection).where(where).update(input);
var status = (result.affectedCount > 0);
return {
success: status,
status: status,
message: (result.error) ? result.error : `${result.affectedCount} document updated`,
};
},
_deleteData: async function(_, params, context) {
const { collection, _id } = params;
const where = { _id: { "$eq": H.ObjectId(_id) } };
var result = await Yekonga.DB.table(collection).where(where).delete();
var status = (result.affectedCount > 0);
return {
success: status,
status: status,
message: (result.error) ? result.error : `${result.affectedCount} document deleted`,
};
},
_addCollection: async function(_, params, context) {
const { collection } = params;
var result = await Yekonga.DB.table(collection).create(input);
return result;
},
_updateCollection: async function(_, params, context) {
const { from, to } = params;
var result = await Yekonga.DB.table(collection).create(input);
return result;
},
_deleteCollection: async function(_, params, context) {
const { collection } = params;
var result = await Yekonga.DB.table(collection).create(input);
return result;
},
_updateConfiguration: async function(_, params, context) {
const { input } = params;
for (const row of input) {
if (row.key) {
var config = null
var keys = row.key.split('.');
keys.reverse();
for (const k of keys) {
var con = {};
con[k] = (config) ? config : row.value;
config = con;
}
}
if (config.logoUrl) {
var publicDir = Array.isArray(Yekonga.Config.public) ?
Yekonga.Config.public[0] :
Yekonga.Config.public;
config.logoUrl = 'uploads/' + H.saveFile(config.logoUrl, `${publicDir}/uploads`);
config.logoUrl == Array.isArray(config.logoUrl) ? config.logoUrl.pop() : config.logoUrl;
}
console.log(config);
H.mergeJSON(Yekonga.Config, config);
const jsonStr = JSON.stringify(Yekonga.Config, null, 4);
H.writeFile('config.json', jsonStr, true);
}
return {
status: true,
message: `Configuration saved`
};
},
_updateStructure: async function(_, params, context) {
const { input, type } = params;
const templateFile = path.join(serverLibrary.__dirname, 'archives', 'structure.json');
const templateConfig = TemplateConfig();
if (type == 'sidebar') {
templateConfig.sidebarMenu = input.data;
} else if (type == 'routes') {
templateConfig.routes = input.data;
} else if (type == 'defaultRoutes') {
templateConfig.defaultRoutes = input.data;
} else if (type == 'profileMenu') {
templateConfig.profileMenu = input.data;
} else if (type == 'quickMenu') {
templateConfig.quickMenu = input.data;
} else if (type == 'report') {
templateConfig.reportConfig = input.data;
}
Yekonga.FrontendTemplateConfiguration = templateConfig;
H.writeFile(templateFile, templateConfig, false);
return {
status: true,
message: `Configuration saved`
};
}
},
}
module.exports = resolvers;