ff-admin-server/src/controller/setupController.ts

142 lines
4 KiB
TypeScript

import { Request, Response } from "express";
import SettingHelper from "../helpers/settingsHelper";
import MailHelper from "../helpers/mailHelper";
import InternalException from "../exceptions/internalException";
/**
* @description Service is currently not configured
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function isSetup(req: Request, res: Response): Promise<any> {
res.sendStatus(204);
}
/**
* @description set club identity
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function setClubIdentity(req: Request, res: Response): Promise<any> {
const name = req.body.name;
const imprint = req.body.imprint;
const privacy = req.body.privacy;
const website = req.body.website;
if (name) {
await SettingHelper.setSetting("club.name", name);
} else {
await SettingHelper.resetSetting("club.name");
}
if (imprint) {
await SettingHelper.setSetting("club.imprint", imprint);
} else {
await SettingHelper.resetSetting("club.imprint");
}
if (privacy) {
await SettingHelper.setSetting("club.privacy", privacy);
} else {
await SettingHelper.resetSetting("club.privacy");
}
if (website) {
await SettingHelper.setSetting("club.website", website);
} else {
await SettingHelper.resetSetting("club.website");
}
res.sendStatus(204);
}
/**
* @description set applucation icon and logo
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function uploadClubImages(req: Request, res: Response): Promise<any> {
if (req.files && !Array.isArray(req.files) && req.files.icon) {
await SettingHelper.setSetting("club.icon", "configured");
} else {
await SettingHelper.resetSetting("club.icon");
}
if (req.files && !Array.isArray(req.files) && req.files.logo) {
await SettingHelper.setSetting("club.logo", "configured");
} else {
await SettingHelper.resetSetting("club.logo");
}
res.sendStatus(204);
}
/**
* @description set app identity
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function setAppIdentity(req: Request, res: Response): Promise<any> {
const custom_login_message = req.body.custom_login_message;
const show_link_to_calendar = req.body.show_link_to_calendar;
if (custom_login_message) {
await SettingHelper.setSetting("app.custom_login_message", custom_login_message);
} else {
await SettingHelper.resetSetting("app.custom_login_message");
}
if (show_link_to_calendar == false || show_link_to_calendar == true) {
await SettingHelper.setSetting("app.show_link_to_calendar", show_link_to_calendar);
} else {
await SettingHelper.resetSetting("app.show_link_to_calendar");
}
res.sendStatus(204);
}
/**
* @description set app identity
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function setMailConfig(req: Request, res: Response): Promise<any> {
const mail = req.body.mail;
const username = req.body.username;
const password = req.body.password;
const host = req.body.host;
const port = req.body.port;
const secure = req.body.secure;
let checkMail = await MailHelper.checkMail(mail);
if (!checkMail) {
throw new InternalException("Mail is not valid");
}
let checkConfig = await MailHelper.verifyTransport({
user: username,
password,
host,
port,
secure,
});
if (!checkConfig) {
throw new InternalException("Config is not valid");
}
await SettingHelper.setSetting("mail.email", mail);
await SettingHelper.setSetting("mail.username", username);
await SettingHelper.setSetting("mail.password", password);
await SettingHelper.setSetting("mail.host", host);
await SettingHelper.setSetting("mail.port", port);
await SettingHelper.setSetting("mail.secure", secure);
res.sendStatus(204);
}