UNPKG

synt_backend

Version:

Synt light-weight node backend service

208 lines (187 loc) 5.67 kB
import { Router } from "express"; const router = Router(); const db = require("./../mysql/models/index"); import * as ValidationHelper from "./../helpers/validations"; const userHelper = require("./../helpers/user"); import { formatSupplier } from "./../helpers/format"; import { attachUploadedFiles } from "./../helpers/db-storage"; router.get("/", getSuppliers); router.get("/:SupplierId", getSupplier); router.post("/", postSupplier); async function getSupplier(req, res) { const { t } = req; let user = await userHelper.getAuthUser(req); const { SupplierId } = req.params; // 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 (user) { db.Supplier.findOne({ where: { id: SupplierId }, include: [ { model: db.ReportType }, { model: db.Company }, { model: db.SupplierFile }, ], }).then(async (Supplier) => { if (Supplier.VMEId !== VME.id) { return res.json({ success: false, error: t( "api.suppliers.errors.notYourVme", "This supplier does not belong to your VME." ), }); } else if (Supplier) { await Promise.all( Supplier.SupplierFiles.map(async (SF) => { SF.setDataValue("presignedUrl", await SF.getPresignedUrl()); }) ); return res.json({ success: true, Supplier: formatSupplier(Supplier), }); } else { return res.json({ success: true, error: t( "api.suppliers.errors.supplierNotExists", "This supplier does not exist. Contact support" ), }); } }); } } async function getSuppliers(req, res) { const { t } = req; console.log("GETTING SUPPLIERS"); try { // verify vme and get vme let user = await userHelper.getAuthUser(req); let VmeValidation = await ValidationHelper.validateVme(t, user.VMEId); //TODO: Validate user roles if (!VmeValidation.success) { return res.json(VmeValidation); } const { VME } = VmeValidation; if (VME) { console.log("GETTING SUPPLIERS"); VME.getSuppliers({ include: [ { model: db.ReportType }, { model: db.Company }, { model: db.SupplierFile }, ], }).then((Suppliers) => { return res.json({ success: true, Suppliers: Suppliers.map((S) => formatSupplier(S)), }); }); } else { return res.json({ success: false, error: t("api.suppliers.errors.noVmeSelected", "No VME selected yet."), }); } } catch (error) { console.log(error); } } async function postSupplier(req, res) { const { t } = req; // verify and format vat number const vatValidation = ValidationHelper.formatVatNumber( req.body.vat_number, t ); if (!vatValidation.success) { return res.json(vatValidation); } // define const const { company_number, country_code } = vatValidation; const { phone, email, current_account, ReportTypes, alias, SupplierFiles } = req.body; // verify vme and get vme let user = await userHelper.getAuthUser(req); const VMEId = user.VMEId; let VmeValidation = await ValidationHelper.validateVme(t, VMEId); //TODO: Validate user roles if (!VmeValidation.success) { return res.json(VmeValidation); } //const { VME } = VmeValidation; // id is used if (!ReportTypes || !ReportTypes.length) { return res.json({ success: false, errors: { ReportTypes: t( "api.suppliers.errors.typeSelectionRequired", "Type selection is required. This is necessary to link to notifications." ), }, }); } if (!phone) { return res.json({ success: false, errors: { phone: t( "api.suppliers.errors.phoneRequired", "Phone number is required." ), }, }); } // find existing company db.Company.findOne({ where: { company_number, country_code } }).then( (company) => { if (company) { company.phone = phone; company.email = email; company.current_account = current_account; company.save(); // save Supplier let SupplierData = { VMEId, CompanyId: company.id, alias, }; db.Supplier.findOne({ where: { CompanyId: company.id, VMEId }, }).then((Supplier) => { if (Supplier) { attachUploadedFiles("Supplier", Supplier, SupplierFiles); Supplier.setReportTypes(ReportTypes.map((T) => T.id)); Supplier.update(SupplierData) .then(() => { console.log("Supplier updated"); }) .catch((e) => console.log(e)); } else { db.Supplier.create(SupplierData) .then((newItem) => { attachUploadedFiles("Supplier", newItem, SupplierFiles); newItem.setReportTypes(ReportTypes.map((T) => T.id)); console.log("Supplier created"); }) .catch((e) => console.log(e)); } }); return res.json({ success: true }); } else { return res.json({ success: false, error: t( "api.suppliers.errors.companyNumberIncorrect", "Company number incorrect." ), }); } } ); } module.exports = router;