From 610799c2085b141cf8768261fa3a553deb0c3f70 Mon Sep 17 00:00:00 2001 From: Shweta Date: Fri, 15 Jan 2021 15:28:35 +0530 Subject: [PATCH 1/4] eslintcache --- client/.eslintcache | 2 +- rce-server/server/index.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/client/.eslintcache b/client/.eslintcache index 8f483d8..4763a7b 100644 --- a/client/.eslintcache +++ b/client/.eslintcache @@ -1 +1 @@ -[{"D:\\Webdev\\remote-code-executor\\client\\src\\index.js":"1","D:\\Webdev\\remote-code-executor\\client\\src\\reducers\\code.js":"2","D:\\Webdev\\remote-code-executor\\client\\src\\components\\home\\Home.js":"3","D:\\Webdev\\remote-code-executor\\client\\src\\components\\code-editor\\CodeEditorIndex.js":"4","D:\\Webdev\\remote-code-executor\\client\\src\\components\\code-editor\\Header.js":"5","D:\\Webdev\\remote-code-executor\\client\\src\\components\\code-editor\\Footer.js":"6","D:\\Webdev\\remote-code-executor\\client\\src\\actions\\type.js":"7","D:\\Webdev\\remote-code-executor\\client\\src\\components\\home\\Header.js":"8","D:\\Webdev\\remote-code-executor\\client\\src\\components\\code-editor\\Editor.js":"9","D:\\Webdev\\remote-code-executor\\client\\src\\components\\code-editor\\ToggleTheme.js":"10","D:\\Webdev\\remote-code-executor\\client\\src\\components\\code-editor\\Input.js":"11","D:\\Webdev\\remote-code-executor\\client\\src\\reducers\\index.js":"12"},{"size":743,"mtime":1610226935752,"results":"13","hashOfConfig":"14"},{"size":683,"mtime":1609936382135,"results":"15","hashOfConfig":"14"},{"size":1331,"mtime":1610388602041,"results":"16","hashOfConfig":"14"},{"size":848,"mtime":1610556042985,"results":"17","hashOfConfig":"14"},{"size":469,"mtime":1610380988937,"results":"18","hashOfConfig":"14"},{"size":238,"mtime":1610380988934,"results":"19","hashOfConfig":"14"},{"size":133,"mtime":1609936382123,"results":"20","hashOfConfig":"14"},{"size":360,"mtime":1610380988954,"results":"21","hashOfConfig":"14"},{"size":5692,"mtime":1610558222358,"results":"22","hashOfConfig":"14"},{"size":1731,"mtime":1610380988943,"results":"23","hashOfConfig":"14"},{"size":1515,"mtime":1610556042988,"results":"24","hashOfConfig":"14"},{"size":115,"mtime":1609936382137,"results":"25","hashOfConfig":"14"},{"filePath":"26","messages":"27","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"28"},"j0ormm",{"filePath":"29","messages":"30","errorCount":0,"warningCount":1,"fixableErrorCount":0,"fixableWarningCount":0,"source":"31","usedDeprecatedRules":"28"},{"filePath":"32","messages":"33","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"28"},{"filePath":"34","messages":"35","errorCount":0,"warningCount":1,"fixableErrorCount":0,"fixableWarningCount":0,"source":"36","usedDeprecatedRules":"28"},{"filePath":"37","messages":"38","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"39","messages":"40","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"41","messages":"42","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"43","messages":"44","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"45","messages":"46","errorCount":0,"warningCount":4,"fixableErrorCount":0,"fixableWarningCount":0,"source":null},{"filePath":"47","messages":"48","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"49","messages":"50","errorCount":0,"warningCount":1,"fixableErrorCount":0,"fixableWarningCount":0,"source":null},{"filePath":"51","messages":"52","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"D:\\Webdev\\remote-code-executor\\client\\src\\index.js",[],["53","54"],"D:\\Webdev\\remote-code-executor\\client\\src\\reducers\\code.js",["55"],"import { SET_OUTPUT, SET_ERROR, SET_LOADING_TRUE } from '../actions/type';\r\n\r\nconst initialState = {\r\n isFetching: false,\r\n output: '',\r\n error: ''\r\n};\r\n\r\nexport default function(state = initialState, action) {\r\n switch (action.type) {\r\n case SET_LOADING_TRUE:\r\n return {\r\n ...state,\r\n isFetching: true\r\n };\r\n case SET_OUTPUT:\r\n return {\r\n ...state,\r\n isFetching: false,\r\n output: action.payload.output,\r\n error: ''\r\n };\r\n case SET_ERROR:\r\n return {\r\n ...state,\r\n isFetching: false,\r\n error: action.payload,\r\n output: ''\r\n };\r\n default:\r\n return state;\r\n }\r\n}\r\n","D:\\Webdev\\remote-code-executor\\client\\src\\components\\home\\Home.js",[],"D:\\Webdev\\remote-code-executor\\client\\src\\components\\code-editor\\CodeEditorIndex.js",["56"],"import React, { useEffect } from \"react\";\r\nimport { ThemeProvider } from \"styled-components\";\r\nimport { lightTheme, darkTheme } from \"../../constants/theme\";\r\nimport { GlobalStyles } from \"../../constants/global\";\r\nimport { useDarkMode } from \"../../utils/useDarkMode\";\r\nimport Header from \"./Header.js\";\r\nimport Footer from \"./Footer.js\";\r\nimport Editor from \"./Editor\";\r\n\r\nconst CodeEditorIndex = () => {\r\n const [theme, toggleTheme, componentMounted] = useDarkMode();\r\n\r\n const themeMode = theme === \"light\" ? lightTheme : darkTheme;\r\n\r\n if (!componentMounted) {\r\n return
;\r\n }\r\n\r\n return (\r\n \r\n \r\n
\r\n
\r\n \r\n \r\n );\r\n};\r\n\r\nexport default CodeEditorIndex;\r\n","D:\\Webdev\\remote-code-executor\\client\\src\\components\\code-editor\\Header.js",[],"D:\\Webdev\\remote-code-executor\\client\\src\\components\\code-editor\\Footer.js",[],"D:\\Webdev\\remote-code-executor\\client\\src\\actions\\type.js",[],"D:\\Webdev\\remote-code-executor\\client\\src\\components\\home\\Header.js",[],"D:\\Webdev\\remote-code-executor\\client\\src\\components\\code-editor\\Editor.js",["57","58","59","60"],"D:\\Webdev\\remote-code-executor\\client\\src\\components\\code-editor\\ToggleTheme.js",[],"D:\\Webdev\\remote-code-executor\\client\\src\\components\\code-editor\\Input.js",["61"],"D:\\Webdev\\remote-code-executor\\client\\src\\reducers\\index.js",[],{"ruleId":"62","replacedBy":"63"},{"ruleId":"64","replacedBy":"65"},{"ruleId":"66","severity":1,"message":"67","line":9,"column":1,"nodeType":"68","endLine":33,"endColumn":2},{"ruleId":"69","severity":1,"message":"70","line":1,"column":17,"nodeType":"71","messageId":"72","endLine":1,"endColumn":26},{"ruleId":"69","severity":1,"message":"73","line":18,"column":7,"nodeType":"71","messageId":"72","endLine":18,"endColumn":10},{"ruleId":"69","severity":1,"message":"74","line":37,"column":10,"nodeType":"71","messageId":"72","endLine":37,"endColumn":22},{"ruleId":"75","severity":1,"message":"76","line":75,"column":6,"nodeType":"77","endLine":75,"endColumn":8,"suggestions":"78"},{"ruleId":"69","severity":1,"message":"79","line":82,"column":9,"nodeType":"71","messageId":"72","endLine":82,"endColumn":18},{"ruleId":"69","severity":1,"message":"80","line":37,"column":7,"nodeType":"71","messageId":"72","endLine":37,"endColumn":12},"no-native-reassign",["81"],"no-negated-in-lhs",["82"],"import/no-anonymous-default-export","Unexpected default export of anonymous function","ExportDefaultDeclaration","no-unused-vars","'useEffect' is defined but never used.","Identifier","unusedVar","'Row' is assigned a value but never used.","'windowHeight' is assigned a value but never used.","react-hooks/exhaustive-deps","React Hook useEffect has a missing dependency: 'dispatch'. Either include it or remove the dependency array.","ArrayExpression",["83"],"'getOutput' is assigned a value but never used.","'error' is assigned a value but never used.","no-global-assign","no-unsafe-negation",{"desc":"84","fix":"85"},"Update the dependencies array to be: [dispatch]",{"range":"86","text":"87"},[2310,2312],"[dispatch]"]ule.css\";\r\nimport \"./styles/style.css\";\r\nimport io from \"socket.io-client\";\r\nimport { Play } from \"react-feather\";\r\n\r\nconst ENDPOINT = \"http://localhost:3000\";\r\n\r\nconst socket = io(ENDPOINT);\r\n\r\nconst Row = styled.div`\r\n display: flex;\r\n flex-direction: row;\r\n flex-grow: 1;\r\n`;\r\n\r\nconst OutputWindow = styled.div`\r\n border-radius: 5px;\r\n padding: 20px;\r\n box-sizing: border-box;\r\n overflow: auto;\r\n flex: 1;\r\n color: ${(props) => (props.error ? \"red\" : \"black\")};\r\n`;\r\n\r\nconst CodeEditor = ({ theme }) => {\r\n const loading = useSelector((state) => state.code.isFetching);\r\n const [isEditorReady, setIsEditorReady] = useState(false);\r\n const [windowWidth, setWindowWidth] = useState(window.innerWidth);\r\n const [windowHeight, setWindowHeight] = useState(window.innerHeight);\r\n const [language, setLanguage] = useState(\"c\");\r\n const [input, setInput] = useState(\"\");\r\n // const [output, setOutput] = useState(\"\");\r\n const [code, setCode] = useState(\"\");\r\n const dispatch = useDispatch();\r\n const valueGetter = useRef();\r\n let output = useSelector((state) => state.code.output);\r\n\r\n let error = useSelector((state) => state.code.error);\r\n\r\n useEffect(() => {\r\n window.addEventListener(\"resize\", updateWindowDimensions);\r\n return () => {\r\n window.removeEventListener(\"resize\", updateWindowDimensions);\r\n };\r\n }, []);\r\n\r\n const updateWindowDimensions = () => {\r\n setWindowWidth(window.innerWidth);\r\n setWindowHeight(window.innerHeight);\r\n };\r\n\r\n useEffect(() => {\r\n console.log(\"socket: browser says ping (1)\");\r\n socket.on(\"setLanguage\", function (data) {\r\n // console.log(data);\r\n setLanguage(data);\r\n });\r\n socket.on(\"setInput\", (data) => {\r\n setInput(data);\r\n });\r\n socket.on(\"setOutput\", (data) => {\r\n dispatch(data);\r\n });\r\n socket.on(\"setCodeExec\", (data) => {\r\n setCode(data);\r\n });\r\n }, []);\r\n\r\n const handleEditorDidMount = (_valueGetter) => {\r\n setIsEditorReady(true);\r\n valueGetter.current = _valueGetter;\r\n };\r\n\r\n const getOutput = () => {\r\n // setOutput(out);\r\n // socket.emit(\"getOutput\", output);\r\n // setOutput(out);\r\n };\r\n const onChangeCode = (newValue, e) => {\r\n // console.log(\"onChange\" + e);\r\n socket.emit(\"getCodeExec\", e);\r\n setCode(e);\r\n };\r\n\r\n const SubmitCode = async () => {\r\n dispatch(setLoadingTrue());\r\n const res = await executeCode(code, language, input);\r\n dispatch(res);\r\n // getOutput();\r\n socket.emit(\"getOutput\", res);\r\n };\r\n\r\n const changeLanguage = (e) => {\r\n socket.emit(\"getLanguage\", e.target.value);\r\n setLanguage(e.target.value);\r\n };\r\n return (\r\n <>\r\n
\r\n 800 ? \"horizontal\" : \"vertical\"}\r\n sizes={[60, 40]}\r\n minSize={windowWidth > 800 ? 0 : 500}\r\n snapOffset={windowWidth > 800 ? 200 : 0}\r\n gutterSize={20}\r\n gutterAlign=\"center\"\r\n className={styles.splitHor}\r\n >\r\n
\r\n
\r\n \r\n \r\n {loading ? \"Loading..\" : \"Run Code\"}\r\n \r\n
\r\n
\r\n {/* */}\r\n \r\n
\r\n
\r\n
\r\n 800 ? [75, 25] : [100, 0]}\r\n minSize={0}\r\n snapOffset={windowWidth > 800 ? 100 : 0}\r\n gutterSize={20}\r\n gutterAlign=\"center\"\r\n className={styles.splitVer}\r\n >\r\n
\r\n
Output
\r\n \r\n {output ? console.log(output) : null}\r\n
\r\n                      {output === \"\" ? error : output}\r\n                    
\r\n
\r\n
\r\n
\r\n \r\n
\r\n \r\n
\r\n
\r\n \r\n
\r\n \r\n );\r\n};\r\n\r\nexport default CodeEditor;\r\n","D:\\Webdev\\remote-code-executor\\client\\src\\components\\code-editor\\ToggleTheme.js",[],"D:\\Webdev\\remote-code-executor\\client\\src\\components\\code-editor\\Input.js",["83"],"import React, { useEffect } from \"react\";\r\nimport styled from \"styled-components\";\r\nimport { useSelector } from \"react-redux\";\r\nimport styles from \"./styles/input.module.css\";\r\nimport io from \"socket.io-client\";\r\n\r\nconst ENDPOINT = \"http://localhost:3000\";\r\nconst socket = io(ENDPOINT);\r\n\r\nconst Div = styled.div`\r\n height: 100%;\r\n // flex: 1;\r\n box-sizing: border-box;\r\n margin: 20;\r\n color: ${(props) => props.error && \"red\"};\r\n background-color: \"#eeeeee\";\r\n overflow: scroll;\r\n outline: none;\r\n`;\r\n\r\nconst TextInput = styled.textarea`\r\n color: ${(props) => (props.theme === \"dark\" ? \"white\" : \"black\")};\r\n background-color: ${(props) =>\r\n props.theme === \"dark\" ? \"#1e1e1e\" : \"white\"};\r\n width: 100%;\r\n height: 100%;\r\n padding: 20px;\r\n outline: none;\r\n resize: none;\r\n border: none;\r\n border-radius: 0;\r\n font-size: 1.2em;\r\n`;\r\n\r\nconst Input = ({ input, setInput, theme }) => {\r\n let output = useSelector((state) => state.code.output);\r\n let error = useSelector((state) => state.code.error);\r\n\r\n useEffect(() => {\r\n console.log(output);\r\n }, [output]);\r\n return (\r\n
\r\n {\r\n console.log(e.target.value);\r\n socket.emit(\"getInput\", e.target.value);\r\n setInput(e.target.value);\r\n }}\r\n className={styles.textarea_input}\r\n placeholder=\"Give input here...\"\r\n />\r\n
\r\n );\r\n};\r\n\r\nexport default Input;\r\n","D:\\Webdev\\remote-code-executor\\client\\src\\components\\home\\Terminal.js",["84","85"],"D:\\Webdev\\remote-code-executor\\client\\src\\constants\\global.js",[],"D:\\Webdev\\remote-code-executor\\client\\src\\constants\\theme.js",[],"D:\\Webdev\\remote-code-executor\\client\\src\\utils\\useDarkMode.js",[],"D:\\Webdev\\remote-code-executor\\client\\src\\actions\\code.js",[],"D:\\Webdev\\remote-code-executor\\client\\src\\components\\code-editor\\Submit.js",[],{"ruleId":"86","replacedBy":"87"},{"ruleId":"88","replacedBy":"89"},{"ruleId":"90","severity":1,"message":"91","line":9,"column":1,"nodeType":"92","endLine":33,"endColumn":2},{"ruleId":"93","severity":1,"message":"94","line":1,"column":17,"nodeType":"95","messageId":"96","endLine":1,"endColumn":26},{"ruleId":"93","severity":1,"message":"97","line":18,"column":7,"nodeType":"95","messageId":"96","endLine":18,"endColumn":10},{"ruleId":"93","severity":1,"message":"98","line":37,"column":10,"nodeType":"95","messageId":"96","endLine":37,"endColumn":22},{"ruleId":"99","severity":1,"message":"100","line":75,"column":6,"nodeType":"101","endLine":75,"endColumn":8,"suggestions":"102"},{"ruleId":"93","severity":1,"message":"103","line":82,"column":9,"nodeType":"95","messageId":"96","endLine":82,"endColumn":18},{"ruleId":"93","severity":1,"message":"104","line":37,"column":7,"nodeType":"95","messageId":"96","endLine":37,"endColumn":12},{"ruleId":"93","severity":1,"message":"105","line":4,"column":20,"nodeType":"95","messageId":"96","endLine":4,"endColumn":24},{"ruleId":"93","severity":1,"message":"106","line":30,"column":7,"nodeType":"95","messageId":"96","endLine":30,"endColumn":11},"no-native-reassign",["107"],"no-negated-in-lhs",["108"],"import/no-anonymous-default-export","Unexpected default export of anonymous function","ExportDefaultDeclaration","no-unused-vars","'useEffect' is defined but never used.","Identifier","unusedVar","'Row' is assigned a value but never used.","'windowHeight' is assigned a value but never used.","react-hooks/exhaustive-deps","React Hook useEffect has a missing dependency: 'dispatch'. Either include it or remove the dependency array.","ArrayExpression",["109"],"'getOutput' is assigned a value but never used.","'error' is assigned a value but never used.","'User' is defined but never used.","'Body' is assigned a value but never used.","no-global-assign","no-unsafe-negation",{"desc":"110","fix":"111"},"Update the dependencies array to be: [dispatch]",{"range":"112","text":"113"},[2310,2312],"[dispatch]"] \ No newline at end of file +[{"/media/shweta/New Volume/projects/RCE-aasf/remote-code-executor/client/src/components/code-editor/Editor.js":"1","/media/shweta/New Volume/projects/RCE-aasf/remote-code-executor/client/src/components/code-editor/Header.js":"2"},{"size":5515,"mtime":1610560348532,"results":"3","hashOfConfig":"4"},{"size":451,"mtime":1610380045186,"results":"5","hashOfConfig":"4"},{"filePath":"6","messages":"7","errorCount":0,"warningCount":4,"fixableErrorCount":0,"fixableWarningCount":0,"source":null},"kizngo",{"filePath":"8","messages":"9","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"/media/shweta/New Volume/projects/RCE-aasf/remote-code-executor/client/src/components/code-editor/Editor.js",["10","11","12","13"],"/media/shweta/New Volume/projects/RCE-aasf/remote-code-executor/client/src/components/code-editor/Header.js",[],{"ruleId":"14","severity":1,"message":"15","line":18,"column":7,"nodeType":"16","messageId":"17","endLine":18,"endColumn":10},{"ruleId":"14","severity":1,"message":"18","line":37,"column":10,"nodeType":"16","messageId":"17","endLine":37,"endColumn":22},{"ruleId":"19","severity":1,"message":"20","line":75,"column":6,"nodeType":"21","endLine":75,"endColumn":8,"suggestions":"22"},{"ruleId":"14","severity":1,"message":"23","line":82,"column":9,"nodeType":"16","messageId":"17","endLine":82,"endColumn":18},"no-unused-vars","'Row' is assigned a value but never used.","Identifier","unusedVar","'windowHeight' is assigned a value but never used.","react-hooks/exhaustive-deps","React Hook useEffect has a missing dependency: 'dispatch'. Either include it or remove the dependency array.","ArrayExpression",["24"],"'getOutput' is assigned a value but never used.",{"desc":"25","fix":"26"},"Update the dependencies array to be: [dispatch]",{"range":"27","text":"28"},[2236,2238],"[dispatch]"] \ No newline at end of file diff --git a/rce-server/server/index.js b/rce-server/server/index.js index f58430b..af97ada 100644 --- a/rce-server/server/index.js +++ b/rce-server/server/index.js @@ -28,16 +28,16 @@ routes(app); io.on("connection", (socket) => { socket.on("getLanguage", function (lang) { - console.log("server " + lang); + // console.log("server " + lang); io.emit("setLanguage", lang); }); socket.on("getInput", function (input) { - console.log("server " + input); + // console.log("server " + input); io.emit("setInput", input); }); socket.on("getOutput", function (output) { - console.log("server " + output); + // console.log("server " + output); io.emit("setOutput", output); }); From 6e3c8134c5c5567c097fe10e29345cff9f0d526b Mon Sep 17 00:00:00 2001 From: Shweta Date: Wed, 20 Jan 2021 20:18:28 +0530 Subject: [PATCH 2/4] added links routes --- .../api/controllers/Links/controller.js | 82 +++++++++++++- .../server/api/controllers/Links/router.js | 5 +- .../api/middlewares/isLogged.handler.js | 2 +- .../server/api/services/auth.service.js | 16 +-- .../server/api/services/link.service.js | 100 ++++++++++++++---- rce-server/server/models/link.js | 18 ++-- 6 files changed, 179 insertions(+), 44 deletions(-) diff --git a/rce-server/server/api/controllers/Links/controller.js b/rce-server/server/api/controllers/Links/controller.js index 6f6f2e7..f6373b1 100644 --- a/rce-server/server/api/controllers/Links/controller.js +++ b/rce-server/server/api/controllers/Links/controller.js @@ -3,18 +3,19 @@ import LinkService from "../../services/link.service"; export class Controller { async generateLink(req, res) { try { + console.log(req.user); if (!req.user) { throw { message: "User must be logged in!!", }; } else { - const output = await LinkService.generate(req.user); - if (output) { - console.log(output); + const link = await LinkService.generate(req.user); + if (link) { + // console.log(output); return res.json({ status: 200, message: "Successfully generated link!!", - output, + link, }); } else { throw { @@ -29,6 +30,79 @@ export class Controller { }); } } + + async deleteLink(req, res) { + try { + if (!req.user) { + throw { + message: "User must be logged in!!", + }; + } else { + const { link } = req.body; + const output = await LinkService.delete(link, req.user); + // console.log(output); + if (output) { + return res.json({ + status: 200, + message: "Successfully ended interview!!", + }); + } + } + } catch (error) { + res.send({ + status: error.status || "500", + message: error.message || "Something Went Wrong", + }); + } + } + + async addEmail(req, res) { + try { + if (!req.user) { + throw { + message: "User must be logged in!!", + }; + } else { + const { link, email } = req.body; + const output = await LinkService.add(link, email, req.user); + if (output) { + return res.json({ + status: 200, + message: "Email added successfully!!", + }); + } + } + } catch (error) { + res.send({ + status: error.status || "500", + message: error.message || "Something Went Wrong", + }); + } + } + + async removeEmail(req, res) { + try { + if (!req.user) { + throw { + message: "User must be logged in!!", + }; + } else { + const { link, email } = req.body; + const output = await LinkService.remove(link, email, req.user); + if (output) { + return res.json({ + status: 200, + message: "Email removed successfully!!", + }); + } + } + } catch (error) { + res.send({ + status: error.status || "500", + message: error.message || "Something Went Wrong", + }); + } + } } export default new Controller(); diff --git a/rce-server/server/api/controllers/Links/router.js b/rce-server/server/api/controllers/Links/router.js index 6a6d341..c508f62 100644 --- a/rce-server/server/api/controllers/Links/router.js +++ b/rce-server/server/api/controllers/Links/router.js @@ -4,4 +4,7 @@ import isLoggedIn from "../../middlewares/isLogged.handler"; export default express .Router() - .post("/generate", isLoggedIn, controller.generateLink); + .post("/generate", isLoggedIn, controller.generateLink) + .post("/delete", isLoggedIn, controller.deleteLink) + .post("/addEmail", isLoggedIn, controller.addEmail) + .post("/removeEmail", isLoggedIn, controller.removeEmail); diff --git a/rce-server/server/api/middlewares/isLogged.handler.js b/rce-server/server/api/middlewares/isLogged.handler.js index b2e6aa7..bf7b53b 100644 --- a/rce-server/server/api/middlewares/isLogged.handler.js +++ b/rce-server/server/api/middlewares/isLogged.handler.js @@ -26,7 +26,7 @@ module.exports = (req, res, next) => { }); }); } catch (error) { - return res.status(err.status || 500).json({ + return res.status(error.status || 500).json({ status: error.status || "500", message: error.message || "Something Went Wrong", }); diff --git a/rce-server/server/api/services/auth.service.js b/rce-server/server/api/services/auth.service.js index 3d4a4d0..52918ae 100644 --- a/rce-server/server/api/services/auth.service.js +++ b/rce-server/server/api/services/auth.service.js @@ -9,22 +9,14 @@ class AuthService { let setUser = ""; const user = await User.findOne({ email }); if (!user) { - const newUser = new User({ + const newUser = await User.create({ email, name, dp, }); - newUser - .save() - .then((user) => { - setUser = user; - }) - .catch((err) => { - throw { - status: err.status, - message: err.message, - }; - }); + if (newUser) { + setUser = newUser; + } } else { setUser = user; } diff --git a/rce-server/server/api/services/link.service.js b/rce-server/server/api/services/link.service.js index 182ee66..368ee44 100644 --- a/rce-server/server/api/services/link.service.js +++ b/rce-server/server/api/services/link.service.js @@ -6,33 +6,95 @@ const BASE_URI = "http://localhost:3000/"; class LinkService { async generate(user) { try { - var setLink = ""; const uid = crypto.randomBytes(16).toString("hex"); const link = BASE_URI + uid; - // console.log(link); - const generatedLink = new Link({ + + const generatedLink = await Link.create({ link, interviewer: user._id, }); - generatedLink - .save() - .then((newLink) => { - // console.log(newLink.link); - // setLink = newLink; - return newLink; - }) - .catch((err) => { + + if (generatedLink) return generatedLink.link; + } catch (error) { + throw error; + } + } + + async delete(link, user) { + try { + const isLink = await Link.findOne({ link }); + if (isLink) { + if (user._id.toString() == isLink.interviewer.toString()) { + const deleted = await Link.deleteOne({ link }); + if (deleted.ok) return true; + } else { throw { - message: "Internal error: " + err.message, - status: err.status, + message: "Only interviewer can end the interview!!", }; - }); - // return setLink; + } + } else { + throw { + message: "Link not found!", + }; + } } catch (error) { - res.send({ - status: error.status || "500", - message: error.message || "Something Went Wrong", - }); + throw error; + } + } + + async add(link, email, user) { + try { + const isLink = await Link.findOne({ link }); + if (isLink) { + if (user._id.toString() == isLink.interviewer.toString()) { + const data = await Link.updateOne( + { link }, + { $push: { email } }, + { new: true } + ); + if (data.ok) { + return true; + } + } else { + throw { + message: "Only interviewer can add emails!!", + }; + } + } else { + throw { + message: "Link not found!", + }; + } + } catch (error) { + throw error; + } + } + + async remove(link, email, user) { + try { + const isLink = await Link.findOne({ link }); + if (isLink) { + if (user._id.toString() == isLink.interviewer.toString()) { + const data = await Link.updateOne( + { link }, + { $pull: { email } }, + { new: true } + ); + if (data.ok) { + return true; + } + } else { + throw { + message: "Only interviewer can remove emails!!", + }; + } + } else { + throw { + message: "Link not found!", + }; + } + } catch (error) { + throw error; } } } diff --git a/rce-server/server/models/link.js b/rce-server/server/models/link.js index b1ff06b..5cb97fe 100644 --- a/rce-server/server/models/link.js +++ b/rce-server/server/models/link.js @@ -12,13 +12,17 @@ const linkSchema = new mongoose.Schema( type: ObjectId, ref: "User", }, - // interviewee: [ - // { - // email: { - // type: String, - // }, - // }, - // ], + email: [ + { + type: String, + }, + ], + interviewee: [ + { + type: ObjectId, + ref: "User", + }, + ], }, { timestamps: true } ); From 2378febf983563ebdf2243d3dee038eda0abb870 Mon Sep 17 00:00:00 2001 From: Shweta Date: Wed, 20 Jan 2021 20:31:18 +0530 Subject: [PATCH 3/4] added environment variables --- rce-server/server/api/middlewares/isLogged.handler.js | 2 +- rce-server/server/api/services/auth.service.js | 2 +- rce-server/server/api/services/link.service.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rce-server/server/api/middlewares/isLogged.handler.js b/rce-server/server/api/middlewares/isLogged.handler.js index bf7b53b..85bb5ec 100644 --- a/rce-server/server/api/middlewares/isLogged.handler.js +++ b/rce-server/server/api/middlewares/isLogged.handler.js @@ -1,7 +1,7 @@ import jwt from "jsonwebtoken"; import mongoose from "mongoose"; import User from "../../models/user"; -const JWT_SECRET = "abcd"; +const JWT_SECRET = process.env.JWT_SECRET; module.exports = (req, res, next) => { try { diff --git a/rce-server/server/api/services/auth.service.js b/rce-server/server/api/services/auth.service.js index 52918ae..fff96c5 100644 --- a/rce-server/server/api/services/auth.service.js +++ b/rce-server/server/api/services/auth.service.js @@ -1,7 +1,7 @@ import jwt from "jsonwebtoken"; import mongoose from "mongoose"; const User = require("../../models/user"); -const JWT_SECRET = "abcd"; +const JWT_SECRET = process.env.JWT_SECRET; class AuthService { async execute(email, name, dp) { diff --git a/rce-server/server/api/services/link.service.js b/rce-server/server/api/services/link.service.js index 368ee44..a06b4b9 100644 --- a/rce-server/server/api/services/link.service.js +++ b/rce-server/server/api/services/link.service.js @@ -1,7 +1,7 @@ import mongoose from "mongoose"; import crypto from "crypto"; const Link = require("../../models/link"); -const BASE_URI = "http://localhost:3000/"; +const BASE_URI = process.env.BASE_URI; class LinkService { async generate(user) { From e09af387388852a49611a49d7b52c0099df0ca30 Mon Sep 17 00:00:00 2001 From: Shweta Date: Wed, 20 Jan 2021 21:06:15 +0530 Subject: [PATCH 4/4] added interview routes --- .../api/controllers/Interview/controller.js | 29 +++++++++++++++++-- .../api/controllers/Interview/router.js | 3 +- .../api/controllers/Links/controller.js | 2 +- .../server/api/controllers/Links/router.js | 4 +-- .../server/api/services/interview.service.js | 27 +++++++++++++++++ .../server/api/services/link.service.js | 2 +- 6 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 rce-server/server/api/services/interview.service.js diff --git a/rce-server/server/api/controllers/Interview/controller.js b/rce-server/server/api/controllers/Interview/controller.js index 3fc7562..9bd46bc 100644 --- a/rce-server/server/api/controllers/Interview/controller.js +++ b/rce-server/server/api/controllers/Interview/controller.js @@ -1,9 +1,32 @@ -import Server from "../../../common/server"; +import InterviewService from "../../services/interview.service"; +const BASE_URI = process.env.BASE_URI; export class Controller { async connect(req, res) { - const uid = req.params.uid; - console.log(uid); + try { + if (!req.user) { + throw { + message: "User must be logged in!!", + }; + } else { + const uid = req.params.uid; + const link = `${BASE_URI}/interview/${uid}`; + console.log(uid); + output = await InterviewService.execute(link, user); + if (output) { + res.json({ + status: 200, + message: "Connected to interview", + output, + }); + } + } + } catch (error) { + res.send({ + status: error.status || "500", + message: error.message || "Something Went Wrong", + }); + } } } diff --git a/rce-server/server/api/controllers/Interview/router.js b/rce-server/server/api/controllers/Interview/router.js index be60390..e94bd35 100644 --- a/rce-server/server/api/controllers/Interview/router.js +++ b/rce-server/server/api/controllers/Interview/router.js @@ -1,4 +1,5 @@ import * as express from "express"; import controller from "./controller"; +import isLoggedIn from "../../middlewares/isLogged.handler"; -export default express.Router().post("/:uid", controller.connect); +export default express.Router().get("/:uid", isLoggedIn, controller.connect); diff --git a/rce-server/server/api/controllers/Links/controller.js b/rce-server/server/api/controllers/Links/controller.js index f6373b1..0413487 100644 --- a/rce-server/server/api/controllers/Links/controller.js +++ b/rce-server/server/api/controllers/Links/controller.js @@ -3,7 +3,7 @@ import LinkService from "../../services/link.service"; export class Controller { async generateLink(req, res) { try { - console.log(req.user); + // console.log(req.user); if (!req.user) { throw { message: "User must be logged in!!", diff --git a/rce-server/server/api/controllers/Links/router.js b/rce-server/server/api/controllers/Links/router.js index c508f62..4de5fa0 100644 --- a/rce-server/server/api/controllers/Links/router.js +++ b/rce-server/server/api/controllers/Links/router.js @@ -6,5 +6,5 @@ export default express .Router() .post("/generate", isLoggedIn, controller.generateLink) .post("/delete", isLoggedIn, controller.deleteLink) - .post("/addEmail", isLoggedIn, controller.addEmail) - .post("/removeEmail", isLoggedIn, controller.removeEmail); + .put("/addEmail", isLoggedIn, controller.addEmail) + .put("/removeEmail", isLoggedIn, controller.removeEmail); diff --git a/rce-server/server/api/services/interview.service.js b/rce-server/server/api/services/interview.service.js new file mode 100644 index 0000000..0ca99f5 --- /dev/null +++ b/rce-server/server/api/services/interview.service.js @@ -0,0 +1,27 @@ +import mongoose from "mongoose"; +const Link = require("../../models/link"); + +class InterviewService { + async execute(link, user) { + try { + const isLink = await Link.findOne({ link }) + .populate("interviewer") + .populate("interviewee"); + if (isLink) { + if (user._id.toString() == isLink.interviewer.toString()) { + return isLink; + } else { + } + } else { + throw { + status: 404, + message: "Invalid link", + }; + } + } catch (error) { + throw error; + } + } +} + +export default new InterviewService(); diff --git a/rce-server/server/api/services/link.service.js b/rce-server/server/api/services/link.service.js index a06b4b9..d888958 100644 --- a/rce-server/server/api/services/link.service.js +++ b/rce-server/server/api/services/link.service.js @@ -7,7 +7,7 @@ class LinkService { async generate(user) { try { const uid = crypto.randomBytes(16).toString("hex"); - const link = BASE_URI + uid; + const link = `${BASE_URI}/interview/${uid}`; const generatedLink = await Link.create({ link,