Exporting database as json

This commit is contained in:
anton 2024-10-17 17:44:52 +02:00
parent 5503749705
commit 36d47b0d12
4 changed files with 83 additions and 2 deletions

View file

@ -0,0 +1,64 @@
import { Request, Response } from "express";
import MemberService from "../../service/memberService";
import AwardService from "../../service/awardService";
import CommunicationTypeService from "../../service/communicationTypeService";
import ExecutivePositionService from "../../service/executivePositionService";
import crypto from "crypto";
import { type BinaryLike } from "node:crypto";
/**
* @description get all data stored in the database as a single json object
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function getDatabaseExport(req: Request, res: Response): Promise<any> {
const { secret } = req.body;
try {
if (!secret) {
res.status(400).send('Empty encryption key');
return;
}
const iv = await new Promise((resolve, reject) => {
// create initialization vector
crypto.randomBytes(16, (err, iv) => {
if (err) {
reject(err);
return;
}
resolve(iv);
});
});
let data: any = {};
console.log('Exporting awards...');
data.awards = await AwardService.getAll();
console.log(`Exported ${data.awards.length} awards`);
console.log('Exporting communication types...');
data.communicationTypes = await CommunicationTypeService.getAll();
console.log(`Exported ${data.communicationTypes.length} communicationTypes`);
console.log('Exporting executivePositions...');
data.executivePositions = await ExecutivePositionService.getAll();
console.log(`Exported ${data.executivePositions.length} executivePositions`);
console.log('Exporting members...');
data.members = await MemberService.getAll();
console.log(`Exported ${data.members.length} members`);
console.log(`Encrypting data...`);
// encrypt data
const dataStr: string = JSON.stringify(data);
let base64Key = crypto.createHash('sha256').update(String(secret)).digest('base64');
const key = Buffer.from(base64Key, 'base64')
let cipher = crypto.createCipheriv("aes-256-gcm", key, iv as BinaryLike);
const encryptedData: string = cipher.update(dataStr, 'utf8', 'hex') + cipher.final('hex');
res.json({
data: encryptedData,
});
}
catch(ex) {
console.log(ex);
res.status(500).send(ex.message ? ex.message : 'Exception!');
}
}

View file

@ -0,0 +1,11 @@
import express, { Request, Response } from "express";
import { getDatabaseExport } from "../../controller/admin/exportController";
const router = express.Router({ mergeParams: true });
router.post("/", async (req: Request, res: Response) => {
await getDatabaseExport(req, res);
});
export default router;

View file

@ -12,6 +12,8 @@ import member from "./member";
import role from "./role"; import role from "./role";
import user from "./user"; import user from "./user";
import database from "./database";
var router = express.Router({ mergeParams: true }); var router = express.Router({ mergeParams: true });
router.use("/award", PermissionHelper.passCheckMiddleware("read", "settings", "award"), award); router.use("/award", PermissionHelper.passCheckMiddleware("read", "settings", "award"), award);
@ -37,4 +39,6 @@ router.use("/member", PermissionHelper.passCheckMiddleware("read", "club", "memb
router.use("/role", PermissionHelper.passCheckMiddleware("read", "user", "role"), role); router.use("/role", PermissionHelper.passCheckMiddleware("read", "user", "role"), role);
router.use("/user", PermissionHelper.passCheckMiddleware("read", "user", "user"), user); router.use("/user", PermissionHelper.passCheckMiddleware("read", "user", "user"), user);
router.use("/database", PermissionHelper.passCheckMiddleware("read", "settings", "database"), database);
export default router; export default router;

View file

@ -11,7 +11,8 @@ export type PermissionModule =
| "communication" | "communication"
| "membership_status" | "membership_status"
| "user" | "user"
| "role"; | "role"
| "database";
export type PermissionType = "read" | "create" | "update" | "delete"; export type PermissionType = "read" | "create" | "update" | "delete";
@ -47,10 +48,11 @@ export const permissionModules: Array<PermissionModule> = [
"membership_status", "membership_status",
"user", "user",
"role", "role",
"database",
]; ];
export const permissionTypes: Array<PermissionType> = ["read", "create", "update", "delete"]; export const permissionTypes: Array<PermissionType> = ["read", "create", "update", "delete"];
export const sectionsAndModules: SectionsAndModulesObject = { export const sectionsAndModules: SectionsAndModulesObject = {
club: ["member", "calendar", "newsletter", "protocoll"], club: ["member", "calendar", "newsletter", "protocoll"],
settings: ["qualification", "award", "executive_position", "communication", "membership_status"], settings: ["qualification", "award", "executive_position", "communication", "membership_status", "database"],
user: ["user", "role"], user: ["user", "role"],
}; };