yekonga-server
Version:
Yekonga Server
567 lines (520 loc) • 19.4 kB
JavaScript
// @ts-nocheck
/*global Yekonga, serverLibrary */
const H = Yekonga.Helper;
const ClazzData = Yekonga.DataTypes.ClazzData;
const FieldData = Yekonga.DataTypes.FieldData;
const fs = serverLibrary.fs;
const path = serverLibrary.path;
module.exports = function() {
var data = {}
var isTemplate = true;
var exists = false;
if (!Yekonga.FrontendTemplateConfiguration) {
var _data = {};
var templateFile = path.join(serverLibrary.__dirname, 'archives', 'structure.json');
if (fs.existsSync(templateFile)) {
_data = H.readFile(templateFile);
if (!_data && (_data || typeof _data != 'object')) {
_data = {}
}
}
if (!_data.sidebarMenu) {
_data.sidebarMenu = getSidebarMenu();
}
if (!_data.profileMenu) {
_data.profileMenu = getProfileMenu();
}
if (!_data.quickMenu) {
_data.quickMenu = getQuickMenu();
}
if (!_data.routes) {
_data.routes = {};
}
if (!_data.defaultRoutes) {
_data.defaultRoutes = getRoutes();
}
_data.reportConfig = getReportConfig();
data = _data;
H.writeFile(templateFile, data, false);
Yekonga.FrontendTemplateConfiguration = data;
} else {
data = Yekonga.FrontendTemplateConfiguration;
}
data.logo = getLogo();
data.name = getAppName();
return data;
};
function getLogo() {
return Yekonga.Helper.getLogo();
}
function getAppName() {
return Yekonga.Config.appName;
}
function getSidebarMenu() {
// const data = [];
const data = [{
id: "general-dashboard",
icon: "ye ye-tachometer-alt",
label: "general.dashboard",
to: "/app",
meta: { isAuth: true, isAdmin: true },
},
{
id: "general-users",
icon: "ye ye-users",
label: "general.users",
to: "/app/users",
meta: { isAuth: true, isAdmin: true },
},
{
id: "general-setting",
icon: "ye ye-shield-virus",
label: "general.securitySettings",
to: "/app/settings",
meta: { isAuth: true, isAdmin: true },
subs: [{
id: "setting-permissions",
label: "authPermission.title",
subs: [{
icon: "ye ye-key-skeleton",
label: "authPermission.title",
to: "/app/permissions/auth-permissions",
meta: { isAuth: true, isAdmin: true },
},
{
icon: "ye ye-shield-virus",
label: "authGroup.title",
to: "/app/permissions/auth-groups",
meta: { isAuth: true, isAdmin: true },
},
{
icon: "ye ye-lock-open-alt",
label: "authGroupPermission.title",
to: "/app/permissions/auth-group-permissions",
meta: { isAuth: true, isAdmin: true },
},
{
icon: "ye ye-user-unlock",
label: "authUserGroup.title",
to: "/app/permissions/auth-user-groups",
meta: { isAuth: true, isAdmin: true },
}
]
},
{
id: "setting-translations",
label: "translatorLanguage.title",
subs: [{
icon: "ye ye-language",
label: "translatorLanguage.title",
to: "/app/translator/translator-languages"
},
{
icon: "ye ye-g_translate",
label: "translatorTranslation.title",
to: "/app/translator/translator-translations"
},
]
},
]
},
];
return data;
}
function getReportConfig() {
let models = {}
let systemModels = Yekonga.SystemModels;
for (const key in systemModels) {
if (Object.hasOwnProperty.call(systemModels, key)) {
/** @type {ClazzData} */
const md = systemModels[key];
const fields = md.fields.map((e) => {
return {
name: e.name,
type: e.dataType,
input: e.input,
isKey: e.isKey,
parentName: e.relationTable,
options: e.options,
}
});
const children = md.childrenRelation.map(e => {
return {
name: e.name,
primaryKey: e.secondaryKey,
foreignKey: e.foreignKey,
relationName: e.relationName,
}
});
const parents = md.parentRelation.map(e => {
return {
name: e.name,
primaryKey: e.secondaryKey,
foreignKey: e.foreignKey,
relationName: e.relationName,
}
});
models[key] = {
name: md.name,
slug: md.slug,
modelName: md.class,
generalName: md.generaName,
generalNamePlural: H.getVariable(H.toPlural(md.name)),
primaryKey: md.secondaryKey,
primaryName: md.primaryName,
fields: fields,
children: children,
parents: parents,
}
}
}
return models;
}
function getProfileMenu() {
const data = [];
data.push({
id: "general-dashboard",
icon: "ye ye-tachometer-alt",
label: "general.dashboard",
to: "/app",
meta: { isAuth: true, isAdmin: true },
});
data.push({
id: "general-profile",
icon: "ye ye-user",
label: "general.profile",
to: "/app/profile",
meta: { isAuth: true, isAdmin: true },
});
return data;
}
function getQuickMenu() {
const data = [];
data.push({
id: "general-dashboard",
icon: "ye ye-tachometer-alt",
label: "general.dashboard",
to: "/app",
meta: { isAuth: true, isAdmin: true },
});
data.push({
id: "general-dashboard",
icon: "ye ye-tachometer-alt",
label: "general.dashboard",
to: "/app",
meta: { isAuth: true, isAdmin: true },
});
data.push({
id: "general-dashboard",
icon: "ye ye-tachometer-alt",
label: "general.dashboard",
to: "/app",
meta: { isAuth: true, isAdmin: true },
});
data.push({
id: "general-dashboard",
icon: "ye ye-tachometer-alt",
label: "general.dashboard",
to: "/app",
meta: { isAuth: true, isAdmin: true },
});
return data;
}
function getRoutes() {
const data = {};
const systemFields = ["_id", "id", "code"];
const dataModels = H.getTableDataWithRelations();
for (const key in dataModels) {
/** @type {ClazzData} */
const model = dataModels[key];
const columns = {};
const orderOptions = {};
const searchFields = []
const filters = [];
const actions = [{
label: `${model.generaName}.delete`,
callback: "onDeleteData"
}];
const dataTypes = [];
const listDataTypes = [];
const viewDataTypes = [];
const formFields = [];
for (const field of model.fields) {
columns[field.name] = `${model.generaName}.${field.name}`;
orderOptions[field.name] = `${model.generaName}.${field.name}`;
searchFields.push(field.name);
if (Array.isArray(field.options) && field.options.length) {
var filterValues = {};
for (const option of field.options) {
var itemClass = H.getClass(field.name);
var actionClass = H.getClass(option.value);
actions.push({
label: `${field.name}.set${actionClass}`,
callback: `onCustomAction("${itemClass}", "${actionClass}")`
}, );
filterValues[option.value] = `${model.generaName}.${option.value}`;
}
filters.push({
key: `${field.name}`,
label: `${model.generaName}.${field.name}`,
values: filterValues
});
}
dataTypes.push({
name: `${field.name}`,
type: `${field.input}`
});
if (!field.isRelation && !systemFields.includes(field.name) && field.name != model.secondaryKey) {
viewDataTypes.push({
name: `${field.name}`,
type: `${field.input}`
});
}
if (!systemFields.includes(field.name) && field.name != model.secondaryKey) {
var formData = {
name: `${field.name}`,
type: `${field.type}`,
input: `${field.input}`,
default: (field.default) ? field.default : null,
};
if (Array.isArray(field.options) && field.options.length) {
formData["options"] = field.options;
}
formFields.push(formData);
}
}
for (const parent of model.parentRelation) {
viewDataTypes.push({
name: `${parent.relationName}`,
type: `select`
});
}
for (const child of model.childrenRelation) {
viewDataTypes.push({
name: `${child.relationName}`,
type: `select`
});
}
data[`${model.slug}`] = {
title: `${model.generaName}.${model.generalKeyPlural}`,
type: 'list',
generalName: `${model.generaName}`,
primaryKey: `${model.secondaryKey}`,
primaryName: `${model.primaryName}`,
profileKey: `${model.profileKey}`,
modelName: `${model.modelName}`,
deleteName: `delete${model.modelName}`,
showHeaderSection: false,
showReport: false,
reportId: null,
importData: {
label: "general.importData",
mode: "link",
action: `/app/${model.slug}/import`
},
addData: {
label: "general.addNew",
mode: "link",
action: `/app/${model.slug}/create`
},
listOptions: {
name: `${model.generaName}Paginate`,
listName: `${model.generalKeyPlural}`,
paginateName: `${model.generaName}Paginate`,
downloadName: `download${model.modelName}`,
query: H.getDataQuery(model),
downloadQuery: H.getDataQuery(model),
downloadOrientation: "PORTRAIT",
args: {},
loadingMode: "paginate",
noResultMessage: `${model.generaName}.noData`,
modeOptions: ["list", "thumb", "image"],
showToolbar: true,
showHeader: true,
showFooter: true,
tableMode: true,
padding: "0px",
height: "300px",
searchFields: searchFields,
columns: columns,
orderOptions: orderOptions,
filters: filters,
fields: formFields,
actions: actions,
conditions: [{
key: "status",
value: "inactive",
color: 'red',
background: '#000',
}],
modes: {
list: [{
fields: [{
name: ['firstName', 'lastName'],
type: "text", //
prefix: "",
suffix: "",
},
{
name: ['email'],
type: "text",
prefix: "general.email",
prefixIcon: "ye ye-envelope",
suffix: "",
suffixIcon: "",
color: "red",
size: "12px",
fontWeight: "bold"
}
]
}, {
fields: [{
name: ['role'],
type: "text", //
prefix: "",
suffix: "",
},
{
name: ['status'],
type: "text",
prefix: "",
prefixIcon: "",
suffix: "",
suffixIcon: "",
color: "red",
size: "12px",
fontWeight: "bold"
}
]
}],
_list: {
primaryEnable: true,
fields: formFields,
primaryField: {
name: [`${model.primaryName}`, "lastName"],
subName: ['email'],
hasProfile: true,
profileUrl: 'profileUrl',
type: "text",
link: "/app/${model.slug}/show/:${model.secondaryKey}/info"
},
columns: [{
fields: [{
name: ['firstName'],
type: "text", //
prefix: "",
suffix: "",
},
{
name: ['email'],
type: "text",
prefix: "general.email",
prefixIcon: "",
suffix: "",
suffixIcon: "",
color: "#0000",
size: "12px",
fontWeight: "normal"
}
]
}]
},
thumb: {
fields: formFields,
primaryField: {
name: [`${model.primaryName}`, "measurement"],
subName: [],
type: "text",
link: "/app/${model.slug}/show/:${model.secondaryKey}/info"
},
},
image: {
fields: formFields,
primaryField: {
name: [`${model.primaryName}`, "measurement"],
subName: [],
type: "text",
link: "/app/${model.slug}/show/:${model.secondaryKey}/info"
},
},
}
}
};
data[`${model.slug}/form`] = {
title: "Create Form template",
type: 'form',
generalName: `${model.generaName}`,
primaryKey: `${model.secondaryKey}`,
modelName: `${model.modelName}`,
queryName: `${model.generaName}`,
query: H.getDataQuery(model),
formQuery: H.getFormQuery(model),
inputType: `${model.modelName}Input`,
createMethod: `create${model.modelName}`,
updateMethod: `update${model.modelName}`,
deleteMethod: `delete${model.modelName}`,
form: formFields
};
data[`${model.slug}/create`] = {
title: "Create resource type template",
type: 'create',
primaryKey: `${model.secondaryKey}`,
primaryName: `${model.primaryName}`,
generalName: `${model.generaName}`,
modelName: `${model.modelName}`,
backLink: `/app/${model.slug}`,
};
data[`${model.slug}/update`] = {
title: "Update resource type template",
type: 'update',
primaryKey: `${model.secondaryKey}`,
primaryName: `${model.primaryName}`,
generalName: `${model.generaName}`,
modelName: `${model.modelName}`,
backLink: `/app/${model.slug}`,
};
data[`${model.slug}/view`] = {
title: "Details resource type template",
type: 'view',
primaryKey: `${model.secondaryKey}`,
primaryName: `${model.primaryName}`,
profileKey: null,
generalName: `${model.generaName}`,
modelName: `${model.modelName}`,
queryName: `${model.generaName}`,
query: H.getDataQuery(model),
dataTypes: viewDataTypes,
backLink: `/app/${model.slug}`,
hasProfile: true,
profileUrl: "",
profileLocation: "top", // top, left, right,
profileProperties: {
height: '300px',
width: '200px',
borderRadius: '20px',
borderWidth: '1px',
borderColor: 'red',
borderStyle: 'dotted',
}
};
data[`${model.slug}/show`] = {
title: "Details resource type template",
type: 'show',
primaryKey: `${model.secondaryKey}`,
primaryName: `${model.primaryName}`,
profileKey: null,
generalName: `${model.generaName}`,
modelName: `${model.modelName}`,
backLink: `/app/${model.slug}`,
showHeader: false,
showReport: false,
reportId: null,
routes: [{
name: `Info`,
label: `general.`,
link: `/app/${model.slug}`
}]
};
}
return data;
}