Skip to content

Commit

Permalink
Move to node scheduler and finish refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ngoerlitz committed Mar 8, 2024
1 parent 50022cf commit d39344c
Show file tree
Hide file tree
Showing 35 changed files with 683 additions and 461 deletions.
2 changes: 0 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ RUN npm install --quiet --unsafe-perm --no-progress --no-audit --include=dev
COPY . .

# Init cron
ADD misc/crontab.txt /crontab.txt
ADD entry.sh /entry.sh
RUN chmod 755 /entry.sh
RUN /usr/bin/crontab /crontab.txt

RUN npm run build

Expand Down
2 changes: 1 addition & 1 deletion db/migrations/20221115171262-create-jobs-table.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { DataType } = require("sequelize-typescript");

const jobStatusEnum = ["queued", "running", "completed"];
const jobStatusEnum = ["queued", "running", "failed", "completed"];
const jobTypeEnum = ["email"];

const DataModelAttributes = {
Expand Down
5 changes: 5 additions & 0 deletions db/migrations/20221115171262-create-syslog-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ const DataModelAttributes = {
},
user_id: {
type: DataType.STRING,
allowNull: true,
},
path: {
type: DataType.STRING,
allowNull: true,
},
method: {
type: DataType.STRING(10),
allowNull: true,
},
remote_addr: {
type: DataType.STRING,
allowNull: true,
},
message: {
type: DataType.TEXT,
allowNull: true,
},
createdAt: DataType.DATE,
updatedAt: DataType.DATE,
Expand Down
3 changes: 0 additions & 3 deletions entry.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#!/bin/sh

# start cron
/usr/sbin/crond -l 8

# Run Node
cd /opt/trainingcenter_backend && npm run run
2 changes: 0 additions & 2 deletions misc/crontab.txt

This file was deleted.

96 changes: 58 additions & 38 deletions misc/mail-templates/message.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,68 @@
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>VATGER Trainingcenter</title>

<style>
.outer {
box-shadow: 4px 3px 8px 0 #dddddd;
border: 1px solid #dfdfdf;
border-radius: 5px;
margin-left: auto;
margin-right: auto;
}

.container-content {
padding: 0 2rem 1rem 2rem;
}

.image-container {
padding: 10px 0;
background-color: #2f55d4;
border-radius: 5px 5px 0 0;
}

footer {
background-color: #f8f9fc;
color: #a9a9a9;
text-align: center;
font-size: 0.8rem;
padding: 10px;
border-radius: 0 0 5px 5px;
}
</style>
</head>

<body style="font-family: Inter, sans-serif; font-size: 15px; font-weight: 400; justify-content: center">
<!-- Hero Start -->
<div style="margin-top: 50px;">
<table style="font-family: Inter, sans-serif; font-size: 15px; font-weight: 400; max-width: 600px; border: none; margin: 0 auto; border-radius: 6px; overflow: hidden; background-color: #fff; box-shadow: 0 0 3px rgba(60, 72, 88, 0.15);">
<thead>
<tr style="background-color: #2f55d4; padding: 3px 0; line-height: 68px; text-align: center; color: #fff; font-size: 24px; font-weight: 700; letter-spacing: 1px;">
<th scope="col">
<img src="https://cdn.vatsim-germany.org/img/vacc_logo_white.png" height="48" alt="VATSIM Germany" style="margin-bottom: 10px; vertical-align: middle">
</th>
</tr>
</thead>

<tbody>
<tr>
<td style="padding: 24px 24px 15px; color: #8492a6;">
Hallo {{ name }},<br/>
{{ message }}
</td>
</tr>

<tr>
<td style="padding: 24px 24px 0; color: #8492a6;">
Solltest du noch Fragen haben wende dich bitte an <a href="mailto:support@vatger.de">support@vatger.de</a>.
</td>
</tr>

<tr>
<td style="padding: 15px 24px 15px; color: #8492a6;">
VATSIM Germany Trainingssystem
</td>
</tr>

<tr>
<td style="padding: 16px 8px; color: #8492a6; background-color: #f8f9fc; text-align: center; font-size: 0.7rem">
Diese automatische Nachricht wurde am {{date_now}} generiert.<br/>
Bitte antworte nicht auf diese E-Mail.
</td>
</tr>
</tbody>
</table>
<div class="outer" style="max-width: 800px;">
<div class="image-container">
<img src="https://cdn.vatsim-germany.org/img/vacc_logo_white.png" height="40" alt="VATSIM Germany" style="margin-bottom: 10px; vertical-align: middle; margin-left: auto; margin-right: auto; display: block">
</div>

<div class="container">
<div class="container-content">
<h4>Hallo {{ name }},</h4>
<p>{{ message_de }}</p>

<p style="margin-top: 30px;">Solltest du noch Fragen haben wende dich bitte an einen deiner Mentoren oder an <a href="mailto:support@vatger.de">support@vatger.de</a>.</p>

<div style="width: 100%; height: 1px; background-color: lightgray"></div>

<h4>Hello {{ name }},</h4>
<p>{{ message_en }}</p>

<p style="margin-top: 30px">Should you have further questions, please contact a mentor using <a href="mailto:support@vatger.de">support@vatger.de</a>.</p>

<p style="margin-top: 40px">VATGER Trainingssystem</p>

</div>

<footer>
Diese Nachricht wurde automatisch am {{ date_now }} generiert.<br/>
Bitte antworte nicht auf diese E-Mail.
</footer>
</div>

</div>
<!-- Hero End -->
</body>
Expand Down
16 changes: 8 additions & 8 deletions misc/scripts/SendReminderMail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import DBConn from "./DBConn";
import dayjs from "dayjs";
import { QueryTypes } from "sequelize";
import { Config } from "../../src/core/Config";
import EmailHelper from "../../src/utility/helper/EmailHelper";
import EmailHelper from "../../src/libraries/EmailLibrary";

DBConn.connectToDatabase().then(async seq => {
if (seq == null) return;
Expand Down Expand Up @@ -36,13 +36,13 @@ DBConn.connectToDatabase().then(async seq => {
date_now: dayjs.utc().format(Config.DATETIME_FORMAT),
};

await EmailHelper.sendMail(
Config.APP_DEBUG ? Config.DEBUG_EMAIL : request.email,
"Weiteres Interesse an Trainings",
"reminder.html",
replacements,
true
);
// await EmailHelper.sendMail(
// Config.APP_DEBUG ? Config.DEBUG_EMAIL : request.email,
// "Weiteres Interesse an Trainings",
// "reminder.html",
// replacements,
// true
// );
}

await seq.close();
Expand Down
4 changes: 3 additions & 1 deletion src/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import { exceptionInterceptorMiddleware } from "./middlewares/ExceptionIntercept

const application: Express = express();

process.on("uncaughtException", (err, origin) => handleUncaughtException(err, origin));
if (Config.APP_DEBUG) {
process.on("uncaughtException", (err, origin) => handleUncaughtException(err, origin));
}

initializeApplication()
.then(() => {
Expand Down
20 changes: 16 additions & 4 deletions src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ import SoloAdminController from "./controllers/solo/SoloAdminController";
import UserEndorsementAdminController from "./controllers/user/UserEndorsementAdminController";
import CPTAdminController from "./controllers/cpt/CPTAdminController";
import { handleUpload } from "./libraries/upload/FileUploadLibrary";
import UserStatisticsController from "./controllers/user/UserStatisticsController";
import { Job } from "./models/Job";
import JobLibrary from "./libraries/JobLibrary";
import dayjs from "dayjs";
import { Config } from "./core/Config";

const routerGroup = (callback: (router: Router) => void) => {
const router = Router();
Expand All @@ -45,10 +50,10 @@ const routerGroup = (callback: (router: Router) => void) => {

export const router = Router();

router.post("/test", (req, res) => {
handleUpload(req);
router.post("/test", async (req, res) => {
console.log(req.body);

res.sendStatus(200);
res.sendStatus(400);
});

router.use(
Expand Down Expand Up @@ -77,6 +82,13 @@ router.use(

r.get("/gdpr", GDPRController.getData);

r.use(
"/statistics",
routerGroup((r: Router) => {
r.get("/rating-times", UserStatisticsController.getUserRatingTimes);
})
);

r.use(
"/notification",
routerGroup((r: Router) => {
Expand Down Expand Up @@ -168,7 +180,7 @@ router.use(
r.get("/data/basic", UserInformationAdminController.getBasicUserDataByID);
r.get("/data/sensitive", UserInformationAdminController.getSensitiveUserDataByID);

r.put("/note", UserNoteAdminController.createUserNote);
r.post("/note", UserNoteAdminController.createUserNote);
r.get("/notes", UserNoteAdminController.getGeneralUserNotes);
r.get("/notes/course", UserNoteAdminController.getNotesByCourseID);

Expand Down
9 changes: 6 additions & 3 deletions src/controllers/course/CourseAdministrationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { TrainingType } from "../../models/TrainingType";
import { sequelize } from "../../core/Sequelize";
import { TrainingTypesBelongsToCourses } from "../../models/through/TrainingTypesBelongsToCourses";
import { generateUUID } from "../../utility/UUID";
import Validator, { ValidationTypeEnum } from "../../utility/Validator";

// TODO: Move all course related things into this controller

Expand Down Expand Up @@ -305,11 +306,13 @@ async function addMentorGroupToCourse(request: Request, response: Response) {
* @param response
*/
async function removeMentorGroupFromCourse(request: Request, response: Response) {
const body = request.body as { course_uuid: string; mentor_group_id: number };
const params = request.params as { course_uuid: string };
const body = request.body as { mentor_group_id: number };

const validation = _CourseAdministrationValidator.validateRemoveMentorGroupRequest(body);
Validator.validate(params, { course_uuid: [ValidationTypeEnum.NON_NULL] });
Validator.validate(body, { mentor_group_id: [ValidationTypeEnum.NON_NULL, ValidationTypeEnum.NUMBER] });

const courseID = await Course.getIDFromUUID(body.course_uuid);
const courseID = await Course.getIDFromUUID(params.course_uuid);
if (courseID == -1) {
response.sendStatus(HttpStatusCode.BadRequest);
return;
Expand Down
27 changes: 10 additions & 17 deletions src/controllers/course/CourseInformationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,22 @@ async function getInformationByUUID(request: Request, response: Response) {
* @param response
*/
async function getUserCourseInformationByUUID(request: Request, response: Response) {
const reqUser: User = response.locals.user;
const course_uuid: string = request.query.uuid?.toString() ?? "";
const user: User = response.locals.user;
const query = request.query as { uuid: string };

if (!(await reqUser.isMemberOfCourse(course_uuid))) {
if (!(await user.isMemberOfCourse(query.uuid))) {
response.status(HttpStatusCode.Forbidden).send({ message: "You are not enrolled in this course" });
return;
}

const user: User | null = await User.findOne({
where: {
id: reqUser.id,
},
include: [
{
association: User.associations.courses,
where: {
uuid: course_uuid,
},
},
],
});
const courses = await user.getCourses();
const course = courses.find(c => c.uuid == query.uuid);

response.send(user?.courses ?? []);
if (course) {
response.send(course);
} else {
response.sendStatus(HttpStatusCode.BadRequest);
}
}

/**
Expand Down
Loading

0 comments on commit d39344c

Please sign in to comment.