Compare commits
	
		
			10 Commits
		
	
	
		
			carousel
			...
			match-sear
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| a7885ecf53 | |||
| 6cedd74473 | |||
| d17fe1d912 | |||
| c1589b9758 | |||
| f8abf7cd48 | |||
| 8464c4debc | |||
| dd6dc787e9 | |||
| 0f480af1f0 | |||
| 7dd862e134 | |||
| 
						 | 
					489387ec9f | 
@@ -16,7 +16,7 @@
 | 
			
		||||
    "web-vitals": "^2.1.4"
 | 
			
		||||
  },
 | 
			
		||||
  "scripts": {
 | 
			
		||||
    "start": "NODE_ENV=development API_HOST=http://localhost:5000 react-scripts start",
 | 
			
		||||
    "start": "NODE_ENV='development' REACT_APP_API_HOST='http://localhost:5000' react-scripts start",
 | 
			
		||||
    "build": "../scripts/build.py",
 | 
			
		||||
    "test": "react-scripts test",
 | 
			
		||||
    "eject": "react-scripts eject"
 | 
			
		||||
 
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 529 KiB  | 
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 496 KiB  | 
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 396 KiB  | 
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 894 KiB  | 
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 592 KiB  | 
@@ -1,21 +0,0 @@
 | 
			
		||||
import React from "react";
 | 
			
		||||
import propTypes from "prop-types";
 | 
			
		||||
import GameInfoCard from "./GameInfoCard";
 | 
			
		||||
 | 
			
		||||
export default class GameInfoCardDisplay extends React.Component {
 | 
			
		||||
    constructor(props) {
 | 
			
		||||
        super(props);
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
    render() {
 | 
			
		||||
        return (
 | 
			
		||||
            <div className="horizontal-scroller">
 | 
			
		||||
                {this.props.recommendedMatches.map((match) => <GameInfoCard key={match.id} match={match}></GameInfoCard>)}
 | 
			
		||||
            </div>
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
GameInfoCardDisplay.propTypes = {
 | 
			
		||||
    recommendedMatches: propTypes.array,
 | 
			
		||||
};
 | 
			
		||||
@@ -1,18 +1,17 @@
 | 
			
		||||
import React from "react";
 | 
			
		||||
import { Carousel } from "react-bootstrap";
 | 
			
		||||
import "../styles/HomeCarousel.css";
 | 
			
		||||
export default class HomeCarousel extends React.Component{
 | 
			
		||||
export default class HomeCarousel extends React.Component {
 | 
			
		||||
    constructor(props) {
 | 
			
		||||
        super(props);
 | 
			
		||||
    }
 | 
			
		||||
    render() {
 | 
			
		||||
        return (
 | 
			
		||||
            <Carousel>
 | 
			
		||||
            <Carousel className="jumbotron" variant="light">
 | 
			
		||||
                <Carousel.Item>
 | 
			
		||||
                    <img
 | 
			
		||||
                        src='https://www.allanpanthera.com/wp-content/uploads/elementor/thumbs/79377445_m-o6r0ydib97moj7m7zg58w32qirim121wxt2i8thqyg.jpg'
 | 
			
		||||
                        className="d-block w-100"
 | 
			
		||||
                        src='/images/carousel/volleyball_normalized.jpg'
 | 
			
		||||
                        alt="Connect Slide"
 | 
			
		||||
                        style={{ height: "300px", width: "2000px"}}
 | 
			
		||||
                    />
 | 
			
		||||
                    <Carousel.Caption>
 | 
			
		||||
                        <div className="captionStyle">
 | 
			
		||||
@@ -23,9 +22,9 @@ export default class HomeCarousel extends React.Component{
 | 
			
		||||
                </Carousel.Item>
 | 
			
		||||
                <Carousel.Item>
 | 
			
		||||
                    <img
 | 
			
		||||
                        src='http://cpadollard.com/wp-content/uploads/2018/01/cpa-dollard-fsc-banner-calendar_2000x300.jpg'
 | 
			
		||||
                        className="d-block w-100"
 | 
			
		||||
                        src='/images/carousel/schedule_normalized.jpg'
 | 
			
		||||
                        alt="Schedule Slide"
 | 
			
		||||
                        style={{ height: "300px", width: "2000px" }}
 | 
			
		||||
                    />
 | 
			
		||||
                    <Carousel.Caption>
 | 
			
		||||
                        <div className="captionStyle">
 | 
			
		||||
@@ -36,9 +35,9 @@ export default class HomeCarousel extends React.Component{
 | 
			
		||||
                </Carousel.Item>
 | 
			
		||||
                <Carousel.Item>
 | 
			
		||||
                    <img
 | 
			
		||||
                        src='https://tadvantagesites-com.cdn-convertus.com/uploads/sites/288/2019/07/Generic-Personal-Watercraft-3.jpg'
 | 
			
		||||
                        src='/images/carousel/rentals_normalized.jpg'
 | 
			
		||||
                        alt="Rent Slide"
 | 
			
		||||
                        style={{ height: "300px", width: "2000px" }}
 | 
			
		||||
                        className="d-block w-100"
 | 
			
		||||
                    />
 | 
			
		||||
                    <Carousel.Caption>
 | 
			
		||||
                        <div className="captionStyle">
 | 
			
		||||
 
 | 
			
		||||
@@ -2,14 +2,15 @@ import React from "react";
 | 
			
		||||
import { Button, Card } from "react-bootstrap";
 | 
			
		||||
import propTypes from "prop-types";
 | 
			
		||||
import { grammaticalListString } from "../utils/strings";
 | 
			
		||||
export default class GameInfoCard extends React.Component {
 | 
			
		||||
export default class MatchInfoCard extends React.Component {
 | 
			
		||||
    constructor(props) {
 | 
			
		||||
        super(props);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    getParticipants() {
 | 
			
		||||
        let participants = [];
 | 
			
		||||
        this.props.match.registeredUsers.array.forEach(user => {
 | 
			
		||||
        console.log(this.props);
 | 
			
		||||
        this.props.match.participants.forEach(user => {
 | 
			
		||||
            participants.push(user.firstName);
 | 
			
		||||
        });
 | 
			
		||||
        return participants;
 | 
			
		||||
@@ -19,10 +20,10 @@ export default class GameInfoCard extends React.Component {
 | 
			
		||||
        return (
 | 
			
		||||
            <Card style={{ width: "20rem" }}>
 | 
			
		||||
                <Card.Body>
 | 
			
		||||
                    <Card.Title>{this.props.match.sport}</Card.Title>
 | 
			
		||||
                    <Card.Subtitle className="mb-2 text-muted">{this.props.match.sport}</Card.Subtitle>
 | 
			
		||||
                    <Card.Title>{this.props.match.sport.name}</Card.Title>
 | 
			
		||||
                    <Card.Subtitle className="mb-2 text-muted">{this.props.match.title}</Card.Subtitle>
 | 
			
		||||
                    <Card.Text>
 | 
			
		||||
                        Join <strong>{grammaticalListString(this.getParticipants(), 4)}</strong> to play a few matches of <strong>{this.props.match.sport}</strong> at <strong>{this.props.match.location}</strong> on <strong>{this.props.match.dateTime.toLocaleDateString("en-US")}</strong>!
 | 
			
		||||
                        Join <strong>{grammaticalListString(this.getParticipants(), 4)}</strong> to play a few matches of <strong>{this.props.match.sport.name}</strong> at <strong>{this.props.match.location.toString()}</strong> on <strong>{new Date(this.props.match.when).toLocaleDateString("en-US")}</strong>!
 | 
			
		||||
                    </Card.Text>
 | 
			
		||||
                    <Button variant="primary">Join!</Button>
 | 
			
		||||
                </Card.Body>
 | 
			
		||||
@@ -31,6 +32,6 @@ export default class GameInfoCard extends React.Component {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
GameInfoCard.propTypes = {
 | 
			
		||||
MatchInfoCard.propTypes = {
 | 
			
		||||
    match: propTypes.object,
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										24
									
								
								sports-matcher/client/src/components/MatchInfoCardDisplay.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								sports-matcher/client/src/components/MatchInfoCardDisplay.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,24 @@
 | 
			
		||||
import React from "react";
 | 
			
		||||
import propTypes from "prop-types";
 | 
			
		||||
import MatchInfoCard from "./MatchInfoCard";
 | 
			
		||||
 | 
			
		||||
export default class MatchInfoCardDisplay extends React.Component {
 | 
			
		||||
    constructor(props) {
 | 
			
		||||
        super(props);
 | 
			
		||||
    }
 | 
			
		||||
    render() {
 | 
			
		||||
        let matches = null;
 | 
			
		||||
        if (this.props.recommendedmatches.length > 0) {
 | 
			
		||||
            matches = this.props.recommendedmatches.map((match) => <MatchInfoCard key={match._id} match={match}></MatchInfoCard>);
 | 
			
		||||
        }
 | 
			
		||||
        return (
 | 
			
		||||
            <div className="horizontal-scroller">
 | 
			
		||||
                {matches}
 | 
			
		||||
            </div>
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
MatchInfoCardDisplay.propTypes = {
 | 
			
		||||
    recommendedmatches: propTypes.array,
 | 
			
		||||
};
 | 
			
		||||
@@ -4,6 +4,9 @@ import Layout from "./Layout";
 | 
			
		||||
import reportWebVitals from "./reportWebVitals";
 | 
			
		||||
import { BrowserRouter } from "react-router-dom";
 | 
			
		||||
import "bootstrap/dist/css/bootstrap.min.css"; // This could be optimized by importing individual css components.
 | 
			
		||||
 | 
			
		||||
console.log(process.env);
 | 
			
		||||
 | 
			
		||||
ReactDOM.render(
 | 
			
		||||
    <React.StrictMode>
 | 
			
		||||
        <BrowserRouter>
 | 
			
		||||
 
 | 
			
		||||
@@ -1,20 +1,31 @@
 | 
			
		||||
import React from "react";
 | 
			
		||||
import { apiClient } from "../utils/httpClients";
 | 
			
		||||
import HomeCarousel from "../components/HomeCarousel";
 | 
			
		||||
import MatchInfoCardDisplay from "../components/MatchInfoCardDisplay";
 | 
			
		||||
export default class Welcome extends React.Component {
 | 
			
		||||
    constructor(props) {
 | 
			
		||||
        super(props);
 | 
			
		||||
        this.recentMatchesRequest = apiClient.get("/match/recent/15");
 | 
			
		||||
        this.state = {
 | 
			
		||||
            displayedMatches: [],
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async componentDidMount() {
 | 
			
		||||
        await this.latestMatches();
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async latestMatches() {
 | 
			
		||||
        let recentMatchesRes = await apiClient.get("/match/recent/15");
 | 
			
		||||
        if (recentMatchesRes.status === 200) {
 | 
			
		||||
            this.setState({ displayedMatches: recentMatchesRes.data.recent });
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    render() {
 | 
			
		||||
        return (
 | 
			
		||||
            <div className="page-root">
 | 
			
		||||
                <div>
 | 
			
		||||
                    {/* <h1>Sports Matcher</h1>
 | 
			
		||||
                    <p>The best place to find a local match for a good game of your favourite sport!</p> */}
 | 
			
		||||
                    <HomeCarousel></HomeCarousel>
 | 
			
		||||
                </div>
 | 
			
		||||
                <HomeCarousel />
 | 
			
		||||
                <div className="text-center p-3 mt-2">
 | 
			
		||||
                    <h2>Why?</h2>
 | 
			
		||||
                    <p>Because you want to play the sports you love while meeting new friends!</p>
 | 
			
		||||
@@ -23,6 +34,7 @@ export default class Welcome extends React.Component {
 | 
			
		||||
                <hr />
 | 
			
		||||
                <div className="p-4">
 | 
			
		||||
                    <h2>Available Matches</h2>
 | 
			
		||||
                    <MatchInfoCardDisplay recommendedmatches={this.state.displayedMatches} />
 | 
			
		||||
                </div>
 | 
			
		||||
            </div>
 | 
			
		||||
        );
 | 
			
		||||
 
 | 
			
		||||
@@ -1,15 +0,0 @@
 | 
			
		||||
.captionStyle {
 | 
			
		||||
    background-color: seashell;
 | 
			
		||||
    color: black;
 | 
			
		||||
    outline: 1px solid black;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.carousel-control-next,
 | 
			
		||||
.carousel-control-prev /*, .carousel-indicators */ {
 | 
			
		||||
    filter: invert(100%);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.carousel-indicators button {
 | 
			
		||||
    filter: invert(100%);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -1,19 +1,5 @@
 | 
			
		||||
.jumbotron {
 | 
			
		||||
  width: 100%;
 | 
			
		||||
  padding-left: 1.5rem;
 | 
			
		||||
  padding-right: 1.5rem;
 | 
			
		||||
  padding-top: 12rem;
 | 
			
		||||
  padding-bottom: 1rem;
 | 
			
		||||
  text-align: center;
 | 
			
		||||
  background-size: cover;
 | 
			
		||||
  background-color: black;
 | 
			
		||||
  color: white;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.jumbotron h1 {
 | 
			
		||||
  font-size: 1.5rem;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.horizontal-scroller {
 | 
			
		||||
  overflow-x: scroll;
 | 
			
		||||
  padding-top: 1rem;
 | 
			
		||||
  padding-bottom: 1rem;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
import axios from "axios";
 | 
			
		||||
 | 
			
		||||
export const apiClient = axios.create({
 | 
			
		||||
    baseURL: process.env.API_HOST,
 | 
			
		||||
    baseURL: process.env.REACT_APP_API_HOST,
 | 
			
		||||
    timeout: 5000,
 | 
			
		||||
});
 | 
			
		||||
@@ -10,7 +10,9 @@ export function grammaticalListString(items, max) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        built += item;
 | 
			
		||||
        built += ", ";
 | 
			
		||||
        if (index < items.length - 1) {
 | 
			
		||||
            built += ", ";
 | 
			
		||||
        }
 | 
			
		||||
        if (index == max - 1) {
 | 
			
		||||
            built += "and ";
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -26,8 +26,14 @@ MatchController.get("/search/:sport", needDatabase, async (req, res) => {
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
MatchController.get("/recent/:limit?", needDatabase, async (req, res) => {
 | 
			
		||||
    let limit = req.params.limit;
 | 
			
		||||
    const user = req.user;
 | 
			
		||||
    let limit = parseInt(req.params.limit);
 | 
			
		||||
    if (!req.params.limit) limit = 10;
 | 
			
		||||
    if (isNaN(limit)) {
 | 
			
		||||
        console.log(typeof (limit));
 | 
			
		||||
        res.status(400).send("Limit parameter is not a number.");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    if (isNaN(limit)) {
 | 
			
		||||
        res.status(400).send("Limit parameter not a number.");
 | 
			
		||||
        return;
 | 
			
		||||
@@ -36,8 +42,15 @@ MatchController.get("/recent/:limit?", needDatabase, async (req, res) => {
 | 
			
		||||
        res.status(400).send("Limit greater than maximum limit of 50.");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    let recent = null;
 | 
			
		||||
    try {
 | 
			
		||||
        const recent = await matchModel.find().where("publicity").gte(2).limit(limit).sort({ createDate: -1 });
 | 
			
		||||
        if (user) {
 | 
			
		||||
            await user.populate("participatingMatches").populate("participatingMatches.participants").populate("participatingMatches.sport");
 | 
			
		||||
            recent = user.participatingMatches;
 | 
			
		||||
        } else {
 | 
			
		||||
            recent = await matchModel.find().where("publicity").gte(2).limit(limit).sort({ createDate: -1 });
 | 
			
		||||
        }
 | 
			
		||||
        await recent.populate("members.$"); // Populates all references.
 | 
			
		||||
        res.status(200).send({ recent: recent });
 | 
			
		||||
    } catch (err) {
 | 
			
		||||
        console.error(err);
 | 
			
		||||
@@ -46,7 +59,6 @@ MatchController.get("/recent/:limit?", needDatabase, async (req, res) => {
 | 
			
		||||
    }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
// TODO: delete, update match.
 | 
			
		||||
MatchController.post("/", needDatabase, authenticationGuard, async (req, res) => {
 | 
			
		||||
    try {
 | 
			
		||||
        const userId = req.session.userId;
 | 
			
		||||
@@ -73,17 +85,58 @@ MatchController.post("/", needDatabase, authenticationGuard, async (req, res) =>
 | 
			
		||||
    }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
MatchController.get("/:matchId", needDatabase, async (req, res) => {
 | 
			
		||||
    if (!req.params.matchId) {
 | 
			
		||||
MatchController.patch("/:id", needDatabase, authenticationGuard, async (req, res) => {
 | 
			
		||||
    const match = await matchModel.findById(req.params.id);
 | 
			
		||||
    if (!match) {
 | 
			
		||||
        res.status(400).send("Invalid match ID provided.");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (req.user._id !== match.creator && req.user.accessLevel < 3) {
 | 
			
		||||
        res.status(401).send("Not authorized.");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (req.body._id) {
 | 
			
		||||
        res.status(400).send("Cannot change ID of match.");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (req.body.creator) {
 | 
			
		||||
        res.status(400).send("Cannot change creator of match.");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    await match.updateOne(req.body);
 | 
			
		||||
    res.status(200).send(match);
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
MatchController.delete("/:id", needDatabase, authenticationGuard, async (req, res) => {
 | 
			
		||||
    const match = await matchModel.findById(req.params.id);
 | 
			
		||||
    if (!match) {
 | 
			
		||||
        res.status(400).send("Invalid match ID provided.");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (req.user._id !== match.creator && req.user.accessLevel < 3) {
 | 
			
		||||
        res.status(401).send("Not authorized.");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    await match.deleteOne();
 | 
			
		||||
    res.status(200).send("Deleted.");
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
MatchController.get("/:id", needDatabase, async (req, res) => {
 | 
			
		||||
    if (!req.params.id) {
 | 
			
		||||
        res.status(404).send("Id must be provided to retrieve match");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    try {
 | 
			
		||||
        const match = await matchModel.findById(req.params.matchId);
 | 
			
		||||
        const match = await matchModel.findById(req.params.id).populate("sport");
 | 
			
		||||
        if (match) {
 | 
			
		||||
            res.status(200).send(match);
 | 
			
		||||
        } else {
 | 
			
		||||
            res.status(404).send("Could not find match with ID: " + req.params.matchId);
 | 
			
		||||
            res.status(404).send("Could not find match with ID: " + req.params.id);
 | 
			
		||||
        }
 | 
			
		||||
    } catch (error) {
 | 
			
		||||
        res.status(500).send("Internal server error.");
 | 
			
		||||
@@ -91,4 +144,51 @@ MatchController.get("/:matchId", needDatabase, async (req, res) => {
 | 
			
		||||
    }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
MatchController.get("/join/:id", needDatabase, authenticationGuard, async (req, res) => {
 | 
			
		||||
    const match = await matchModel.findById(req.params.id);
 | 
			
		||||
    const user = req.user;
 | 
			
		||||
    if (!match) {
 | 
			
		||||
        res.status(400).send("Invalid match ID provided.");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (user.participatingMatches.includes(match._id)) {
 | 
			
		||||
        res.status(400).send("Already participating in match.");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    match.participants.push(user._id);
 | 
			
		||||
    user.participatingMatches.push(match._id);
 | 
			
		||||
 | 
			
		||||
    await match.save();
 | 
			
		||||
    await user.save();
 | 
			
		||||
 | 
			
		||||
    res.status(200).send("Joined.");
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
MatchController.get("/leave/:id", needDatabase, authenticationGuard, async (req, res) => {
 | 
			
		||||
    const match = await matchModel.findById(req.params.id);
 | 
			
		||||
    const user = req.user;
 | 
			
		||||
 | 
			
		||||
    if (!match) {
 | 
			
		||||
        res.status(400).send("Invalid match ID provided.");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!user.participatingMatches.includes(match._id)) {
 | 
			
		||||
        res.status(400).send("Not part of match.");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const userIndex = match.participants.indexOf(user._id);
 | 
			
		||||
    match.participants.splice(userIndex, 1);
 | 
			
		||||
    await match.save();
 | 
			
		||||
 | 
			
		||||
    const matchIndex = user.participatingMatches.indexOf(match._id);
 | 
			
		||||
    user.participatingMatches.splice(matchIndex, 1);
 | 
			
		||||
    await user.save();
 | 
			
		||||
 | 
			
		||||
    res.status(200).send("Left match.");
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
export default MatchController;
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
import express from "express";
 | 
			
		||||
import { authenticationGuard } from "../middleware/authority.js";
 | 
			
		||||
import { needDatabase } from "../middleware/database.js";
 | 
			
		||||
import userModel from "../schemas/userModel.js";
 | 
			
		||||
import User from "../schemas/userModel.js";
 | 
			
		||||
const UserController = express.Router();
 | 
			
		||||
 | 
			
		||||
@@ -49,84 +50,88 @@ UserController.get("/logout", authenticationGuard, (req, res) => {
 | 
			
		||||
    });
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
UserController.get("/email/:userId?", needDatabase, authenticationGuard, async (req, res) => {
 | 
			
		||||
    if (!req.params.userId) req.params.userId = req.session.userId;
 | 
			
		||||
    const curUser = await User.findById(req.session.userId);
 | 
			
		||||
    const selUser = req.session.userId === req.params.userId ? curUser : await User.findById(req.params.userId);
 | 
			
		||||
    if (selUser.email.public || curUser._id === selUser._id || curUser.accessLevel > 2) {
 | 
			
		||||
        res.status(200).send({ email: selUser.email });
 | 
			
		||||
UserController.get("/:id?", needDatabase, authenticationGuard, async (req, res) => {
 | 
			
		||||
    let user = null;
 | 
			
		||||
    if (req.params.id) {
 | 
			
		||||
        if (req.user.accessLevel > 2) {
 | 
			
		||||
            user = await userModel.findById(req.params.id);
 | 
			
		||||
        } else {
 | 
			
		||||
            res.status(401).send("Unauthorized.");
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        res.status(401).send("Could not authenticate request.");
 | 
			
		||||
        user = req.user;
 | 
			
		||||
    }
 | 
			
		||||
    user.password = undefined;
 | 
			
		||||
    res.status(200).send(user);
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
UserController.get("/firstName/:userId?", needDatabase, authenticationGuard, async (req, res) => {
 | 
			
		||||
    if (!req.params.userId) req.params.userId = req.session.userId;
 | 
			
		||||
    const curUser = await User.findById(req.session.userId);
 | 
			
		||||
    const selUser = req.session.userId === req.params.userId ? curUser : await User.findById(req.params.userId);
 | 
			
		||||
    if (selUser.firstName.public || curUser._id === selUser._id || curUser.accessLevel > 2) {
 | 
			
		||||
        res.status(200).send({ firstName: selUser.firstName });
 | 
			
		||||
UserController.patch("/:id?", needDatabase, authenticationGuard, async (req, res) => {
 | 
			
		||||
    let user = null;
 | 
			
		||||
    if (req.params.id) {
 | 
			
		||||
        if (req.user.accessLevel > 2) {
 | 
			
		||||
            user = await userModel.findById(req.params.id);
 | 
			
		||||
        } else {
 | 
			
		||||
            res.status(401).send("Unauthorized.");
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        res.status(401).send("Could not authenticate request.");
 | 
			
		||||
        user = req.user;
 | 
			
		||||
    }
 | 
			
		||||
    if (req.body._id) {
 | 
			
		||||
        res.status(400).send("Cannot change user ID.");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (req.body.createdMatches) {
 | 
			
		||||
        res.status(400).send("Cannot directly change the list of created matches.");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (req.body.password) {
 | 
			
		||||
        res.status(400).send("Cannot directly change user password.");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (req.body.participatingMatches) {
 | 
			
		||||
        res.status(400).send("Cannot directly change the list of participating matches.");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (req.body.joinDate) {
 | 
			
		||||
        res.status(400).send("Cannot change the join date.");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (req.body.accessLevel && req.user.accessLevel < 3) {
 | 
			
		||||
        res.status(401).send("Unauthorized to change the access level of this user.");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    await user.updateOne(req.body);
 | 
			
		||||
    res.status(200).send("Updated.");
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
UserController.get("/lastName/:userId?", needDatabase, authenticationGuard, async (req, res) => {
 | 
			
		||||
    if (!req.params.userId) req.params.userId = req.session.userId;
 | 
			
		||||
    const curUser = await User.findById(req.session.userId);
 | 
			
		||||
    const selUser = req.session.userId === req.params.userId ? curUser : await User.findById(req.params.userId);
 | 
			
		||||
    if (selUser.lastName.public || curUser._id === selUser._id || curUser.accessLevel > 2) {
 | 
			
		||||
        res.status(200).send({ email: selUser.lastName });
 | 
			
		||||
/* TODO: Implement middleware for removing users.
 | 
			
		||||
 | 
			
		||||
UserController.delete("/:id?", needDatabase, authenticationGuard, async (req, res) => {
 | 
			
		||||
    let user = null;
 | 
			
		||||
    if (req.params.id) {
 | 
			
		||||
        if (req.user.accessLevel > 2) {
 | 
			
		||||
            user = await userModel.findById(req.params.id);
 | 
			
		||||
        } else {
 | 
			
		||||
            res.status(401).send("Unauthorized.");
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        res.status(401).send("Could not authenticate request.");
 | 
			
		||||
        user = req.user;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    await user.deleteOne();
 | 
			
		||||
    res.status(200).send("Deleted user.");
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
UserController.get("/phone/:userId?", needDatabase, authenticationGuard, async (req, res) => {
 | 
			
		||||
    if (!req.params.userId) req.params.userId = req.session.userId;
 | 
			
		||||
    const curUser = await User.findById(req.session.userId);
 | 
			
		||||
    const selUser = req.session.userId === req.params.userId ? curUser : await User.findById(req.params.userId);
 | 
			
		||||
    if (selUser.phone.public || curUser._id === selUser._id || curUser.accessLevel > 2) {
 | 
			
		||||
        res.status(200).send({ phone: selUser.phone });
 | 
			
		||||
    } else {
 | 
			
		||||
        res.status(401).send("Could not authenticate request.");
 | 
			
		||||
    }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
UserController.get("/participatingMatches/:userId?", needDatabase, authenticationGuard, async (req, res) => {
 | 
			
		||||
    if (!req.params.userId) req.params.userId = req.session.userId;
 | 
			
		||||
    const curUser = await User.findById(req.session.userId);
 | 
			
		||||
    const selUser = req.session.userId === req.params.userId ? curUser : await User.findById(req.params.userId);
 | 
			
		||||
    if (selUser.participatingMatches.public || curUser._id === selUser._id || curUser.accessLevel > 2) {
 | 
			
		||||
        res.status(200).send({ participatingMatches: selUser.participatingMatches });
 | 
			
		||||
    } else {
 | 
			
		||||
        res.status(401).send("Could not authenticate request.");
 | 
			
		||||
    }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
UserController.get("/joinDate/:userId?", needDatabase, authenticationGuard, async (req, res) => {
 | 
			
		||||
    if (!req.params.userId) req.params.userId = req.session.userId;
 | 
			
		||||
    const curUser = await User.findById(req.session.userId);
 | 
			
		||||
    const selUser = req.session.userId === req.params.userId ? curUser : await User.findById(req.params.userId);
 | 
			
		||||
    if (curUser._id === selUser._id || curUser.accessLevel > 2) {
 | 
			
		||||
        res.status(200).send({ joinDate: selUser.joinDate });
 | 
			
		||||
    } else {
 | 
			
		||||
        res.status(401).send("Could not authenticate request.");
 | 
			
		||||
    }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
UserController.get("/createdMatches/:userId?", needDatabase, authenticationGuard, async (req, res) => {
 | 
			
		||||
    if (!req.params.userId) req.params.userId = req.session.userId;
 | 
			
		||||
    const curUser = await User.findById(req.session.userId);
 | 
			
		||||
    const selUser = req.session.userId === req.params.userId ? curUser : await User.findById(req.params.userId);
 | 
			
		||||
    if (curUser._id === selUser._id || curUser.accessLevel > 2) {
 | 
			
		||||
        res.status(200).send({ createdMatches: selUser.createdMatches });
 | 
			
		||||
    } else {
 | 
			
		||||
        res.status(401).send("Could not authenticate request.");
 | 
			
		||||
    }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
// TODO: Finish update requests using put.
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
UserController.post("/", needDatabase, async (req, res) => {
 | 
			
		||||
    try {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
import MongoStore from "connect-mongo";
 | 
			
		||||
import session from "express-session";
 | 
			
		||||
import { mongooseDbName, mongoURI } from "../database/mongoose.js";
 | 
			
		||||
import userModel from "../schemas/userModel.js";
 | 
			
		||||
const sessionConf = {
 | 
			
		||||
    secret: process.env.SESSION_SECRET || "super duper secret string.",
 | 
			
		||||
    cookie: {
 | 
			
		||||
@@ -16,11 +17,12 @@ if (process.env.NODE_ENV === "production") {
 | 
			
		||||
}
 | 
			
		||||
export const userSession = session(sessionConf);
 | 
			
		||||
 | 
			
		||||
export function authenticationGuard(req, res, next) {
 | 
			
		||||
export async function authenticationGuard(req, res, next) {
 | 
			
		||||
    if (req.session.userId) {
 | 
			
		||||
        req.user = await userModel.findById(req.session.userId);
 | 
			
		||||
        next();
 | 
			
		||||
    } else {
 | 
			
		||||
        res.sendStatus(401);
 | 
			
		||||
        res.status(401).send("Not authorized.");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -24,4 +24,17 @@ const matchSchema = new mongoose.Schema({
 | 
			
		||||
    createDate: { type: Date, required: true, default: Date.now }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
matchSchema.pre("remove", function (next) {
 | 
			
		||||
    const match = this;
 | 
			
		||||
    match.populate("creator").populate("participants");
 | 
			
		||||
 | 
			
		||||
    match.participants.forEach(participant => {
 | 
			
		||||
        const index = participant.participatingMatches.indexOf(match._id);
 | 
			
		||||
        participant.participatingMatches.splice(index, 1);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    match.creator.createdMatches.splice(match.creator.createdMatches.indexOf(match._id), 1);
 | 
			
		||||
    next();
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
export default mongoose.model(ModelNameRegister.Match, matchSchema);
 | 
			
		||||
		Reference in New Issue
	
	Block a user