Exporting database as json
This commit is contained in:
parent
5503749705
commit
36d47b0d12
4 changed files with 83 additions and 2 deletions
64
src/controller/admin/exportController.ts
Normal file
64
src/controller/admin/exportController.ts
Normal 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!');
|
||||
}
|
||||
}
|
11
src/routes/admin/database.ts
Normal file
11
src/routes/admin/database.ts
Normal 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;
|
|
@ -12,6 +12,8 @@ import member from "./member";
|
|||
import role from "./role";
|
||||
import user from "./user";
|
||||
|
||||
import database from "./database";
|
||||
|
||||
var router = express.Router({ mergeParams: true });
|
||||
|
||||
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("/user", PermissionHelper.passCheckMiddleware("read", "user", "user"), user);
|
||||
|
||||
router.use("/database", PermissionHelper.passCheckMiddleware("read", "settings", "database"), database);
|
||||
|
||||
export default router;
|
||||
|
|
|
@ -11,7 +11,8 @@ export type PermissionModule =
|
|||
| "communication"
|
||||
| "membership_status"
|
||||
| "user"
|
||||
| "role";
|
||||
| "role"
|
||||
| "database";
|
||||
|
||||
export type PermissionType = "read" | "create" | "update" | "delete";
|
||||
|
||||
|
@ -47,10 +48,11 @@ export const permissionModules: Array<PermissionModule> = [
|
|||
"membership_status",
|
||||
"user",
|
||||
"role",
|
||||
"database",
|
||||
];
|
||||
export const permissionTypes: Array<PermissionType> = ["read", "create", "update", "delete"];
|
||||
export const sectionsAndModules: SectionsAndModulesObject = {
|
||||
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"],
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue