Skip to content

Commit

Permalink
Merge branch 'main' into component-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jhagendoornCP authored Jun 3, 2024
2 parents 0b92a6f + 5b96b1e commit 5fc73fe
Show file tree
Hide file tree
Showing 51 changed files with 2,615 additions and 1,019 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy
# More GitHub Actions for Azure: https://github.com/Azure/actions

name: Build and deploy Node.js app to Azure Web App - gather-app-inv
name: Build and deploy Node.js app to Azure Web App - gather-app-307

on:
push:
Expand All @@ -24,8 +24,8 @@ jobs:
- name: npm install, build, and test
run: |
npm install --workspaces=false --legacy-peer-deps
npm run -w backend build --if-present
npm run -w backend test --if-present
npm run build -w backend build --if-present
npm run test -w backend test --if-present
working-directory: backend

- name: Zip artifact for deployment
Expand Down Expand Up @@ -59,14 +59,14 @@ jobs:
- name: Login to Azure
uses: azure/login@v1
with:
client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_7234F5539E704699B646CABB541CBF19 }}
tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_EDB7C80261714FECB4D9646EC7145443 }}
subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_6CD2C32FCA004CC99FCD4833293EA065 }}
client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_03BA6874737C4AC5A41D6370B2F87080 }}
tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_90BB6E72E2B34689BB0C942CB41E9672 }}
subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_6D44115D9FF541AFB144E3E14FE8B81A }}

- name: "Deploy to Azure Web App"
id: deploy-to-webapp
uses: azure/webapps-deploy@v2
with:
app-name: "gather-app-inv"
app-name: "gather-app-307"
slot-name: "Production"
package: .
24 changes: 16 additions & 8 deletions .github/workflows/remove-old-artifacts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@ name: Remove old artifacts
on:
schedule:
# Every day at 1am
- cron: '0 1 * * *'
- cron: "0 1 * * *"

jobs:
remove-old-artifacts:
runs-on: ubuntu-latest
timeout-minutes: 10

steps:
- name: Remove old artifacts
uses: c-hive/[email protected]
with:
age: '5 days' # '<number> <unit>', e.g. 5 days, 2 years, 90 seconds, parsed by Moment.js
# Optional inputs
# skip-tags: true
# skip-recent: 5
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up GitHub CLI
run: |
echo ${GITHUB_PAT} | gh auth login --with-token
env:
GITHUB_PAT: ${{ secrets.PAT_TOKEN }}

- name: Remove old artifacts
uses: c-hive/[email protected]
with:
age: "5 days" # '<number> <unit>', e.g. 5 days, 2 years, 90 seconds, parsed by Moment.js
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
4 changes: 2 additions & 2 deletions backend/auth.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import bcrypt from "bcrypt";
import jwt from "jsonwebtoken";
import User, { IUser } from "./models/userSchema";
import User, { IUser } from "./models/userSchema.js";
import dotenv from "dotenv";
import { Request, Response } from "express";
import connectDB from "./connection";
import connectDB from "./connection.js";

dotenv.config();

Expand Down
17 changes: 11 additions & 6 deletions backend/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import express, { Express, Request, Response, NextFunction } from "express";
import { userEndpoints } from "./routes/userRoutes";
import { groupEndpoints } from "./routes/groupRoutes";
import { basketEndpoints } from "./routes/basketRoutes";
import { itemEndpoints } from "./routes/itemRoutes";
import { loginUser } from "./auth";
import { userEndpoints } from "./routes/userRoutes.js";
import { groupEndpoints } from "./routes/groupRoutes.js";
import { basketEndpoints } from "./routes/basketRoutes.js";
import { itemEndpoints } from "./routes/itemRoutes.js";
import { loginUser } from "./auth.js";
import jwt from "jsonwebtoken";

const app: Express = express();
Expand All @@ -20,7 +20,12 @@ app.use((req: Request, res: Response, next: NextFunction) => {
"Access-Control-Allow-Methods",
"GET, POST, PATCH, OPTIONS, DELETE, PUT",
);
next();
// Handle preflight requests
if (req.method === "OPTIONS") {
res.status(204).end(); // Respond with 204 No Content
} else {
next(); // Pass to the next middleware or route handler
}
});

// Testing middleware
Expand Down
47 changes: 0 additions & 47 deletions backend/itemSchema.ts

This file was deleted.

6 changes: 4 additions & 2 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "backend.ts",
"main": "index.ts",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 0",
"dev": "nodemon index.ts",
"dev": "node --loader ts-node/esm ./index.ts",
"start": "node --loader ts-node/esm ./index.ts",
"lint": "npx eslint --report-unused-disable-directives --max-warnings 0 && npx prettier --check ."
},
"author": "",
Expand Down
98 changes: 42 additions & 56 deletions backend/routes/basketRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,62 @@
import express from "express";
import { Request, Response } from "express";
import Basket, { IBasket } from "../models/basketSchema";
import connectDB from "../connection";
import Basket, { IBasket } from "../models/basketSchema.js";
import connectDB from "../connection.js";
import { authenticateUser } from "../auth.js";

const router = express.Router();

router.get("/", async (req: Request, res: Response) => {
router.get("/", authenticateUser, async (req: Request, res: Response) => {
connectDB();
try {
const users = await Basket.find({});
if (users.length === 0) {
res.status(404).send("No baskets found"); // Return a 404 status code if no users are found
const baskets = await Basket.find({});
if (baskets.length === 0) {
res.status(404).send("No baskets found"); // Return a 404 status code if no baskets are found
} else {
res.send(users); // Return the found users
res.send(baskets); // Return the found baskets
}
} catch (error) {
res.status(500).send("Internal Server Error"); // Handle any unexpected errors
}
});
router.get("/:bid", async (req: Request, res: Response) => {
connectDB();
// Use findById correctly with the id parameter from the request
const basket = await Basket.findById(req.params.bid).maxTimeMS(2000);

// Check if group is null or undefined
if (!basket) {
return res.status(404).send("No basket found."); // Use return to exit the function after sending the response
}

// Send the found user
res.send(basket);
console.log("Sent Group");
});

router.get("/:basketid", async (req: Request, res: Response) => {
// Ensure the database connection
connectDB();

try {
// Use findById correctly with the id parameter from the request
const basketById = await Basket.findById(req.params.basketid);

// Check if basket is null or undefined
if (!basketById) {
return res.status(404).send("No basket found"); // Use return to exit the function after sending the response
}
router.get(
"/:basketid",
authenticateUser,
async (req: Request, res: Response) => {
// Ensure the database connection
connectDB();

// Send the found user
res.send(basketById);
console.log("Sent Basket:", basketById);
} catch (error) {
console.log("Now trying to find by BasketName");
try {
const basketsByName = await Basket.find({
basketName: req.params.basketid,
});
console.log(basketsByName);
if (!basketsByName) {
return res.status(404).send("No baskets found"); // Use return to exit the function after sending the response
// Use findById correctly with the id parameter from the request
const basketById = await Basket.findById(req.params.basketid);

// Check if basket is null or undefined
if (!basketById) {
// If not found by ObjectId, try to find by basketName
const basketsByName = await Basket.find({
basketName: req.params.basketid,
});

if (!basketsByName.length) {
return res.status(404).send("No baskets found"); // Use return to exit the function after sending the response
}

// Send the found baskets
return res.send(basketsByName);
}

// Send the found user
res.send(basketsByName);
console.log("Sent Baskets", basketsByName);
// Send the found basket by ObjectId
res.send(basketById);
console.log("Sent Basket:", basketById);
} catch (error) {
console.error("Error fetching basket:", error); // Log the error for debugging
res.status(500).send("Internal Server Error");
}
}
});
},
);

router.post("/", async (req: Request, res: Response) => {
router.post("/", authenticateUser, async (req: Request, res: Response) => {
connectDB();
try {
console.log("Creating a new basket with data:", req.body);
Expand All @@ -97,14 +83,14 @@ router.post("/", async (req: Request, res: Response) => {
}
});

router.patch("/:id", async (req: Request, res: Response) => {
// Get user ID from URL
router.patch("/:id", authenticateUser, async (req: Request, res: Response) => {
// Get basket ID from URL
const { id } = req.params;
const updatedData: Partial<IBasket> = req.body; //Not a full update only partial
const updatedData: Partial<IBasket> = req.body; // Not a full update, only partial

try {
connectDB();

console.log(updatedData);
const updatedBasket = await Basket.findByIdAndUpdate(id, updatedData, {
new: true,
runValidators: true,
Expand All @@ -120,14 +106,14 @@ router.patch("/:id", async (req: Request, res: Response) => {
}
});

router.delete("/:id", async (req: Request, res: Response) => {
router.delete("/:id", authenticateUser, async (req: Request, res: Response) => {
connectDB();
const { id } = req.params;
try {
const basket = await Basket.findByIdAndDelete(id);

if (!basket) {
return res.status(404).send({ message: "basket not found" });
return res.status(404).send({ message: "Basket not found" });
}

res.status(200).send({ message: "Basket Deleted Successfully", basket });
Expand Down
Loading

0 comments on commit 5fc73fe

Please sign in to comment.