UNPKG

synt_backend

Version:

Synt light-weight node backend service

186 lines (165 loc) 4.25 kB
const express = require("express"); const router = express.Router(); const db = require("./../mysql/models/index"); const userHelper = require("./../helpers/user"); const notifier = require("./../helpers/notifier"); // routes router.post("/contact", postContact); router.get("/incident/:token", getIncident); router.post("/incident/comment", postIncidentComment); module.exports = router; async function postIncidentComment(req, res) { const { t } = req; const { message, user_name, token } = req.body; db.Incident.findOne({ where: { token }, }).then(async (Incident) => { if (Incident) { const IncidentComment = { UserId: null, IncidentId: Incident.id, message, user_name, }; if (!message || message === "") { return res.json({ success: false, errors: { message: t( "api.public.errors.messageRequired", "Message is required." ), }, }); } if (!user_name || user_name === "") { return res.json({ success: false, errors: { user_name: t( "api.public.errors.nameRequired", "Shipper name is required." ), }, }); } db.IncidentComment.create(IncidentComment) .then((response) => { console.log(response); }) .catch((err) => { console.log(err); }); return res.json({ success: true }); } else { return res.json({ success: false, error: t( "api.public.errors.missingMessage", "This message does not exist. Contact support" ), }); } }); } async function getIncident(req, res) { const { t } = req; const { token } = req.params; db.Incident.findOne({ where: { token }, include: [ { model: db.ReportType }, { model: db.VME, include: [{ model: db.Company }] }, { model: db.Supplier, include: [{ model: db.Company }] }, { model: db.IncidentComment, include: [ { model: db.User, attributes: { exclude: [ "email", "phone", "email_verified_at", "phone_verified_at", ], }, }, ], }, ], }).then(async (Incident) => { if (Incident) { return res.json({ success: true, Incident, }); } else { return res.json({ success: false, error: t( "api.public.errors.missingMessage", "This message does not exist. Contact support" ), }); } }); } async function postContact(req, res) { const { t } = req; const { email, phone, description } = req.body; const Contact = { email, phone, description }; if (!description) { return res.json({ success: false, errors: { description: t( "api.public.errors.messageRequired", "Message is required." ), }, }); } try { // verify vme and get vme let User = await userHelper.getAuthUser(req); if (User) { // user is logged in Contact.UserId = User.id; } else { // public flow if (!email) { return res.json({ success: false, errors: { email: t("api.public.errors.emailRequired", "Email is required"), }, }); } if (!phone) { return res.json({ success: false, errors: { phone: t("api.public.errors.phoneRequired", "Phone is required"), }, }); } } db.Contact.create(Contact); // do not wait for save //Send a mail to admins (new entry) db.User.findAll({ where: { is_admin: true } }) .then((Admins) => { Admins.forEach((Admin) => { notifier.sendMail(Admin.email, "new_contact", { Contact, Admin, User, }); }); }) .catch((err) => console.error(err)); res.json({ success: true }); } catch (error) { return res.json({ success: false, error }); } }