Skip to content

Commit

Permalink
feat: xsl export
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeen committed Jan 22, 2022
1 parent 1c37cb0 commit ce24c90
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 19 additions & 5 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -88,20 +90,25 @@ 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,
);
}

if (!results) {
const query = {
...columnsToInclude(columns, includes),
...paginationParams(page, limit),
...pagination,
};

results = await Project.findAndCountAll({
Expand All @@ -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) => {
Expand Down
19 changes: 16 additions & 3 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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];
Expand All @@ -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,
);

Expand Down Expand Up @@ -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) => {
Expand Down
1 change: 1 addition & 0 deletions src/validations/projects.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
1 change: 1 addition & 0 deletions src/validations/units.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down

0 comments on commit ce24c90

Please sign in to comment.