/**
|
* HomeviewerController.js
|
*
|
* This controller handles home viewer pages
|
*/
|
|
const FurnplanInstanceHistoryService = require("../services/FurnplanInstaceHistoryService.js");
|
|
module.exports = {
|
|
/*
|
* Form
|
*/
|
form: function (req, res) {
|
return res.view();
|
},
|
|
form_post: function (req, res) {
|
if (!req.param("number") || req.param("number").length === 0) {
|
return res.json({ "status": "error", "message": req.__("fv.home.form.error.number.missing") });
|
}
|
|
const number = req.param("number").replace(" ", "");
|
|
// use access manager for protection from brute force attacks
|
AccessManagerService.create(req.auth_cookie);
|
|
const am = req.auth_cookie.accessManager;
|
|
am.setMaxTries(3).setPause(15);
|
|
// if the user tried more than 3 times a wrong number
|
const waitMessage = {
|
"status": "error",
|
"message": req.__("fv.home.form.error.number.wait"),
|
"seconds": am.getSeconds()
|
};
|
|
if (!am.canTry()) return res.json(waitMessage);
|
|
Project.findOne({ number: number }).exec(function (err, project) {
|
try {
|
if (err) throw err;
|
|
if (project) {
|
|
if (project.expiresAt.setDate(project.expiresAt.getDate() + 1) < new Date()) {
|
am.failed();
|
// send message according to remaining tries
|
if (!am.canTry()) {
|
waitMessage.seconds = am.getSeconds();
|
return res.json(waitMessage);
|
}
|
return res.json({ "status": "error", "message": req.__("fv.home.form.error.expired") });
|
}
|
// delete access manager if everything went fine
|
delete am;
|
delete req.auth_cookie.accessManager;
|
|
return res.json({ "status": "ok", "path": "/homeviewer/" + project.identifier });
|
}
|
else {
|
throw "Project not Found";
|
}
|
}
|
catch (e) {
|
// increase try count if the number doesn't exist
|
am.failed();
|
// send message according to remaining tries
|
if (!am.canTry()) {
|
waitMessage.seconds = am.getSeconds();
|
return res.json(waitMessage);
|
}
|
else {
|
return res.json({ "status": "error", "message": req.__("fv.home.form.error.number.wrong") });
|
}
|
}
|
});
|
},
|
|
/**
|
* Viewer page by guid (for uploaded data)
|
*/
|
guid: async function (req, res) {
|
let hideBackButton = false;
|
|
AccessManagerService.create(req.auth_cookie);
|
|
const am = req.auth_cookie.accessManager;
|
|
am.setMaxTries(3).setPause(15);
|
|
if (!am.canTry()) {
|
return res.view("error/index", {
|
data: {
|
title: "fv.error.title.error",
|
url: "/homeviewer/form",
|
message: "fv.error.to_many_reloads"
|
}
|
});
|
}
|
|
try {
|
let configuration;
|
let configId = "";
|
|
const project = await Project.findOne({ $or: [{ identifier: req.param("guid") }, { number: req.param("guid") }] });
|
|
if (!project) {
|
Winston.warn("Can't load project with guid " + req.param("guid"));
|
}
|
|
const customerNo = project.get("customerNo");
|
|
const query = await UseCaseConfigurationStorePermissionService.addStorePermission(
|
{
|
customerNo,
|
usageIntention: "homeviewer"
|
},
|
customerNo
|
);
|
|
const customHomeviewerConfiguration = await UseCaseConfiguration.findOne(query);
|
|
if (customHomeviewerConfiguration) {
|
const newConfiguration = await ConfigurationManager.newConfiguration();
|
|
configuration = customHomeviewerConfiguration.get("configuration");
|
configuration = ConfigurationManager.merge(newConfiguration, configuration);
|
configId = customHomeviewerConfiguration._id;
|
|
hideBackButton = true;
|
}
|
else {
|
configuration = await ConfigurationManager.getHomeviewerConfiguration();
|
|
if (req.options.readOnly) {
|
configuration.toolbar_button_homeviewer_finish_planning = false;
|
}
|
}
|
|
if (project.expiresAt.setDate(project.expiresAt.getDate() + 1) < new Date()) {
|
throw Error("fv.home.form.error.expired");
|
}
|
|
// guid was valid, so authenticate the visitor anonymously if he isn't authenticated yet
|
const user = req.user || await Opus.login(project.customerNo, "homeViewer", "homeViewer");
|
|
user.data.cloudID = project.number;
|
|
// add anonymous role and save user in session
|
if (user.permissions.indexOf("anonymous") < 0) {
|
user.permissions.push("anonymous");
|
}
|
|
await Session.update({ _id: req.session._id }, { $addToSet: { users: user } });
|
|
// save hit to this project for usage statistics
|
const hit = await Hit.create({
|
client: req.headers["user-agent"],
|
project: project
|
});
|
|
if (!hit) {
|
throw Error("Creation of hit has failed for project guid " + req.param("guid"));
|
}
|
|
await FurnplanInstanceHistoryService.writeDocument(user.opusSessionId, user.customerNo, req.url, configId, "Homeviewer", req.headers["user-agent"] || req.headers["User-Agent"] || "");
|
|
return res.view({
|
project: project.toJSON(),
|
g_contact: {
|
contactEmail: project.get("contactEmail"),
|
contactPhone: project.get("contactPhone"),
|
contactName: project.get("contactName")
|
},
|
isLocalRequest: Helper.isLocalRequest(req.connection.remoteAddress),
|
g_auth_id: req.auth_cookie.id,
|
g_configuration: configuration,
|
g_sessionId: user.opusSessionId,
|
g_language: req.query.lang || req.getLocale(),
|
g_reCaptcha: Config.furnview.reCaptchaSiteKey || ""
|
});
|
}
|
catch (e) {
|
am.failed();
|
|
// send message according to remaining tries
|
if (!am.canTry()) {
|
return res.view("error/index", {
|
data: {
|
title: "fv.error.title.error",
|
url: "/homeviewer/form",
|
message: e.message,
|
hideBackButton
|
}
|
});
|
}
|
|
return res.view("error/index", {
|
data: {
|
title: "fv.error.title.error",
|
url: "/homeviewer/form",
|
message: e.message || "fv.error.to_many_reloads",
|
hideBackButton
|
}
|
});
|
}
|
},
|
|
sendRequest: async function (req, res) {
|
req.body.toMyselfOnly = req.body.toMyselfOnly === "true";
|
|
const user = req.user;
|
|
if (!user) {
|
return res.status(403).json({ status: "forbidden" });
|
}
|
|
try {
|
const cleanNumber = req.body.number.replace(" ", "");
|
const exportProject = await Project.findOne({ number: cleanNumber });
|
|
const dealerMailLocals = {
|
number: req.body.number,
|
mail: exportProject.get("email"),
|
name: req.body.firstName + " " + req.body.lastName,
|
phone: req.body.phone,
|
message: req.body.message || "-"
|
};
|
|
const customerMailLocals = {
|
number: req.body.number,
|
link: `https://view.furnplan.de/homeviewer/${exportProject.get("identifier")}`,
|
|
name: req.body.firstName + " " + req.body.lastName,
|
phone: req.body.phone,
|
mail: exportProject.get("email"),
|
message: req.body.message || "-",
|
|
sellerName: exportProject.get("contactName"),
|
sellerPhone: exportProject.get("contactPhone"),
|
sellerMail: exportProject.get("contactEmail")
|
};
|
|
Mailer.sendHomeviewerRequest(exportProject.get("contactEmail"), exportProject.get("email"), dealerMailLocals, customerMailLocals, exportProject.get("uiLanguage"), req.body.toMyselfOnly);
|
|
return res.json({ status: "ok" });
|
}
|
catch (e) {
|
return res.status(404).json({ status: "not found" });
|
}
|
},
|
|
shareByMail: async function (req, res) {
|
const user = req.user;
|
|
if (!user) {
|
return res.status(403).json({ status: "forbidden" });
|
}
|
|
try {
|
const cleanNumber = req.body.number.replace(" ", "");
|
const exportProject = await Project.findOne({ number: cleanNumber });
|
|
const locals = {
|
number: req.body.number,
|
link: `https://view.furnplan.de/shareviewer/${exportProject.get("identifier")}`
|
};
|
|
Mailer.shareHomeviewerByMail(exportProject.get("email"), locals, exportProject.get("uiLanguage"));
|
|
return res.json({ status: "ok" });
|
}
|
catch (e) {
|
return res.status(404).json({ status: "not found" });
|
}
|
}
|
};
|