From 445c676c8de66541eb80585998321db5c4c664ba Mon Sep 17 00:00:00 2001 From: omarkoraa Date: Sat, 4 May 2019 15:40:40 +0200 Subject: [PATCH] Create New Form Type , New Form type Array , Dependencies --- client/src/App.js | 499 ++++++++++-------- .../src/components/pages/CreateANewCompany.js | 3 + client/src/components/pages/NewDepenency.js | 447 ++++++++++++++++ client/src/components/pages/NewFormType.js | 360 +++++++++++++ .../src/components/pages/NewFormTypeArray.js | 335 ++++++++++++ controllers/dependenciesController.js | 110 ++++ controllers/formTypeController.js | 11 + package.json | 3 +- routes/api/dependencies.js | 59 +++ routes/api/formTypes.js | 140 +++-- server.js | 3 +- validations/dynamicFormValidations.js | 6 + 12 files changed, 1720 insertions(+), 256 deletions(-) create mode 100644 client/src/components/pages/NewDepenency.js create mode 100644 client/src/components/pages/NewFormType.js create mode 100644 client/src/components/pages/NewFormTypeArray.js create mode 100644 controllers/dependenciesController.js create mode 100644 routes/api/dependencies.js diff --git a/client/src/App.js b/client/src/App.js index f564e48..0d3bd84 100644 --- a/client/src/App.js +++ b/client/src/App.js @@ -1,158 +1,184 @@ -import React, { Component } from 'react'; -import { Provider } from 'react-redux'; //new stuff -import store from './store'; //new stuff -import { BrowserRouter as Router, Route } from 'react-router-dom'; -import Navbar from 'react-bootstrap/Navbar'; -import Nav from 'react-bootstrap/Nav'; -import NavDropdown from 'react-bootstrap/NavDropdown'; -import Form from 'react-bootstrap/Form'; -import Button from 'react-bootstrap/Button'; -import FormControl from 'react-bootstrap/FormControl'; -import './App.scss'; -import LandingBody from './components/pages/landingBody'; -import Profile from './components/pages/profilePage'; -import Footer from './components/layout/footer'; -import Companies from './components/pages/Companies'; -import Side from './components/layout/Side'; -import NewNavBar from './components/layout/NewNavBar'; -import 'bootstrap/dist/css/bootstrap.min.css'; -import Navbar1 from './components/NavBar'; -import AdminProfile from './components/pages/AdminProfile'; -import adminPage from './components/pages/adminPage'; -import Register1 from './components/Register'; -import Login from './components/pages/Login'; -import Home from './components/Home'; -import DropdownTrial from './components/form/DropdownNationalities'; -import DropdownCities from './components/form/DropdownCities'; -import DropdownGovernorates from './components/form/DropdownGovernorates'; -import UnassignedForm from './components/pages/unassignedForm'; -import AssignToReviewer from './components/pages/assignToReviewer'; -import GetCaseReviewer from './components/pages/GetCaseReviewer'; -import EditProfile from './components/pages/EditProfile'; -import ChangePassword from './components/pages/ChangePassword'; -import GetFormByCompanyName from './components/form/GetFormByCompanyName'; -import GetCaseLawyer from './components/pages/getCaseLawyer'; -import ForgotPassword from './containers/ForgotPassword'; -import ResetPassword from './containers/ResetPassword'; -import About from './components/pages/about'; -import ApprovedCompanies from './components/pages/ApprovedCompanies'; -import companyPDF from './components/pages/companyPdf'; -import CreateANewCompany from './components/pages/CreateANewCompany'; -import AttributeArray from './components/pages/AttributeArray'; -import InProgressInvestorCases from './components/pages/inProgressInvestorCases'; -import EditInvCompany from './components/pages/EditInvCompany'; -import EditLawyerCompany from './components/pages/editLawyerForms'; +import React, { Component } from "react"; +import { Provider } from "react-redux"; //new stuff +import store from "./store"; //new stuff +import { BrowserRouter as Router, Route } from "react-router-dom"; +import Navbar from "react-bootstrap/Navbar"; +import Nav from "react-bootstrap/Nav"; +import NavDropdown from "react-bootstrap/NavDropdown"; +import Form from "react-bootstrap/Form"; +import Button from "react-bootstrap/Button"; +import FormControl from "react-bootstrap/FormControl"; +import "./App.scss"; +import LandingBody from "./components/pages/landingBody"; +import Profile from "./components/pages/profilePage"; +import Footer from "./components/layout/footer"; +import Companies from "./components/pages/Companies"; +import Side from "./components/layout/Side"; +import NewNavBar from "./components/layout/NewNavBar"; +import "bootstrap/dist/css/bootstrap.min.css"; +import Navbar1 from "./components/NavBar"; +import AdminProfile from "./components/pages/AdminProfile"; +import adminPage from "./components/pages/adminPage"; +import Register1 from "./components/Register"; +import Login from "./components/pages/Login"; +import Home from "./components/Home"; +import DropdownTrial from "./components/form/DropdownNationalities"; +import DropdownCities from "./components/form/DropdownCities"; +import DropdownGovernorates from "./components/form/DropdownGovernorates"; +import UnassignedForm from "./components/pages/unassignedForm"; +import AssignToReviewer from "./components/pages/assignToReviewer"; +import GetCaseReviewer from "./components/pages/GetCaseReviewer"; +import EditProfile from "./components/pages/EditProfile"; +import ChangePassword from "./components/pages/ChangePassword"; +import GetFormByCompanyName from "./components/form/GetFormByCompanyName"; +import GetCaseLawyer from "./components/pages/getCaseLawyer"; +import ForgotPassword from "./containers/ForgotPassword"; +import ResetPassword from "./containers/ResetPassword"; +import About from "./components/pages/about"; +import ApprovedCompanies from "./components/pages/ApprovedCompanies"; +import companyPDF from "./components/pages/companyPdf"; +import CreateANewCompany from "./components/pages/CreateANewCompany"; +import AttributeArray from "./components/pages/AttributeArray"; +import InProgressInvestorCases from "./components/pages/inProgressInvestorCases"; +import EditInvCompany from "./components/pages/EditInvCompany"; +import EditLawyerCompany from "./components/pages/editLawyerForms"; +import NewFormType from "./components/pages/NewFormType"; +import NewFormTypeArray from "./components/pages/NewFormTypeArray"; +import NewDependency from "./components/pages/NewDepenency"; class App extends Component { - constructor() { - super(); - localStorage.setItem('lang', localStorage.getItem('lang') || 'en'); - //this.connecToServer = this.connecToServer.bind(this); - } - // connecToServer() { - // fetch('/'); - // } + constructor() { + super(); + localStorage.setItem("lang", localStorage.getItem("lang") || "en"); + //this.connecToServer = this.connecToServer.bind(this); + } + // connecToServer() { + // fetch('/'); + // } - // componentDidMount() { - // this.connecToServer(); - // } + // componentDidMount() { + // this.connecToServer(); + // } - state = { - lang: localStorage.getItem('lang'), - formId: {} - }; - changelang = (lang) => { - localStorage.setItem('lang', lang); - this.setState({ lang: lang }); - console.log(lang); - }; + state = { + lang: localStorage.getItem("lang"), + formId: {} + }; + changelang = lang => { + localStorage.setItem("lang", lang); + this.setState({ lang: lang }); + console.log(lang); + }; - componentDidMount() { - document.title = 'GAFI'; - } + componentDidMount() { + document.title = "GAFI"; + } - setFormId = (formId) => { - this.setState({ formId: formId }); - if (formId.status === 'Unassigned') document.location.href = '/editUnassigned'; - else if (formId.type === 'SSCForm') document.location.href = '/editsscform'; - else document.location.href = '/editspcform'; - }; + setFormId = formId => { + this.setState({ formId: formId }); + if (formId.status === "Unassigned") + document.location.href = "/editUnassigned"; + else if (formId.type === "SSCForm") document.location.href = "/editsscform"; + else document.location.href = "/editspcform"; + }; - render() { - var currentLocation = window.location.pathname; - return ( - - -
- -
- } - /> + render() { + var currentLocation = window.location.pathname; + return ( + + +
+ +
+ ( + + )} + /> - } - /> - } - /> - } - /> - } - /> - } - /> - } - /> - } - /> - - - - - } - /> - } - /> - + ( + + )} + /> + ( + + )} + /> + } + /> + ( + + )} + /> + ( + + )} + /> + ( + + )} + /> + } + /> + + + + + ( + + )} + /> + ( + + )} + /> + - } - /> - {/* ( + + )} + /> + {/* ( @@ -174,78 +200,117 @@ class App extends Component { /> )} /> */} - } - /> - } - /> - } - /> + ( + + )} + /> + ( + + )} + /> + ( + + )} + /> - } - /> - - } - /> - } - /> - ( - - )} - /> - } - /> - } - /> - } - /> -
+ ( + + )} + /> + + ( + + )} + /> + ( + + )} + /> + ( + + )} + /> + ( + + )} + /> + ( + + )} + /> + ( + + )} + /> + ( + + )} + /> + ( + + )} + /> + ( + + )} + /> +
- {currentLocation === '/' ? ( - - ) : ( - - )} - {currentLocation === '/' ? null :
} - -
- - - ); - } + {currentLocation === "/" ? ( + + ) : ( + + )} + {currentLocation === "/" ? null :
} + +
+
+ + ); + } } export default App; diff --git a/client/src/components/pages/CreateANewCompany.js b/client/src/components/pages/CreateANewCompany.js index 1b59a1e..8e19f5e 100644 --- a/client/src/components/pages/CreateANewCompany.js +++ b/client/src/components/pages/CreateANewCompany.js @@ -321,7 +321,10 @@ class CreateANewCompany extends Component { //console.log(error.response.data.error.details['0'].message) // alert(error.response.data.error.details['0'].message) + if(error.response.data.error.details) alert(error.response.data.error.details['0'].message || error.response.data.error || error.response.data.errmsg||error.response.data); + else + alert(error.response.data.errmsg||error.response.data.error||error.response.data) //console.log(error); }); }; diff --git a/client/src/components/pages/NewDepenency.js b/client/src/components/pages/NewDepenency.js new file mode 100644 index 0000000..6904668 --- /dev/null +++ b/client/src/components/pages/NewDepenency.js @@ -0,0 +1,447 @@ +import "bootstrap/dist/css/bootstrap.css"; +import React, { Component } from "react"; +import ReactDOM from "react-dom"; +import axios from "axios"; +import MuiThemeProvider from "material-ui/styles/MuiThemeProvider"; +import AppBar from "material-ui/AppBar"; +import RaisedButton from "material-ui/RaisedButton"; +import TextField from "material-ui/TextField"; +import { + MDBRow, + MDBCol, + MDBInput, + MDBBtn, + MDBDropdown, + MDBDropdownToggle, + MDBDropdownItem, + MDBDropdownMenu +} from "mdbreact"; +import DropDownMenu from "material-ui/DropDownMenu"; +import MenuItem from "material-ui/MenuItem"; +import trans from "../translations/spcTranslation"; +//import {Dropdown} from 'react-bootstrap'; +import { Dropdown } from "semantic-ui-react"; +import DropdownItem from "react-bootstrap/DropdownItem"; +import Footer from "../layout/footer"; +import { Button } from "react-bootstrap"; +import { conditionalExpression } from "@babel/types"; + +var mongoose = require("mongoose"); + +class NewDependency extends Component { + constructor(props) { + super(props); + this.state = { + atts: false, + forms: [], + formType: "", + att: "", + dependAtt: "", + is: "", + min: "", + max: "", + valid: "", + length: "", + payload: {} + }; + } + + changeHandler = event => { + this.setState({ [event.target.name]: event.target.value }); + }; + + componentDidMount = () => { + axios + .get("/routes/api/formTypes/", { + headers: { Authorization: localStorage.getItem("jwtToken") } + }) + .then(res => { + //console.log(res.data.data); + this.setState({ forms: res.data.data }); + }) + .catch(err => { + alert(err); + }); + }; + + handleClick1 = error => { + error.preventDefault(); + if ( + this.state.formType !== "" && + this.state.formType !== "Please Provide a Form Type" + ) { + var payload = this.state.payload; + payload.formType = this.state.formType; + this.setState({ atts: true, payload: payload }); + } else alert("Please provide a Form Type"); + }; + + handleClick2 = error => { + error.preventDefault(); + if ( + this.state.att === "" || + this.state.att === "Please Provide an Attribute" + ) { + alert("Please Provide an Attribute"); + return; + } + if ( + this.state.dependAtt === "" || + this.state.dependAtt === "Please Provide an Attribute to depend on" + ) { + alert("Please provide a depending Attribute"); + return; + } + var formType = this.state.formType; + // console.log(formType) + var forms = this.state.forms; + var form; + for (let i = 0; i < forms.length; i++) { + if (forms[i].formType === formType) form = forms[i]; + } + + var att = this.state.att + var consts = form[att] + consts = consts.split(",") + var attType = consts[0] + + var dependAtt = this.state.dependAtt + var consts = form[dependAtt] + consts = consts.split(",") + var dependAttType = consts[0] + + if(this.state.is===""){ + alert("Please provide a value for depending Attribute") + return + } + if(this.state.is!=="" && dependAttType==='number'){ + if(!parseInt(this.state.is)) { + alert("Please provida a number in the value of depending attribute") + return + } + } + if (this.state.min !== "") + if (!parseInt(this.state.min)) { + alert("Please provide a number in the minimum field"); + return; + } + if (this.state.max !== "") + if (!parseInt(this.state.max)) { + alert("Please provide a number in the maximum field"); + return; + } + + if(this.state.value!=="" && attType==='number'){ + if(!parseInt(this.state.value)) { + alert("Please provida a number in the exact value field") + return + } + } + if (this.state.length !== "") + if (!parseInt(this.state.length)) { + alert("Please provide a number in the length field"); + return; + } + + var constraints = ""; + + constraints = constraints + this.state.dependAtt + ","; + constraints = constraints + this.state.is + ","; + constraints = constraints + this.state.min + ","; + constraints = constraints + this.state.max + ","; + constraints = constraints + this.state.value + ","; + constraints = constraints + this.state.length + ","; + + + var payload = this.state.payload; + payload[this.state.att] = constraints; + this.setState({ payload: payload }); + alert("Attribute Added Successfully"); + }; + + handleClick3 = () => { + this.setState({ + att: "", + dependAtt: "", + is: "", + min: "", + max: "", + valid: "", + length: "", + value:"" + }); + }; + + handleClick4 = () => { + var payload = this.state.payload; + axios + .post("/routes/api/dependencies/NewDependencies", payload, { + headers: { Authorization: localStorage.getItem("jwtToken") } + }) + .then(res => { + alert("Form Type Array created successfully"); + document.location.href = "/createNewDependency"; + }) + .catch(err => { + console.log(err.response.data); + alert(err.response.data.error); + }); + }; + + getAtts = () => { + var formType = this.state.formType; + // console.log(formType) + var forms = this.state.forms; + var form; + for (let i = 0; i < forms.length; i++) { + if (forms[i].formType === formType) form = forms[i]; + } + var props = []; + for (var prop in form) { + console.log(prop); + var constraints = form[prop]; + constraints = constraints.split(","); + if ( + prop !== "formTypeArray" && + prop !== "_id" && + (constraints[0] === "string" || constraints[0] === "number") + ) + props.push(prop); + } + return props.map(att => { + return ; + }); + }; + + getFormType = () => { + var forms = this.state.forms; + return forms.map(Form => { + if (Form.formType) return ; + }); + }; + + getDependatts = () => { + if ( + this.state.att === "" || + this.state.att === "Please Provide an Attribute" + ) + return; + var att = this.state.att; + var formType = this.state.formType; + // console.log(formType) + var forms = this.state.forms; + var form; + for (let i = 0; i < forms.length; i++) { + if (forms[i].formType === formType) form = forms[i]; + } + var props = []; + for (var prop in form) { + console.log(prop); + var constraints = form[prop]; + constraints = constraints.split(","); + if (prop === att) break; + if ( + prop !== "formTypeArray" && + prop !== "_id" && + (constraints[0] === "string" || constraints[0] === "number") + ) + props.push(prop); + } + return props.map(dependatt => { + return ; + }); + }; + + getDeps = () => { + var payload = this.state.payload; + var props = []; + for (var prop in payload) { + if(prop!=="formType") + props.push(prop); + } + return props.map(att => { + return
{att}
; + }); + }; + + render = () => { + var start = ( +
+
+
+
+
+ + +
+ + +
+
+ +
+
+ ); + var min = ( +
+ + + + + +
+ ); + + var max = ( +
+ + + + + +
+ ); + + var attributes = ( +
+
+
+
+
+

{this.state.formType}

+

Avaliable Dependencies till now

+ {this.getDeps()} + + + +
+ + +
+
+ + +
+ + +
+
+ + + + + + + + {min} + {max} + + + + + + + + + + + + + + + + +
+
+ ); + return this.state.atts ? attributes : start; + }; +} + +export default NewDependency; diff --git a/client/src/components/pages/NewFormType.js b/client/src/components/pages/NewFormType.js new file mode 100644 index 0000000..a147fc6 --- /dev/null +++ b/client/src/components/pages/NewFormType.js @@ -0,0 +1,360 @@ +import "bootstrap/dist/css/bootstrap.css"; +import React, { Component } from "react"; +import ReactDOM from "react-dom"; +import axios from "axios"; +import MuiThemeProvider from "material-ui/styles/MuiThemeProvider"; +import AppBar from "material-ui/AppBar"; +import RaisedButton from "material-ui/RaisedButton"; +import TextField from "material-ui/TextField"; +import { + MDBRow, + MDBCol, + MDBInput, + MDBBtn, + MDBDropdown, + MDBDropdownToggle, + MDBDropdownItem, + MDBDropdownMenu +} from "mdbreact"; +import DropDownMenu from "material-ui/DropDownMenu"; +import MenuItem from "material-ui/MenuItem"; +import trans from "../translations/spcTranslation"; +//import {Dropdown} from 'react-bootstrap'; +import { Dropdown } from "semantic-ui-react"; +import DropdownItem from "react-bootstrap/DropdownItem"; +import Footer from "../layout/footer"; +import { Button } from "react-bootstrap"; +import { conditionalExpression } from "@babel/types"; + +var mongoose = require("mongoose"); + +class NewFormType extends Component { + constructor(props) { + super(props); + this.state = { + atts: false, + attributeName: "", + formType: "", + type: "", + required: "not required", + unique: "not unique", + min: "", + max: "", + frontend: "", + array: "", + payload: {} + }; + } + + changeHandler = event => { + this.setState({ [event.target.name]: event.target.value }); + }; + + handleClick1 = error => { + error.preventDefault(); + if (this.state.formType !== "") { + var payload = this.state.payload; + payload.formType = this.state.formType; + this.setState({ atts: true, payload: payload }); + } else alert("Please provide a Form Type Name"); + }; + + handleClick2 = error => { + error.preventDefault(); + if (this.state.attributeName === "") + alert("Please Provide an Attribute Name"); + if ( + this.state.type === "" || + this.state.type === "Please Provide an Input Type" + ) { + alert("Please provide a valid input type"); + return; + } + + var constraints = ""; + + constraints = constraints + this.state.type + ","; + constraints = constraints + this.state.required + ","; + + if (this.state.type === "array") { + constraints = constraints + this.state.array + ",,,,"; + } else { + if (this.state.type === "string" || this.state.type === "number") { + if (this.state.min !== "") + if (!parseInt(this.state.min)) { + alert("Please provide a number in the minimum field"); + return; + } + constraints = constraints + this.state.min + ","; + if (this.state.max !== "") + if (!parseInt(this.state.max)) { + alert("Please provide a number in the maximum field"); + return; + } + constraints = constraints + this.state.min + ","; + } else constraints = constraints + ",,"; + constraints = constraints + this.state.unique + ","; + if (this.state.type === "date") constraints = constraints + "datepicker"; + else constraints = constraints + this.state.frontend; + } + var payload = this.state.payload; + payload[this.state.attributeName] = constraints; + this.setState({ payload: payload }); + alert("Attribute Added Successfully"); + }; + + handleClick3 = () => { + this.setState({ + attributeName: "", + formType: "", + type: "", + required: "not required", + unique: "not unique", + min: "", + max: "", + frontend: "", + array: "" + }); + }; + + handleClick4 = () => { + var payload = this.state.payload; + axios + .post("/routes/api/formTypes/NewFormType", payload, { + headers: { Authorization: localStorage.getItem("jwtToken") } + }) + .then(res => { + alert("Form Type created successfully"); + document.location.href = "/createNewFormType"; + }) + .catch(err => { + alert(err); + }); + }; + + getAtts = () => { + var payload = this.state.payload; + var props = []; + for (var prop in payload) { + if (prop !== "formType") props.push(prop); + } + return props.map(att => { + return
{att}
; + }); + }; + + render = () => { + var start = ( +
+
+
+
+
+ +
+ + + + + + +
+
+
+ ); + var array = ( + + + + + + ); + var min = ( +
+ + + + + +
+ ); + + var max = ( +
+ + + + + +
+ ); + var notdate = ( + +
+ + +
+
+ ); + + var attributes = ( +
+
+
+
+
+

{this.state.formType}

+

Avaliable Attributes till now

+ {this.getAtts()} + + + + + + + + +
+ + +
+
+
+ If you choose an array Please provide an Array Name below and go to + new Form Type Array Creation +
+ + +
+ + +
+
+ + {this.state.type === "string" ? min : null} + {this.state.type === "string" ? max : null} + {this.state.type === "number" ? min : null} + {this.state.type === "number" ? max : null} + + +
+ + +
+
+ + {this.state.type === "date" ? null : notdate} + {this.state.type === "array" ? array : null} + + + +
+
+ ); + return this.state.atts ? attributes : start; + }; +} + +export default NewFormType; diff --git a/client/src/components/pages/NewFormTypeArray.js b/client/src/components/pages/NewFormTypeArray.js new file mode 100644 index 0000000..39a985b --- /dev/null +++ b/client/src/components/pages/NewFormTypeArray.js @@ -0,0 +1,335 @@ +import "bootstrap/dist/css/bootstrap.css"; +import React, { Component } from "react"; +import ReactDOM from "react-dom"; +import axios from "axios"; +import MuiThemeProvider from "material-ui/styles/MuiThemeProvider"; +import AppBar from "material-ui/AppBar"; +import RaisedButton from "material-ui/RaisedButton"; +import TextField from "material-ui/TextField"; +import { + MDBRow, + MDBCol, + MDBInput, + MDBBtn, + MDBDropdown, + MDBDropdownToggle, + MDBDropdownItem, + MDBDropdownMenu +} from "mdbreact"; +import DropDownMenu from "material-ui/DropDownMenu"; +import MenuItem from "material-ui/MenuItem"; +import trans from "../translations/spcTranslation"; +//import {Dropdown} from 'react-bootstrap'; +import { Dropdown } from "semantic-ui-react"; +import DropdownItem from "react-bootstrap/DropdownItem"; +import Footer from "../layout/footer"; +import { Button } from "react-bootstrap"; +import { conditionalExpression } from "@babel/types"; + +var mongoose = require("mongoose"); + +class NewFormTypeArray extends Component { + constructor(props) { + super(props); + this.state = { + atts: false, + attributeName: "", + formTypeArray: "", + type: "", + required: "not required", + unique: "not unique", + min: "", + max: "", + frontend: "", + payload: {} + }; + } + + changeHandler = event => { + this.setState({ [event.target.name]: event.target.value }); + }; + + handleClick1 = error => { + error.preventDefault(); + if (this.state.formType !== "") { + var payload = this.state.payload; + payload.formTypeArray = this.state.formTypeArray; + this.setState({ atts: true, payload: payload }); + } else alert("Please provide a Form Type Name"); + }; + + handleClick2 = error => { + error.preventDefault(); + if (this.state.attributeName === "") + alert("Please Provide an Attribute Name"); + if ( + this.state.type === "" || + this.state.type === "Please Provide an Input Type" + ) { + alert("Please provide a valid input type"); + return; + } + + var constraints = ""; + + constraints = constraints + this.state.type + ","; + constraints = constraints + this.state.required + ","; + + if (this.state.type === "string" || this.state.type === "number") { + if (this.state.min !== "") + if (!parseInt(this.state.min)) { + alert("Please provide a number in the minimum field"); + return; + } + constraints = constraints + this.state.min + ","; + if (this.state.max !== "") + if (!parseInt(this.state.max)) { + alert("Please provide a number in the maximum field"); + return; + } + constraints = constraints + this.state.min + ","; + } else constraints = constraints + ",,"; + constraints = constraints + this.state.unique + ","; + if (this.state.type === "date") constraints = constraints + "datepicker"; + else constraints = constraints + this.state.frontend; + var payload = this.state.payload; + payload[this.state.attributeName] = constraints; + this.setState({ payload: payload }); + alert("Attribute Added Successfully"); + }; + + handleClick3 = () => { + this.setState({ + attributeName: "", + formTypeArray: "", + type: "", + required: "not required", + unique: "not unique", + min: "", + max: "", + frontend: "" + }); + }; + + handleClick4 = () => { + var payload = this.state.payload; + axios + .post("/routes/api/formTypes/NewFormTypeArray", payload, { + headers: { Authorization: localStorage.getItem("jwtToken") } + }) + .then(res => { + alert("Form Type Array created successfully"); + document.location.href = "/createNewFormTypeArray"; + }) + .catch(err => { + console.log(err.response.data) + alert(err.response.data.error); + }); + }; + + getAtts = () => { + var payload = this.state.payload; + var props = []; + for (var prop in payload) { + if(prop!=="formTypeArray") + props.push(prop); + } + return props.map(att => { + return
{att}
; + }); + }; + + render = () => { + var start = ( +
+
+
+
+
+ +
+ + + + + + +
+
+
+ ); + + var min = ( +
+ + + + + +
+ ); + + var max = ( +
+ + + + + +
+ ); + var notdate = ( + +
+ + +
+
+ ); + + var attributes = ( +
+
+
+
+
+

{this.state.formType}

+

Avaliable Attributes till now

+ {this.getAtts()} + + + + + + + +
+ + +
+
+ + +
+ + +
+
+ + {this.state.type === "string" ? min : null} + {this.state.type === "string" ? max : null} + {this.state.type === "number" ? min : null} + {this.state.type === "number" ? max : null} + + +
+ + +
+
+ + {this.state.type === "date" ? null : notdate} + + + +
+
+ ); + return this.state.atts ? attributes : start; + }; +} + +export default NewFormTypeArray; diff --git a/controllers/dependenciesController.js b/controllers/dependenciesController.js new file mode 100644 index 0000000..c47c9fc --- /dev/null +++ b/controllers/dependenciesController.js @@ -0,0 +1,110 @@ +const mongoose = require("mongoose"); +const Dependencies = require("../models/Dependencies"); + +exports.search = async (att, value) => { + if (!att) { + return await Dependencies.find() + .then(res => { + return res; + }) + .catch(err => { + return { error: err }; + }); + } + if (att === "_id") { + return await Dependencies.findById(value) + .then(res => { + return res; + }) + .catch(err => { + return { error: err }; + }); + } + if (att == "formType") { + return await Dependencies.find({ formType: value }) + .then(res => { + return res; + }) + .catch(err => { + return { error: err }; + }); + } + +}; + +exports.create = async body => { + return await Dependencies.create(body) + .then(res => { + return res; + }) + .catch(err => { + return { error: err }; + }); +}; + +exports.update = async (att, value, body) => { + if (!att) { + return { error: "Can't update form, provide a value to update with" }; + } + if (att === "_id") { + var dependencies = await Dependencies.findByIdAndUpdate(value, body).catch(err => { + return { error: err }; + }); + if (dependencies.error) return dependencies; + return await Dependencies.findById(value) + .then(res => { + return res; + }) + .catch(err => { + return { error: err }; + }); + } + if( att === "formType"){ + var dependencies = await Dependencies.updateMany({formType:value}, body).catch(err => { + return { error: err }; + }); + if (dependencies.error) return dependencies; + return await Dependencies.find({formType:value}) + .then(res => { + return res; + }) + .catch(err => { + return { error: err }; + }); + } + var dependencies = await Dependencies.updateMany({[att]:value}, body).catch(err => { + return { error: err }; + }); + if (dependencies.error) return dependencies; + return await Dependencies.find({[att]:value}) + .then(res => { + return res; + }) + .catch(err => { + return { error: err }; + }); +}; + +exports.remove = async (att,value)=>{ + if (!att) { + return {error: "Can't be deleted , Please provide an attribute to delete with"} + } + if (att === "_id") { + return await Dependencies.findByIdAndDelete(value) + .then(res => { + return res; + }) + .catch(err => { + return { error: err }; + }); + } + if (att == "formType") { + return await Dependencies.deleteMany({ formType: value }) + .then(res => { + return res; + }) + .catch(err => { + return { error: err }; + }); + } +} diff --git a/controllers/formTypeController.js b/controllers/formTypeController.js index 74c0786..cea56d7 100644 --- a/controllers/formTypeController.js +++ b/controllers/formTypeController.js @@ -80,6 +80,17 @@ exports.update = async (att, value, body) => { return { error: err }; }); } + var formType = await FormType.updateMany({[att]:value}, body).catch(err => { + return { error: err }; + }); + if (formType.error) return formType; + return await FormType.find({[att]:value}) + .then(res => { + return res; + }) + .catch(err => { + return { error: err }; + }); }; exports.remove = async (att,value)=>{ diff --git a/package.json b/package.json index c4803b0..8ebe427 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,6 @@ "mongoose": "^5.4.22", "mongoose-type-url": "^1.0.5", "nodemailer": "^6.1.1", - "nodemon": "^1.18.10", "passport": "^0.4.0", "passport-jwt": "^4.0.0", "passport-local": "^1.0.0", @@ -38,7 +37,7 @@ "devDependencies": { "jest": "^23.6.0", "jest-cli": "^24.7.1", - "nodemon": "^1.18.10" + "nodemon": "^1.19.0" }, "scripts": { "test": "jest --detectOpenHandles", diff --git a/routes/api/dependencies.js b/routes/api/dependencies.js new file mode 100644 index 0000000..25c2e3a --- /dev/null +++ b/routes/api/dependencies.js @@ -0,0 +1,59 @@ +const express = require("express"); +const router = express.Router(); +const dependenciesController = require("../../controllers/dependenciesController"); +const formTypeController = require("../../controllers/formTypeController"); +const bcrypt = require("bcrypt"); +const jwt = require("jsonwebtoken"); +const tokenKey = require("../../config/keys_dev").secretOrKey; +const passport = require("passport"); +require("../../config/passport")(passport); + +router.get( + "/", + passport.authenticate("jwt", { session: false }), + async (req, res) => { + const dependencies = await dependenciesController.search(); + return res.json({ data: dependencies }); + } +); + +router.get("/test", async (req, res) => { + return await dependenciesController.search(); +}); + +router.post( + "/NewDependencies", + passport.authenticate("jwt", { session: false }), + async (req, res) => { + if (req.user.userType === "Admin") { + if (req.body.formType === undefined) + return res.status(400).json({ error: "Can't create Dependency" }); + const avaialableDependency = await dependenciesController.search(); + if (avaialableDependency.error) + return res.status(400).json({ error: avaialableDependency.error }); + if (avaialableDependency === []) { + const dependency = await dependenciesController.create(req.body); + if (dependency.error) + return res.status(400).json({ error: dependency.error }); + return res.json({ + msg: "Dependency created successfully", + data: dependency + }); + } else { + const dependency = await dependenciesController.update( + "_id", + avaialableDependency[0]._id, + req.body + ); + if (dependency.error) + return res.status(400).json({ error: dependency.error }); + return res.json({ + msg: "Dependency updated successfully", + data: dependency + }); + } + } else return res.status(401).json({ error: "Not Authorized" }); + } +); + +module.exports = router; diff --git a/routes/api/formTypes.js b/routes/api/formTypes.js index 3527dc3..127f89f 100644 --- a/routes/api/formTypes.js +++ b/routes/api/formTypes.js @@ -7,45 +7,113 @@ const tokenKey = require("../../config/keys_dev").secretOrKey; const passport = require("passport"); require("../../config/passport")(passport); -router.get("/" , passport.authenticate('jwt', { session: false }), async (req, res) => { +router.get( + "/", + passport.authenticate("jwt", { session: false }), + async (req, res) => { const FormTypes = await formTypeController.search(); - return res.json({ data: FormTypes}); - }); - - - router.get('/getAllFormTypes', passport.authenticate('jwt', { session: false }), async (req, res) => { - const forms = await formTypeController.search() - console.log(forms) - const types=[]; - for (i=0;i { + const forms = await formTypeController.search(); + console.log(forms); + const types = []; + for (i = 0; i < forms.length; i++) { + if (forms[i].toJSON().formType) { + types.push(forms[i].toJSON().formType); + } } - return res.json({ data:types}); -}) -router.get('/getAllFormTypeArrays', passport.authenticate('jwt', { session: false }), async (req, res) => { - const forms = await formTypeController.search() - //console.log(forms) - const types=[]; - for (i=0;i { + const forms = await formTypeController.search(); + //console.log(forms) + const types = []; + for (i = 0; i < forms.length; i++) { + if (forms[i].toJSON().formTypeArray) { //console.log(forms[i]) - types.push(forms[i].toJSON().formTypeArray) + types.push(forms[i].toJSON().formTypeArray); } + } + return res.json({ data: types }); + } +); + +router.get( + "/getByFormType/:formType", + passport.authenticate("jwt", { session: false }), + async (req, res) => { + const formType2 = req.params.formType; + const certainForm = await formTypeController.search("formType", formType2); + return res.json({ data: certainForm }); } - return res.json({ data:types}); -}) - -router.get('/getByFormType/:formType', passport.authenticate('jwt', { session: false }), async (req, res) => { - const formType2 = req.params.formType - const certainForm = await formTypeController.search('formType',formType2) - return res.json({ data:certainForm}); -}) -router.get('/getByFormTypeArray/:formTypeArray', passport.authenticate('jwt', { session: false }), async (req, res) => { - const formType2 = req.params.formTypeArray - const certainForm = await formTypeController.search('formTypeArray',formType2).then(res=>{return res}).catch(err=> {return err}) - console.log(certainForm) - return res.json({ data:certainForm}); -}) +); + +router.get( + "/getByFormTypeArray/:formTypeArray", + passport.authenticate("jwt", { session: false }), + async (req, res) => { + const formType2 = req.params.formTypeArray; + const certainForm = await formTypeController + .search("formTypeArray", formType2) + .then(res => { + return res; + }) + .catch(err => { + return err; + }); + console.log(certainForm); + return res.json({ data: certainForm }); + } +); + +router.get("/test", async (req, res) => { + return await formTypeController.search(); +}); + +router.post( + "/NewFormType", + passport.authenticate("jwt", { session: false }), + async (req, res) => { + if (req.user.userType === "Admin") { + if (req.body.formType === undefined) + return res.status(400).json({ error: "Can't create FormType" }); + const formType = await formTypeController.create(req.body); + if (formType.error) + return res.status(400).json({ error: formType.error }); + return res.json({ + msg: "Form Type created successfully", + data: formType + }); + } else return res.status(401).json({ error: "Not Authorized" }); + } +); + +router.post( + "/NewFormTypeArray", + passport.authenticate("jwt", { session: false }), + async (req, res) => { + if (req.user.userType === "Admin") { + if (req.body.formTypeArray === undefined) + return res.status(400).json({ error: "Can't create Form Type Array" }); + const formType = await formTypeController.create(req.body); + if (formType.error) + return res.status(400).json({ error: formType.error }); + return res.json({ + msg: "Form Type Array created successfully", + data: formType + }); + } else return res.status(401).json({ error: "Not Authorized" }); + } +); + module.exports = router; diff --git a/server.js b/server.js index 2bdc601..5be014d 100644 --- a/server.js +++ b/server.js @@ -11,6 +11,7 @@ const governorates = require('./routes/api/governorates'); const dynamicForms = require('./routes/api/dynamicForms'); const userDynamicForms = require('./routes/api/userDynamicForms'); const formTypes = require('./routes/api/formTypes'); +const dependencies = require('./routes/api/dependencies') const app = express(); //const morgan = require('morgan'); @@ -40,7 +41,7 @@ app.use('/routes/api/admins', admins); app.use('/routes/api/externalentities', externalentities); app.use('/routes/api/nationalities', nationalities); app.use('/routes/api/governorates', governorates); - +app.use('/routes/api/dependencies', dependencies); app.use('/routes/api/dynamicForms', dynamicForms); app.use('/routes/api/fakeServer', fakeServer); app.use('/routes/api/userDynamicForms', userDynamicForms); diff --git a/validations/dynamicFormValidations.js b/validations/dynamicFormValidations.js index d289070..30be95c 100644 --- a/validations/dynamicFormValidations.js +++ b/validations/dynamicFormValidations.js @@ -629,6 +629,9 @@ module.exports = { updateSchema[prop] = Joi.string(); if (depends) { + let dependConstraints = validations[depends[0]] + let dependConstraintArray = dependConstraints.split(",") + if(dependConstraintArray[0]==='number') depends[1]=parseInt(depends[1]) if (depends[5] !== "") updateSchema[prop] = Joi.when(depends[0], { is: depends[1], @@ -698,6 +701,9 @@ module.exports = { if (depends) { // //console.log(depends[4]) // //console.log(depends[1]) + let dependConstraints = validations[depends[0]] + let dependConstraintArray = dependConstraints.split(",") + if(dependConstraintArray[0]==='number') depends[1]=parseInt(depends[1]) if (depends[5] !== "") updateSchema[prop] = Joi.when(depends[0], { is: depends[1],