split services / delete dish / mail init

This commit is contained in:
2020-08-28 18:54:30 +02:00
parent 3674e4ce3c
commit f304463b46
17 changed files with 507 additions and 204 deletions

View File

@@ -1,5 +1,19 @@
import express from "express";
import * as services from "../services/services.js";
import { changeUserPass, fetchUser } from "../services/databaseServices.js";
import {
composeNewContact,
createUser,
prepareSafeUser,
} from "../services/dataPrepServices.js";
import {
newError,
handleError,
checkPassword,
generateAuthToken,
checkEmailTaken,
validateUserToken,
hashPass,
} from "../services/services.js";
import * as config from "../config/index.js";
import AgileCRMManager from "agile_crm";
const { CRM_USER, CRM_EMAIL, CRM_KEY } = config;
@@ -11,29 +25,56 @@ var agileAPI = new AgileCRMManager(CRM_USER, CRM_KEY, CRM_EMAIL);
router.post("/login", async (req, res) => {
try {
if (!req.body.password || !req.body.email) {
throw services.newError("No input data", 204);
throw newError("No input data", 204);
}
const user = await services.fetchUser(req.body.email);
await services.checkPassword(req.body.password, user.password);
const safeUser = services.prepareSafeUser(user);
var token = services.generateAuthToken(safeUser);
const user = await fetchUser(req.body.email);
await checkPassword(req.body.password, user.password);
const safeUser = prepareSafeUser(user);
var token = generateAuthToken(safeUser);
res.header("x-auth-token", token).status(202).send(safeUser);
} catch (error) {
services.handleError(error, res);
handleError(error, res);
}
});
// REGISTER
router.post("/register", async (req, res) => {
try {
await services.checkEmailTaken(req.body.email);
const user = await services.createUser(req);
await checkEmailTaken(req.body.email);
const user = await createUser(req);
await user.save();
const contact = services.composeNewContact(user);
const contact = composeNewContact(user);
agileAPI.contactAPI.add(contact, null, null);
res.sendStatus(201);
} catch (e) {
services.handleError(e, res);
handleError(e, res);
}
});
// CHANGE PASSWORD
router.post("/changepass", async (req, res) => {
try {
if (!req.body.password || !req.body.email || !req.body.newPass) {
throw newError("No input data", 204);
}
const token = req.headers["x-auth-token"];
validateUserToken(token);
const user = await fetchUser(req.body.email);
await checkPassword(req.body.password, user.password);
const newPassword = await hashPass(req.body.newPass);
await changeUserPass(user._id, newPassword);
res.status(200).send("Password changed");
} catch (error) {
handleError(error, res);
}
});
// RESET PASSWORD
router.post("/resetpassword", (req, res) => {
try {
//
} catch (error) {
handleError(error, res);
}
});