/**
|
* FurnplanNodeController.js
|
*/
|
|
const FurnviewError = require("furncloud-library").Errors.FurnviewError;
|
const cookieParserUtils = require("client-sessions").util;
|
|
const authCookieDefine = {
|
cookieName: "auth_cookie",
|
secret: "86ut6a6is23tijkz5fs51xugzcxcfi4v3kqobrax9wa8pmuu5emnv2pvzg8jgvqx",
|
cookie: {
|
maxAge: 1000 * 60 * 60 * 24 * 365 * 2, // 1000 milli seconds * 60 seconds * 60 minutes * 24 hours * 365 days * 2 years
|
httpOnly: true
|
}
|
};
|
|
module.exports = {
|
|
get: function (req, res) {
|
const user = req.user;
|
const userId = req.auth_cookie.id;
|
const queryParameters = req.query;
|
|
FurnplanNodeManager
|
.getNode(user, userId, queryParameters)
|
.then((data) => {
|
res.json(data);
|
})
|
.catch((error) => {
|
if (error instanceof FurnviewError.SessionAlreadyExists) {
|
Winston.error("User has already a furnplan session: ", error);
|
res.json(422, { error: error.code });
|
}
|
else {
|
Winston.error("Unable to start a furnplan session", error);
|
res.json(503, { error: "Service unavailable" });
|
}
|
});
|
},
|
|
freeze: function (req, res) {
|
const user = req.user;
|
const authCookie = req.cookies.auth_cookie || req.auth_cookie.id || "";
|
if (!authCookie) {
|
res.json(500, { error: "Internal server error!" });
|
}
|
FurnplanNodeManager
|
.freezeNode(user, authCookie)
|
.then(function (data) {
|
if (data) {
|
let authCookieEncoded = authCookie;
|
if (/([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}){1}/.test(authCookieEncoded)) {
|
authCookieEncoded = cookieParserUtils.encode(authCookieDefine, authCookieEncoded);
|
}
|
const aToken = Buffer.from(authCookieEncoded).toString("base64");
|
let reverseExtendToken = "";
|
for (let t = aToken.length - 1; t >= 0; t--) {
|
reverseExtendToken = reverseExtendToken + aToken[t] + String.fromCharCode(Math.floor(Math.random() * (90 - 65)) + 65);
|
}
|
data.aToken = reverseExtendToken;
|
}
|
res.json(data);
|
})
|
.catch((error) => {
|
if (error instanceof FurnviewError.SessionAlreadyFrozen) {
|
Winston.error("User has already a furnplan session: ", error);
|
res.json(422, { error: error.code });
|
}
|
else {
|
Winston.error("Unable to start a furnplan session", error);
|
res.json(503, { error: "Service unavailable" });
|
}
|
});
|
},
|
|
unfreeze: function (req, res) {
|
if (!req.query.arToken) {
|
res.json(500, { error: "Internal server error!" });
|
}
|
const user = req.user;
|
FurnplanNodeManager
|
.unfreezeNode(user, req.query.arToken)
|
.then(function (data) {
|
res.json(data);
|
})
|
.catch((error) => {
|
if (error instanceof FurnviewError.SessionAlreadyFrozen) {
|
Winston.error("User has already a furnplan session: ", error);
|
res.json(422, { error: error.code });
|
}
|
else {
|
Winston.error("Unable to start a furnplan session", error);
|
res.json(503, { error: "Service unavailable" });
|
}
|
});
|
},
|
|
destroyAll: function (req, res) {
|
|
},
|
|
destroy: function (req, res) {
|
|
}
|
};
|