Skip to content

Commit

Permalink
Merge pull request #32 from michacurri/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
michacurri authored Feb 17, 2021
2 parents 7030266 + 16af50b commit e37c413
Show file tree
Hide file tree
Showing 17 changed files with 586 additions and 202 deletions.
25 changes: 22 additions & 3 deletions api/controllers/profileController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Profile = require("../models/profileModel");

module.exports = {
createProfile: async ({
admin,
firstName,
lastName,
email,
Expand All @@ -14,6 +15,7 @@ module.exports = {
workorders,
}) => {
const newProfile = new Profile({
admin,
firstName,
lastName,
phone,
Expand All @@ -36,12 +38,11 @@ module.exports = {
},
findProfileById: async (id) => {
try {
const profile = await Profile.findById(id)
.populate("workorders")
.exec();
const profile = await Profile.findById(id).populate("workorders").exec();
//* return everything but password
return {
id: profile._id,
admin: profile.admin,
firstName: profile.firstName,
lastName: profile.lastName,
email: profile.email,
Expand All @@ -51,4 +52,22 @@ module.exports = {
throw err;
}
},
findProfileByAny: async (inputValue) => {
try {
const profileRes = await Profile.find({
$or: [
{ firstName: inputValue },
{ lastName: inputValue },
{ email: inputValue },
// TODO - mongoose.js CastError: Cast to number failed for value
// { phone: inputValue },
],
})
.populate("workorders")
.exec();
return profileRes;
} catch (err) {
throw err;
}
},
};
23 changes: 18 additions & 5 deletions api/models/profileModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const { Schema } = mongoose;

const profileSchema = new Schema(
{
admin: {
type: Boolean,
required: true,
default: false,
},
firstName: {
type: String,
required: true,
Expand All @@ -20,20 +25,28 @@ const profileSchema = new Schema(
phone: {
type: Number,
required: true,
minlength: 10,
maxlength: 11,
match: /^\D?(\d{3})\D?\D?(\d{3})\D?(\d{4})$/,
},
email: {
type: String,
required: true,
required: true,
unique: true,
match: /^.+@.+$/,
},
password: {
type: String,
required: true,
minlength: 8,
match: /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])(?=.{8,})/,
},
workorders: [{
type: Schema.Types.ObjectId,
ref: 'Workorder'
}]
workorders: [
{
type: Schema.Types.ObjectId,
ref: "Workorder",
},
],
},
{
timestamps: { createdAt: "created_at", updatedAt: "updated_at" },
Expand Down
28 changes: 24 additions & 4 deletions api/routes/profileRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ const {
createProfile,
findProfileByEmail,
findProfileById,
findProfileByAny,
} = require("../controllers/profileController");
const { verifyToken } = require("../middleware/verifyToken");
const { createToken } = require("../tokens/tokenService");

const router = express.Router();
// router.get("/search/email/:email", findProfileByEmail);

router.route("/create").post(async (req, res) => {
const { firstName, lastName, email, phone, password } = req.body;
Expand Down Expand Up @@ -54,7 +54,6 @@ router.route("/create").post(async (req, res) => {
});

router.route("/login").post(async (req, res) => {
console.log("route: login");
const { email, password } = req.body;
if (!email || email === " ") {
res.status(400).json({ message: "email must be provided" });
Expand Down Expand Up @@ -87,13 +86,34 @@ router.route("/login").post(async (req, res) => {
}
});

// where workorder route used to be
router
.route("/search/:searchValue")
.get(async (req, res) => {
const { searchValue } = req.params;
if (!searchValue || searchValue === "") {
res.status(400).json({ message: "nothing to search" });
return;
} else {
try {
const match = await findProfileByAny(searchValue);
if (!match) {
res
.status(400)
.json({ message: `Search for "${searchValue}" returned no results` });
return;
}
res.status(200).json(match);
} catch (err) {
console.log(err);
res.status(500).json({ message: "Internal server error" });
}
}
});

router
.use(verifyToken)
.route("/this-profile")
.get(async (req, res) => {
// console.log(`/this-profile: ${req.profile.id}`);
try {
const profile = await findProfileById(req.profile.id);
res.json({ data: profile });
Expand Down
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
"object-path": "^0.11.5",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-input-mask": "^2.0.4",
"react-router-dom": "^5.2.0",
"react-scripts": "^4.0.1",
"react-virtualized": "^9.22.3"
},
"proxy": "http://localhost:5000",
"scripts": {
"start:client": "react-scripts start",
"start:server": "nodemon server.js --ignore src",
Expand Down
30 changes: 16 additions & 14 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const express = require("express");
const app = express();
const path = require("path");
require("dotenv/config");
const cors = require("cors");
Expand All @@ -9,12 +8,15 @@ const mongoose = require("mongoose");
const DB_URI = process.env.DB_URI || "mongodb://localhost:27017/aero";
const PORT = process.env.PORT || 5000;

app.use(cookieParser());
app.use(express.json());
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cookieParser());
app.use(
cors({
origin: "http://localhost:3000/" || "https://aero-workorder-management.herokuapp.com",
origin:
"http://localhost:3000/" ||
"https://aero-workorder-management.herokuapp.com",
credentials: true,
exposedHeaders: ["Set-Cookie"],
allowedHeaders: ["Content-Type", "Authorization"],
Expand All @@ -27,30 +29,30 @@ const workorder = require("./api/routes/workorderRoutes");
app.use("/api/profile", profile);
app.use("/api/workorder", workorder);

if (process.env.NODE_ENV === 'production') {
app.use(express.static('./build'));
if (process.env.NODE_ENV === "production") {
app.use(express.static("./build"));
// only add this part if you are using React Router
app.get('/*', (req,res) =>{
console.log(path.join(__dirname+'/build/index.html'));
res.sendFile(path.join(__dirname+'/build/index.html'));
app.get("/*", (req, res) => {
console.log(path.join(__dirname + "/build/index.html"));
res.sendFile(path.join(__dirname + "/build/index.html"));
});
}

app.use(express.static("/"));

mongoose
.connect(DB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
})
.then(() => {
app.listen(PORT, function () {
console.log(`Server is now listening on port ${PORT}!`);
});
console.log(`Successfully connected to database server ${DB_URI}`);
})
.catch((err) => {
console.log({ error: err });
});

app.use(express.static("/"));

app.listen(PORT, function () {
console.log(`Server is now listening on port ${PORT}!`);
});
11 changes: 6 additions & 5 deletions src/backend/authorization/AuthContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const useStyles = makeStyles((theme) => ({
grid: {
maxWidth: "calc(50% - 16px)",
flexGrow: "50%",
flexBasis: "200px"
}
flexBasis: "200px",
},
}));

const AuthContainer = ({ loadUserProfile }) => {
Expand Down Expand Up @@ -76,6 +76,7 @@ const AuthContainer = ({ loadUserProfile }) => {
loadUserProfile();
} catch (err) {
setError(err.message);
console.log(error);
}
};

Expand All @@ -95,6 +96,8 @@ const AuthContainer = ({ loadUserProfile }) => {
<Grid item className={classes.grid}>
<Paper className={classes.paperText}>
<TextField
autoFocus
type="email"
id="email"
label="Email Address"
name="email"
Expand All @@ -108,6 +111,7 @@ const AuthContainer = ({ loadUserProfile }) => {
<Grid className={classes.grid}>
<Paper className={classes.paperText}>
<TextField
type="password"
name="password"
label="Password"
id="password"
Expand All @@ -130,9 +134,6 @@ const AuthContainer = ({ loadUserProfile }) => {
</Switch>
{/* prettier-ignore */}
<nav>
{/* <ul>
<li><Link to={`${link}`} onClick={changeLink}>{`${link}`}</Link></li>
</ul> */}
<Button component={Link} to={`${link}`} onClick={changeLink} variant="outlined" color="primary" >{`${link}`}</Button>
</nav>
</Fragment>
Expand Down
Empty file.
28 changes: 22 additions & 6 deletions src/components/root/Header.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { Fragment, useContext } from "react";
import { UserContext } from "../../backend/authorization/UserContext";
import { makeStyles } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button'
import { makeStyles } from "@material-ui/core/styles";
import Button from "@material-ui/core/Button";
// import theme from '../../styles/theme'

const useStyles = makeStyles((theme) => ({
root: {
'& > *': {
"& > *": {
margin: theme.spacing(1),
},
},
Expand Down Expand Up @@ -35,17 +35,33 @@ function Header({ loginClick, setLoginClick }) {
<Fragment>
<h2>AERO</h2>
{loginClick ? (
<Button variant="outlined" color="secondary" onClick={toggleLoginClick}>Close</Button>
<Button
className={classes.root}
variant="outlined"
color="secondary"
onClick={toggleLoginClick}
>
Close
</Button>
) : (
<Button variant="outlined" color="secondary" onClick={toggleLoginClick}>Login / Signup</Button>
<Button
className={classes.root}
variant="outlined"
color="secondary"
onClick={toggleLoginClick}
>
Login / Signup
</Button>
)}
</Fragment>
);
} else {
content = (
<Fragment>
<h2>AERO</h2>
<Button variant="outlined" color="secondary" onClick={logout}>Logout</Button>
<Button variant="outlined" color="secondary" onClick={logout}>
Logout
</Button>
</Fragment>
);
}
Expand Down
Loading

0 comments on commit e37c413

Please sign in to comment.