Skip to content

Commit

Permalink
Merge pull request #128 from UoaWDCC/fix/code
Browse files Browse the repository at this point in the history
Added test cases and code to handle error in mapping function
  • Loading branch information
Harsheel12 authored Jul 2, 2024
2 parents c474438 + e57dc80 commit 9ac7c3f
Show file tree
Hide file tree
Showing 61 changed files with 4,161 additions and 2,717 deletions.
File renamed without changes.
49 changes: 49 additions & 0 deletions .github/workflows/prettier.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Format Code

on:
push:
branches:
- "**"

jobs:
format:
runs-on: ubuntu-latest
defaults:
run:
working-directory: .

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}

- name: Setup Node.js environment
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install dependencies
run: yarn install --frozen-lockfile

- name: Format code with Prettier
run: yarn format

- name: Check for changes
id: git_changes
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
if [ -n "$(git status --porcelain)" ]; then
echo "changes_detected=true" >> "$GITHUB_ENV"
else
echo "changes_detected=false" >> "$GITHUB_ENV"
fi
- name: Commit and push changes
if: ${{ env.changes_detected == 'true' }}
run: |
git add .
git commit -m "Format code with Prettier"
git push
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.github/
9 changes: 9 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"tabWidth": 2,
"semi": true,
"singleQuote": false,
"trailingComma": "es5",
"endOfLine": "auto",
"printWidth": 80,
"plugins": ["prettier-plugin-tailwindcss"]
}
28 changes: 18 additions & 10 deletions api/controller/adminController.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import { Request, Response } from 'express';
import { Request, Response } from "express";
import asyncHandler from "../middleware/asyncHandler";

export const createNewEvent = asyncHandler(async (req: Request, res: Response) => {
export const createNewEvent = asyncHandler(
async (req: Request, res: Response) => {
throw new Error("Not implemented yet");
});
}
);

export const getStagingEvents = asyncHandler(async (req: Request, res: Response) => {
export const getStagingEvents = asyncHandler(
async (req: Request, res: Response) => {
throw new Error("Not implemented yet");
});
}
);

export const getEventInformation = asyncHandler(async (req: Request, res: Response) => {
export const getEventInformation = asyncHandler(
async (req: Request, res: Response) => {
throw new Error("Not implemented yet");
});
}
);

export const updateEventInformation = asyncHandler(async (req: Request, res: Response) => {
export const updateEventInformation = asyncHandler(
async (req: Request, res: Response) => {
throw new Error("Not implemented yet");
});
}
);

export const getMembers = asyncHandler(async (req: Request, res: Response) => {
throw new Error("Not implemented yet");
throw new Error("Not implemented yet");
});
12 changes: 7 additions & 5 deletions api/controller/creditController.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Request, Response } from 'express';
import { Request, Response } from "express";
import asyncHandler from "../middleware/asyncHandler";
export const getTeamInfo = asyncHandler(async (req: Request, res: Response) => {
throw new Error("Not implemented yet");
throw new Error("Not implemented yet");
});

export const getCredits = asyncHandler(async (req: Request, res: Response) => {
throw new Error("Not implemented yet");
throw new Error("Not implemented yet");
});

export const getOurPurpose = asyncHandler(async (req: Request, res: Response) => {
export const getOurPurpose = asyncHandler(
async (req: Request, res: Response) => {
throw new Error("Not implemented yet");
});
}
);
23 changes: 13 additions & 10 deletions api/controller/eventController.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { Request, Response } from 'express';
import Events from '../db/sampleEvents';
import { Request, Response } from "express";
import Events from "../db/sampleEvents";
import asyncHandler from "../middleware/asyncHandler";

const getEvents = asyncHandler(async (req: Request, res: Response): Promise<void> => {
const getEvents = asyncHandler(
async (req: Request, res: Response): Promise<void> => {
res.json(Events);
});
}
);


const getEventById = asyncHandler(async (req: Request, res: Response): Promise<void> => {
const getEventById = asyncHandler(
async (req: Request, res: Response): Promise<void> => {
const id = req.params.id;
const event = Events.find((event) => event._id === id);

if (event) {
res.json(event);
res.json(event);
} else {
res.status(404);
throw new Error('Event not found');
res.status(404);
throw new Error("Event not found");
}
});
}
);

export { getEvents, getEventById };
15 changes: 8 additions & 7 deletions api/controller/photoController.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
// controllers/photoController.ts
import { Request, Response } from 'express';
import asyncHandler from "../middleware/asyncHandler";// Adjust the import path as necessary
import { Request, Response } from "express";
import asyncHandler from "../middleware/asyncHandler"; // Adjust the import path as necessary

export const uploadPhoto = asyncHandler(async (req: Request, res: Response) => {
throw new Error("Not implemented yet");
throw new Error("Not implemented yet");
});

export const getPhotoById = asyncHandler(async (req: Request, res: Response) => {
export const getPhotoById = asyncHandler(
async (req: Request, res: Response) => {
throw new Error("Not implemented yet");
});

}
);

export const deletePhoto = asyncHandler(async (req: Request, res: Response) => {
throw new Error("Not implemented yet");
throw new Error("Not implemented yet");
});
4 changes: 2 additions & 2 deletions api/db/User.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema, model } from 'mongoose';
import { Schema, model } from "mongoose";

const userSchema = new Schema({
firstName: {
Expand All @@ -15,6 +15,6 @@ const userSchema = new Schema({
},
});

const User = model('User', userSchema);
const User = model("User", userSchema);

export default User;
1 change: 0 additions & 1 deletion api/db/config/env.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import dotenv from "dotenv";

dotenv.config();

(function checkProcessEnvIntegrity() {
if (!process.env.DATABASE_HOST) {
throw Error(
Expand Down
9 changes: 5 additions & 4 deletions api/middleware/asyncHandler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Request, Response, NextFunction } from 'express';
import { Request, Response, NextFunction } from "express";

const asyncHandler = (
fn: (req: Request, res: Response, next: NextFunction) => Promise<any>) => (req: Request, res: Response, next: NextFunction) => {
const asyncHandler =
(fn: (req: Request, res: Response, next: NextFunction) => Promise<any>) =>
(req: Request, res: Response, next: NextFunction) => {
Promise.resolve(fn(req, res, next)).catch(next);
};
};

export default asyncHandler;
25 changes: 13 additions & 12 deletions api/middleware/authMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { Request, Response, NextFunction } from 'express';
import { Request, Response, NextFunction } from "express";
import asyncHandler from "./asyncHandler";


export const protect = asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
export const protect = asyncHandler(
async (req: Request, res: Response, next: NextFunction) => {
// Authentication and authorization logic here
// If user is authenticated and authorized, call next()
// Otherwise, respond wit an error
next();
});
}
);

export const authorize = (...roles: string[]) => {
return (req: Request, res: Response, next: NextFunction) => {
// Authorization logic here
// Check if the authenticated user's role is in the allowed roles
// If so, call next()
// Otherwise, respond with an error
next();
};
};
return (req: Request, res: Response, next: NextFunction) => {
// Authorization logic here
// Check if the authenticated user's role is in the allowed roles
// If so, call next()
// Otherwise, respond with an error
next();
};
};
41 changes: 23 additions & 18 deletions api/middleware/errorMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
import { Request, Response, NextFunction } from 'express';
import { Error } from 'mongoose';
import dotenv from 'dotenv';
import { Request, Response, NextFunction } from "express";
import { Error } from "mongoose";
import dotenv from "dotenv";

dotenv.config();

const notFound = (req: Request, res: Response, next: NextFunction): void => {
const error = new Error(`Not Found - ${req.originalUrl}`);
res.status(404);
next(error);
const error = new Error(`Not Found - ${req.originalUrl}`);
res.status(404);
next(error);
};

const errorHandler = (err: Error & { name?: string; kind?: string }, req: Request, res: Response, next: NextFunction): void => {
let statusCode: number = res.statusCode === 200 ? 500 : res.statusCode;
let message: string = err.message;
const errorHandler = (
err: Error & { name?: string; kind?: string },
req: Request,
res: Response,
next: NextFunction
): void => {
let statusCode: number = res.statusCode === 200 ? 500 : res.statusCode;
let message: string = err.message;

if (err.name === 'CastError' && err.kind === 'ObjectId') {
statusCode = 404;
message = 'Resource not found';
}
if (err.name === "CastError" && err.kind === "ObjectId") {
statusCode = 404;
message = "Resource not found";
}

res.status(statusCode).json({
message: message,
stack: process.env.NODE_ENV === 'production' ? null : err.stack,
});
res.status(statusCode).json({
message: message,
stack: process.env.NODE_ENV === "production" ? null : err.stack,
});
};

export { notFound, errorHandler };
export { notFound, errorHandler };
34 changes: 20 additions & 14 deletions api/models/users.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { pgTable, serial, varchar, boolean, timestamp } from 'drizzle-orm/pg-core';
import {
pgTable,
serial,
varchar,
boolean,
timestamp,
} from "drizzle-orm/pg-core";

export const users = pgTable('users', {
user_id: serial('user_id').primaryKey(),
email: varchar('email', { length: 255 }).notNull().unique(),
uoa_id: varchar('uoa_id', { length: 50 }),
upi: varchar('upi', { length: 50 }),
institution: varchar('institution', { length: 50 }),
year: varchar('year', { length: 50 }),
study_field: varchar('study_field', { length: 255 }),
name: varchar('name', { length: 255 }),
is_admin: boolean('is_admin').default(false),
is_paid: boolean('is_paid').default(false),
is_info_confirmed: boolean('is_info_confirmed').default(false),
created_at: timestamp('created_at').defaultNow(),
export const users = pgTable("users", {
user_id: serial("user_id").primaryKey(),
email: varchar("email", { length: 255 }).notNull().unique(),
uoa_id: varchar("uoa_id", { length: 50 }),
upi: varchar("upi", { length: 50 }),
institution: varchar("institution", { length: 50 }),
year: varchar("year", { length: 50 }),
study_field: varchar("study_field", { length: 255 }),
name: varchar("name", { length: 255 }),
is_admin: boolean("is_admin").default(false),
is_paid: boolean("is_paid").default(false),
is_info_confirmed: boolean("is_info_confirmed").default(false),
created_at: timestamp("created_at").defaultNow(),
});
22 changes: 14 additions & 8 deletions api/routes/adminRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import express from 'express';
import { createNewEvent, getStagingEvents, getEventInformation, updateEventInformation, getMembers} from "../controller/adminController";
import { protect } from '../middleware/authMiddleware';
import express from "express";
import {
createNewEvent,
getStagingEvents,
getEventInformation,
updateEventInformation,
getMembers,
} from "../controller/adminController";
import { protect } from "../middleware/authMiddleware";

const router = express.Router();

router.use(protect);

router.post('/new-event', createNewEvent);
router.get('/staging', getStagingEvents);
router.get('/event-information', getEventInformation);
router.put('/event-information/:eventId', updateEventInformation);
router.get('/members', getMembers);
router.post("/new-event", createNewEvent);
router.get("/staging", getStagingEvents);
router.get("/event-information", getEventInformation);
router.put("/event-information/:eventId", updateEventInformation);
router.get("/members", getMembers);

export default router;
10 changes: 5 additions & 5 deletions api/routes/authRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import express from 'express';
import {signUp, logIn, clerkSignUp} from "../controller/authController"
import express from "express";
import { signUp, logIn, clerkSignUp } from "../controller/authController";

const router = express.Router();

router.post('/sign-up', signUp);
router.post('/sign-in', logIn);
router.post('/clerk-sign-up', clerkSignUp);
router.post("/sign-up", signUp);
router.post("/sign-in", logIn);
router.post("/clerk-sign-up", clerkSignUp);

export default router;
Loading

0 comments on commit 9ac7c3f

Please sign in to comment.