Skip to content

Commit

Permalink
enhance: customize for UZH usage (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
rschlaefli authored Sep 24, 2023
1 parent 3d4c425 commit 782844f
Show file tree
Hide file tree
Showing 23 changed files with 1,131 additions and 30 deletions.
1 change: 1 addition & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ DATABASE_USER=escapp
DATABASE_PASS=escapp
DATABASE_NAME=escapp
DATABASE_HOST=localhost
DATABASE_URL=postgres://escapp:escapp@localhost:5432/escapp
4 changes: 2 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ app.use(express.static(path.join(__dirname, "public")));

app.use(i18n({
"translationsPath": path.join(__dirname, "i18n"),
"siteLangs": ["en", "es"],
"locales": ["en", "es"],
"siteLangs": ["en", "es", "de"],
"locales": ["en", "es", "de"],
"cookieLangName": "locale",
"defaultLang": "en",
"textsVarName": "i18n"
Expand Down
4 changes: 2 additions & 2 deletions controllers/api_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ exports.checkParticipantSafe = async (req, res, next) => {
const user = await authenticate(email, password, token);

if (user) {
if (!req.escapeRoom.forceLang && user.lang && i18n.lang !== user.lang && (user.lang === "es" || user.lang === "en")) {
if (!req.escapeRoom.forceLang && user.lang && i18n.lang !== user.lang && (user.lang === "es" || user.lang === "en" || user.lang === "de")) {
// eslint-disable-next-line global-require
res.locals.i18n_lang = user.lang === "es" ? "es" : "en";
res.locals.i18n_lang = ["en", "es", "de"].includes(user.lang) ? user.lang : "en";
// eslint-disable-next-line global-require
res.locals.i18n_texts = require(`../i18n/${res.locals.i18n_lang}`);
res.locals.i18n = res.locals.i18n_texts;
Expand Down
15 changes: 11 additions & 4 deletions controllers/escapeRoom_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ const {nextStep, prevStep} = require("../helpers/progress");
const {saveInterface, getERPuzzles, paginate, validationError} = require("../helpers/utils");
const es = require("../i18n/es");
const en = require("../i18n/en");
const de = require("../i18n/de");

const LANGUAGE_FILES = {
en,
es,
de
};

// Autoload the escape room with id equals to :escapeRoomId
exports.load = async (req, res, next, escapeRoomId) => {
Expand All @@ -19,8 +26,8 @@ exports.load = async (req, res, next, escapeRoomId) => {
if (res.locals) {
if (!req.session || req.session && req.session.user && req.session.user.isStudent) {
if (escapeRoom.forceLang && req.cookies && req.cookies.locale !== escapeRoom.forceLang) {
res.locals.i18n_texts = escapeRoom.forceLang === "es" ? es : en;
res.locals.i18n_lang = escapeRoom.forceLang === "es" ? "es" : "en";
res.locals.i18n_texts = LANGUAGE_FILES[escapeRoom.forceLang] || en;
res.locals.i18n_lang = Object.keys(LANGUAGE_FILES).includes(escapeRoom.forceLang) ? escapeRoom.forceLang : "en";
res.locals.i18n = res.locals.i18n_texts;
}
}
Expand Down Expand Up @@ -121,7 +128,7 @@ exports.create = async (req, res) => {
const escapeRoom = models.escapeRoom.build({title, subject, duration, "forbiddenLateSubmissions": forbiddenLateSubmissions === "on", invitation, description, supportLink, "scope": scope === "private", "teamSize": teamSize || 0, authorId, forceLang}); // Saves only the fields question and answer into the DDBB
const {i18n} = res.locals;

escapeRoom.forceLang = forceLang === "en" || forceLang === "es" ? forceLang : null;
escapeRoom.forceLang = Object.keys(LANGUAGE_FILES).includes(forceLang) ? forceLang : null;

try {
const er = await escapeRoom.save({"fields": ["title", "teacher", "subject", "duration", "description", "forbiddenLateSubmissions", "scope", "teamSize", "authorId", "supportLink", "invitation", "forceLang"]});
Expand Down Expand Up @@ -193,7 +200,7 @@ exports.update = async (req, res) => {
escapeRoom.invitation = body.invitation !== undefined ? body.invitation.toString() : undefined;
escapeRoom.scope = body.scope === "private";
escapeRoom.teamSize = body.teamSize || 0;
escapeRoom.forceLang = body.forceLang === "en" || body.forceLang === "es" ? body.forceLang : null;
escapeRoom.forceLang = Object.keys(LANGUAGE_FILES).includes(body.forceLang) ? body.forceLang : null;
const progressBar = body.progress;

try {
Expand Down
2 changes: 1 addition & 1 deletion db_migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function callback (err, stdout, stderr) {
}

if (process.env.DATABASE_URL) {
exec(`./node_modules/.bin/sequelize db:migrate --uri ${process.env.DATABASE_URL}`, callback);
exec(`./node_modules/.bin/sequelize db:migrate --url ${process.env.DATABASE_URL}`, callback);
} else if (process.env.DATABASE_HOST && process.env.DATABASE_USER && process.env.DATABASE_PASS && process.env.DATABASE_NAME) {
exec("./node_modules/.bin/sequelize db:migrate --config models/config.js", callback);
} else {
Expand Down
Loading

0 comments on commit 782844f

Please sign in to comment.