Skip to content

Commit

Permalink
feat: add unified search option
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Nov 14, 2022
1 parent 76b5d5e commit fc172f5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export const findAll = async (req, res) => {
order,
marketplaceIdentifiers,
hasMarketplaceIdentifier,
includeProjectInfoInSearch = false,
} = req.query;

let where = orgUid != null && orgUid !== 'all' ? { orgUid } : undefined;
Expand Down Expand Up @@ -155,6 +156,7 @@ export const findAll = async (req, res) => {
orgUid,
pagination,
Unit.defaultColumns,
includeProjectInfoInSearch,
);

const mappedResults = ftsResults.rows.map((ftsResult) =>
Expand Down
48 changes: 41 additions & 7 deletions src/models/units/units.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ class Unit extends Model {
return super.destroy(values, options);
}

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

const handlerMap = {
Expand All @@ -126,6 +132,7 @@ class Unit extends Model {
'unitCount',
].includes(col),
),
includeProjectInfo,
);
}

Expand Down Expand Up @@ -192,7 +199,13 @@ class Unit extends Model {
};
}

static async findAllSqliteFts(searchStr, orgUid, pagination, columns = []) {
static async findAllSqliteFts(
searchStr,
orgUid,
pagination,
columns = [],
includeProjectInfo = false,
) {
const { offset, limit } = pagination;

let fields = '*';
Expand All @@ -214,13 +227,36 @@ class Unit extends Model {
searchStr = searchStr.replace('+', ''); // If query starts with +, replace it
}

let sql = `SELECT ${fields} FROM units_fts WHERE units_fts MATCH :search`;
let sql = `
SELECT ${fields}
FROM units_fts
WHERE units_fts MATCH :search`;

if (includeProjectInfo) {
sql = `SELECT units_fts.*
FROM units_fts
WHERE units_fts MATCH :search1
UNION
SELECT units_fts.*
FROM units_fts
INNER JOIN issuances on units_fts.issuanceId = issuances.id
INNER JOIN projects_fts on issuances.warehouseProjectId = projects_fts.warehouseProjectId
WHERE projects_fts MATCH :search2
`;
}

if (orgUid) {
sql = `${sql} AND orgUid = :orgUid`;
}

const replacements = { search: searchStr, orgUid };
let replacements = { search: searchStr, orgUid };
if (includeProjectInfo) {
replacements = {
search1: searchStr,
search2: searchStr,
orgUid,
};
}

const count = (
await sequelize.query(sql, {
Expand Down Expand Up @@ -332,9 +368,7 @@ class Unit extends Model {
isUpdateComment,
);

const currentAuthor = currentDataLayer.filter(
(kv) => kv.key === 'author',
);
const currentAuthor = currentDataLayer.filter((kv) => kv.key === 'author');
const isUpdateAuthor = currentAuthor.length > 0;
const authorChangeList = keyValueToChangeList(
'author',
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 @@ -60,6 +60,7 @@ export const unitsGetQuerySchema = Joi.object()
xls: Joi.boolean(),
marketplaceIdentifiers: Joi.array().items(Joi.string()).single(),
hasMarketplaceIdentifier: Joi.boolean(),
includeProjectInfoInSearch: Joi.boolean(),
})
.with('page', 'limit');

Expand Down

0 comments on commit fc172f5

Please sign in to comment.