diff --git a/package.json b/package.json index 1a6140c0..56509b1e 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "joi": "^17.5.0", "lodash": "^4.17.21", "mysql2": "^2.3.3", + "node-xlsx": "^0.21.0", "rxjs": "^7.5.1", "sequelize": "^6.12.0-alpha.1", "sequelize-mock": "^0.10.2", diff --git a/src/controllers/project.controller.js b/src/controllers/project.controller.js index 8338713f..a312ba81 100644 --- a/src/controllers/project.controller.js +++ b/src/controllers/project.controller.js @@ -26,6 +26,8 @@ import { } from '../utils/data-assertions'; import { createProjectRecordsFromCsv } from '../utils/csv-utils'; +import {createXlsFromSequelizeResults, sendXls} from '../utils/xls'; +import * as stream from "stream"; export const create = async (req, res) => { const newRecord = _.cloneDeep(req.body); @@ -64,9 +66,9 @@ export const create = async (req, res) => { }; export const findAll = async (req, res) => { - let { page, limit, search, orgUid, columns } = req.query; + let { page, limit, search, orgUid, columns, xls } = req.query; let where = orgUid ? { orgUid } : undefined; - + const includes = Project.getAssociatedModels(); if (columns) { @@ -88,12 +90,17 @@ export const findAll = async (req, res) => { } let results; + let pagination = paginationParams(page, limit); + if (xls) { + pagination = {page: undefined, limit: undefined}; + } + if (search) { results = await Project.fts( search, orgUid, - paginationParams(page, limit), + pagination, columns, ); } @@ -101,7 +108,7 @@ export const findAll = async (req, res) => { if (!results) { const query = { ...columnsToInclude(columns, includes), - ...paginationParams(page, limit), + ...pagination, }; results = await Project.findAndCountAll({ @@ -111,7 +118,14 @@ export const findAll = async (req, res) => { }); } - return res.json(optionallyPaginatedResponse(results, page, limit)); + const response = optionallyPaginatedResponse(results, page, limit); + + if (!xls) { + return res.json(response); + } else { + return sendXls(Project.name, createXlsFromSequelizeResults(response, Project), res); + } + }; export const findOne = async (req, res) => { diff --git a/src/controllers/units.controller.js b/src/controllers/units.controller.js index f77bc59c..d2a38959 100644 --- a/src/controllers/units.controller.js +++ b/src/controllers/units.controller.js @@ -21,6 +21,7 @@ import { } from '../utils/data-assertions'; import { createUnitRecordsFromCsv } from '../utils/csv-utils'; +import {createXlsFromSequelizeResults} from "../utils/xls.js"; export const create = async (req, res) => { try { @@ -58,7 +59,7 @@ export const create = async (req, res) => { }; export const findAll = async (req, res) => { - let { page, limit, columns, orgUid, search } = req.query; + let { page, limit, columns, orgUid, search, xls } = req.query; let where = orgUid ? { orgUid } : undefined; const includes = [Qualification, Vintage]; @@ -82,12 +83,17 @@ export const findAll = async (req, res) => { } let results; + let pagination = paginationParams(page, limit); + + if (xls) { + pagination = {page: undefined, limit: undefined}; + } if (search) { results = await Unit.fts( search, orgUid, - paginationParams(page, limit), + pagination, Unit.defaultColumns, ); @@ -134,8 +140,15 @@ export const findAll = async (req, res) => { ...paginationParams(page, limit), }); } + + const response = optionallyPaginatedResponse(results, page, limit); + + if (!xls) { + return res.json(response); + } else { + createXlsFromSequelizeResults(response, Unit); + } - res.json(optionallyPaginatedResponse(results, page, limit)); }; export const findOne = async (req, res) => { diff --git a/src/validations/projects.validations.js b/src/validations/projects.validations.js index 47111e8a..f61de531 100644 --- a/src/validations/projects.validations.js +++ b/src/validations/projects.validations.js @@ -35,6 +35,7 @@ export const projectsGetQuerySchema = Joi.object() columns: Joi.array().items(Joi.string()).single(), orgUid: Joi.string(), warehouseProjectId: Joi.string(), + xls: Joi.boolean(), }) .with('page', 'limit'); diff --git a/src/validations/units.validations.js b/src/validations/units.validations.js index b8ab3dff..0e837698 100644 --- a/src/validations/units.validations.js +++ b/src/validations/units.validations.js @@ -70,6 +70,7 @@ export const unitsGetQuerySchema = Joi.object() warehouseUnitId: Joi.string(), columns: Joi.array().items(Joi.string()).single(), orgUid: Joi.string(), + xls: Joi.boolean(), }) .with('page', 'limit');