From 033232eba154709aceb8cb63ed28997b60eba276 Mon Sep 17 00:00:00 2001 From: philc Date: Tue, 2 Apr 2024 21:45:18 +0200 Subject: [PATCH] clean --- models/Odmdb.js | 4 +- routes/odmdb.js | 123 ++++++++++++++++++++++-------------------------- 2 files changed, 58 insertions(+), 69 deletions(-) diff --git a/models/Odmdb.js b/models/Odmdb.js index 5dee504..57b2838 100644 --- a/models/Odmdb.js +++ b/models/Odmdb.js @@ -232,7 +232,7 @@ Odmdb.Schema = (objectPathname, validschema, lg="en") => { res.data.schema.properties[p].type == "object" && res.data.schema.properties[p]["$ref"] ) { - const subschema = path.resolve(`${objectPathname}/${res.data.schema.properties[p]["$ref"]}.json`); + const subschema = path.resolve(`../../${res.data.schema.properties[p]["$ref"]}.json`); if (Object.keys(res.data.schema).length == 0) { res.status = 404; res.msg = "missingref"; @@ -250,7 +250,7 @@ Odmdb.Schema = (objectPathname, validschema, lg="en") => { 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"]}_${lg}.json`) + const optionsfile = path.resolve(`../../${res.data.schema.properties[p].options["$ref"]}_${lg}.json`) if (log) console.log(currentmod,"Lien vers options:", optionsfile) if (!fs.existsSync(optionsfile)){ res.status = 404; diff --git a/routes/odmdb.js b/routes/odmdb.js index 8ffc2bb..cb87ded 100644 --- a/routes/odmdb.js +++ b/routes/odmdb.js @@ -10,67 +10,74 @@ const isAuthenticated = require("../middlewares/isAuthenticated.js"); const router = express.Router(); /** - * @api {get} /adminapi/odmdb/schemas - objects Get + * @api {get} /adminapi/odmdb/schemas/:tribe - objects Get * @apiGroup Odmdb * @apiName getIndex * @apiDescription Get objects available result is store in data.apx.conf for schema conf of adminapi schema (pagans,towns,... ) and data.apx.objectnames as array of schema name. Schema related to tribe are store in data.tribe.conf and data.tribe.objectnames where tribe come from header.xtribe - * + * @apiParams {string} tribe to get list of schema related to tribe * @apiSuccess {object} contain data.indexname * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * {"status":200, "ref":"Odmdb", "msg":"objectslist", "data":{apx:{conf,objectnames:[]},tribe:{conf,objectnames:[]}}} */ -router.get("/schemas", checkHeaders, isAuthenticated, (req, res) => { +router.get("/schemas/:tribe", checkHeaders, isAuthenticated, (req, res) => { const data = { - tribe: req.session.header.xtribe, + tribename: req.params.tribe, apx: { conf: {}, objectnames: [] }, tribe: { conf: {}, objectnames: [] }, }; - glob.sync(`../schema/*.json`).forEach((f) => { - const objectname = path.basename(f, ".json"); + let trb + glob.sync(`..{/,/../${req.params.tribe}/}schema/*.json`).forEach(f=>{ + const objectname=path.basename(f,".json"); + console.log(f) + trb = (f.includes(req.params.tribe))? "tribe":"apx"; if (objectname == "conf") { - data.apx.conf = fs.readJSONSync(f); + data[trb].conf = fs.readJSONSync(f); } else { - data.apx.objectnames.push(objectname); + data[trb].objectnames.push(objectname); } - }); - glob - .sync(`../../${req.session.header.xtribe}/schema/*.json`) - .forEach((f) => { - const objectname = path.basename(f, ".json"); - if (objectname == "conf") { - data.tribe.conf = fs.readJSONSync(f); - } else { - data.tribe.objectnames.push(objectname); - } - }); + }) res.status(200).json({ status: 200, ref: "Odmdb", msg: "objectslist", data }); }); /** - * @api {get} /adminapi/odmdb/schema/:source/:objectname - Schema Get in the language header + * @api {get} /adminapi/odmdb/schema/:tribe/:objectname - Schema Get in the language header * @apiGroup Odmdb * @apiName getIndex - * @apiDescription Get schema in the requested language if login have accessright + * @apiDescription Get schema in the requested language if login have accessright. object $ref or $id are replace by the relevant schema, option $ref are replace by enum list of authorised value * - * @apiParams {string} source (adminapi || tribe) looking for adminapi or in tribe (header.xtribe) + * @apiParams {string} tribe (adminapi,smatchit,..) to looking for * @apiParams {string} objectname requested must exist in adminapi or tribe * @apiSuccess {object} contain data.schema * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * {"status":200, "ref":"Odmdb", "msg":"schema", "data":{schema,objectname,lg} */ -router.get("/schema/:source/:objectname", checkHeaders, isAuthenticated, (req, res) => { - if (!['adminapi','tribe'].includes(req.params.source)){ - res(406).json({status:406,ref:"Odmdb",msg:"mustbetribeoradminapi",data:{source:req.params.source}}) - }else{ - let objectPathname=`../../adminapi/objects/${req.params.objectname}` - if (req.params.source=="tribe"){ - objectPathname=`../../${req.headers.xtribe}/objects/${req.params.objectname}` - } +router.get("/schema/:tribe/:objectname", checkHeaders, isAuthenticated, (req, res) => { + const objectPathname=`../../${req.params.tribe}/objects/${req.params.objectname}` console.log(objectPathname) const retschema = Odmdb.Schema(objectPathname, true, req.header.xlang) res.status(retschema.status).json(retschema); - } +}); +/** + * @api {get} /adminapi/odmdb/options/:tribe/objects/options/:optionname - Get option list in header language + * @apiGroup Odmdb + * @apiName getOption + * @apiDescription Get schema in the requested language if login have accessright. object $ref or $id are replace by the relevant schema, option $ref are replace by enum list of authorised value + * + * @apiParams {string} tribe (adminapi,smatchit,..) to looking for + * @apiParams {string} objectname requested must exist in adminapi or tribe + * @apiSuccess {object} contain data.schema + * @apiSuccessExample {json} Success-Response: + * HTTP/1.1 200 OK + * {"status":200, "ref":"Odmdb", "msg":"schema", "data":{schema,objectname,lg} + */ +router.get("/options/:tribe/objects/options/:optionname", checkHeaders, isAuthenticated, (req, res) => { + const objectPathname=`../../${req.params.tribe}/objects/options/${req.params.optionname}_${req.header.xlang}.json` + if (fs.existsSync(objectPathname)){ + res.status(200).json({status:200,ref:"Odmdb",msg:"optionfind",data:fs.readJsonSync(objectPathname)}) + }else{ + res.status(404).json({status:404,ref:"Odmdb",msg:"optionnotfound",data:{objectPathname}}) + } }); /** @@ -102,13 +109,7 @@ router.get( isAuthenticated, (req, res) => { console.log("passe"); - // indexname = objectname_key_value.json - let objectLocation = "../nationchains/"; - if (!conf.api.nationObjects.includes(req.params.objectname)) { - objectLocation += `tribes/${req.session.header.xtribe}/`; - // check if accessright - } - const indexpath = `${objectLocation}/${req.params.objectname}/idx/${req.params.indexname}`; + const indexpath = `../../${req.params.tribe}/objects/${req.params.objectname}/idx/${req.params.indexname}`; if (fs.existsSync(indexpath)) { res.status(200).json({ ref: "Odmdb", @@ -129,13 +130,13 @@ router.get( ); /** - * @api {get} /adminapi/odmdb/rebuildidx/:objectname - index refresh all + * @api {get} /adminapi/odmdb/rebuildidx/:tribe/:objectname - index refresh all * @apiGroup Odmdb * @apiName refreshAllIndex * @apiDescription Rebuild all index for an object, this can be usefull in case crash or any data conflict. * * @apiParam {string} objectname Mandatory - * + * @apiParam {string} tribe adminapi or tribe name (smatchit), where object is store * @apiSuccess {object} indexfile content * @apiSuccessExample {json} successreindex * HTTP/1.1 200 OK @@ -148,18 +149,14 @@ router.get( * */ router.get( - "/rebuildidx/:objectname", + "/rebuildidx/:tribe/:objectname", checkHeaders, isAuthenticated, (req, res) => { console.log("reindex"); // check validity and accessright - const objectPathname = conf.api.nationObjects.includes( - req.params.objectname - ) - ? `../nationchains/${req.params.objectname}` - : `${conf.dirtown}/tribes/${req.session.header.xtribe}/${req.params.objectname}`; - //console.log(objectPathname); + + const objectPathname=`../../${req.params.tribe}/objects/${req.params.objectname}` if (!fs.existsSync(objectPathname)) { res.status(404).json({ status: 404, @@ -199,13 +196,13 @@ router.get( ); /** - * @api {post} /adminapi/odmdb/itm/:objectname - item Create + * @api {post} /adminapi/odmdb/itm/:tribe/:objectname - item Create * @apiGroup Odmdb * @apiName postItm * @apiPermission none * @apiDescription Add an new item (data) into a collection of objectname items. Before Check data integrity with the relevant schema. * Then create a new primarykey. For multilanguage return see nationchains/model/lg/Odmdb_xx.json. - * + * @apiParam {string} tribe adminapi or tribe name (smatchit), where object is store * @apiParam {string} objectname Place where to create new item, schema and version are available in /objectname/conf.json * * @apiBody {Object} data must pass Checkjson.js with schema @@ -225,23 +222,23 @@ router.get( * {"status":404,"ref":"Odmdb","msg":"see nationchains/model/lg/Odmdb_xx.json","data":"object to render with msg"} * */ -router.post("/itm/:objectname", checkHeaders, isAuthenticated, (req, res) => { +router.post("/itm/:tribe/:objectname", checkHeaders, isAuthenticated, (req, res) => { // Create an item of an object with no specificities // if specificities then create a route / model that import odmdb - const objectPathname=`../../${req.session.header.xtribe}/objects/${req.params.objectname}`; + const objectPathname=`../../${req.params.tribe}/objects/${req.params.objectname}`; const postitm=Odmdb.cud(objectPathname,"C",req.body,{xprofils:req.session.header.xprofils,xalias:req.session.header.xalias}); res.status(postitm.status).json(postitm); }); /** - * @api {put} /adminapi/odmdb/itm/:objectname - item Update + * @api {put} /adminapi/odmdb/itm/:tribe/:objectname - item Update * @apiGroup Odmdb * @apiName putItm * @apiPermission none * @apiDescription Update an item (data) into a collection of objectname items. Before Check data integrity with the relevant schema. * Then create a new primarykey. For multilanguage return see nationchains/model/lg/Odmdb_xx.json. - * + * @apiParam {string} tribe adminapi or tribe name (smatchit), where object is store * @apiParam {string} objectname Place where to create new item, schema and version are available in /objectname/conf.json * * @apiBody {Object} data must pass Checkjson.js with schema @@ -261,10 +258,10 @@ router.post("/itm/:objectname", checkHeaders, isAuthenticated, (req, res) => { * {"status":404,"ref":"Odmdb","msg":"see nationchains/model/lg/Odmdb_xx.json","data":"object to render with msg"} * */ -router.put("/itm/:objectname", checkHeaders, isAuthenticated, (req, res) => { +router.put("/itm/:tribe/:objectname", checkHeaders, isAuthenticated, (req, res) => { // Create an item of an object with no specificities // if specificities then create a route / model that import odmdb - const objectPathname=`../../${req.session.header.xtribe}/objects/${req.params.objectname}`; + const objectPathname=`../../${req.params.tribe}/objects/${req.params.objectname}`; const postitm=Odmdb.cud(objectPathname,"U",req.body,{xprofils:req.session.header.xprofils,xalias:req.session.header.xalias}); res.status(postitm.status).json(postitm); }); @@ -297,11 +294,11 @@ router.get( ); /** - * @api {get} /adminapi/odmdb/itm/:objectname/:primaryindex - item Get + * @api {get} /adminapi/odmdb/itm/:tribe/:objectname/:primaryindex - item Get * @apiGroup Odmdb * @apiName getItemFromId * @apiDescription Get itm for a primaryid of an object - * + * @apiParam {string} tribe adminapi or tribe name (smatchit), where object is store * @apiParam {String} objectname name Mandatory if in conf.nationObjects then file is into nationchains/ else in /tribes/xtribe/objectname * @apiParam {String} primaryindex the unique id where item is store * @@ -319,19 +316,11 @@ router.get( */ // indexname = objectname_key_value.json router.get( - "/itm/:objectname/:primaryindex", + "/itm/:tribe/:objectname/:primaryindex", checkHeaders, isAuthenticated, (req, res) => { - const objectName = req.params.objectname; - const objectId = req.params.primaryindex; - let objectLocation = "../nationchains/"; - if (!conf.api.nationObjects.includes(objectName)) { - objectLocation += `tribes/${req.session.header.xtribe}/${objectName}`; - // check if accessright on object on item - // in case not res.status(403) - } - const objectpath = `${objectLocation}/${objectName}/itm/${objectId}`; + const objectpath = `../../${req.params.tribe}/objects/${req.params.objectname}/itm/${req.params.primaryindex}.json`; if (fs.existsSync(objectpath)) { res.status(200).json({ data: fs.readJsonSync(objectpath) });