Commit 8c96c6d7 authored by antoine masson's avatar antoine masson
Browse files

v0.3

- Add template files func : template variable and translation can be saved in public/template
- Templates files are automatically loaded if database is empty
- Possibility to upload and download templates
parent 76a2d988
......@@ -2,5 +2,6 @@ module.exports = {
port: process.env.PORT || 3000,
token:process.env.TOKEN ||"2fe#AYOIdcXyz!7QLUO2@QxJkSNoYK0*AO65lrE4b*nR3JdnRD4Yf2PIthXipcLAZ892a59sj*Q4UDBRtoe5U&C2m2FboDIQ5X3$",
uploads_dir:'./public/uploads/',
template_dir:'./public/template/',
bcrypt_salt:10,
};
const mongoose = require('mongoose');
const crypto = require('crypto')
const fs = require("fs");
const API = require("../config/API");
const Survey = mongoose.model('Survey');
const Token = mongoose.model('Token');
......@@ -14,6 +16,36 @@ function randomValueHex(len) {
.slice(0, len) // return required number of characters
}
async function json2mango(datajson,mongo){
for (let i in datajson){
datajson[i]['_id'] = new mongoose.Types.ObjectId();
}
await mongo.create(datajson);
}
exports.initSurveyTemplate = async()=>{
if(await LanguageTrans.countDocuments() === 0){
fs.readFile(API.template_dir+'LanguageTrans.json', 'utf-8', async (err, data) => {
if (err) {
throw err;
}
await json2mango(JSON.parse(data.toString()), LanguageTrans)
})
console.log("LanguageTrans initialized")
}
if(await LanguageDef.countDocuments() === 0){
fs.readFile(API.template_dir+'LanguageDef.json', 'utf-8', async (err, data) => {
if (err) {
throw err;
}
await json2mango(JSON.parse(data.toString()), LanguageDef)
})
console.log("LanguageDef initialized")
}
};
exports.createNewSurvey = async (req, res) => {
try {
req.body.general.short_name=req.body.general.short_name.toUpperCase().replace(/ /g, '');
......@@ -155,6 +187,62 @@ exports.list_all_langdef = async (req, res) => {
}
};
exports.save_langdef = async (req,res) =>{
try{
LanguageDef.find().
exec(async function (err, langdefs) {
fs.writeFileSync(API.template_dir+'LanguageDef.json', JSON.stringify(langdefs,null,4));
await res.status(201).json({message:"Configuration file saved",status:"OK"});
});
} catch (err) {
res.status(400).json({ message: err,status:"Error"});
}
};
exports.download_langdef = async (req,res) =>{
try{
LanguageDef.find().
exec(async function (err, langdefs) {
res.set("Content-Type", "application/octet-stream");
res.set("Content-Disposition", "attachment;filename=LanguageDef.json");
res.send(JSON.stringify(langdefs,null,4));
});
} catch (err) {
res.status(400).json({ message: err,status:"Error"});
}
};
exports.upload_langdef = async (req,res) => {
try{
if (!req.files || Object.keys(req.files).length === 0) {
return res.status(400).json({ message: "No file provided",status:"Error"});
}
await LanguageDef.deleteMany();
await json2mango(JSON.parse(req.files.templatefile.data.toString()), LanguageDef)
await res.status(201).json({message:"Configuration file uploaded",status:"OK"});
} catch (err) {
res.status(400).json({ message: err,status:"Error"});
}
};
exports.load_langdef= async (req,res) =>{
try{
await LanguageDef.deleteMany();
fs.readFile(API.template_dir+'LanguageDef.json', 'utf-8', async (err, data) => {
if (err) {
throw err;
}
await json2mango(JSON.parse(data.toString()), LanguageDef)
})
await res.status(201).json({message:"Configuration file loaded",status:"OK"});
} catch (err) {
res.status(400).json({ message: err,status:"Error"});
}
};
exports.delete_a_langdef = async (req, res) => {
try {
await LanguageDef.deleteOne({ _id: req.params.id});
......@@ -205,6 +293,56 @@ exports.list_all_langtrans = async (req, res) => {
res.status(400).json({ message: err,status:"Error"});
}
};
exports.save_langtrans = async (req,res) =>{
try{
LanguageTrans.find().
exec(async function (err, langtrans) {
fs.writeFileSync(API.template_dir+'LanguageTrans.json', JSON.stringify(langtrans,null,4));
await res.status(201).json({message:"Translation file saved",status:"OK"});
});
} catch (err) {
res.status(400).json({ message: err,status:"Error"});
}
};
exports.load_langtrans = async (req,res) =>{
try{
await LanguageTrans.deleteMany({});
fs.readFile(API.template_dir+'LanguageTrans.json', 'utf-8', async (err, data) => {
if (err) {
throw err;
}
await json2mango(JSON.parse(data.toString()), LanguageTrans)
})
await res.status(201).json({message:"Translation file loaded",status:"OK"});
} catch (err) {
res.status(400).json({ message: err,status:"Error"});
}
};
exports.download_langtrans = async (req,res) =>{
try{
LanguageTrans.find().
exec(async function (err, langtrans) {
res.set("Content-Type", "application/octet-stream");
res.set("Content-Disposition", "attachment;filename=LanguageTrans.json");
await res.send(langtrans);
});
} catch (err) {
res.status(400).json({ message: err,status:"Error"});
}
};
exports.upload_langtrans = async (req,res) => {
try{
if (!req.files || Object.keys(req.files).length === 0) {
return res.status(400).json({ message: "No file provided",status:"Error"});
}
await LanguageTrans.deleteMany();
await json2mango(JSON.parse(req.files.templatefile.data.toString()), LanguageTrans)
await res.status(201).json({message:"Translation file uploaded",status:"OK"});
} catch (err) {
res.status(400).json({ message: err,status:"Error"});
}
};
exports.delete_a_langtrans = async (req, res) => {
try {
......
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const morgan = require("morgan");
const DEBUG = require('debug')('API:index');
const fileUpload = require('express-fileupload');
// const zip = require('express-zip');
mongo_config = require('./config/DB');
api_config = require('./config/API');
const mongo_config = require('./config/DB');
const api_config = require('./config/API');
global.AdminUser = require('./models/AdminUserModel');
global.Token=require('./models/TokenModel');
global.Survey=require('./models/SurveyModel');
global.Language=require('./models/SurveyLanguageModel');
const SurveyController = require( "./controllers/SurveyController.js");
const routes = require('./routes/Route');
const routesAdminUsers = require('./routes/AdminUserRoute');
......@@ -29,11 +33,15 @@ mongoose.connect(
err => { console.log('Can not connect to the database'+ err)}
);
const index = express();
SurveyController.initSurveyTemplate();
const index = express();
index.use(morgan("dev"));
index.use(cors());
index.use(bodyParser.urlencoded({ extended: true }));
index.use(bodyParser.json());
index.use(fileUpload());
routes(index);
routesAdminUsers(index);
......
This diff is collapsed.
[
{
"type": "String",
"field": "WELCOME",
"description": "Welcome text",
"createdAt": "2021-01-20T21:56:23.148Z",
"updatedAt": "2021-01-20T21:56:23.148Z",
"id": "6032c9f757ff477c472e182e"
},
{
"type": "String",
"field": "NEXT",
"description": "Next Btn",
"createdAt": "2021-01-20T21:56:17.093Z",
"updatedAt": "2021-01-20T21:56:17.093Z",
"id": "6032c9f757ff477c472e182d"
},
{
"type": "String",
"field": "AGREEMENT",
"description": "Text that user should accept on the welcome page",
"createdAt": "2021-02-09T19:15:48.034Z",
"updatedAt": "2021-02-09T19:15:48.034Z",
"id": "6032c9f757ff477c472e182f"
},
{
"type": "String",
"field": "AGREE",
"description": "AGREE btn",
"createdAt": "2021-02-09T19:16:03.364Z",
"updatedAt": "2021-02-09T19:16:03.364Z",
"id": "6032c9f757ff477c472e1830"
},
{
"type": "String",
"field": "REFUSE",
"description": "Refuse btn",
"createdAt": "2021-02-09T19:16:11.618Z",
"updatedAt": "2021-02-09T19:16:11.618Z",
"id": "6032c9f757ff477c472e1831"
},
{
"type": "String",
"field": "PREVIOUS",
"description": "Previous Btn",
"createdAt": "2021-02-09T19:16:21.393Z",
"updatedAt": "2021-02-09T19:16:21.393Z",
"id": "6032c9f757ff477c472e1832"
},
{
"type": "String",
"field": "AGREETITLE",
"description": "Agreement text title",
"createdAt": "2021-02-09T19:17:15.957Z",
"updatedAt": "2021-02-09T19:17:15.957Z",
"id": "6032c9f757ff477c472e1834"
},
{
"type": "String",
"field": "POC",
"description": "PersoneOfContact Text",
"createdAt": "2021-02-09T19:16:43.101Z",
"updatedAt": "2021-02-09T19:16:43.101Z",
"id": "6032c9f757ff477c472e1833"
},
{
"type": "String",
"field": "SELECTONE",
"description": "Validation Text : You need to select one value",
"createdAt": "2021-02-14T12:38:30.004Z",
"updatedAt": "2021-02-14T12:38:30.004Z",
"id": "6032c9f757ff477c472e1835"
},
{
"type": "String",
"field": "USEMIN",
"description": "Validation Text : Please use at least",
"createdAt": "2021-02-14T12:39:26.095Z",
"updatedAt": "2021-02-14T12:39:26.095Z",
"id": "6032c9f757ff477c472e1837"
},
{
"type": "String",
"field": "USEMAX",
"description": "Validation Text : Please use maximum",
"createdAt": "2021-02-14T12:39:11.596Z",
"updatedAt": "2021-02-14T12:39:11.596Z",
"id": "6032c9f757ff477c472e1836"
},
{
"type": "String",
"field": "CHARACTERS",
"description": "Word characters",
"createdAt": "2021-02-14T12:39:47.817Z",
"updatedAt": "2021-02-14T12:39:47.817Z",
"id": "6032c9f757ff477c472e1838"
},
{
"type": "String",
"field": "ALLOWEDMAX",
"description": "Validation Text Allowed maximum is",
"createdAt": "2021-02-14T12:40:31.860Z",
"updatedAt": "2021-02-14T12:40:31.860Z",
"id": "6032c9f757ff477c472e1839"
},
{
"type": "String",
"field": "MANDATORYFIELD",
"description": "Validation text : Mandatory field",
"createdAt": "2021-02-14T12:41:14.347Z",
"updatedAt": "2021-02-14T12:41:14.347Z",
"id": "6032c9f757ff477c472e183b"
},
{
"type": "String",
"field": "ALLOWEDMIN",
"description": "Validation text : Allowed miniumum is",
"createdAt": "2021-02-14T12:40:47.527Z",
"updatedAt": "2021-02-14T12:40:47.527Z",
"id": "6032c9f757ff477c472e183a"
},
{
"type": "String",
"field": "MOVEBOX",
"description": "Validation text : You need to sort all the boxes",
"createdAt": "2021-02-17T15:19:24.085Z",
"updatedAt": "2021-02-17T15:19:24.085Z",
"id": "6032c9f757ff477c472e183c"
}
]
\ No newline at end of file
[
{
"language": "ENGLISH",
"flag": "gb",
"fields": [
{
"_id": "602d34083d523f5ca9f794a3",
"field": "NEXT",
"text": "next",
"id": "602d34083d523f5ca9f794a3"
},
{
"_id": "602d34083d523f5ca9f794a4",
"field": "WELCOME",
"text": "welcome",
"id": "602d34083d523f5ca9f794a4"
},
{
"_id": "602d34083d523f5ca9f794a5",
"field": "ACCEPT",
"text": "accept",
"id": "602d34083d523f5ca9f794a5"
},
{
"_id": "602d34083d523f5ca9f794a6",
"field": "REFUSE",
"text": "refuse",
"id": "602d34083d523f5ca9f794a6"
},
{
"_id": "602d34083d523f5ca9f794a7",
"field": "PREVIOUS",
"text": "previous",
"id": "602d34083d523f5ca9f794a7"
},
{
"_id": "602d34083d523f5ca9f794a8",
"field": "POC",
"text": "Person of Contact",
"id": "602d34083d523f5ca9f794a8"
},
{
"_id": "602d34083d523f5ca9f794a9",
"field": "AGREEMENT",
"text": "I accept that my data are used ...",
"id": "602d34083d523f5ca9f794a9"
},
{
"_id": "602d34083d523f5ca9f794aa",
"field": "AGREETITLE",
"text": "Data Use Agreements",
"id": "602d34083d523f5ca9f794aa"
},
{
"_id": "602d34083d523f5ca9f794ab",
"field": "AGREE",
"text": "agree",
"id": "602d34083d523f5ca9f794ab"
},
{
"_id": "602d34083d523f5ca9f794ac",
"field": "SELECTONE",
"text": "You need to select one value",
"id": "602d34083d523f5ca9f794ac"
},
{
"_id": "602d34083d523f5ca9f794ad",
"field": "USEMAX",
"text": "Please use maximum",
"id": "602d34083d523f5ca9f794ad"
},
{
"_id": "602d34083d523f5ca9f794ae",
"field": "USEMIN",
"text": "Please use maximum",
"id": "602d34083d523f5ca9f794ae"
},
{
"_id": "602d34083d523f5ca9f794af",
"field": "CHARACTERS",
"text": "characters",
"id": "602d34083d523f5ca9f794af"
},
{
"_id": "602d34083d523f5ca9f794b0",
"field": "ALLOWEDMAX",
"text": "Allowed maximum is",
"id": "602d34083d523f5ca9f794b0"
},
{
"_id": "602d34083d523f5ca9f794b1",
"field": "ALLOWEDMIN",
"text": "Allowed miniumum is",
"id": "602d34083d523f5ca9f794b1"
},
{
"_id": "602d34083d523f5ca9f794b2",
"field": "MANDATORYFIELD",
"text": "Mandatory field",
"id": "602d34083d523f5ca9f794b2"
},
{
"_id": "602d34083d523f5ca9f794b3",
"field": "MOVEBOX",
"text": "You need to sort all the boxes",
"id": "602d34083d523f5ca9f794b3"
}
],
"updatedAt": "2021-02-17T15:19:36.676Z",
"createdAt": "2021-02-17T15:19:36.676Z",
"status": "TODO",
"id": "6032bcaa6ebb9c7a9092f188"
},
{
"language": "FRENCH",
"flag": "fr",
"fields": [
{
"_id": "602d341b3d523f5ca9f794b4",
"field": "NEXT",
"text": "suivant",
"id": "602d341b3d523f5ca9f794b4"
},
{
"_id": "602d341b3d523f5ca9f794b5",
"field": "WELCOME",
"text": "bienvenue",
"id": "602d341b3d523f5ca9f794b5"
},
{
"_id": "602d341b3d523f5ca9f794b6",
"field": "AGREEMENT",
"text": "J'accepte que mes données soient ...",
"id": "602d341b3d523f5ca9f794b6"
},
{
"_id": "602d341b3d523f5ca9f794b7",
"field": "ACCEPT",
"text": "accepter",
"id": "602d341b3d523f5ca9f794b7"
},
{
"_id": "602d341b3d523f5ca9f794b8",
"field": "REFUSE",
"text": "refuser",
"id": "602d341b3d523f5ca9f794b8"
},
{
"_id": "602d341b3d523f5ca9f794b9",
"field": "PREVIOUS",
"text": "précédent",
"id": "602d341b3d523f5ca9f794b9"
},
{
"_id": "602d341b3d523f5ca9f794ba",
"field": "POC",
"text": "Personne de Contact",
"id": "602d341b3d523f5ca9f794ba"
},
{
"_id": "602d341b3d523f5ca9f794bb",
"field": "AGREETITLE",
"text": " Accord pour l’utilisation des données",
"id": "602d341b3d523f5ca9f794bb"
},
{
"_id": "602d341b3d523f5ca9f794bc",
"field": "AGREE",
"text": "accepter",
"id": "602d341b3d523f5ca9f794bc"
},
{
"_id": "602d341b3d523f5ca9f794bd",
"field": "SELECTONE",
"text": "Vous devez selectionner une valeur",
"id": "602d341b3d523f5ca9f794bd"
},
{
"_id": "602d341b3d523f5ca9f794be",
"field": "USEMAX",
"text": "Merci d'utiliser un maximum de",
"id": "602d341b3d523f5ca9f794be"
},
{
"_id": "602d341b3d523f5ca9f794bf",
"field": "USEMIN",
"text": "Merci d'utiliser un minimum de",
"id": "602d341b3d523f5ca9f794bf"
},
{
"_id": "602d341b3d523f5ca9f794c0",
"field": "CHARACTERS",
"text": "lettres",
"id": "602d341b3d523f5ca9f794c0"
},
{
"_id": "602d341b3d523f5ca9f794c1",
"field": "ALLOWEDMAX",
"text": "Le maximum autorisé est",
"id": "602d341b3d523f5ca9f794c1"
},
{
"_id": "602d341b3d523f5ca9f794c2",
"field": "ALLOWEDMIN",
"text": "Le minimum autorisé est",
"id": "602d341b3d523f5ca9f794c2"
},
{
"_id": "602d341b3d523f5ca9f794c3",
"field": "MANDATORYFIELD",
"text": "Chanps obligatoire",
"id": "602d341b3d523f5ca9f794c3"
},
{
"_id": "602d341b3d523f5ca9f794c4",
"field": "MOVEBOX",
"text": "Veuillez classer toutes les boites",
"id": "602d341b3d523f5ca9f794c4"
}
],
"updatedAt": "2021-02-17T15:19:55.318Z",
"createdAt": "2021-02-17T15:19:55.318Z",
"status": "TODO",
"id": "6032bcaa6ebb9c7a9092f189"
}
]
\ No newline at end of file
......@@ -17,12 +17,22 @@ module.exports = app => {
app.post("/surveys/langdef", auth(true), Builder.createNewLangdef);
app.delete("/surveys/langdef/:id", auth(true), Builder.delete_a_langdef);
app.put("/surveys/langdef/:id", auth(true), Builder.update_a_langdef);
app.get("/surveys/savelangdef", auth(true), Builder.save_langdef);
app.get("/surveys/loadlangdef", auth(true), Builder.load_langdef);
app.get("/surveys/downlangdef", auth(true), Builder.download_langdef);
app.post("/surveys/uploadlangdef", auth(true), Builder.upload_langdef);