diff --git a/package-lock.json b/package-lock.json index 42e905e1..0609f0e8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "cadt", - "version": "1.3.2", + "version": "1.4.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "cadt", - "version": "1.3.2", + "version": "1.4.0", "hasInstallScript": true, "dependencies": { "body-parser": "^1.20.2", @@ -21,6 +21,7 @@ "js-yaml": "^4.1.0", "lodash": "^4.17.21", "log-update": "^4.0.0", + "multer": "*", "mysql2": "^2.3.3", "node-xlsx": "^0.21.0", "regenerator-runtime": "^0.13.9", @@ -2943,6 +2944,11 @@ "node": ">= 8" } }, + "node_modules/append-field": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/append-field/-/append-field-1.0.0.tgz", + "integrity": "sha512-klpgFSWLW1ZEs8svjfb7g4qWY0YS5imI82dTg+QahUvJ8YqAY0P10Uk8tTyh9ZGuYEZEMaeJYCF5BFuX552hsw==" + }, "node_modules/aproba": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/aproba/-/aproba-2.0.0.tgz", @@ -3244,8 +3250,7 @@ "node_modules/buffer-from": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", - "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==", - "dev": true + "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==" }, "node_modules/busboy": { "version": "1.6.0", @@ -4255,8 +4260,7 @@ "node_modules/core-util-is": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", - "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==", - "dev": true + "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==" }, "node_modules/cors": { "version": "2.8.5", @@ -6384,8 +6388,7 @@ "node_modules/isarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", - "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==", - "dev": true + "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==" }, "node_modules/isexe": { "version": "2.0.0", @@ -6960,7 +6963,6 @@ "version": "1.2.8", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==", - "dev": true, "funding": { "url": "https://github.com/sponsors/ljharb" } @@ -7121,6 +7123,75 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, + "node_modules/multer": { + "version": "1.4.5-lts.1", + "resolved": "https://registry.npmjs.org/multer/-/multer-1.4.5-lts.1.tgz", + "integrity": "sha512-ywPWvcDMeH+z9gQq5qYHCCy+ethsk4goepZ45GLD63fOu0YcNecQxi64nDs3qluZB+murG3/D4dJ7+dGctcCQQ==", + "dependencies": { + "append-field": "^1.0.0", + "busboy": "^1.0.0", + "concat-stream": "^1.5.2", + "mkdirp": "^0.5.4", + "object-assign": "^4.1.1", + "type-is": "^1.6.4", + "xtend": "^4.0.0" + }, + "engines": { + "node": ">= 6.0.0" + } + }, + "node_modules/multer/node_modules/concat-stream": { + "version": "1.6.2", + "resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-1.6.2.tgz", + "integrity": "sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw==", + "engines": [ + "node >= 0.8" + ], + "dependencies": { + "buffer-from": "^1.0.0", + "inherits": "^2.0.3", + "readable-stream": "^2.2.2", + "typedarray": "^0.0.6" + } + }, + "node_modules/multer/node_modules/mkdirp": { + "version": "0.5.6", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.6.tgz", + "integrity": "sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==", + "dependencies": { + "minimist": "^1.2.6" + }, + "bin": { + "mkdirp": "bin/cmd.js" + } + }, + "node_modules/multer/node_modules/readable-stream": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz", + "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==", + "dependencies": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "node_modules/multer/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "node_modules/multer/node_modules/string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "dependencies": { + "safe-buffer": "~5.1.0" + } + }, "node_modules/mysql2": { "version": "2.3.3", "resolved": "https://registry.npmjs.org/mysql2/-/mysql2-2.3.3.tgz", @@ -7786,8 +7857,7 @@ "node_modules/process-nextick-args": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", - "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==", - "dev": true + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" }, "node_modules/promise-inflight": { "version": "1.0.1", @@ -9386,8 +9456,7 @@ "node_modules/typedarray": { "version": "0.0.6", "resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz", - "integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==", - "dev": true + "integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==" }, "node_modules/typescript": { "version": "5.0.4", @@ -9819,7 +9888,6 @@ "version": "4.0.2", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==", - "dev": true, "engines": { "node": ">=0.4" } @@ -12033,6 +12101,11 @@ "picomatch": "^2.0.4" } }, + "append-field": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/append-field/-/append-field-1.0.0.tgz", + "integrity": "sha512-klpgFSWLW1ZEs8svjfb7g4qWY0YS5imI82dTg+QahUvJ8YqAY0P10Uk8tTyh9ZGuYEZEMaeJYCF5BFuX552hsw==" + }, "aproba": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/aproba/-/aproba-2.0.0.tgz", @@ -12275,8 +12348,7 @@ "buffer-from": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", - "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==", - "dev": true + "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==" }, "busboy": { "version": "1.6.0", @@ -13070,8 +13142,7 @@ "core-util-is": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", - "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==", - "dev": true + "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==" }, "cors": { "version": "2.8.5", @@ -14683,8 +14754,7 @@ "isarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", - "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==", - "dev": true + "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==" }, "isexe": { "version": "2.0.0", @@ -15153,8 +15223,7 @@ "minimist": { "version": "1.2.8", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", - "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==", - "dev": true + "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==" }, "minimist-options": { "version": "4.1.0", @@ -15275,6 +15344,68 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, + "multer": { + "version": "1.4.5-lts.1", + "resolved": "https://registry.npmjs.org/multer/-/multer-1.4.5-lts.1.tgz", + "integrity": "sha512-ywPWvcDMeH+z9gQq5qYHCCy+ethsk4goepZ45GLD63fOu0YcNecQxi64nDs3qluZB+murG3/D4dJ7+dGctcCQQ==", + "requires": { + "append-field": "^1.0.0", + "busboy": "^1.0.0", + "concat-stream": "^1.5.2", + "mkdirp": "^0.5.4", + "object-assign": "^4.1.1", + "type-is": "^1.6.4", + "xtend": "^4.0.0" + }, + "dependencies": { + "concat-stream": { + "version": "1.6.2", + "resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-1.6.2.tgz", + "integrity": "sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw==", + "requires": { + "buffer-from": "^1.0.0", + "inherits": "^2.0.3", + "readable-stream": "^2.2.2", + "typedarray": "^0.0.6" + } + }, + "mkdirp": { + "version": "0.5.6", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.6.tgz", + "integrity": "sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==", + "requires": { + "minimist": "^1.2.6" + } + }, + "readable-stream": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz", + "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "requires": { + "safe-buffer": "~5.1.0" + } + } + } + }, "mysql2": { "version": "2.3.3", "resolved": "https://registry.npmjs.org/mysql2/-/mysql2-2.3.3.tgz", @@ -15774,8 +15905,7 @@ "process-nextick-args": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", - "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==", - "dev": true + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" }, "promise-inflight": { "version": "1.0.1", @@ -16951,8 +17081,7 @@ "typedarray": { "version": "0.0.6", "resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz", - "integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==", - "dev": true + "integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==" }, "typescript": { "version": "5.0.4", @@ -17260,8 +17389,7 @@ "xtend": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", - "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==", - "dev": true + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==" }, "y18n": { "version": "5.0.8", diff --git a/package.json b/package.json index acd3dc08..03c2f45f 100644 --- a/package.json +++ b/package.json @@ -36,12 +36,12 @@ "csvtojson": "^2.0.10", "dotenv": "^16.0.3", "express": "^4.18.2", - "express-fileupload": "^1.2.1", "express-joi-validation": "^5.0.1", "joi": "^17.5.0", "js-yaml": "^4.1.0", "lodash": "^4.17.21", "log-update": "^4.0.0", + "multer": "*", "mysql2": "^2.3.3", "node-xlsx": "^0.21.0", "regenerator-runtime": "^0.13.9", diff --git a/src/controllers/fileStore.controller.js b/src/controllers/fileStore.controller.js index 51c0f853..3953654d 100644 --- a/src/controllers/fileStore.controller.js +++ b/src/controllers/fileStore.controller.js @@ -1,5 +1,3 @@ -import _ from 'lodash'; - import crypto from 'crypto'; import { FileStore } from '../models'; @@ -86,26 +84,29 @@ export const getFile = async (req, res) => { export const addFileToFileStore = async (req, res) => { try { - if (_.get(req, 'files.file.data')) { - const { fileName } = req.body; - if (!fileName) { - throw new Error('Missing file name, can not upload file'); - } - const buffer = req.files.file.data; - const base64File = buffer.toString('base64'); - const SHA256 = crypto - .createHash('sha256') - .update(base64File) - .digest('base64'); - await FileStore.addFileToFileStore(SHA256, fileName, base64File); - return res.json({ - message: - 'File is being added to the file store, please wait for it to confirm.', - fileId: SHA256, - }); - } else { + if (!req.file) { throw new Error('Missing file data, can not upload file.'); } + + const { originalname: fileName, buffer } = req.file; + + if (!fileName) { + throw new Error('Missing file name, can not upload file'); + } + + const base64File = buffer.toString('base64'); + const SHA256 = crypto + .createHash('sha256') + .update(base64File) + .digest('base64'); + + await FileStore.addFileToFileStore(SHA256, fileName, base64File); + + return res.json({ + message: + 'File is being added to the file store, please wait for it to confirm.', + fileId: SHA256, + }); } catch (error) { console.trace(error); res.status(400).json({ diff --git a/src/controllers/offer.controller.js b/src/controllers/offer.controller.js index c4fd0161..487e7f31 100644 --- a/src/controllers/offer.controller.js +++ b/src/controllers/offer.controller.js @@ -80,7 +80,11 @@ export const importOfferFile = async (req, res) => { await assertNoPendingCommits(); await assertNoActiveOfferFile(); - const offerFileBuffer = req.files.file.data; + if (!req.file) { + throw new Error('No file uploaded'); + } + + const offerFileBuffer = req.file.buffer; const offerFile = offerFileBuffer.toString('utf-8'); const offerParsed = JSON.parse(offerFile); offerParsed.fee = _.get(CONFIG, 'DEFAULT_FEE', 300000000); diff --git a/src/controllers/organization.controller.js b/src/controllers/organization.controller.js index 3ad1a629..920e3120 100644 --- a/src/controllers/organization.controller.js +++ b/src/controllers/organization.controller.js @@ -1,4 +1,3 @@ -import _ from 'lodash'; import { sequelize } from '../database'; import { Organization } from '../models/organizations'; @@ -28,8 +27,8 @@ export const editHomeOrg = async (req, res) => { let icon; - if (_.get(req, 'files.file.data')) { - const buffer = req.files.file.data; + if (req.file) { + const buffer = req.file.buffer; icon = `data:image/png;base64, ${buffer.toString('base64')}`; } else { icon = ''; @@ -66,8 +65,8 @@ export const createV2 = async (req, res) => { const { name } = req.body; let icon; - if (_.get(req, 'files.file.data')) { - const buffer = req.files.file.data; + if (req.file) { + const buffer = req.file.buffer; icon = `data:image/png;base64, ${buffer.toString('base64')}`; } else { icon = ''; diff --git a/src/controllers/project.controller.js b/src/controllers/project.controller.js index 56eb825f..28f577b2 100644 --- a/src/controllers/project.controller.js +++ b/src/controllers/project.controller.js @@ -276,13 +276,11 @@ export const updateFromXLS = async (req, res) => { await assertHomeOrgExists(); await assertNoPendingCommits(); - const { files } = req; - - if (!files || !files.xlsx) { + if (!req.file) { throw new Error('File Not Received'); } - const xlsxParsed = transformMetaUid(xlsx.parse(files.xlsx.data)); + const xlsxParsed = transformMetaUid(xlsx.parse(req.file.buffer)); const stagedDataItems = tableDataFromXlsx(xlsxParsed, Project); const collapsedData = collapseTablesData(stagedDataItems, Project); diff --git a/src/controllers/units.controller.js b/src/controllers/units.controller.js index 6bdde914..3d103ba0 100644 --- a/src/controllers/units.controller.js +++ b/src/controllers/units.controller.js @@ -303,13 +303,11 @@ export const updateFromXLS = async (req, res) => { await assertHomeOrgExists(); await assertNoPendingCommits(); - const { files } = req; - - if (!files || !files.xlsx) { + if (!req.file) { throw new Error('File Not Received'); } - const xlsxParsed = transformMetaUid(xlsx.parse(files.xlsx.data)); + const xlsxParsed = transformMetaUid(xlsx.parse(req.file.buffer)); const stagedDataItems = tableDataFromXlsx(xlsxParsed, Unit); const collapsedData = collapseTablesData(stagedDataItems, Unit); diff --git a/src/middleware.js b/src/middleware.js index e32eb26c..dd156c9c 100644 --- a/src/middleware.js +++ b/src/middleware.js @@ -1,11 +1,10 @@ 'use strict'; import _ from 'lodash'; - import express from 'express'; import bodyParser from 'body-parser'; import cors from 'cors'; -import fileUpload from 'express-fileupload'; +import multer from 'multer'; import { V1Router } from './routes/v1'; import { getConfig } from './utils/config-loader'; import { logger } from './config/logger.cjs'; @@ -38,7 +37,8 @@ app.use( app.use(express.json()); app.use(bodyParser.urlencoded({ extended: false })); -app.use(fileUpload()); +const upload = multer(); +app.use(upload.array()); // Common assertions on every endpoint app.use(async function (req, res, next) { diff --git a/src/routes/v1/resources/filestore.js b/src/routes/v1/resources/filestore.js index 08504019..cc1cd66a 100644 --- a/src/routes/v1/resources/filestore.js +++ b/src/routes/v1/resources/filestore.js @@ -2,10 +2,13 @@ import express from 'express'; import joiExpress from 'express-joi-validation'; +import multer from 'multer'; const validator = joiExpress.createValidator({ passError: true }); const FileStoreRouter = express.Router(); +const upload = multer(); + import { FileStoreController } from '../../../controllers'; import { getFileSchema, subscribedSchema } from '../../../validations'; @@ -25,7 +28,7 @@ FileStoreRouter.get('/get_file_list', (req, res) => { return FileStoreController.getFileList(req, res); }); -FileStoreRouter.post('/add_file', (req, res) => { +FileStoreRouter.post('/add_file', upload.single('file'), (req, res) => { return FileStoreController.addFileToFileStore(req, res); }); diff --git a/src/routes/v1/resources/offer.js b/src/routes/v1/resources/offer.js index 3dc7e2ec..7e8dd9eb 100644 --- a/src/routes/v1/resources/offer.js +++ b/src/routes/v1/resources/offer.js @@ -2,8 +2,10 @@ import express from 'express'; import { OfferController } from '../../../controllers'; +import multer from 'multer'; const OfferRouter = express.Router(); +const upload = multer(); OfferRouter.get('/', (req, res) => { return OfferController.generateOfferFile(req, res); @@ -13,7 +15,7 @@ OfferRouter.delete('/', (req, res) => { return OfferController.cancelActiveOffer(req, res); }); -OfferRouter.post('/accept/import', (req, res) => { +OfferRouter.post('/accept/import', upload.single('file'), (req, res) => { return OfferController.importOfferFile(req, res); }); diff --git a/src/routes/v1/resources/organization.js b/src/routes/v1/resources/organization.js index a692fcbb..e60ff9fd 100644 --- a/src/routes/v1/resources/organization.js +++ b/src/routes/v1/resources/organization.js @@ -2,6 +2,7 @@ import express from 'express'; import joiExpress from 'express-joi-validation'; +import multer from 'multer'; import { OrganizationController } from '../../../controllers'; import { @@ -18,6 +19,7 @@ import { const validator = joiExpress.createValidator({ passError: true }); const OrganizationRouter = express.Router(); +const upload = multer(); OrganizationRouter.get('/', (req, res) => { return OrganizationController.findAll(req, res); @@ -47,11 +49,11 @@ OrganizationRouter.post( }, ); -OrganizationRouter.post('/create', (req, res) => { +OrganizationRouter.post('/create', upload.single('file'), (req, res) => { return OrganizationController.createV2(req, res); }); -OrganizationRouter.put('/edit', (req, res) => { +OrganizationRouter.put('/edit', upload.single('file'), (req, res) => { return OrganizationController.editHomeOrg(req, res); }); diff --git a/src/routes/v1/resources/projects.js b/src/routes/v1/resources/projects.js index 0067da40..6165b2e9 100644 --- a/src/routes/v1/resources/projects.js +++ b/src/routes/v1/resources/projects.js @@ -3,6 +3,7 @@ import express from 'express'; import { ProjectController } from '../../../controllers'; import joiExpress from 'express-joi-validation'; +import multer from 'multer'; import { projectsPostSchema, @@ -13,6 +14,7 @@ import { const validator = joiExpress.createValidator({ passError: true }); const ProjectRouter = express.Router(); +const upload = multer(); ProjectRouter.get('/', validator.query(projectsGetQuerySchema), (req, res) => { return req.query.warehouseProjectId @@ -36,7 +38,11 @@ ProjectRouter.put( (req, res) => ProjectController.transfer(req, res), ); -ProjectRouter.put('/xlsx', ProjectController.updateFromXLS); +ProjectRouter.put( + '/xlsx', + upload.single('xlsx'), + ProjectController.updateFromXLS, +); ProjectRouter.delete( '/', diff --git a/src/routes/v1/resources/units.js b/src/routes/v1/resources/units.js index ed30655a..5f737fbc 100644 --- a/src/routes/v1/resources/units.js +++ b/src/routes/v1/resources/units.js @@ -1,46 +1,45 @@ -'use strict'; - -import express from 'express'; -import { UnitController } from '../../../controllers'; -import joiExpress from 'express-joi-validation'; - -import { - unitsGetQuerySchema, - unitsPostSchema, - unitsUpdateSchema, - unitsDeleteSchema, - unitsSplitSchema, -} from '../../../validations'; - -const validator = joiExpress.createValidator({ passError: true }); -const UnitRouter = express.Router(); - -UnitRouter.get('/', validator.query(unitsGetQuerySchema), (req, res) => { - return req.query.warehouseUnitId - ? UnitController.findOne(req, res) - : UnitController.findAll(req, res); -}); - -UnitRouter.post('/', validator.body(unitsPostSchema), UnitController.create); -UnitRouter.put('/', validator.body(unitsUpdateSchema), UnitController.update); - -UnitRouter.delete( - '/', - validator.body(unitsDeleteSchema), - UnitController.destroy, -); - -UnitRouter.post( - '/split', - validator.body(unitsSplitSchema), - UnitController.split, -); - -UnitRouter.post('/batch', UnitController.batchUpload); - -UnitRouter.put( - '/xlsx', - UnitController.updateFromXLS, -); - -export { UnitRouter }; +'use strict'; + +import express from 'express'; +import { UnitController } from '../../../controllers'; +import joiExpress from 'express-joi-validation'; +import multer from 'multer'; + +import { + unitsGetQuerySchema, + unitsPostSchema, + unitsUpdateSchema, + unitsDeleteSchema, + unitsSplitSchema, +} from '../../../validations'; + +const validator = joiExpress.createValidator({ passError: true }); +const UnitRouter = express.Router(); +const upload = multer(); + +UnitRouter.get('/', validator.query(unitsGetQuerySchema), (req, res) => { + return req.query.warehouseUnitId + ? UnitController.findOne(req, res) + : UnitController.findAll(req, res); +}); + +UnitRouter.post('/', validator.body(unitsPostSchema), UnitController.create); +UnitRouter.put('/', validator.body(unitsUpdateSchema), UnitController.update); + +UnitRouter.delete( + '/', + validator.body(unitsDeleteSchema), + UnitController.destroy, +); + +UnitRouter.post( + '/split', + validator.body(unitsSplitSchema), + UnitController.split, +); + +UnitRouter.post('/batch', UnitController.batchUpload); + +UnitRouter.put('/xlsx', upload.single('xlsx'), UnitController.updateFromXLS); + +export { UnitRouter }; diff --git a/src/utils/data-assertions.js b/src/utils/data-assertions.js index ab88c1fa..d4d6b08a 100644 --- a/src/utils/data-assertions.js +++ b/src/utils/data-assertions.js @@ -143,11 +143,11 @@ export const assertOrgUidIsValid = async (orgUid, fieldName) => { }; export const assertCsvFileInRequest = (req) => { - if (!_.get(req, 'files.csv')) { - throw new Error('Can not file the required csv file in request'); + if (!req.file) { + throw new Error('Cannot find the required csv file in request'); } - return req.files.csv; + return req.file; }; export const assertOrgIsHomeOrg = async (orgUid) => {