From fc172f5b7d206b7f8f593a892ea94ad299b0f0cc Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Mon, 14 Nov 2022 09:49:30 -0500 Subject: [PATCH] feat: add unified search option --- src/controllers/units.controller.js | 2 ++ src/models/units/units.model.js | 48 ++++++++++++++++++++++++---- src/validations/units.validations.js | 1 + 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/controllers/units.controller.js b/src/controllers/units.controller.js index 4aa21ada..b9c20560 100644 --- a/src/controllers/units.controller.js +++ b/src/controllers/units.controller.js @@ -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; @@ -155,6 +156,7 @@ export const findAll = async (req, res) => { orgUid, pagination, Unit.defaultColumns, + includeProjectInfoInSearch, ); const mappedResults = ftsResults.rows.map((ftsResult) => diff --git a/src/models/units/units.model.js b/src/models/units/units.model.js index e0180ba9..20d8311d 100644 --- a/src/models/units/units.model.js +++ b/src/models/units/units.model.js @@ -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 = { @@ -126,6 +132,7 @@ class Unit extends Model { 'unitCount', ].includes(col), ), + includeProjectInfo, ); } @@ -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 = '*'; @@ -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, { @@ -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', diff --git a/src/validations/units.validations.js b/src/validations/units.validations.js index d055c3bb..0f9bc6d3 100644 --- a/src/validations/units.validations.js +++ b/src/validations/units.validations.js @@ -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');