Skip to content

Commit

Permalink
Small changes to GDPR Downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
ngoerlitz committed Mar 15, 2024
1 parent c5590b8 commit 8569767
Show file tree
Hide file tree
Showing 14 changed files with 190 additions and 102 deletions.
6 changes: 3 additions & 3 deletions db/migrations/20221115171243-create-user-data-table.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {DataType} from "sequelize-typescript";
import {QueryInterface} from "sequelize";
import { DataType } from "sequelize-typescript";
import { QueryInterface } from "sequelize";

export const USER_DATA_TABLE_NAME = "user_data"
export const USER_DATA_TABLE_NAME = "user_data";

export const USER_DATA_TABLE_ATTRIBUTES = {
user_id: {
Expand Down
2 changes: 1 addition & 1 deletion db/seeders/20221121101837-PermissionSeeder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {QueryInterface} from "sequelize";
import { QueryInterface } from "sequelize";

const now = new Date();

Expand Down
38 changes: 19 additions & 19 deletions src/controllers/training-session/TrainingSessionAdminController.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import {NextFunction, Request, Response} from "express";
import {User} from "../../models/User";
import { NextFunction, Request, Response } from "express";
import { User } from "../../models/User";
import _TrainingSessionAdminValidator from "./_TrainingSessionAdminValidator";
import {Course} from "../../models/Course";
import {TrainingSession} from "../../models/TrainingSession";
import {generateUUID} from "../../utility/UUID";
import { Course } from "../../models/Course";
import { TrainingSession } from "../../models/TrainingSession";
import { generateUUID } from "../../utility/UUID";
import dayjs from "dayjs";
import {TrainingRequest} from "../../models/TrainingRequest";
import {TrainingSessionBelongsToUsers} from "../../models/through/TrainingSessionBelongsToUsers";
import {HttpStatusCode} from "axios";
import { TrainingRequest } from "../../models/TrainingRequest";
import { TrainingSessionBelongsToUsers } from "../../models/through/TrainingSessionBelongsToUsers";
import { HttpStatusCode } from "axios";
import NotificationLibrary from "../../libraries/notification/NotificationLibrary";
import {Config} from "../../core/Config";
import {TrainingType} from "../../models/TrainingType";
import {Op} from "sequelize";
import {TrainingLog} from "../../models/TrainingLog";
import {UsersBelongsToCourses} from "../../models/through/UsersBelongsToCourses";
import {sequelize} from "../../core/Sequelize";
import {MentorGroup} from "../../models/MentorGroup";
import JobLibrary, {JobTypeEnum} from "../../libraries/JobLibrary";
import Validator, {ValidationTypeEnum} from "../../utility/Validator";
import Logger, {LogLevels} from "../../utility/Logger";
import { Config } from "../../core/Config";
import { TrainingType } from "../../models/TrainingType";
import { Op } from "sequelize";
import { TrainingLog } from "../../models/TrainingLog";
import { UsersBelongsToCourses } from "../../models/through/UsersBelongsToCourses";
import { sequelize } from "../../core/Sequelize";
import { MentorGroup } from "../../models/MentorGroup";
import JobLibrary, { JobTypeEnum } from "../../libraries/JobLibrary";
import Validator, { ValidationTypeEnum } from "../../utility/Validator";
import Logger, { LogLevels } from "../../utility/Logger";

/**
* Creates a new training session with one user and one mentor
Expand All @@ -38,7 +38,7 @@ async function createTrainingSession(request: Request, response: Response) {

Validator.validate(body, {
training_type_id: [ValidationTypeEnum.NON_NULL, ValidationTypeEnum.NUMBER],
user_ids: [ValidationTypeEnum.VALID_JSON] // Parses to number[]
user_ids: [ValidationTypeEnum.VALID_JSON], // Parses to number[]
});

// 1. Find out which of these users is actually enrolled in the course. To do this, query the course and it's members, and check against the array of user_ids. Create a new actual array with only those people
Expand Down
86 changes: 84 additions & 2 deletions src/controllers/user/GDPRController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NextFunction, Request, Response } from "express";
import { User } from "../../models/User";
import { EndorsementGroup } from "../../models/EndorsementGroup";

async function getData(request: Request, response: Response, next: NextFunction) {
try {
Expand All @@ -10,10 +11,91 @@ async function getData(request: Request, response: Response, next: NextFunction)
where: {
id: user.id,
},
include: [
User.associations.user_data,
User.associations.user_settings,
{
association: User.associations.user_solo,
attributes: {
exclude: ["id", "created_by", "solo_used", "vateud_solo_id", "createdAt", "updatedAt"],
},
},
{
association: User.associations.mentor_groups,
attributes: {
exclude: ["id", "createdAt", "updatedAt"],
},
through: {
attributes: ["group_admin", "can_manage_course"],
},
},
{
association: User.associations.mentor_sessions,
attributes: {
exclude: ["id", "uuid", "cpt_examiner_id", "cpt_atsim_passed", "training_station_id", "training_type_id", "createdAt", "updatedAt"],
},
},
{
association: User.associations.endorsement_groups,
attributes: {
exclude: ["id", "createdAt", "updatedAt"],
},
through: {
attributes: [],
},
include: [
{
association: EndorsementGroup.associations.stations,
attributes: ["callsign"],
through: {
attributes: [],
},
},
],
},
{
association: User.associations.training_sessions,
attributes: ["completed", "date", "mentor_id"],
through: {
attributes: [],
},
},
{
association: User.associations.training_requests,
attributes: ["comment", "status", "expires", "createdAt"],
},
{
association: User.associations.training_logs,
attributes: ["content", "author_id", "createdAt"],
through: {
attributes: [],
},
},
{
association: User.associations.courses,
attributes: ["name"],
through: {
attributes: ["completed", "createdAt"],
},
},
{
association: User.associations.fast_track_requests,
attributes: ["rating", "status", "createdAt"],
},
{
association: User.associations.roles,
attributes: ["id"],
through: {
attributes: [],
},
},
{
association: User.associations.user_notes,
attributes: ["author_id", "content", "createdAt"],
},
],
});

console.log("USER: ", foundUser);

response.send(foundUser);
} catch (e) {
next(e);
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/user/UserStatisticsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ async function getUserTrainingSessionCount(request: Request, response: Response,
const sessions = await user.getTrainingSessions();
const completedSession = sessions.filter(s => s.completed);

response.send({count: sessions.length, completedCount: completedSession.length});
response.send({ count: sessions.length, completedCount: completedSession.length });
} catch (e) {
next(e);
}
}

export default {
getUserRatingTimes,
getUserTrainingSessionCount
getUserTrainingSessionCount,
};
2 changes: 1 addition & 1 deletion src/core/tasks/SendEmailTask.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import JobLibrary from "../../libraries/JobLibrary";
import EmailLibrary, {EMailPayload, EMailTypes} from "../../libraries/EmailLibrary";
import EmailLibrary, { EMailPayload, EMailTypes } from "../../libraries/EmailLibrary";
import dayjs from "dayjs";

// This file maps between the type of the email and the corresponding html file used to actually render the template
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/EmailLibrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export type EMailPayload = {
recipient: string;
subject: string;
replacements:
Record<"message", { message_de: string; message_en: string; name: string }> |
Record<"reminder", { name: string; expiry_date: string; link: string }>;
| Record<"message", { message_de: string; message_en: string; name: string }>
| Record<"reminder", { name: string; expiry_date: string; link: string }>;
};

async function sendMail(options: SendMailOptions, nonPooled = true) {
Expand Down
6 changes: 3 additions & 3 deletions src/libraries/JobLibrary.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Job } from "../models/Job";
import { generateUUID } from "../utility/UUID";
import {EMailPayload} from "./EmailLibrary";
import {VateudCorePayload} from "./vateud/VateudCoreLibraryTypes";
import { EMailPayload } from "./EmailLibrary";
import { VateudCorePayload } from "./vateud/VateudCoreLibraryTypes";

export enum JobTypeEnum {
EMAIL = "email",
VATEUD_CORE = "vateud_core"
VATEUD_CORE = "vateud_core",
}

async function scheduleJob(type: JobTypeEnum, payload: EMailPayload | VateudCorePayload) {
Expand Down
53 changes: 28 additions & 25 deletions src/libraries/vateud/VateudCoreLibrary.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import axios, {Method} from "axios";
import JobLibrary, {JobTypeEnum} from "../JobLibrary";
import axios, { Method } from "axios";
import JobLibrary, { JobTypeEnum } from "../JobLibrary";
import {
VateudCoreSoloCreateResponseT,
VateudCoreSoloCreateT,
VateudCoreSoloRemoveResponseT,
VateudCoreSoloRemoveT,
VateudCoreTypeEnum
VateudCoreTypeEnum,
} from "./VateudCoreLibraryTypes";
import {Config} from "../../core/Config";
import {UserSolo} from "../../models/UserSolo";
import Logger, {LogLevels} from "../../utility/Logger";
import { Config } from "../../core/Config";
import { UserSolo } from "../../models/UserSolo";
import Logger, { LogLevels } from "../../utility/Logger";

type SendT = {
method: Method;
endpoint: string;
data?: any;
}
};

/**
* Sends the actual request to VATEUD Core
Expand All @@ -30,11 +30,11 @@ async function _send<T>(props: SendT): Promise<T | undefined> {
try {
const res = await axios({
headers: {
'x-api-key': Config.VATEUD_CORE_CONFIG.API_KEY,
"x-api-key": Config.VATEUD_CORE_CONFIG.API_KEY,
},
url: `${Config.URI_CONFIG.VATEUD_API_BASE}/${props.endpoint}`,
method: props.method,
data: props.data
data: props.data,
});

return res.data as T;
Expand All @@ -54,7 +54,7 @@ async function createSolo(soloInfo: VateudCoreSoloCreateT) {
const res = await _send<VateudCoreSoloCreateResponseT>({
endpoint: "/solo",
method: "post",
data: soloInfo.post_data
data: soloInfo.post_data,
});
if (!res) {
// If the request fails, we schedule a job to attempt it additional times.
Expand All @@ -67,20 +67,23 @@ async function createSolo(soloInfo: VateudCoreSoloCreateT) {
user_id: soloInfo.post_data.user_id,
position: soloInfo.post_data.position,
instructor_cid: soloInfo.post_data.instructor_cid,
expire_at: soloInfo.post_data.expires_at
}
}
expire_at: soloInfo.post_data.expires_at,
},
},
});
return undefined;
}

await UserSolo.update({
vateud_solo_id: res.data.id
}, {
where: {
id: soloInfo.local_solo_id
await UserSolo.update(
{
vateud_solo_id: res.data.id,
},
{
where: {
id: soloInfo.local_solo_id,
},
}
});
);

return res;
}
Expand All @@ -93,7 +96,7 @@ async function createSolo(soloInfo: VateudCoreSoloCreateT) {
async function removeSolo(soloInfo: VateudCoreSoloRemoveT) {
const res = await _send<VateudCoreSoloRemoveResponseT>({
endpoint: `/solo/${soloInfo.vateud_solo_id}`,
method: "delete"
method: "delete",
});
if (!res) {
await JobLibrary.scheduleJob(JobTypeEnum.VATEUD_CORE, {
Expand All @@ -102,14 +105,14 @@ async function removeSolo(soloInfo: VateudCoreSoloRemoveT) {
data: {
solo_remove: {
local_solo_id: soloInfo.local_solo_id,
vateud_solo_id: soloInfo.vateud_solo_id
}
}
vateud_solo_id: soloInfo.vateud_solo_id,
},
},
});
}
}

export default {
createSolo,
removeSolo
}
removeSolo,
};
22 changes: 11 additions & 11 deletions src/libraries/vateud/VateudCoreLibraryTypes.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import {Method} from "axios";
import { Method } from "axios";

export enum VateudCoreTypeEnum {
SOLO_CREATE = "solo_create",
SOLO_REMOVE = "solo_remove"
SOLO_REMOVE = "solo_remove",
}

export type VateudCorePayload = {
type: VateudCoreTypeEnum;
method: Method;
data:
Record<"solo_create", { local_solo_id: number; user_id: number; position: string; instructor_cid: number; expire_at: string }> |
Record<"solo_remove", { local_solo_id: number; vateud_solo_id: number }>
}
| Record<"solo_create", { local_solo_id: number; user_id: number; position: string; instructor_cid: number; expire_at: string }>
| Record<"solo_remove", { local_solo_id: number; vateud_solo_id: number }>;
};

export type VateudCoreSoloCreateT = {
local_solo_id: number;
Expand All @@ -21,8 +21,8 @@ export type VateudCoreSoloCreateT = {
instructor_cid: number;
starts_at: string;
expires_at: string;
}
}
};
};

export type VateudCoreSoloCreateResponseT = {
success: boolean;
Expand All @@ -35,15 +35,15 @@ export type VateudCoreSoloCreateResponseT = {
facility: number;
created_at: string;
updated_at: string;
}
}
};
};

export type VateudCoreSoloRemoveT = {
local_solo_id: number;
vateud_solo_id: number;
}
};

export type VateudCoreSoloRemoveResponseT = {
success: boolean;
message: string;
}
};
Loading

0 comments on commit 8569767

Please sign in to comment.