Skip to content

Commit

Permalink
Photo pipeline (#42)
Browse files Browse the repository at this point in the history
* photo pipelining, switch to using mysql2/promise for better transactions

* fix casing

* utilize transactions, generateDB models

* add logging

* rename DB models acccordingly

* double check mime type on backend

* fix mime time casing and account creation

* remove unused sql from init procedure
  • Loading branch information
theosiemensrhodes authored Jan 22, 2025
1 parent 004d23a commit b9f45d7
Show file tree
Hide file tree
Showing 42 changed files with 1,226 additions and 1,602 deletions.
14 changes: 11 additions & 3 deletions backend/mysql.js → backend/generate-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,28 @@ const config = {
"database" : process.env.RDS_DB
},
"tableNameCasing": "pascal",
"interfaceNameFormat": "${table}",
"interfaceNameFormat": "${table}DB",
"singularTableNames": true,
"filename": "./src/common/generated.ts",
"filename": "./src/common/databaseModels.ts",
"typeMap": {
"Buffer": ["longblob", "mediumblob"],
"string": ["time", "date"]
}
};

const contents = await Client
let contents = await Client
.fromConfig(config)
.fetchDatabase()
.mapTables((t) => {
t.extends = "RowDataPacket"
return t;
})
.toTypescript();

contents = `import { RowDataPacket } from "mysql2";
${contents}`

writeFile(config.filename, contents, err => {
if (err) console.error(err)
});
4 changes: 4 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
"dotenv": "^16.4.5",
"express": "^4.19.2",
"jsonwebtoken": "^9.0.2",
"morgan": "^1.10.0",
"multer": "^1.4.5-lts.1",
"mysql": "^2.18.1",
"mysql2": "^3.11.3",
"nodemailer": "^6.9.15",
"sharp": "^0.33.5",
"uuid": "^10.0.0"
},
"devDependencies": {
Expand All @@ -34,10 +36,12 @@
"@types/cors": "^2.8.17",
"@types/express": "^5.0.0",
"@types/jsonwebtoken": "^9.0.7",
"@types/morgan": "^1.9.9",
"@types/multer": "^1.4.12",
"@types/mysql": "^2.15.26",
"@types/node": "^22.7.4",
"@types/nodemailer": "^6.4.16",
"@types/sharp": "^0.31.1",
"@types/uuid": "^10.0.0",
"nodemon": "^3.1.7",
"ts-node": "^10.9.2",
Expand Down
38 changes: 23 additions & 15 deletions backend/src/common/generated.ts
Original file line number Diff line number Diff line change
@@ -1,89 +1,97 @@
import { RowDataPacket } from "mysql2";

/*
* This file was generated by a tool.
* Rerun sql-ts to regenerate this file.
*/
export interface Admin {
export interface AdminDB extends RowDataPacket {
'admin_id': string;
'f_name': string;
'fk_user_id'?: string | null;
'l_name': string;
}
export interface Availability {
export interface AvailabilityDB extends RowDataPacket {
'availability_id'?: number;
'day': number;
'end_time': string;
'fk_volunteer_id': string;
'start_time': string;
}
export interface Class {
export interface ClassDB extends RowDataPacket {
'category'?: string | null;
'class_id'?: number;
'class_name': string;
'end_date': string;
'fk_image_id'?: string | null;
'fk_instructor_id': string;
'instructions'?: string | null;
'start_date': string;
'subcategory'?: string | null;
'zoom_link': string;
}
export interface ClassImage {
export interface ClassImageDB extends RowDataPacket {
'fk_class_id'?: number | null;
'image'?: Buffer | null;
'image_id'?: number;
}
export interface ClassPreference {
export interface ClassPreferenceDB extends RowDataPacket {
'class_rank'?: number | null;
'fk_class_id'?: number | null;
'fk_volunter_id'?: string | null;
}
export interface Instructor {
export interface ImageDB extends RowDataPacket {
'image': Buffer;
'image_id': string;
}
export interface InstructorDB extends RowDataPacket {
'email': string;
'f_name': string;
'fk_user_id'?: string | null;
'instructor_id': string;
'l_name': string;
}
export interface PendingShiftCoverage {
export interface PendingShiftCoverageDB extends RowDataPacket {
'pending_volunteer': string;
'request_id': number;
}
export interface Schedule {
export interface ScheduleDB extends RowDataPacket {
'day': number;
'end_time': string;
'fk_class_id': number;
'schedule_id'?: number;
'start_time': string;
}
export interface ShiftCoverageRequest {
export interface ShiftCoverageRequestDB extends RowDataPacket {
'covered_by'?: string | null;
'fk_schedule_id': number;
'fk_volunteer_id': string;
'request_id'?: number;
'shift_date': string;
}
export interface Shift {
export interface ShiftDB extends RowDataPacket {
'checked_in'?: any | null;
'duration': number;
'fk_schedule_id': number;
'fk_volunteer_id': string;
'shift_date': string;
}
export interface User {
export interface UserDB extends RowDataPacket {
'created_at'?: Date | null;
'email': string;
'password'?: string | null;
'fk_image_id'?: string | null;
'password': string;
'role': string;
'user_id': string;
}
export interface VolunteerClass {
export interface VolunteerClassDB extends RowDataPacket {
'fk_class_id': number;
'fk_volunteer_id': string;
}
export interface VolunteerProfilePic {
export interface VolunteerProfilePicDB extends RowDataPacket {
'fk_volunteer_id': string;
'profile_pic': Buffer;
}
export interface Volunteer {
export interface VolunteerDB extends RowDataPacket {
'active'?: any | null;
'bio'?: string | null;
'city'?: string | null;
Expand Down
2 changes: 1 addition & 1 deletion backend/src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Request } from "express";

export interface AuthenticatedUserRequest extends Request {
user?: {
password: any;
password: string;
user_id: string;
role: string;
};
Expand Down
13 changes: 8 additions & 5 deletions backend/src/config/authCheck.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import jwt from "jsonwebtoken";
import dotenv from "dotenv";
import { Response, NextFunction } from "express";
import { NextFunction, Response } from "express";
import jwt from "jsonwebtoken";
import {
AuthenticatedUserRequest,
DecodedJwtPayload,
} from "../common/types.js";
import { getUserById } from "../controllers/userController.js";
import UserModel from "../models/userModel.js";

// Load environment variables
dotenv.config();

// Define environment variables
const TOKEN_SECRET = process.env.TOKEN_SECRET;

const userModel = new UserModel();

async function isAuthorized(
req: AuthenticatedUserRequest,
res: Response,
Expand All @@ -39,7 +41,7 @@ async function isAuthorized(
const decoded = jwt.verify(token, TOKEN_SECRET) as DecodedJwtPayload;

try {
const result = await getUserById(decoded.user_id);
const result = await userModel.getUserById(decoded.user_id);

// Attach the user to the request
req.user = result;
Expand Down Expand Up @@ -77,4 +79,5 @@ async function isAdmin(
next();
}

export { isAuthorized, isAdmin };
export { isAdmin, isAuthorized };

4 changes: 2 additions & 2 deletions backend/src/config/database.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import mysql, { PoolConfig, Pool } from 'mysql';
import { config } from "dotenv";
import mysql, { Pool, PoolOptions } from 'mysql2/promise';

config();

const configuration: PoolConfig = {
const configuration: PoolOptions = {
host: process.env.RDS_HOSTNAME || '',
user: process.env.RDS_USERNAME || '',
password: process.env.RDS_PASSWORD || '',
Expand Down
23 changes: 23 additions & 0 deletions backend/src/config/fileUpload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import multer from "multer";
import path from "path";

const storage = multer.memoryStorage();

// multer for uploading images
export const imageUploadMiddleware = multer({
storage,
fileFilter: (_req, file, cb) => {
// Allowed ext
const allowedExts = ['jpeg', 'jpg', 'png', 'gif'];
// Check ext
const extname = allowedExts.includes(path.extname(file.originalname).replace('.', '').toLowerCase());
// Check mime
const mimetype = allowedExts.map(ext => `image/${ext}`).includes(file.mimetype);

if(mimetype && extname){
cb(null, true);
} else {
cb(null, false);
}
}
}).single('image');
23 changes: 12 additions & 11 deletions backend/src/controllers/adminController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Response } from "express";
import VolunteerModel from "../models/volunteerModel.js";
import { Request, Response } from "express";

import { VolunteerDB } from "../common/generated.js";
import { AuthenticatedUserRequest } from "../common/types.js";

const volunteerModel = new VolunteerModel();
Expand All @@ -16,10 +17,10 @@ async function getUnverifiedVolunteers(
volunteers: unverifiedVolunteers,
});
} catch (error: any) {
res.status(error.status).json({
error: error.message,
});
}
return res.status(error.status ?? 500).json({
error: error.message
});
}
}

async function verifyVolunteer(
Expand All @@ -39,17 +40,17 @@ async function verifyVolunteer(
// Update the user's active status
try {
await volunteerModel.updateVolunteer(volunteer_id, {
active: 1,
});
active: true,
} as VolunteerDB);

return res.status(200).json({
message: "User verified successfully",
});
} catch (error: any) {
return res.status(500).json({
error: error.message,
});
}
return res.status(error.status ?? 500).json({
error: error.message
});
}
}

export { getUnverifiedVolunteers, verifyVolunteer };
Loading

0 comments on commit b9f45d7

Please sign in to comment.