Skip to content

Commit

Permalink
feat: unit columns/cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeen committed Jan 10, 2022
1 parent ee56682 commit 53b4921
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 37 deletions.
4 changes: 0 additions & 4 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ export const create = async (req, res) => {
export const findAll = async (req, res) => {
let { page, limit, search, orgUid, columns, useMock } = req.query;

if (columns && !Array.isArray(columns)) {
columns = [columns];
}

if (useMock) {
res.json(ProjectMock.findAll({ ...paginationParams(page, limit) }));
return;
Expand Down
53 changes: 23 additions & 30 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {
Unit,
Qualification,
Vintage,
Organization,
Organization, ProjectLocation, CoBenefit, RelatedProject, Project,
} from '../models';
import { optionallyPaginatedResponse, paginationParams } from './helpers';
import {columnsToInclude, optionallyPaginatedResponse, paginationParams} from './helpers';

export const create = async (req, res) => {
try {
Expand Down Expand Up @@ -55,45 +55,38 @@ export const create = async (req, res) => {
};

export const findAll = async (req, res) => {
const { page, limit } = req.query;

let { page, limit, columns } = req.query;
if (req.query.useMock) {
res.json(UnitMock.findAll({ ...paginationParams(page, limit) }));
return;
}

if (req.query.onlyEssentialColumns) {
return res.json(
optionallyPaginatedResponse(
await Unit.findAndCountAll({
distinct: true,
attributes: [
'orgUid',
'unitLink',
'registry',
'unitType',
'unitCount',
'unitStatus',
'unitStatusDate',
],
}),
page,
limit,
),

const includes = [
Qualification,
];

if (columns) {
// Remove any unsupported columns
columns = columns.filter((col) =>
Unit.defaultColumns
.concat(includes.map((model) => model.name + 's'))
.includes(col),
);
} else {
columns = Unit.defaultColumns;
}

// If only FK fields have been specified, select just ID
if (!columns.length) {
columns = ['warehouseUnitId'];
}

res.json(
optionallyPaginatedResponse(
await Unit.findAndCountAll({
distinct: true,
include: [
{
model: Qualification,
as: 'qualifications',
},
Vintage,
],
...columnsToInclude(columns, includes),
...paginationParams(page, limit),
}),
page,
Expand Down
1 change: 1 addition & 0 deletions src/models/units/units.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { Model } = Sequelize;

class Unit extends Model {
static changes = new rxjs.Subject();
static defaultColumns = Object.keys(ModelTypes);

static associate() {
Unit.hasOne(Vintage);
Expand Down
3 changes: 1 addition & 2 deletions src/routes/v1/resources/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ const querySchema = Joi.object()
limit: Joi.number(),
search: Joi.string(),
columns: Joi.alternatives().try(
Joi.array().items(Joi.string()),
Joi.string(),
Joi.array().items(Joi.string()).single(),
),
})
.with('page', 'limit');
Expand Down
4 changes: 3 additions & 1 deletion src/routes/v1/resources/units.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const querySchema = Joi.object()
.keys({
page: Joi.number(),
limit: Joi.number(),
search: Joi.string(),
columns: Joi.alternatives().try(
Joi.array().items(Joi.string()).single(),
),
})
.with('page', 'limit');

Expand Down

0 comments on commit 53b4921

Please sign in to comment.