UNPKG

synt_backend

Version:

Synt light-weight node backend service

212 lines (200 loc) 5.81 kB
const express = require("express"); const router = express.Router(); import "dotenv/config"; import * as ValidationHelper from "./../helpers/validations"; const userHelper = require("./../helpers/user"); import db from "../mysql/models"; // routes router.get("/", getDistributionKeys); router.get("/:DistributionKeyId", getDistributionKey); router.post("/", postDistributionKey); module.exports = router; async function getDistributionKeys(req, res) { const { t } = req; try { let user = await userHelper.getAuthUser(req); if (user) { // verify vme and get vme let VmeValidation = await ValidationHelper.validateVme(t, user.VMEId); //TODO: Validate user roles if (!VmeValidation.success) { return res.json(VmeValidation); } const { VME } = VmeValidation; VME.getDistributionKeys({ include: [ { model: db.Lot, include: [ { model: db.LotPhase, include: [ { model: db.User, attributes: { exclude: [ "phone", "email_verified_at", "phone_verified_at", ], }, include: db.Company, }, ], }, ], }, ], }) .then((DistributionKeys) => { DistributionKeys = DistributionKeys.map((DK) => { let Lots = []; DK.Lots.forEach((L) => { let last_period = L.LotPhases.find((LP) => !LP.ends_at); let Commissioner = last_period?.Users.find( (U) => U.LotPhaseUser.is_commissioner ); Lots.push({ ...L.toJSON(), Commissioner }); }); return { ...DK.toJSON(), Lots }; }); return res.json({ success: true, DistributionKeys }); }) .catch((err) => console.log(err)); } } catch (error) { return res.json({ success: false, error }); } } async function getDistributionKey(req, res) { const { t } = req; try { let user = await userHelper.getAuthUser(req); const { DistributionKeyId } = req.params; if (user) { // verify vme and get vme let VmeValidation = await ValidationHelper.validateVme(t, user.VMEId); //TODO: Validate user roles if (!VmeValidation.success) { return res.json(VmeValidation); } const { VME } = VmeValidation; db.DistributionKey.findOne({ where: { id: DistributionKeyId }, include: [ { model: db.Lot, }, ], }) .then((DistributionKey) => { if (DistributionKey.VMEId !== VME.id) { return res.json({ success: false, error: t( "api.distributionKeys.errors.notYourVme", "This distribution key is not part of your VME." ), }); } else if (DistributionKey) { return res.json({ success: true, DistributionKey }); } else { return res.json({ success: false, error: t( "api.distributionKeys.errors.keyNotFound", "Distribution key not found." ), }); } }) .catch((err) => { return res.json({ success: false, error: err }); }); } } catch (error) { return res.json({ success: false, error }); } } async function postDistributionKey(req, res) { const { t } = req; const { id, name, description, type } = req.body; let { Lots } = req.body; if (!name) { return res.json({ success: false, errors: { name: t( "api.distributionKeys.errors.nameRequired", "Distribution key needs a name." ), }, }); } if (!type) { return res.json({ success: false, errors: { name: t( "api.distributionKeys.errors.typeRequired", "Distribution key needs a type." ), }, }); } const DistributionKey = { name, description, type }; let user = await userHelper.getAuthUser(req); // verify vme and get vme let VmeValidation = await ValidationHelper.validateVme(t, user.VMEId); //TODO: Validate user roles if (!VmeValidation.success) { return res.json(VmeValidation); } const { VME } = VmeValidation; if (VME.is_frozen) { return res.json({ success: false, error: t( "api.distributionKeys.errors.vmeFrozen", "VME is frozen. You cannot make changes." ), }); } Lots = (Lots || []).map((L) => (L.selected ? L.id : null)).filter(Boolean); if (id) { db.DistributionKey.findOne({ where: { id, VMEId: VME.id }, }).then((DK) => { DK.setLots(Lots).then(() => { return res.json({ success: true }); }); }); db.DistributionKey.update( { ...DistributionKey }, { where: { id, VMEId: VME.id }, } ); } else { db.DistributionKey.findOne({ where: { name, VMEId: VME.id }, }).then((DK) => { if (DK) { return res.json({ success: false, errors: { name: t( "api.distributionKeys.errors.nameUniq", "The name has already been chosen." ), }, }); } else { db.DistributionKey.create({ ...DistributionKey, VMEId: VME.id }) .then((DK) => { DK.setLots(Lots).then(() => { return res.json({ success: true }); }); }) .catch((e) => console.log(e)); } }); } }