Skip to content

Commit

Permalink
chore: fix merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Jan 9, 2022
2 parents 978e59a + 712babc commit 91442ce
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 67 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"express": "~4.16.1",
"express-joi-validation": "^5.0.0",
"joi": "^17.5.0",
"lodash": "^4.17.21",
"rxjs": "^7.5.1",
"sequelize": "^6.12.0-alpha.1",
"socket.io": "^4.4.0",
Expand Down
19 changes: 18 additions & 1 deletion src/controllers/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

import _ from "lodash";

export const paginationParams = (page, limit) => {
if (page === undefined || limit === undefined) {
return {
Expand Down Expand Up @@ -32,4 +34,19 @@ export const optionallyPaginatedResponse = ({count, rows}, page, limit) => {
} else {
return rows;
}
}
}

export const columnsToInclude = (userColumns, foreignKeys) => {
const attributeModelMap = foreignKeys.map(relationship => relationship.name + 's');
return {
attributes: userColumns.filter(
column => !attributeModelMap.includes(column)
),
include: _.intersection(
userColumns,
attributeModelMap,
).map(fk => foreignKeys.find(model => {
return model.name === fk.substring(0, fk.length - 1);
}))
}
}
102 changes: 45 additions & 57 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ import {
Organization,
} from '../models';

import { optionallyPaginatedResponse, paginationParams } from './helpers';
import ModelTypes from './../models/projects/projects.modeltypes.cjs';
import {
columnsToInclude,
optionallyPaginatedResponse,
paginationParams,
} from './helpers';

export const create = async (req, res) => {
const newRecord = _.cloneDeep(req.body);
Expand Down Expand Up @@ -52,80 +55,65 @@ export const create = async (req, res) => {
};

export const findAll = async (req, res) => {
const { page, limit, search, orgUid, columns, useMock } = req.query;
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;
}

const defaultColumns = Object.keys(ModelTypes);
const includes = [
ProjectLocation,
Qualification,
Vintage,
CoBenefit,
RelatedProject,
];

let columnsList = [];
if (columns) {
columnsList = columns.split(',');
// Check to ensure passed in columns are valid
if (
columnsList.filter((col) => defaultColumns.includes(col)).length !==
columnsList.length
) {
console.error('Invalid column specified');
res.status(400).send('Invalid column specified');
return;
}
// Remove any unsupported columns
columns = columns.filter((col) =>
Project.defaultColumns
.concat(includes.map((model) => model.name + 's'))
.includes(col),
);
} else {
columnsList = defaultColumns;
columns = Project.defaultColumns;
}

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

let results;

if (search) {
const supportedSearchFields = columnsList.filter(
(col) => !['createdAt', 'updatedAt'].includes(col),
results = await Project.fts(
search,
orgUid,
paginationParams(page, limit),
columns,
);

if (dialect === 'sqlite') {
results = await Project.findAllSqliteFts(
search,
orgUid,
paginationParams(page, limit),
supportedSearchFields,
);
} else if (dialect === 'mysql') {
results = await Project.findAllMySQLFts(
search,
orgUid,
paginationParams(page, limit),
supportedSearchFields,
);
}
return res.json(optionallyPaginatedResponse(results, page, limit));
}

const query = {
attributes: columnsList,
include: getRequestedForignModels(req.query.columns, [
ProjectLocation,
Qualification,
Vintage,
CoBenefit,
RelatedProject,
]),
};
if (!results) {
const query = {
...columnsToInclude(columns, includes),
...paginationParams(page, limit),
};

results = await Project.findAndCountAll({
distinct: true,
...query,
});
}

return res.json(
optionallyPaginatedResponse(
await Project.findAndCountAll({
distinct: true,
...query,
...paginationParams(page, limit),
}),
page,
limit,
),
);
return res.json(optionallyPaginatedResponse(results, page, limit));
};

export const findOne = async (req, res) => {
Expand Down
32 changes: 24 additions & 8 deletions src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
import Sequelize from 'sequelize';
import rxjs from 'rxjs';

const { Model } = Sequelize;

import { sequelize } from '../database';
Expand All @@ -10,22 +9,20 @@ import { RelatedProject } from './../related-projects';
import { Vintage } from './../vintages';
import { Qualification } from './../qualifications';
import { ProjectLocation } from './../locations';
import { Rating } from './../ratings';
import { CoBenefit } from './../co-benefits';

import ModelTypes from './projects.modeltypes.cjs';
import { optionallyPaginatedResponse } from '../../controllers/helpers.js';

class Project extends Model {
static changes = new rxjs.Subject();

static defaultColumns = Object.keys(ModelTypes);

static associate() {
Project.hasMany(RelatedProject);
Project.hasMany(Vintage);
Project.hasMany(ProjectLocation);
Project.hasMany(Qualification);
Project.hasMany(Rating);
Project.hasMany(Vintage);
Project.hasMany(CoBenefit);
Project.hasMany(ProjectLocation);
Project.hasMany(RelatedProject);
}

static async create(values, options) {
Expand All @@ -48,6 +45,25 @@ class Project extends Model {

return destroyResult;
}

static async fts(searchStr, orgUid, pagination, columns = []) {
const dialect = sequelize.getDialect();

const handlerMap = {
sqlite: Project.findAllSqliteFts,
mysql: Project.findAllMySQLFts,
}

return handlerMap[dialect](searchStr, orgUid, pagination, columns.filter(
(col) => !['createdAt', 'updatedAt'].includes(col),
).filter((col) => ![
ProjectLocation,
Qualification,
Vintage,
CoBenefit,
RelatedProject,
].map(model => model.name + 's').includes(col)));
}

static async findAllMySQLFts(searchStr, orgUid, pagination, columns = []) {
const { offset, limit } = pagination;
Expand Down
2 changes: 1 addition & 1 deletion src/routes/v1/resources/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const querySchema = Joi.object()
page: Joi.number(),
limit: Joi.number(),
search: Joi.string(),
columns: Joi.string(),
columns: Joi.alternatives().try(Joi.array().items(Joi.string()), Joi.string()),
})
.with('page', 'limit');

Expand Down

0 comments on commit 91442ce

Please sign in to comment.