Skip to content

Commit

Permalink
fix: projects returns entire result set on fts
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Mar 2, 2022
1 parent 086489c commit a05dcb1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 40 deletions.
14 changes: 13 additions & 1 deletion src/controllers/project.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ from 'lodash';

import { Sequelize } from 'sequelize';
import xlsx from 'node-xlsx';
import { uuid as uuidv4 } from 'uuidv4';

Expand Down Expand Up @@ -133,7 +134,18 @@ export const findAll = async (req, res) => {
}

if (search) {
results = await Project.fts(search, orgUid, pagination, columns);
const ftsResults = await Project.fts(search, orgUid, pagination, columns);
const mappedResults = ftsResults.rows.map((ftsResult) =>
_.get(ftsResult, 'dataValues.warehouseProjectId'),
);

if (!where) {
where = {};
}

where.warehouseProjectId = {
[Sequelize.Op.in]: mappedResults,
};
}

if (!results) {
Expand Down
49 changes: 16 additions & 33 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import _ from 'lodash';
import { uuid as uuidv4 } from 'uuidv4';
import { Sequelize } from 'sequelize';

import { Staging, Unit, Label, Issuance, Organization } from '../models';

Expand Down Expand Up @@ -141,41 +142,23 @@ export const findAll = async (req, res) => {
}

if (search) {
results = await Unit.fts(search, orgUid, pagination, Unit.defaultColumns);

// Lazy load the associations when doing fts search, not ideal but the page sizes should be small

if (columns.includes('labels')) {
results.rows = await Promise.all(
results.rows.map(async (result) => {
result.dataValues.labels = await Label.findAll({
include: [
{
model: Unit,
where: {
warehouseUnitId: result.dataValues.warehouseUnitId,
},
attributes: [],
as: 'unit',
require: true,
},
],
});
return result;
}),
);
}
const ftsResults = await Unit.fts(
search,
orgUid,
pagination,
Unit.defaultColumns,
);
const mappedResults = ftsResults.rows.map((ftsResult) =>
_.get(ftsResult, 'dataValues.warehouseUnitId'),
);

if (columns.includes('issuances')) {
results.rows = await Promise.all(
results.rows.map(async (result) => {
result.dataValues.issuance = await Issuance.findByPk(
result.dataValues.issuanceId,
);
return result;
}),
);
if (!where) {
where = {};
}

where.warehouseProjectId = {
[Sequelize.Op.in]: mappedResults,
};
}

if (!results) {
Expand Down
4 changes: 1 addition & 3 deletions src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,7 @@ class Project extends Model {
projectTag,
estimatedAnnualAverageEmissionReduction,
timeStaged
)
AGAINST ':search'
ORDER BY timeStaged DESC
) AGAINST ':search'
`;

if (orgUid) {
Expand Down
4 changes: 1 addition & 3 deletions src/models/units/units.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,7 @@ class Unit extends Model {
cooresponingAdjustmentDeclaration,
correspondingAdjustmentStatus,
timeStaged
)
AGAINST ':search'
ORDER BY timeStaged DESC
) AGAINST ':search'
`;

if (orgUid) {
Expand Down

0 comments on commit a05dcb1

Please sign in to comment.