-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f264604
commit 84b8f40
Showing
4 changed files
with
112 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,59 @@ | ||
import express from "express"; | ||
import dotenv from "dotenv"; | ||
import mongoose from "mongoose"; | ||
import express from 'express'; | ||
import dotenv from 'dotenv'; | ||
import mongoose from 'mongoose'; | ||
|
||
import eventRoute from "./event/event-route"; | ||
import userRoute from "./user/user-route"; | ||
import registerRoute from "./register/register-route"; | ||
import paymentRoute from "./payment/payment-route"; | ||
import webhookRoute from "./webhook/webhook-route"; | ||
import cors from "cors"; | ||
import { verifyToken } from "./middleware/verifyToken"; | ||
import { verifyAdmin } from "./middleware/verifyAdmin"; | ||
import firebase_admin from "firebase-admin"; | ||
import eventRoute from './event/event-route'; | ||
import userRoute from './user/user-route'; | ||
import registerRoute from './register/register-route'; | ||
import paymentRoute from './payment/payment-route'; | ||
import webhookRoute from './webhook/webhook-route'; | ||
import cors from 'cors'; | ||
import { verifyToken } from './middleware/verifyToken'; | ||
import { verifyAdmin } from './middleware/verifyAdmin'; | ||
import firebase_admin from 'firebase-admin'; | ||
|
||
const conf = dotenv.config(); | ||
if (conf.error) { | ||
throw conf.error; | ||
} | ||
|
||
const app = express(); | ||
const port = process.env.PORT || 5000; | ||
const dbURL = `${process.env.DB_URL}`; | ||
const firebaseConfig = { | ||
apiKey: process.env.FIREBASE_API_KEY, | ||
authDomain: process.env.FIREBASE_AUTH_DOMAIN, | ||
databaseURL: process.env.FIREBASE_DATABASE_URL, | ||
projectId: process.env.FIREBASE_PROJECT_ID, | ||
storageBucket: process.env.FIREBASE_STORAGE_BUCKET, | ||
messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID, | ||
appId: process.env.FIREBASE_APP_ID, | ||
measurementId: process.env.FIREBASE_MEASUREMENT_ID, | ||
apiKey: process.env.FIREBASE_API_KEY, | ||
authDomain: process.env.FIREBASE_AUTH_DOMAIN, | ||
databaseURL: process.env.FIREBASE_DATABASE_URL, | ||
projectId: process.env.FIREBASE_PROJECT_ID, | ||
storageBucket: process.env.FIREBASE_STORAGE_BUCKET, | ||
messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID, | ||
appId: process.env.FIREBASE_APP_ID, | ||
measurementId: process.env.FIREBASE_MEASUREMENT_ID, | ||
}; | ||
firebase_admin.initializeApp(firebaseConfig); | ||
app.use(cors()); | ||
app.use((req, res, next) => { | ||
if (req.originalUrl.startsWith("/stripe_webhooks")) { | ||
// we need raw instead of json so that we can use webhook signing | ||
next(); | ||
} else { | ||
express.json()(req, res, next); | ||
} | ||
if (req.originalUrl.startsWith('/stripe_webhooks')) { | ||
// we need raw instead of json so that we can use webhook signing | ||
next(); | ||
} else { | ||
express.json()(req, res, next); | ||
} | ||
}); | ||
|
||
app.use(verifyToken); | ||
app.use(verifyAdmin); | ||
|
||
app.use("/events", eventRoute); | ||
app.use("/users", userRoute); | ||
app.use("/register", registerRoute); | ||
app.use("/payment", paymentRoute); | ||
app.use("/stripe_webhooks", webhookRoute); | ||
app.use('/events', eventRoute); | ||
app.use('/users', userRoute); | ||
app.use('/register', registerRoute); | ||
app.use('/payment', paymentRoute); | ||
app.use('/stripe_webhooks', webhookRoute); | ||
|
||
mongoose | ||
.connect(dbURL) | ||
.then(() => { | ||
app.listen(port, () => { | ||
console.log( | ||
`Connected to database. Server is running on port: ${port}` | ||
); | ||
}); | ||
}) | ||
.catch((error) => { | ||
console.log(error); | ||
.connect(dbURL) | ||
.then(() => { | ||
app.listen(port, () => { | ||
console.log(`Connected to database. Server is running on port: ${port}`); | ||
}); | ||
}) | ||
.catch((error) => { | ||
console.log(error); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,70 @@ | ||
import sgMail from "@sendgrid/mail"; | ||
import dotenv from "dotenv"; | ||
import fs from "fs"; | ||
import path from "path"; | ||
import sgMail from '@sendgrid/mail'; | ||
import dotenv from 'dotenv'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
const conf = dotenv.config(); | ||
if (conf.error) { | ||
throw conf.error; | ||
} | ||
|
||
const apiKey = process.env.SENDGRID_API_KEY; | ||
const fromEmail = process.env.SENDGRID_FROM_EMAIL; | ||
if (apiKey == null) { | ||
throw new Error("SENDGRID_API_KEY is not defined in your environment."); | ||
throw new Error('SENDGRID_API_KEY is not defined in your environment.'); | ||
} | ||
sgMail.setApiKey(apiKey); | ||
export default class EmailService { | ||
public static async _sendEmail( | ||
to: string, | ||
subject: string, | ||
name: string, | ||
title: string, | ||
message: string | ||
) { | ||
if (fromEmail == null) { | ||
throw new Error( | ||
"SENDGRID_FROM_EMAIL is not defined in your environment." | ||
); | ||
} | ||
// FIXME: This is a hacky way to use templates | ||
// setup a template_id in sendgrid and use that instead if possible | ||
const templatePath = path.join( | ||
__dirname, | ||
"./email-templates", | ||
`send-confirmation.html` | ||
); | ||
const html = fs | ||
.readFileSync(templatePath, "utf8") | ||
.replace(/{{name}}/g, name) | ||
.replace(/{{title}}/g, title) | ||
.replace(/{{message}}/g, message); | ||
public static async _sendEmail( | ||
to: string, | ||
subject: string, | ||
name: string, | ||
title: string, | ||
message: string | ||
) { | ||
if (fromEmail == null) { | ||
throw new Error( | ||
'SENDGRID_FROM_EMAIL is not defined in your environment.' | ||
); | ||
} | ||
// FIXME: This is a hacky way to use templates | ||
// setup a template_id in sendgrid and use that instead if possible | ||
const templatePath = path.join( | ||
__dirname, | ||
'./email-templates', | ||
`send-confirmation.html` | ||
); | ||
const html = fs | ||
.readFileSync(templatePath, 'utf8') | ||
.replace(/{{name}}/g, name) | ||
.replace(/{{title}}/g, title) | ||
.replace(/{{message}}/g, message); | ||
|
||
console.log("finished loading file"); | ||
console.log('finished loading file'); | ||
|
||
const msg = { | ||
to, | ||
from: fromEmail, | ||
subject, | ||
html, | ||
}; | ||
const msg = { | ||
to, | ||
from: fromEmail, | ||
subject, | ||
html, | ||
}; | ||
|
||
await sgMail.send(msg); | ||
console.log("Email sent"); | ||
return; | ||
} | ||
await sgMail.send(msg); | ||
console.log('Email sent'); | ||
return; | ||
} | ||
|
||
public static async sendEventEmail( | ||
toEmail: string, | ||
name: string, | ||
eventName: string, | ||
eventDate: string, | ||
eventLocation: string, | ||
paymentMethod: string | ||
) { | ||
const subject = `ASPA UOA ${eventName} Registration`; | ||
const title = "ASPA UOA"; | ||
const message = `You have registered for ${eventName} on ${eventDate} at ${eventLocation} successfully! | ||
public static async sendEventEmail( | ||
toEmail: string, | ||
name: string, | ||
eventName: string, | ||
eventDate: string, | ||
eventLocation: string, | ||
paymentMethod: string | ||
) { | ||
const subject = `ASPA UOA ${eventName} Registration`; | ||
const title = 'ASPA UOA'; | ||
const message = `You have registered for ${eventName} on ${eventDate} at ${eventLocation} successfully! | ||
Your payment for this event was made with ${paymentMethod}. | ||
We look forward to seeing you there! | ||
`; | ||
await this._sendEmail(toEmail, subject, name, title, message); | ||
} | ||
await this._sendEmail(toEmail, subject, name, title, message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters