odmdb ans wwws for pageadmin.html

This commit is contained in:
2024-05-02 07:36:23 +02:00
parent 7c4b285995
commit fe64375566
5 changed files with 503 additions and 209 deletions

View File

@@ -214,42 +214,41 @@ Odmdb.Schema = (objectPathname, validschema, lg = "en") => {
}
}
};
const convoptionstoenum=(propertie,lg)=>{
const convoptionstoenum = (propertie, lg) => {
if (!propertie.options) return propertie;
if (!(propertie.options["$ref"])){
propertie.msg="missingref"
return propertie
if (!propertie.options["$ref"]) {
propertie.msg = "missingref";
return propertie;
}
let optionsfile;
let optionstype;
if (propertie.options["$ref"].includes("/options/")) {
optionstype = "options";
optionsfile = path.resolve(
`../../${propertie.options["$ref"]}_${lg}.json`
);
let optionstype;
if (propertie.options["$ref"].includes("/options/")) {
propertie.comment = `options:${propertie.options["$ref"]}}`;
optionstype = "options";
optionsfile = path.resolve(
`../../${propertie.options["$ref"]}_${lg}.json`
);
}
if (propertie.options["$ref"].includes("/idx/")) {
(propertie.comment = `itms:${propertie.options["$ref"]}}`),
(optionstype = "idx");
optionsfile = path.resolve(`../../${propertie.options["$ref"]}.json`);
}
if (log) console.log(currentmod, "Lien vers options:", optionsfile);
if (!fs.existsSync(optionsfile)) {
propertie.msg = "missingref";
return propertie;
} else {
delete propertie.options;
if (optionstype == "options") {
propertie.enum = fs.readJSONSync(optionsfile).lst_idx;
}
if (propertie.options["$ref"].includes("/idx/")) {
optionstype = "idx";
optionsfile = path.resolve(
`../../${propertie.options["$ref"]}.json`
);
if (optionstype == "idx") {
propertie.enum = fs.readJSONSync(optionsfile);
}
if (log) console.log(currentmod, "Lien vers options:", optionsfile);
if (!fs.existsSync(optionsfile)) {
propertie.msg = "missingref";
return propertie;
} else {
delete propertie.options
if (optionstype == "options") {
propertie.enum =
fs.readJSONSync(optionsfile).lst_idx;
}
if (optionstype == "idx") {
propertie.enum = fs.readJSONSync(optionsfile);
}
}
return propertie
}
}
return propertie;
};
if (log) console.log(currentmod, `${objectPathname}/conf.json`);
const res = {
@@ -274,23 +273,29 @@ Odmdb.Schema = (objectPathname, validschema, lg = "en") => {
};
}
// get $ref from $def
if (res.data.schema["$defs"]){
Object.keys(res.data.schema["$defs"]).forEach(ss=>{
Object.keys(res.data.schema["$defs"][ss].properties).forEach(pp=>{
res.data.schema["$defs"][ss].properties[pp]=convoptionstoenum(res.data.schema["$defs"][ss].properties[pp],lg)
})
})
if (res.data.schema["$defs"]) {
Object.keys(res.data.schema["$defs"]).forEach((ss) => {
Object.keys(res.data.schema["$defs"][ss].properties).forEach((pp) => {
res.data.schema["$defs"][ss].properties[pp] = convoptionstoenum(
res.data.schema["$defs"][ss].properties[pp],
lg
);
});
});
}
Object.keys(res.data.schema.properties).forEach((p) => {
if (
res.data.schema.properties[p].type == "object" &&
res.data.schema.properties[p]["$ref"]
) {
let subschema
const localdef=res.data.schema.properties[p]["$ref"].includes("#/")
let subschema;
const localdef = res.data.schema.properties[p]["$ref"].includes("#/");
if (
localdef &&
!(res.data.schema["$defs"] && res.data.schema["$defs"][propertie["$ref"]])
!(
res.data.schema["$defs"] &&
res.data.schema["$defs"][propertie["$ref"]]
)
) {
res.status = 404;
res.msg = "missinglocalref";
@@ -298,21 +303,29 @@ Odmdb.Schema = (objectPathname, validschema, lg = "en") => {
return res;
}
if (localdef) {
res.data.schema.properties[p]=res.data.schema["$defs"][res.data.schema.properties[p]["$ref"]]
}else{
subschema = Odmdb.Schema(path.resolve(res.data.schema.properties[p]["$ref"]), validschema, lg)
if(subschema.status==200){
res.data.schema.properties[p]=subschema.data.schema;
}else{
subschema.data.originschemaproperty=p
return subschema
}
res.data.schema.properties[p] =
res.data.schema["$defs"][res.data.schema.properties[p]["$ref"]];
} else {
subschema = Odmdb.Schema(
path.resolve(res.data.schema.properties[p]["$ref"]),
validschema,
lg
);
if (subschema.status == 200) {
res.data.schema.properties[p] = subschema.data.schema;
} else {
subschema.data.originschemaproperty = p;
return subschema;
}
}
}
if (res.data.schema.properties[p].options){
if (res.data.schema.properties[p].options) {
//remplace options par enum:[]
res.data.schema.properties[p]=convoptionstoenum(res.data.schema.properties[p],lg)
}
res.data.schema.properties[p] = convoptionstoenum(
res.data.schema.properties[p],
lg
);
}
});
if (!res.data.schema.apxid) {
@@ -359,24 +372,78 @@ Odmdb.Schema = (objectPathname, validschema, lg = "en") => {
}
return res;
};
Odmdb.search = (objectPath, objectName, search) => {
Odmdb.search = (objectPathname, objsearch, role) => {
/*
@search= {
txt: string,
algo: match | pattern | fuzzy
fieldstring:[list of field],
indexfilter:{index1:[val1,val2 | ] }
if (fields exist):
return data:{id:{field:value}]}
else:
return data:[id]
exemple:
objsearch= {
apxid:['toto','titi'],fields:['firstname']
}
Return data:[uuids]
return data:{toto:{firstname:"Paul"},titi:"fistname:"Jacques"}
ADD HERE OTHER OPTION USING IDX FOR PERFORMANCE
example: search exact match hill in townId
heavy search={txt:"hill",algo:"match",fieldstring:"toxnId"}
light search={txt:"hill", algo:"match", indexfilter:{"key":"townId","value":[]}}
light search={txt:"hill", algo:"match", indexfilter:{"key":"nationId","value":"ants"}}
*/
const schema = Odmdb.schema(objectPath, objectName);
if (schema.status != 200) return schema;
const getschema = Odmdb.Schema(objectPathname, true);
if (getschema.status != 200) return getschema;
//console.log(getschema.data);
const apxid = getschema.data.apxid;
let subsearch = objsearch.apxid
? objsearch.apxid
: fs.readJSONSync(`${objectPathname}/idx/lst_${apxid}.json`);
// ADD HERE OTHER FILTRATION SEARCH depending of question
if (objsearch.question) {
// remove or add in subsearch
}
if (objsearch.fields) {
const resultat = {};
const accessright = Odmdb.accessright(
getschema.data.schema.apxaccessrights,
role
);
//console.log(accessright);
const ifields={}
subsearch.forEach((i) => {
const ifields = {};
if (fs.existsSync(`${objectPathname}/itm/${i}.json`)) {
const itm = fs.readJSONSync(`${objectPathname}/itm/${i}.json`);
if (itm.owner && itm.owner == role.xalias) {
role.xprofils.push("owner");
}
objsearch.fields.forEach((f) => {
if (accessright.R.includes(f)) {
ifields[f] = itm[f];
} else {
ifields[f] = "unauthorized";
}
});
} else {
ifields.notfound = true;
}
resultat[i] = ifields;
});
return {
status: 200,
ref: "Odmdb",
msg: "resultsearchlist",
data: resultat,
};
} else {
return {
status: 200,
ref: "Odmdb",
msg: "resultsearchlist",
data: [subsearch],
};
}
};
Odmdb.r = (objectPathname, apxid, role) => {

View File

@@ -7,82 +7,155 @@ const Odmdb = require("./Odmdb.js");
const conf = require(`../../../conf.json`);
const Wwws = {};
Wwws.initlocaldata = (tribe, appname, profils,lg) => {
Wwws.initlocaldata = (tribe, appname, pagename, version, profils, lg) => {
const fileparam = `../../${tribe}/objects/wwws/itm/${appname}.json`;
console.log(fileparam);
if (!fs.existsSync(fileparam)) {
return res
.status(404)
.json({ status: 404, ref: "Wwws", msg: "appdoesnotexist", data: {} });
return { status: 404, ref: "Wwws", msg: "appdoesnotexist", data: {} };
}
const locals = fs.readJSONSync(fileparam);
if (!locals.pages[pagename]) {
return {
status: 200,
ref: "Wwws",
msg: "pagedoesnotexist",
data: { pagename },
};
}
if (locals.pages[pagename].version == version) {
return { status: 200, ref: "Wwws", msg: "nonewdatamodel", data: {} };
}
let authorize = false;
profils.forEach((p) => {
authorize = authorize || locals.pages[pagename].profils.includes(p);
});
if (!authorize) {
return {
status: 200,
ref: "Wwws",
msg: "forbidenaccess",
data: { pagename, profils },
};
}
//check version
const initname = `../../tmp/initlocaldata/${tribe}_${appname}_${pagename}_${lg}_${locals.pages[pagename].version}.json`;
if (fs.existsSync(initname) && 1 != 1) {
const init = fs.readJsonSync(initname);
if (init.app.version == locals.app.version) {
return { status: 200, ref: "Wwws", msg: "datamodelupdate", data: init };
}
}
const localstorage = {
headers: locals.headers,
version: locals.pages[pagename].version,
headers: locals.apxtri.headers,
confpages:locals.pages[pagename].confpage,
req: {},
itm: {},
itm:{},
itms: {},
options: {},
tpl: {},
tpldata:{},
ref: {},
schema: {},
};
localstorage.headers.xlang=lg;
profils.forEach((p) => {
if (locals.profils[p]) {
if (locals.profils[p].req){
localstorage.req[p] = locals.profils[p].req;
localstorage.headers.xlang = lg;
// A faire plus tard charger tous les referentiele et les data pour une page adminpage
/* if (pagename=="pageadmin"){
// load any referentialdata
glob.Sync(`../../${tribe}/objects/*.json`).forEach(f=>{
if (!localstorage.schema.includes(`${tribe}/objects/${path.basename(f,".json")}`)){
localstorage.schema.push(`${tribe}/objects/${path.basename(f,".json")}`)
}
if (locals.profils[p].ref){
Object.keys(locals.profils[p].ref).forEach(r=>{
const src= `../../${locals.profils[p].ref[r]}_${lg}.json`;
if (fs.existsSync(src)){
localstorage.ref[r]=fs.readJSONSync(src)
}else{
localstorage.ref[r]=`Check your ${fileparam} for ref in ${p} and ${r}`
}
})
})
}
*/
const loc = locals.pages[pagename];
if (loc.itms){
Object.keys(loc.itms).forEach((r) => {
const src = `../../${loc.itms[r]}.json`;
if (fs.existsSync(src)) {
localstorage.itms[r] = fs.readJSONSync(src);
} else {
localstorage.itms[r] = `Check your ${fileparam} for itms in ${pagename} and ${r}`;
}
if (locals.profils[p].options){
Object.keys(locals.profils[p].options).forEach(r=>{
const src= `../../${locals.profils[p].options[r]}_${lg}.json`;
if (fs.existsSync(src)){
localstorage.options[r]=fs.readJSONSync(src)
}else{
localstorage.options[r]=`Check your ${fileparam} for options in ${p} profil and ${r}`
}
})
});
}
if (loc.ref) {
Object.keys(loc.ref).forEach((r) => {
const src = `../../${loc.ref[r]}_${lg}.json`;
if (fs.existsSync(src)) {
localstorage.ref[r] = fs.readJSONSync(src);
} else {
localstorage.ref[
r
] = `Check your ${fileparam} for ref in ${pagename} and ${r}`;
}
if (locals.profils[p].tpl){
Object.keys(locals.profils[p].tpl).forEach(r=>{
let src= `../../${locals.profils[p].tpl[r]}`;
if (!fs.existsSync(src)){
src+=`_${lg}.mustache`
}
if (fs.existsSync(src)){
localstorage.tpl[r]=fs.readJSONSync(src)
}else{
localstorage.tpl[r]=`Check your ${fileparam} for template in ${p} profil and ${r}`
}
})
});
}
if (loc.options) {
Object.keys(loc.options).forEach((r) => {
const src = `../../${loc.options[r]}_${lg}.json`;
if (fs.existsSync(src)) {
localstorage.options[r] = fs.readJSONSync(src);
} else {
localstorage.options[
r
] = `Check your ${fileparam} for options in ${pagename} profil and ${r}`;
}
if (locals.profils[p].schema){
locals.profils[p].schema.forEach(objpath=>{
const name=path.basename(objpath)
const schema=Odmdb.Schema(`../../${objpath}`,false,lg)
if (schema.status==200){
localstorage.schema[name]=schema.data.schema
}else{
console.log(schema)
localstorage.schema[name]=`Check your ${fileparam} for schema in ${p} profil`
}
})
});
}
if (loc.tpl) {
Object.keys(loc.tpl).forEach((r) => {
let src = `../../${loc.tpl[r]}`;
console.log(path.resolve(src))
if (!fs.existsSync(src)) {
src += `_${lg}.mustache`;
}
}
});
if (fs.existsSync(src)) {
localstorage.tpl[r] = fs.readFileSync(src,'utf-8');
} else {
localstorage.tpl[
r
] = `Check your ${fileparam} for template in ${pagename} profil and ${r}`;
}
});
}
if (loc.tpldata) {
Object.keys(loc.tpldata).forEach((r) => {
let src = `../../${loc.tpldata[r]}`;
console.log(path.resolve(src))
if (!fs.existsSync(src)) {
src += `_${lg}.json`;
}
if (fs.existsSync(src)) {
localstorage.tpldata[r] = fs.readJSONSync(src);
} else {
localstorage.tpldata[
r
] = `Check your ${fileparam} for template in ${pagename} profil and ${r} in tpldata`;
}
});
}
if (loc.schema) {
loc.schema.forEach((objpath) => {
const name = path.basename(objpath);
const schema = Odmdb.Schema(`../../${objpath}`, false, lg);
if (schema.status == 200) {
localstorage.schema[name] = schema.data.schema;
} else {
console.log(schema);
localstorage.schema[
name
] = `Check your ${fileparam} for schema in ${pagename} profil`;
}
});
}
return {
status: 200,
ref: "Wwws",
msg: "initdbsuccess",
data: { localstorage },
msg: "datamodelupdate",
data: localstorage
};
};