This commit is contained in:
2024-02-16 08:39:42 +01:00
parent a95bfaf00f
commit c89b62befc
7 changed files with 173 additions and 128 deletions

View File

@@ -6,7 +6,8 @@ const axios = require("axios");
const conf = require(`../../conf/townconf.json`);
const Checkjson = require(`./Checkjson.js`);
const { promiseHooks } = require("v8");
const currentmod = "Odmdb";
const log = conf.api.activelog.includes(currentmod);
/**
* This manage Objects for indexing, searching, checking and act to CRUD
* @objectPathName = objectpath/objectname
@@ -25,7 +26,7 @@ const { promiseHooks } = require("v8");
* }
*
* Specifics key in schema to apxtri:
* apxid : the field value to use to store item
* apxid : the field value to use to store item apx
* apxuniquekey : list of field that has to be unique you cannot have 2 itm with same key value
* apxidx : list of index file /idx/
* { "name":"lst_fieldA", "keyval": "alias" }, => lst_fieldA.json = [fieldAvalue1,...]
@@ -47,7 +48,8 @@ const { promiseHooks } = require("v8");
const Odmdb = {};
/**
* @api syncObject
*const Checkjson = require(`../../../../../apxtri/models/Checkjson`);
@api syncObject
* @param {string} url to an existing object conf (/objectname/conf.json)
* @param {timestamp} timestamp
* 0 => rebuild local object from all_{idapx}.json
@@ -150,40 +152,71 @@ Odmdb.updateObject = (objectPathname, meta) => {};
*
* todo only local schema => plan a sync each 10minutes
* @schemaPath local path adminapi/schema/objectName.json or /tribename/schema/objectName
* @validschema boolean if necessary to check schema or not mainly use when change schema
* @validschema boolean if necessary to check schema or not mainly use when change schema;
* @lg language you want to get schema
* @return {status:200,data:{conf:"schemaconf",schema:"schemacontent"} }
*/
Odmdb.Schema = (objectPathname, validschema) => {
const getpath = (schemaPath) => {
Odmdb.Schema = (objectPathname, validschema, lg="en") => {
const replacelg = (data)=>{
// data.en version schema de base, data.fr version schema traduite
Object.keys(data.lg).forEach(k=>{
console.log(k)
if (data.lg[k].title) data.en[k].title = data.lg[k].title
if (data.lg[k].description) data.en[k].description = data.lg[k].description
if (data.lg.properties){
console.log('properties')
console.log(data.en.properties)
console.log(data.lg.properties)
const res = replacelg({en:data.en.properties,lg:data.lg.properties})
data.lg.properties=res.lg
data.en.properties=res.en
}
})
return data
}
const getschemalg = (schemaPath,lg) => {
if (schemaPath.slice(-5) != ".json") schemaPath += ".json";
if (schemaPath.substring(0, 4) == "http") {
// lance requete http pour recuperer le schema avec un await axios
} else {
schemaPath = `../nationchains/tribes/${schemaPath}`;
/*if (schemaPath.substring(0, 9) == "adminapi/") {
schemaPath = `${conf.dirapi}/${schemaPath}`;
} else {
schemaPath = `${conf.dirtown}/tribes/${schemaPath}`;
}*/
console.log(path.resolve(schemaPath))
if (!fs.existsSync(schemaPath)) {
return {};
} else {
return fs.readJsonSync(schemaPath);
let schemalg = fs.readJsonSync(schemaPath);
if (lg!="en"){
let lgtrans={}
try{
lgtrans=fs.readJsonSync(schemaPath.replace('/schema/','/schema/lg/').replace('.json',`_${lg}.json`));
const res= replacelg({en:schemalg,lg:lgtrans})
//console.log(res.en.title,res.lg.title)
schemalg=res.en
}catch(err){
// console.log('Err',err)
// no translation file deliver en by default
}
}
return schemalg
}
}
};
console.log(`${objectPathname}/conf.json`);
const confschema = fs.readJsonSync(`${objectPathname}/conf.json`);
console.log(confschema);
const res = {
status: 200,
ref: "Odmdb",
msg: "getschema",
data: { conf: confschema },
};
res.data.schema = getpath(confschema.schema);
if (log) console.log(currentmod,`${objectPathname}/conf.json`);
const res = {
status: 200,
ref: "Odmdb",
msg: "getschema",
data: {},
};
if (Object.keys(res.data.schema).length == 0) {
if (fs.existsSync(`${objectPathname}/conf.json`)) {
res.data.conf=fs.readJsonSync(`${objectPathname}/conf.json`);
res.data.schema = getschemalg(res.data.conf.schema,lg)
}else{
res.data.conf={}
}
if (!res.data.schema || Object.keys(res.data.schema).length == 0 ) {
return {
status: 404,
ref: "Odmdb",
@@ -192,9 +225,9 @@ Odmdb.Schema = (objectPathname, validschema) => {
};
}
//looking for type:object with $ref to load and replace by ref content (ref must be adminapi/ or tribeid/)
//@todo only 1 level $ref if multi level need to rewrite with recursive call
Object.keys(res.data.schema.properties).forEach((p) => {
//looking for type:object with $ref to load and replace by ref content (ref must be adminapi/ or tribeid/)
if (
res.data.schema.properties[p].type == "object" &&
res.data.schema.properties[p]["$ref"]
@@ -210,6 +243,25 @@ Odmdb.Schema = (objectPathname, validschema) => {
res.data.schema.properties[p] = subschema;
}
}
//looking for options:{"$ref":"../objects/options/xxx.json"}
//to add enum:[] = content of options available
if (
res.data.schema.properties[p].options &&
res.data.schema.properties[p].options["$ref"]
) {
const optionsfile = path.resolve(`${objectPathname}/${res.data.schema.properties[p].options["$ref"]}`)
if (!fs.existsSync(optionsfile)){
res.status = 404;
res.msg = "missingref";
res.data.missingref = res.data.schema.properties[p]["$ref"];
return res;
}else{
if (!res.data.schema.apxref) {res.data.schema.apxref=[]}
if (!res.data.schema.apxref.includes(res.data.schema.properties[p].options["$ref"]))
res.data.schema.apxref.push(res.data.schema.properties[p].options["$ref"])
res.data.schema.properties[p].enum=fs.readJSONSync(optionsfile)
}
}
});
if (!res.data.schema.apxid) {
@@ -300,7 +352,7 @@ Odmdb.r = (objectPathname, apxid, role) => {
status: 403,
ref: "Odmdb",
msg: "profilnotallow",
data: { person: apxid, },
data: { person: apxid },
};
}
const data = {};
@@ -384,8 +436,8 @@ Odmdb.ASUPreads = (objectPathname, apxidlist, role, propertiesfilter) => {
* example: {"C":[],"R":[properties list],"U":[properties ist],"D":[]}
*/
Odmdb.accessright = (apxaccessrights, role) => {
//console.log("apxaccessrights",apxaccessrights)
//console.log("role",role)
//if (log) console.log(currentmod,"apxaccessrights",apxaccessrights)
//if (log) console.log(currentmod,"role",role)
const accessright = {};
role.xprofils.forEach((p) => {
if (apxaccessrights[p]) {
@@ -397,7 +449,7 @@ Odmdb.accessright = (apxaccessrights, role) => {
...new Set([...accessright[act], ...apxaccessrights[p][act]]),
];
}
//console.log(act,accessright[act])
//if (log) console.log(currentmod,act,accessright[act])
});
}
});
@@ -483,7 +535,7 @@ Odmdb.cud = (objectPathname, crud, itm, role, runindex = true) => {
getschema.data.schema.apxaccessrights,
role
);
console.log("accessright", accessright);
if (log) console.log(currentmod,"accessright", accessright);
if (
(crud == "C" && !accessright.C) ||
(crud == "D" && !accessright.D) ||
@@ -526,17 +578,17 @@ Odmdb.cud = (objectPathname, crud, itm, role, runindex = true) => {
if (chkdata.status != 200) return chkdata;
if (!getschema.data.schema.apxuniquekey)
getschema.data.schema.apxuniquekey = [];
console.log(`${objectPathname}/itm/${chkdata.data.apxid}.json`);
console.log(chkdata.data.itm);
if (log) console.log(currentmod,`${objectPathname}/itm/${chkdata.data.apxid}.json`);
if (log) console.log(currentmod,chkdata.data.itm);
fs.outputJSONSync(
`${objectPathname}/itm/${chkdata.data.apxid}.json`,
chkdata.data.itm
);
}
//console.log("getschema", getschema);
//if (log) console.log(currentmod,"getschema", getschema);
//rebuild index if requested
console.log("runidx", runindex);
console.log(objectPathname);
if (log) console.log(currentmod,"runidx", runindex);
if (log) console.log(currentmod,objectPathname);
if (runindex) Odmdb.runidx(objectPathname, getschema.data.schema);
getschema.data.conf.lastupdatedata = dayjs().toISOString();
fs.outputJSONSync(`${objectPathname}/conf.json`, getschema.data.conf);
@@ -559,7 +611,7 @@ Odmdb.cud = (objectPathname, crud, itm, role, runindex = true) => {
*
*/
Odmdb.runidx = (objectPathname, schema) => {
console.log(`idx for ${objectPathname}`);
if (log) console.log(currentmod,`idx for ${objectPathname}`);
if (!schema || !schema.apxid) {
const getschema = Odmdb.Schema(objectPathname, true);
if (getschema.status != 200) return getschema;
@@ -634,13 +686,18 @@ Odmdb.runidx = (objectPathname, schema) => {
ventil[n].data[val].push(itm[schema.apxid]);
});
}
if (keep && ventil[n].type == "distribution" && ventil[n].isobject && itm[ventil[n].keyval.split('.')[0]]) {
if (
keep &&
ventil[n].type == "distribution" &&
ventil[n].isobject &&
itm[ventil[n].keyval.split(".")[0]]
) {
let itmval = JSON.parse(JSON.stringify(itm));
console.log( ventil[n].keyval)
console.log(itmval)
if (log) console.log(currentmod,ventil[n].keyval);
if (log) console.log(currentmod,itmval);
ventil[n].keyval
.split(".")
.forEach((i) => itmval = itmval[i] ? itmval[i] : null);
.forEach((i) => (itmval = itmval[i] ? itmval[i] : null));
if (itmval) {
if (!ventil[n].data[itmval]) ventil[n].data[itmval] = [];
ventil[n].data[itmval].push(itm[schema.apxid]);
@@ -649,7 +706,7 @@ Odmdb.runidx = (objectPathname, schema) => {
});
});
Object.keys(ventil).forEach((n) => {
//console.log(`${objectPathname}/idx/${ventil[n].name}.json`)
//if (log) console.log(currentmod,`${objectPathname}/idx/${ventil[n].name}.json`)
fs.outputJSON(
`${objectPathname}/idx/${ventil[n].name}.json`,
ventil[n].data
@@ -687,15 +744,15 @@ Odmdb.ASUPidxfromitm = (
idxs = [],
schema
) => {
console.log(`idxfromitem for ${objectPathname} action:${crud}`);
if (log) console.log(currentmod,`idxfromitem for ${objectPathname} action:${crud}`);
if (!schema || !schema.apxid) {
const getschema = Odmdb.Schema(objectPathname, true);
if (getschema.status != 200) return getschema;
schema = getschema.data.schema;
}
console.log(schema.apxuniquekey);
if (log) console.log(currentmod,schema.apxuniquekey);
const itms = crud == "I" ? glob.sync(`${objectPathname}/itm/*.json`) : [itm];
console.log(itms);
if (log) console.log(currentmod,itms);
if (crud == "I") {
//reinit all idx
idxs.forEach((idx) => {
@@ -708,7 +765,7 @@ Odmdb.ASUPidxfromitm = (
if (crud == "I") {
itm = fs.readJSONSync(i);
}
//console.log(itm);
//if (log) console.log(currentmod,itm);
idxs.forEach((idx) => {
const keyvalisunique = schema.apxuniquekey.includes(idx.keyval); // check if keyval is unique mean store as an object (or string) else store as an array
const idxsrc = `${objectPathname}/idx/${idx.name}.json`;
@@ -722,8 +779,8 @@ Odmdb.ASUPidxfromitm = (
idxtoreindex.push(idx); //@todo
}
}
console.log(idx.keyval);
console.log(itm[idx.keyval]);
if (log) console.log(currentmod,idx.keyval);
if (log) console.log(currentmod,itm[idx.keyval]);
if (
["C", "U", "I"].includes(crud) &&

View File

@@ -5,5 +5,7 @@
"errsendmail":"Une erreur s'est produite lors de l'envoie de l'email",
"successfullsentemail":"Email correctement envoyé",
"errsendsms":"Une erreur s'est produite lors de l'envoie du sms",
"successfullsentsms":"Sms bien envoyé à {{To}}"
"successfullsentsms":"Sms bien envoyé à {{To}}",
"registersuccess":"Vous avez bien été enregistré pour être recontacté.",
"formaterror":"Verifier vos données"
}