UNPKG

synt_backend

Version:

Synt light-weight node backend service

271 lines (254 loc) 7.82 kB
import { Router } from "express"; import { formatLot, findCommissioners } from "../helpers/format"; import db from "../mysql/models"; const router = Router(); import * as ValidationHelper from "./../helpers/validations"; const userHelper = require("./../helpers/user"); router.get("/", getLots); router.get("/commissioners", getCommissioners); router.get("/:LotId", getLot); router.post("/", postLot); async function getLot(req, res) { const { t } = req; try { let user = await userHelper.getAuthUser(req); const { LotId } = 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.Lot.findOne({ where: { id: LotId }, include: [ { model: db.LotPhase, include: [ { model: db.User, attributes: { exclude: [ "phone", "email", "email_verified_at", "phone_verified_at", ], }, }, ], }, ], }) .then((Lot) => { if (Lot.VMEId !== VME.id) { return res.json({ success: false, error: t( "api.lots.errors.notYourVme", "This lot is not part of your VME." ), }); } else if (Lot) { return res.json({ success: true, Lot: formatLot(Lot) }); } else { return res.json({ success: false, error: t("api.lots.errors.lotNotFound", "Lot not found"), }); } }) .catch((err) => { return res.json({ success: false, error: err }); }); } } catch (error) { return res.json({ success: false, error }); } } async function getCommissioners(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; findCommissioners(VME).then((Commissioners) => { return res.json({ success: true, Commissioners }); }); } } catch (error) { return res.json({ success: false, error }); } } async function getLots(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; return VME.getLots({ include: [ { model: db.LotPhase, include: [ { model: db.User, attributes: { exclude: ["phone", "email_verified_at", "phone_verified_at"], }, include: [{ model: db.Company }], }, ], }, ], }).then((Lots) => { return res.json({ success: true, Lots }); }); } } catch (error) { return res.json({ success: false, error }); } } async function postLot(req, res) { const { t } = req; try { let User = await userHelper.getAuthUser(req); if (User) { const { name, id, LotPhases } = req.body; let { is_common } = req.body; if (typeof is_common === "undefined") { is_common = false; } const share = parseInt(req.body.share); if (!name) { return res.json({ success: false, errors: { name: t("api.lots.errors.nameRequired", "Lot needs a name."), }, }); } if (!share && share !== 0) { return res.json({ success: false, errors: { share: t( "api.lots.errors.quotaInvalid", "Quota must have a valid value." ), }, }); } // UserId: the person who last changed the lot const LotData = { name, share, is_common, UserId: User.id }; // 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) { // alleen gebruikers veranderen } // verify current lot share total let add = true; VME.getLots().then(async (Lots) => { let total_shares = 0; Lots.forEach((Lot) => { if (Lot.name === name && !id) { add = false; return res.json({ success: false, errors: { name: t("api.lots.errors.nameUniq", { defaultValue: "The name '{{name}}' has already been added. Use a unique name.", name, }), }, }); } if (Lot.id !== id) total_shares += Lot.share; }); if (total_shares + share > VME.total_shares) { add = false; // not necessary return res.json({ success: false, errors: { share: t("api.lots.errors.quotaHigh", { defaultValue: "Quota is too high. Currently {{total_shares}}/{{vme_total_shares}} designated.", total_shares, vme_total_shares: VME.total_shares, }), }, }); } else { if (add) { let Lot; if (id) { Lot = await db.Lot.findOne({ where: { id }, include: [{ model: db.LotPhase, include: db.User }], }); Lot.update(LotData); } else { Lot = await db.Lot.create({ VMEId: VME.id, ...LotData }); } (LotPhases || []).forEach(async (LP) => { // update existing let e = (Lot.LotPhases || []).find((lp) => lp.id === LP.id); // needs to be first let ids = LP.Users.map((U) => U.id); let newLP; if (e) { // exists let r = (e.Users || []).map((U) => ids.includes(U.id) ? false : U.id ); e.removeUsers(r); // set will changes users e.set(LP); e.save(); } else { // add new newLP = await db.LotPhase.create({ ...LP, id: null, LotId: Lot.id, }); } db.LotPhaseUser.bulkCreate( LP.Users.map((U) => ({ type: U.type, is_commissioner: U.is_commissioner, LotPhaseId: newLP?.id || LP.id, UserId: U.id, })), { updateOnDuplicate: ["type", "is_commissioner"], } ); }); console.log(Lot); } return res.json({ success: true }); } }); } } catch (error) { return res.json({ success: false, error }); } } module.exports = router;