Skip to content

Commit

Permalink
fix: units columns fts edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeen committed Jan 13, 2022
1 parent 554cce6 commit 3fa4ff9
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/models/units/units.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,29 @@ const virtualColumns = {
type: Sequelize.VIRTUAL,
get() {
const rawValue = this.getDataValue('serialNumberBlock');
if (!rawValue) {
return undefined;
}
return rawValue.split('-')[0];
},
},
unitBlockEnd: {
type: Sequelize.VIRTUAL,
get() {
const rawValue = this.getDataValue('serialNumberBlock');
if (!rawValue) {
return undefined;
}
return rawValue.split('-')[1];
},
},
unitCount: {
type: Sequelize.VIRTUAL,
get() {
const rawValue = this.getDataValue('serialNumberBlock');
if (!rawValue) {
return undefined;
}
const blocks = rawValue.split('-');
const blockStart = Number(blocks[0].split(/(\d+)/)[1]);
const blockEnd = Number(blocks[1].split(/(\d+)/)[1]);
Expand Down Expand Up @@ -90,15 +99,21 @@ class Unit extends Model {
static async fts(searchStr, orgUid, pagination, columns = []) {
const dialect = sequelize.getDialect();

if (!columns.includes('serialNumberBlock')) {
columns.push('serialNumberBlock');
}

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

// Check if we need to include the virtual field dep
for (const col of Object.keys(virtualColumns)) {
if (columns.includes(col)) {
if (!columns.includes('serialNumberBlock')) {
columns.push('serialNumberBlock');
}
break;
}
}

return handlerMap[dialect](
searchStr,
orgUid,
Expand Down

0 comments on commit 3fa4ff9

Please sign in to comment.