import Knex from "knex"; import { exportDB, importDB, wipeDB } from "../db/Data"; import { EndpointError, EndpointHandler, handleErrorsInEndpoint } from "./types"; import * as api from '../../client/src/api/api'; import { DBExportEndpoint } from "../../client/src/api/api"; export const DBExport: EndpointHandler = async (req: any, res: any, knex: Knex) => { try { let db = await exportDB(req.user.id, knex); res.status(200); res.setHeader('Content-disposition', 'attachment; filename= mudbase.json'); res.setHeader('Content-type', 'application/json'); res.send( JSON.stringify(db, null, 2) ); } catch (e) { handleErrorsInEndpoint(e) } } export const DBImport: EndpointHandler = async (req: any, res: any, knex: Knex) => { if (!api.checkDBImportRequest(req.body)) { const e: EndpointError = { name: "EndpointError", message: 'Invalid DBImport request', httpStatus: 400 }; throw e; } const reqObject: api.DBImportRequest = req.body; const { id: userId } = req.user; console.log("User ", userId, ": Import DB "); try { let mappings = await importDB(userId, reqObject, knex) res.status(200).send(mappings); } catch (e) { handleErrorsInEndpoint(e); } } export const DBWipe: EndpointHandler = async (req: any, res: any, knex: Knex) => { console.log("User ", req.user.id, ": Wipe DB"); try { await wipeDB(req.user.id, knex); res.status(200).send(); } catch (e) { handleErrorsInEndpoint(e) } } export const DataEndpoints: [ string, string, boolean, EndpointHandler ][] = [ [ api.DBExportEndpoint, 'get', true, DBExport ], [ api.DBImportEndpoint, 'post', true, DBImport ], [ api.DBWipeEndpoint, 'post', true, DBWipe ], ];