fix __dirapi by conf.dirapi
This commit is contained in:
@@ -50,7 +50,7 @@ const checkHeaders = (req, res, next) => {
|
||||
if (!req.header("xlang") && req.header("Content-Language"))
|
||||
req.params.xlang = req.header("Content-Language");
|
||||
let missingheader = [];
|
||||
console.log("req.headers", req.headers);
|
||||
//console.log("req.headers", req.headers);
|
||||
for (const h of conf.api.exposedHeaders) {
|
||||
//console.log( h, req.header( h ) )
|
||||
if (req.params[h]) {
|
||||
|
@@ -11,6 +11,7 @@ Checkjson.schema = {};
|
||||
Checkjson.schema.properties = {};
|
||||
Checkjson.schema.properties.type = {};
|
||||
Checkjson.schema.properties.type.string = (str) => typeof str === "string";
|
||||
Checkjson.schema.properties.type.array = (val)=> Array.isArray(val);
|
||||
Checkjson.schema.properties.type.number = (n) => typeof n === "number";
|
||||
Checkjson.schema.properties.type.boolean = (n) => typeof n === "boolean";
|
||||
Checkjson.schema.properties.type.integer = (n) =>
|
||||
@@ -46,8 +47,9 @@ Checkjson.schema.properties.range = (
|
||||
};
|
||||
Checkjson.schema.properties.pattern = (str, pattern) => {
|
||||
try {
|
||||
new RegExp(pattern);
|
||||
pattern= new RegExp(pattern);
|
||||
} catch (e) {
|
||||
console.log('err pattern in checkjon',pattern);
|
||||
return false;
|
||||
}
|
||||
return pattern.test(str);
|
||||
@@ -56,10 +58,10 @@ Checkjson.schema.properties.enum = (str, enumvalues) =>
|
||||
typeof str === "string" && enumvalues.includes(str);
|
||||
// see format https://json-schema.org/understanding-json-schema/reference/string.html#format
|
||||
Checkjson.schema.properties.format = {
|
||||
"date-time": / /,
|
||||
"date-time": /\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d{1,3}/,
|
||||
stringalphaonly:/^[A-Za-z0-9]{3,}$/,
|
||||
time: / /,
|
||||
date: / /,
|
||||
time: /[0-2]\d:[0-5]\d:[0-5]\d\.\d{1,3}/,
|
||||
date: /\d{4}-[01]\d-[0-3]\d/,
|
||||
duration: / /,
|
||||
email:
|
||||
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
|
||||
@@ -91,8 +93,9 @@ Checkjson.schema.validation = (schema) => {
|
||||
!Checkjson.schema.properties.type[properties[p].type]
|
||||
) {
|
||||
res.err.push({
|
||||
info: "|Checkjson|typedoesnotexistinschema",
|
||||
moreinfo: ` ${properties[p].type}`,
|
||||
ref:"Checkjson",
|
||||
msg:"schemaerrtypedoesnotexist",
|
||||
data: {propertie:p,type:properties[p].type}
|
||||
});
|
||||
}
|
||||
if (
|
||||
@@ -103,8 +106,9 @@ Checkjson.schema.validation = (schema) => {
|
||||
properties[p].type.forEach((tp) => {
|
||||
if (!Checkjson.schema.properties.type[tp])
|
||||
res.err.push({
|
||||
info: "|Checkjson|typedoesnotexistinschema",
|
||||
moreinfo: `${tp} of ${properties[p].type}`,
|
||||
ref:"Checkjson",
|
||||
msg:"schemaerrtypedoesnotexist",
|
||||
data: {propertie:p,type:properties[p].type}
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -113,14 +117,16 @@ Checkjson.schema.validation = (schema) => {
|
||||
!Checkjson.schema.properties.format[properties[p].format]
|
||||
) {
|
||||
res.err.push({
|
||||
info: "|Checkjson|formatdoesnotexistinschema",
|
||||
moreinfo: ` ${properties[p].format}`,
|
||||
ref:"Checkjson",
|
||||
msg:"schemaerrformatdoesnotexist",
|
||||
data: {propertie:p,format:properties[p].format}
|
||||
});
|
||||
}
|
||||
if (properties[p].enum && !Array.isArray(properties[p].enum)) {
|
||||
res.err.push({
|
||||
info: "|Checkjson|enumisnotarrayinschema",
|
||||
moreinfo: ` ${properties[p].enum}`,
|
||||
ref:"Checkjson",
|
||||
msg:"schemaerrenumnotarray",
|
||||
data: {propertie:p,enum:properties[p].enum}
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -159,8 +165,9 @@ Checkjson.schema.data = (schema, data, withschemacheck) => {
|
||||
});
|
||||
if (!valid)
|
||||
res.err.push({
|
||||
info: "|Checkjson|dataerrpropertie",
|
||||
moreinfo: `${p} : ${data[p]}`,
|
||||
ref:"Checkjson",
|
||||
msg:"dataerrpropertie",
|
||||
data: {key:p,value:data[p]}
|
||||
});
|
||||
|
||||
if (
|
||||
@@ -168,17 +175,19 @@ Checkjson.schema.data = (schema, data, withschemacheck) => {
|
||||
!Checkjson.schema.properties.minLength(data[p], properties[p].minLength)
|
||||
) {
|
||||
res.err.push({
|
||||
info: "|Checkjson|dataerrpropertie",
|
||||
moreinfo: `${p} : ${data[p]} minLength:${properties[p].minLength}`,
|
||||
});
|
||||
ref:"Checkjson",
|
||||
msg:"dataerrpropertie",
|
||||
data:{key:p,value:data[p],minLength:properties[p].minLength}
|
||||
});
|
||||
}
|
||||
if (
|
||||
properties[p].maxLength &&
|
||||
!Checkjson.schema.properties.maxLength(data[p], properties[p].maxLength)
|
||||
) {
|
||||
res.err.push({
|
||||
info: "|Checkjson|dataerrpropertie",
|
||||
moreinfo: `${p} : ${data[p]} maxLength:${properties[p].maxLength}`,
|
||||
ref:"Checkjson",
|
||||
msg:"dataerrpropertie",
|
||||
data:{key:p,value:data[p],maxLength:properties[p].maxLength}
|
||||
});
|
||||
}
|
||||
if (
|
||||
@@ -186,8 +195,9 @@ Checkjson.schema.data = (schema, data, withschemacheck) => {
|
||||
!Checkjson.schema.properties.multipleOf(data[p], properties[p].multipleOf)
|
||||
) {
|
||||
res.err.push({
|
||||
info: "|Checkjson|dataerrpropertie",
|
||||
moreinfo: `${p} : ${data[p]} not a multipleOf:${properties[p].multipleOf}`,
|
||||
ref:"Checkjson",
|
||||
msg:"dataerrpropertie",
|
||||
data:{key:p,value:data[p],multipleOf:properties[p].multipleOf}
|
||||
});
|
||||
}
|
||||
if (
|
||||
@@ -207,8 +217,9 @@ Checkjson.schema.data = (schema, data, withschemacheck) => {
|
||||
)
|
||||
) {
|
||||
res.err.push({
|
||||
info: "|Checkjson|dataerrpropertie",
|
||||
moreinfo: `${p} : ${data[p]} not in range ${properties[p].minimum} exclu: ${properties[p].exclusiveMinimum} and ${properties[p].maximum} exclu: ${properties[p].exclusiveMaximum}`,
|
||||
ref:"Checkjson",
|
||||
msg:"dataerrpropertie",
|
||||
data:{key:p,value:data[p],minimum:properties[p].minimum,maximum:properties[p].maximum,exclusiveMinimum:properties[p].exclusiveMinimum,exclusiveMaximum:properties[p].exclusiveMaximum}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -217,8 +228,9 @@ Checkjson.schema.data = (schema, data, withschemacheck) => {
|
||||
!Checkjson.schema.properties.enum(data[p], properties[p].enum)
|
||||
) {
|
||||
res.err.push({
|
||||
info: "|Checkjson|dataerrpropertie",
|
||||
moreinfo: `${p} : ${data[p]} not in enum list :${properties[p].enum}`,
|
||||
ref:"Checkjson",
|
||||
msg:"dataerrpropertie",
|
||||
data:{key:p,value:data[p],enumlst:properties[p].enum}
|
||||
});
|
||||
}
|
||||
if (properties[p].format) {
|
||||
@@ -230,14 +242,16 @@ Checkjson.schema.data = (schema, data, withschemacheck) => {
|
||||
!Checkjson.schema.properties.pattern(data[p], properties[p].pattern)
|
||||
) {
|
||||
res.err.push({
|
||||
info: "|Checkjson|dataerrpropertie",
|
||||
moreinfo: `${p} : ${data[p]} problem pattern or format ${properties[p].pattern}`,
|
||||
ref:"Checkjson",
|
||||
msg:"dataerrpropertie",
|
||||
data:{key:p,value:data[p],pattern:properties[p].pattern}
|
||||
});
|
||||
}
|
||||
} else if (schema.required && schema.required.includes(p)) {
|
||||
res.err.push({
|
||||
info: "|Checkjson|dataerrpropertiesrequired",
|
||||
moreinfo: `${p}`,
|
||||
ref:"Checkjson",
|
||||
msg:"dataerrpropertierequired",
|
||||
data:{key:p,required:true}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
@@ -42,7 +42,7 @@ Nations.updateobjectsfromfreshesttown = (dnstownlist, objectidx) => {
|
||||
objlist.forEach((o) => {
|
||||
let objconf = {
|
||||
name: o,
|
||||
schema: `nationchains/schema/${o}.jsons`,
|
||||
schema: `adminapi/schema/${o}.jsons`,
|
||||
lastupdate: -1,
|
||||
};
|
||||
if (fs.existsSync(`${conf.dirapi}/nationchains/${o}/conf.json`)) {
|
||||
@@ -54,62 +54,64 @@ Nations.updateobjectsfromfreshesttown = (dnstownlist, objectidx) => {
|
||||
});
|
||||
//console.log(localversion);
|
||||
for (let t = 0; t < dnstownlist.length; t++) {
|
||||
let promiseconf = [];
|
||||
let objecttotest = [];
|
||||
objlist.forEach((o) => {
|
||||
//console.log(`https://${dnstownlist[t].dns}/nationchains/${o}/conf.json`);
|
||||
objecttotest.push(o);
|
||||
promiseconf.push(
|
||||
axios.get(`https://${dnstownlist[t].dns}/nationchains/${o}/conf.json`)
|
||||
);
|
||||
});
|
||||
Promise.all(promiseconf)
|
||||
.then((reps) => {
|
||||
let promiseidx = [];
|
||||
let objecttoupdate = [];
|
||||
let objlastupdate = [];
|
||||
for (let i = 0; i < objecttotest.length; i++) {
|
||||
if (
|
||||
parseInt(reps[i].data.lastupdate) >
|
||||
parseInt(localversion[reps[i].data.name][1])
|
||||
) {
|
||||
// add promise to get data
|
||||
/*console.log(
|
||||
if (conf.townId != dnstownlist[t].townId) { // to avoid update itself
|
||||
let promiseconf = [];
|
||||
let objecttotest = [];
|
||||
objlist.forEach((o) => {
|
||||
//console.log(`https://${dnstownlist[t].dns}/nationchains/${o}/conf.json`);
|
||||
objecttotest.push(o);
|
||||
promiseconf.push(
|
||||
axios.get(`https://${dnstownlist[t].dns}/nationchains/${o}/conf.json`)
|
||||
);
|
||||
});
|
||||
Promise.all(promiseconf)
|
||||
.then((reps) => {
|
||||
let promiseidx = [];
|
||||
let objecttoupdate = [];
|
||||
let objlastupdate = [];
|
||||
for (let i = 0; i < objecttotest.length; i++) {
|
||||
if (
|
||||
parseInt(reps[i].data.lastupdate) >
|
||||
parseInt(localversion[reps[i].data.name][1])
|
||||
) {
|
||||
// add promise to get data
|
||||
/*console.log(
|
||||
`https://${dnstownlist[t].dns}/nationchains/${
|
||||
reps[i].data.name
|
||||
}/idx/${objectidx[reps[i].data.name]}`
|
||||
);*/
|
||||
objecttoupdate.push(objecttotest[i]);
|
||||
objlastupdate.push(reps[i].data.lastupdate);
|
||||
promiseidx.push(
|
||||
axios.get(
|
||||
`https://${dnstownlist[t].dns}/nationchains/${
|
||||
reps[i].data.name
|
||||
}/idx/${objectidx[reps[i].data.name]}`
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
Promise.all(promiseidx)
|
||||
.then((rets) => {
|
||||
for (let j = 0; j < objecttoupdate.length; j++) {
|
||||
Odmdb.updatefromidxall(
|
||||
objecttoupdate[j],
|
||||
objectidx[objecttoupdate[j]],
|
||||
rets[j].data,
|
||||
objlastupdate[j]
|
||||
objecttoupdate.push(objecttotest[i]);
|
||||
objlastupdate.push(reps[i].data.lastupdate);
|
||||
promiseidx.push(
|
||||
axios.get(
|
||||
`https://${dnstownlist[t].dns}/nationchains/${
|
||||
reps[i].data.name
|
||||
}/idx/${objectidx[reps[i].data.name]}`
|
||||
)
|
||||
);
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log("ERR get idx data");
|
||||
console.log(err);
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log("ERR get conf lastupdate");
|
||||
console.log(err);
|
||||
});
|
||||
}
|
||||
Promise.all(promiseidx)
|
||||
.then((rets) => {
|
||||
for (let j = 0; j < objecttoupdate.length; j++) {
|
||||
Odmdb.updatefromidxall(
|
||||
objecttoupdate[j],
|
||||
objectidx[objecttoupdate[j]],
|
||||
rets[j].data,
|
||||
objlastupdate[j]
|
||||
);
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log("ERR get idx data");
|
||||
console.log(err);
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log("ERR get conf lastupdate");
|
||||
console.log(err);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
Nations.synchronizeold = () => {
|
||||
|
@@ -2,6 +2,7 @@ const glob = require("glob");
|
||||
const path = require("path");
|
||||
const fs = require("fs-extra");
|
||||
const dayjs = require("dayjs");
|
||||
const axios = require("axios");
|
||||
const conf = require(`${process.env.dirtown}/conf.json`);
|
||||
const Checkjson = require(`./Checkjson.js`);
|
||||
|
||||
@@ -148,7 +149,7 @@ Odmdb.schema = (schemaPath, objectName, withschemacheck) => {
|
||||
};
|
||||
};
|
||||
|
||||
Odmdb.Checkjson = (objectPath, objectName, data, withschemacheck) => {
|
||||
//Odmdb.Checkjson = (objectPath, objectName, data, withschemacheck) => {
|
||||
/*
|
||||
@objectPath path to the folder that contain /objects/objectName/ /lg/objectName_{lg}.json /schema/objectName.json
|
||||
@objectName name of object
|
||||
@@ -161,19 +162,18 @@ Odmdb.Checkjson = (objectPath, objectName, data, withschemacheck) => {
|
||||
or unconsitent data and schema from Checkjson.js Checkjson.schema.data
|
||||
|
||||
*/
|
||||
const res = { status: 200 };
|
||||
/* const res = { status: 200,ref="Odmdb",msg:"",data:{} };
|
||||
//get schema link of object
|
||||
const schemaPath = fs.readJsonSync(
|
||||
`${objectPath}/${objectName}/idx/confjson`
|
||||
`${objectPath}/${objectName}/idx/conf.json`
|
||||
)["schema"];
|
||||
if (schemaPath.substring(0, 4) == "http") {
|
||||
// lance requete http pour recuperer le schema
|
||||
} else {
|
||||
schema == "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
|
||||
res.data.schema = Odmdb.schema(objectPath, objectName, withschemacheck);
|
||||
}
|
||||
// check schema validity
|
||||
const schema = Odmdb.schema(objectPath, objectName, withschemacheck);
|
||||
if (schema.status != 200) return schema;
|
||||
// check schema validity in case withschemacheck
|
||||
if (schema.status != 200) return ;
|
||||
console.log("SCHEMA for checking:");
|
||||
console.log(schema.data);
|
||||
console.log("DATA to check:");
|
||||
@@ -207,6 +207,43 @@ Odmdb.Checkjson = (objectPath, objectName, data, withschemacheck) => {
|
||||
}
|
||||
return res;
|
||||
};
|
||||
*/
|
||||
Odmdb.getSchema=async (schemaPath,validschema)=>{
|
||||
/**
|
||||
* @schemaPath public http link or local path adminapi/schema/objectName.json or /tribename/schema/objectName
|
||||
* @return schema or {}
|
||||
*/
|
||||
const res={status:200,data:{schema:{}}}
|
||||
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 {
|
||||
if (schemaPath.substring(0,9)=="adminapi/"){
|
||||
schemaPath=`${conf.dirapi}/${schemaPath}`
|
||||
}else{
|
||||
schemaPath=`${conf.dirtown}/tribes/${schemaPath}`
|
||||
}
|
||||
if (!fs.existsSync(schemaPath)){
|
||||
return {status:404, ref:"Odmdb", msg:"schemanotfound", data:{schemaPath,schema:{}}}
|
||||
}
|
||||
res.data.schema=fs.readJsonSync(schemaPath)
|
||||
if (validschema ||1==1){
|
||||
const check = Checkjson.schema.validation(res.data.schema)
|
||||
if (check.err.length>0) {
|
||||
res.status=check.status
|
||||
res.data.err=check.err
|
||||
}
|
||||
//check json schema for Odmdb context
|
||||
if (!res.data.schema.apxprimarykey || !res.data.schema.properties[res.data.schema.apxprimarykey]){
|
||||
// primarykey require for Odmdb
|
||||
res.status=406
|
||||
if (!res.data.err) res.data.err=[];
|
||||
res.data.err.push({ref:"Odmdb",msg:"novalidprimarykey",data:{apxprimarykey:res.data.schema.apxprimarykey}})
|
||||
}
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
Odmdb.search = (objectPath, objectName, search) => {
|
||||
/*
|
||||
@search= {
|
||||
@@ -228,51 +265,78 @@ Odmdb.search = (objectPath, objectName, search) => {
|
||||
};
|
||||
Odmdb.get = (objectPath, objectName, uuidprimarykeyList, fieldList) => {
|
||||
/*
|
||||
@objectPath where object are store (where /object/conf.json indicate where the schema is)
|
||||
@uuidprimarykeyList list of uuid requested
|
||||
@fieldList key to return for each object
|
||||
Return objectName {status:200; data:{found:[{primarykey,field}],notfound:[uuid]}
|
||||
if all primarykey exist then data.notfound does not exist
|
||||
if all primarykey does not exist data.found does not exist
|
||||
Return {status:200; data:{uuid:{data filter by @fieldList},uuid:"notfound"}}
|
||||
*/
|
||||
const res = { status: 200, data: {} };
|
||||
uuidprimarykeyList.forEach((id) => {
|
||||
if (fs.existsSync(`${objectPath}/${objectName}/${id}.json`)) {
|
||||
if (!res.data.found) res.data.found = [];
|
||||
uuidprimarykeyList.forEach(id => {
|
||||
if (fs.existsSync(`${objectPath}/${objectName}/itm/${id}.json`)) {
|
||||
const objectdata = fs.readJsonSync(
|
||||
`${objectPath}/${objectName}/${id}.json`
|
||||
`${objectPath}/${objectName}/itm/${id}.json`
|
||||
);
|
||||
if (!fieldList) {
|
||||
res.data.found.push(objectdata);
|
||||
res.data[id]=objectdata;
|
||||
} else {
|
||||
const objinfo = {};
|
||||
fieldlList.forEach((k) => {
|
||||
if (objectdata[k]) objinfo[k] = objectdata[k];
|
||||
});
|
||||
res.data.found.push(objinfo);
|
||||
res.data[id]=objinfo;
|
||||
}
|
||||
} else {
|
||||
if (!res.data.notfound) res.data.notfound = [];
|
||||
res.data[id]="notfound";
|
||||
}
|
||||
});
|
||||
return res;
|
||||
};
|
||||
Odmdb.create = (objectPath, objectName, data) => {
|
||||
Odmdb.create = (objectPath, objectName, data, accessright) => {
|
||||
/*
|
||||
Create an objects data into objectName
|
||||
@objectPath path to the folder that contain /objects/objectName/ /objectsInfo/objectName_lg.json /objectsMeta/objectName.json
|
||||
@objectName name of object
|
||||
@data data to check based on objectsMeta definition
|
||||
@accessright a string with accessright of the user on this objectName ex: "CRUDO" or "R" or "O"
|
||||
*/
|
||||
};
|
||||
Odmdb.update = (objectPath, objectName, data) => {
|
||||
Odmdb.update = async (objectPath, objectName, data, id, accessright) => {
|
||||
/*
|
||||
Create an objects data into objectName
|
||||
@objectPath path to the folder that contain /objects/objectName/ /objectsInfo/objectName_lg.json /objectsMeta/objectName.json
|
||||
@objectName name of object
|
||||
@data data to check based on objectsMeta definition
|
||||
*/
|
||||
if (!fs.existsSync(`${objectPath}/${objectName}/itm/${id}.json`)){
|
||||
return {status:404,ref:"Odmdb",msg:"itmnotfound",data:{objectPath,objectName,id}}
|
||||
}
|
||||
const currentobj=fs.readJSONSync(`${objectPath}/${objectName}/itm/${id}.json`)
|
||||
Object.keys(data).forEach(k=>{
|
||||
currentobj[k]=data[k]
|
||||
})
|
||||
if (currentobj.dt_update) currentobj.dt_update=dayjs().toISOString();
|
||||
const schemaPath = fs.readJsonSync(
|
||||
`${objectPath}/${objectName}/conf.json`
|
||||
)["schema"];
|
||||
const getschema = await Odmdb.getSchema(schemaPath);
|
||||
if (getschema.status!=200 || Object.keys(getschema.data.schema).length==0) {
|
||||
console.log('this is not suppose to happen in Odmdb',Object.keys(getschema.data.schema))
|
||||
return getschema
|
||||
}
|
||||
const schema=getschema.data.schema;
|
||||
const check = Checkjson.schema.data(schema,currentobj,false);
|
||||
console.log(check)
|
||||
if (check.err.length==0){
|
||||
// update
|
||||
fs.outputJsonSync(`${objectPath}/${objectName}/itm/${id}.json`,currentobj)
|
||||
//@todo select index file to generate depending of k update currently we re-index all
|
||||
|
||||
return {status:200,ref:"Odmdb",msg:"updatesuccessfull"}
|
||||
}else{
|
||||
return {status:409, ref:"Odmdb",msg:"datavsschemaunconsistent",data:check.err}
|
||||
}
|
||||
};
|
||||
Odmdb.delete = (objectPath, objectName, data) => {
|
||||
Odmdb.delete = (objectPath, objectName, data,accessright) => {
|
||||
/*
|
||||
Create an objects data into objectName
|
||||
@objectPath path to the folder that contain /objects/objectName/ /objectsInfo/objectName_lg.json /objectsMeta/objectName.json
|
||||
@@ -313,7 +377,8 @@ Odmdb.updatefromidxall = (objectname, idxname, data, lastupdate) => {
|
||||
localidx[id].dt_update > data[id].dt_update
|
||||
) {
|
||||
// means local information is fresher than the one in data for replacement
|
||||
conflastupdate = dayjs();
|
||||
// .toISIString ex: 2019-01-25T02:00:00.000Z'
|
||||
conflastupdate = dayjs().toISOString();
|
||||
} else {
|
||||
// replace itm with data
|
||||
localidx[id] = data[id];
|
||||
|
@@ -86,9 +86,9 @@ Pagans.create = (alias, publicKey) => {
|
||||
* @todo use Odmdb to add a pagan
|
||||
*/
|
||||
let apxpagans = {};
|
||||
if (fs.existsSync(`${__dirapi}/nationchains/pagans/idx/alias_all.json`)) {
|
||||
if (fs.existsSync(`${conf.dirapi}/nationchains/pagans/idx/alias_all.json`)) {
|
||||
apxpagans = fs.readJsonSync(
|
||||
`${__dirapi}/nationchains/pagans/idx/alias_all.json`
|
||||
`${conf.dirapi}/nationchains/pagans/idx/alias_all.json`
|
||||
);
|
||||
}
|
||||
apxpagans[alias] = { alias, publicKey };
|
||||
@@ -100,7 +100,7 @@ Pagans.create = (alias, publicKey) => {
|
||||
alias,
|
||||
publicKey,
|
||||
});
|
||||
return { status: 200, data: { alias, publicKey } };
|
||||
return { status: 200, ref:"Pagans", msg:"identitycreate",data: { alias, publicKey } };
|
||||
};
|
||||
|
||||
Pagans.personupdate = (alias, tribe, persondata) => {
|
||||
|
@@ -6,9 +6,141 @@ const jwt = require("jwt-simple");
|
||||
const UUID = require("uuid");
|
||||
const conf = require(`${process.env.dirtown}/conf.json`);
|
||||
const Checkjson = require(`./Checkjson.js`);
|
||||
const Odmdb = require("./Odmdb.js");
|
||||
|
||||
const Towns = {};
|
||||
Towns.changeowner = (newowner, requestby) => {
|
||||
|
||||
Towns.create = () => {
|
||||
// Create a new town from conf (generate in apxtribe.js if town not already exist in the server)
|
||||
console.log(
|
||||
`RUNNING A NEW SETUP with nation ${conf.nationId} and town ${conf.townId} to be accessible in dns http://${conf.dns}`
|
||||
);
|
||||
const initconf = fs.readJSONSync(
|
||||
`${conf.dirapi}/adminapi/www/adminapx/initconf.json`
|
||||
);
|
||||
// Synchronize nationchains/
|
||||
const { updateobjectsfromfreshesttown } = require("./api/models/Nations.js");
|
||||
updateobjectsfromfreshesttown(initconf.towns, {
|
||||
pagans: "alias_all.json",
|
||||
towns: "townId_all.json",
|
||||
nations: "nationId_all.json",
|
||||
});
|
||||
|
||||
initconf.dirapi = conf.dirapi;
|
||||
initconf.dirtown = conf.dirtown;
|
||||
initconf.nationId = conf.nationId;
|
||||
initconf.townId = conf.townId;
|
||||
initconf.sudoerUser = process.env.USER;
|
||||
if (!initconf.dns.includes(conf.dns)) {
|
||||
initconf.dns.push(conf.dns);
|
||||
}
|
||||
initconf.nginx.include.push(`${initconf.dirapi}/adminapi/www/nginx_*.conf`);
|
||||
initconf.nginx.include.push(`${initconf.dirtown}/tribes/**/www/nginx_*.conf`);
|
||||
initconf.nginx.logs = `${initconf.dirtown}/logs/nginx/adminapx`;
|
||||
fs.ensureDirSync(`${initconf.dirtown}/logs/nginx`);
|
||||
fs.ensureDirSync(`${initconf.dirtown}/tmp/tokens`);
|
||||
|
||||
initconf.nginx.website = "adminapx";
|
||||
initconf.nginx.fswww = `${initconf.dirapi}/adminapi/www`;
|
||||
initconf.nginx.pageindex = "index_en.html";
|
||||
const { exec } = require("child_process");
|
||||
exec(
|
||||
`sudo chown -R ${process.env.USER}:${process.env.USER} /etc/nginx`,
|
||||
(error, stdout, stderr) => {
|
||||
if (error) {
|
||||
console.log("\x1b[42m", error, stdout, stderr, "x1b[0m");
|
||||
console.log(
|
||||
`impossible to change owner of /etc/nginx by ${initconf.sudoerUser}:${initconf.sudoerUser}`
|
||||
);
|
||||
fs.removeSync(initconf.dirtown);
|
||||
process.exit();
|
||||
} else {
|
||||
console.log(
|
||||
`successfull sudo chown -R ${process.env.USER}:${process.env.USER} /etc/nginx`
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
// create town env
|
||||
fs.outputJsonSync(`${initconf.dirtown}/conf.json`, initconf, { space: 2 });
|
||||
const nginxconf = fs.readFileSync(
|
||||
"./adminapi/www/adminapx/conf/nginx.conf.mustache",
|
||||
"utf8"
|
||||
);
|
||||
const proxyparams = fs.readFileSync(
|
||||
"./adminapi/www/adminapx/conf/nginxproxyparams.mustache",
|
||||
"utf8"
|
||||
);
|
||||
const websiteconf = fs.readFileSync(
|
||||
"./adminapi/www/adminapx/conf/nginxmodelwebsite.conf.mustache",
|
||||
"utf8"
|
||||
);
|
||||
|
||||
// saved and change nginx conf
|
||||
if (!fs.existsSync("/etc/nginx/nginxconf.saved")) {
|
||||
fs.moveSync("/etc/nginx/nginx.conf", "/etc/nginx/nginxconf.saved");
|
||||
console.log(
|
||||
"your previous /etc/nginx/nginx.conf was backup in /etc/nginx/nginxconf.saved"
|
||||
);
|
||||
}
|
||||
fs.outputFileSync(
|
||||
"/etc/nginx/nginx.conf",
|
||||
mustache.render(nginxconf, initconf),
|
||||
"utf8"
|
||||
);
|
||||
fs.outputFileSync(
|
||||
"/etc/nginx/proxy_params",
|
||||
mustache.render(proxyparams, initconf),
|
||||
"utf8"
|
||||
);
|
||||
fs.outputFileSync(
|
||||
`${initconf.dirapi}/adminapi/www/nginx_adminapx.conf`,
|
||||
mustache.render(websiteconf, initconf),
|
||||
"utf8"
|
||||
);
|
||||
exec(initconf.nginx.restart, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
console.log("\x1b[42m", error, stdout, stderr, "x1b[0m");
|
||||
//@todo supprimer la derniere config nginx et relancer
|
||||
fs.moveSync("/etc/nginx/nginxconf.saved", "/etc/nginx/nginx.conf");
|
||||
console.log("Restart yarn dev with correct parameter");
|
||||
// cleanup
|
||||
fs.removeSync(initconf.dirtown);
|
||||
} else {
|
||||
//@TODO à finaliser en test sur machien pour creation de nouvelles villes
|
||||
// add town in nationchains
|
||||
const gettown = Odmdb.get(`${initconf.dirapi}/nationchains`, "towns", [
|
||||
initconf.townId,
|
||||
]);
|
||||
if (gettown.data[initconf.townId] == "notfound") {
|
||||
Odmdb.create(
|
||||
`${initconf.dirapi}/nationschains`,
|
||||
"towns",
|
||||
{
|
||||
townId: initconf.townId,
|
||||
nationId: initconf.nationId,
|
||||
dns: initconf.dns,
|
||||
IP: "127.0.0.1",
|
||||
status: "unchain",
|
||||
tribes: [],
|
||||
},
|
||||
false
|
||||
);
|
||||
} else if (gettown.data[initconf.townId].dns !== initconf.dns) {
|
||||
//reinstallation d'une town sur un autre serveur maj du dns , l'ip le status et les tribes se mettent à jour via l'interface
|
||||
const updtown = Odmdb.update(
|
||||
`${initconf.dirapi}/nationchains`,
|
||||
"towns",
|
||||
{ dns: initconf.dns },
|
||||
initconf.townId
|
||||
);
|
||||
}
|
||||
console.log(`ready to use http://${initconf.dns}`);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Towns.changeowner = async (newowner, requestby) => {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@@ -21,16 +153,47 @@ Towns.changeowner = (newowner, requestby) => {
|
||||
};
|
||||
}
|
||||
if (!conf.mayorId || conf.mayorId == requestby) {
|
||||
// update object town + town/conf.json + setup_xx.json
|
||||
// update object town + town/conf.json + setup_xx.json
|
||||
const gettown = Odmdb.get(`${conf.dirapi}/nationchains`, "towns", [
|
||||
conf.townId,
|
||||
]);
|
||||
console.log(`before town: ${conf.townId}`, gettown);
|
||||
if (gettown.data[conf.townId] == "notfound") {
|
||||
return {
|
||||
status: 404,
|
||||
ref: "towns",
|
||||
msg: "townIdnotfound",
|
||||
data: { townId: conf.townId },
|
||||
};
|
||||
}
|
||||
gettown.data[conf.townId].mayorId = newowner;
|
||||
const objup = await Odmdb.update(
|
||||
`${conf.dirapi}/nationchains`,
|
||||
"towns",
|
||||
gettown.data[conf.townId],
|
||||
conf.townId
|
||||
);
|
||||
//update the itm town
|
||||
if (objup.status != 200) {
|
||||
return objup;
|
||||
}
|
||||
console.log(`after town update: ${conf.townId}`, gettown);
|
||||
|
||||
conf.mayorId = newowner;
|
||||
fs.outputJsonSync(`${process.env.dirtown}/conf.json`, conf);
|
||||
const setup = fs.readJSONSync(`${dirapi}/adminapi/www/adminapx/conf/setup_xx.json`)
|
||||
setup.mayorId=newowner;
|
||||
fs.outputJsonSync(`${dirapi}/adminapi/www/adminapx/conf/setup_xx.json`,setup);
|
||||
fs.outputJsonSync(`${process.env.dirtown}/conf.json`, conf);
|
||||
const setup = fs.readJSONSync(
|
||||
`${conf.dirapi}/adminapi/www/adminapx/conf/setup_xx.json`
|
||||
);
|
||||
conf.mayorId = newowner;
|
||||
//update the setup file for webapp adminapi
|
||||
fs.outputJsonSync(
|
||||
`${conf.dirapi}/adminapi/www/adminapx/conf/setup_xx.json`,
|
||||
setup
|
||||
);
|
||||
return {
|
||||
status: 200,
|
||||
ref: "towns",
|
||||
msg: "newownerchangesusccess",
|
||||
msg: "newownerchangesuccess",
|
||||
data: { alias: newowner },
|
||||
};
|
||||
}
|
||||
|
@@ -114,7 +114,7 @@ router.post("/", checkHeaders, isAuthenticated, (req, res) => {
|
||||
* - check that alias does not already exist (if yes then verifiedsigne would be false)
|
||||
* Need to wait next block chain to be sure that alias is register in the blokchain
|
||||
*/
|
||||
console.log("pass ici", req.body);
|
||||
//console.log("pass ici", req.body);
|
||||
const feedback = { alias: req.body.alias, publickey: req.body.publickey };
|
||||
const newpagan = Pagans.create(req.body.alias, req.body.publickey);
|
||||
if (newpagan.status == 200) {
|
||||
@@ -136,20 +136,29 @@ router.post("/", checkHeaders, isAuthenticated, (req, res) => {
|
||||
if (req.app.locals.tribeids.includes(req.body.trustedtribe)) {
|
||||
delete feedback.withemail;
|
||||
const persondata = { recovery: feedback };
|
||||
res.send(
|
||||
const persoup = Pagans.personupdate(req.body.alias, req.body.trustedtribe, persondata)
|
||||
res.status(persoup.status).json(persoup)
|
||||
/*res.send(
|
||||
Pagans.personupdate(req.body.alias, req.body.trustedtribe, persondata)
|
||||
);
|
||||
);*/
|
||||
} else {
|
||||
res.send({
|
||||
status: 404,
|
||||
res.status(404).json({
|
||||
status:404,
|
||||
ref: "Pagans",
|
||||
msg: "tribedoesnotexist",
|
||||
data: { tribe: req.body.trustedtribe },
|
||||
});
|
||||
/*res.send({
|
||||
status: 404,
|
||||
ref: "Pagans",
|
||||
msg: "tribedoesnotexist",
|
||||
data: { tribe: req.body.trustedtribe },
|
||||
});*/
|
||||
}
|
||||
} else {
|
||||
newpagan.data = feedback;
|
||||
res.send(newpagan);
|
||||
res.status(newpagan.status).json(newpagan);
|
||||
//res.send(newpagan);
|
||||
}
|
||||
} else {
|
||||
//error to create pagan
|
||||
@@ -166,10 +175,9 @@ router.put("/person", checkHeaders, isAuthenticated, (req, res) => {
|
||||
* add/update a person = alias + tribe with specific accessright and specific schema link to tribe
|
||||
* @todo add tribe/schema/person.json
|
||||
*/
|
||||
console.log(req.body);
|
||||
res.send(
|
||||
Pagans.personupdate(req.body.alias, req.session.header.xtribe, req.body)
|
||||
);
|
||||
//console.log(req.body);
|
||||
const persoup = Pagans.personupdate(req.body.alias, req.session.header.xtribe, req.body);
|
||||
res.status(persoup.status).json(persoup);
|
||||
});
|
||||
router.delete("/:alias", checkHeaders, isAuthenticated, (req, res) => {
|
||||
/**
|
||||
|
@@ -22,8 +22,10 @@ router.get("/changeowner/:alias",checkHeaders, isAuthenticated, (req, res) => {
|
||||
* @apiError (404) {object} {ref:"towns",msg:"aliasnotallow",data: { alias} }
|
||||
*
|
||||
**/
|
||||
res.send(Towns.changeowner(req.params.alias, req.session.header.alias));
|
||||
res.send(Towns.changeowner(req.params.alias, req.session.header.xalias));
|
||||
});
|
||||
|
||||
//=======================================================================================
|
||||
router.get("/person/:alias", checkHeaders, isAuthenticated, (req, res) => {
|
||||
/**
|
||||
* @api {get} /pagans/person:alias
|
||||
|
Reference in New Issue
Block a user