Skip to content

Commit

Permalink
feat: add orgUid query param
Browse files Browse the repository at this point in the history
  • Loading branch information
frantzarty committed Dec 21, 2021
1 parent 1a7b1a2 commit f66066d
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 28 deletions.
2 changes: 2 additions & 0 deletions migrations/20211212200953-fulltext-search.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
await queryInterface.sequelize.query(`
CREATE VIRTUAL TABLE projects_fts USING fts5(
id,
orgUid,
warehouseProjectId,
projectId,
projectLocationId,
Expand Down Expand Up @@ -34,6 +35,7 @@ module.exports = {
await queryInterface.sequelize.query(`
CREATE VIRTUAL TABLE units_fts USING fts5(
id,
orgUid,
buyer,
registry,
blockIdentifier,
Expand Down
4 changes: 4 additions & 0 deletions migrations/20211219182106-sqlite-triggers-projects.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
CREATE TRIGGER project_insert_fts AFTER INSERT ON projects BEGIN
INSERT INTO projects_fts(
id,
orgUid,
warehouseProjectId,
currentRegistry,
registryOfOrigin,
Expand All @@ -29,6 +30,7 @@ module.exports = {
projectTag
) VALUES (
new.id,
new.orgUid,
new.warehouseProjectId,
new.currentRegistry,
new.registryOfOrigin,
Expand Down Expand Up @@ -63,6 +65,7 @@ module.exports = {
DELETE FROM projects_fts WHERE id = old.id;
INSERT INTO projects_fts(
id,
orgUid,
warehouseProjectId,
currentRegistry,
registryOfOrigin,
Expand All @@ -85,6 +88,7 @@ module.exports = {
projectTag
) VALUES (
new.id,
new.orgUid,
new.warehouseProjectId,
new.currentRegistry,
new.registryOfOrigin,
Expand Down
4 changes: 4 additions & 0 deletions migrations/20211219184405-sqlite-triggers-units.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
CREATE TRIGGER unit_insert_fts AFTER INSERT ON units BEGIN
INSERT INTO units_fts(
id,
orgUid,
buyer,
registry,
blockIdentifier,
Expand All @@ -22,6 +23,7 @@ module.exports = {
unitTag
) VALUES (
new.id,
new.orgUid,
new.buyer,
new.registry,
new.blockIdentifier,
Expand Down Expand Up @@ -49,6 +51,7 @@ module.exports = {
DELETE FROM units_fts WHERE id = old.id;
INSERT INTO units_fts(
id,
orgUid,
projectId,
buyer,
registry,
Expand All @@ -67,6 +70,7 @@ module.exports = {
vintageId
) VALUES (
new.id,
new.orgUid,
new.projectId,
new.buyer,
new.registry,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"start": "node --experimental-specifier-resolution=node ./src/server.js",
"test": "mocha tests/**/*.spec.js --reporter spec --exit",
"release": "./node_modules/.bin/standard-version && git push --tags",
"postinstall": "npm run requirements-check"
"postinstall": "npm run requirements-check",
"resetdb": "rm ./data.sqlite3 && npx sequelize-cli db:migrate && npx sequelize-cli db:seed:all"
},
"dependencies": {
"body-parser": "^1.19.0",
Expand Down
34 changes: 20 additions & 14 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,31 @@ export const findAll = async (req, res) => {
}

const dialect = sequelize.getDialect();
const { search, orgUid } = req.query;

if (req.query.search) {
if (search) {
if (dialect === 'sqlite') {
res.json(await Project.findAllSqliteFts(req.query.search));
res.json(await Project.findAllSqliteFts(search, orgUid));
} else if (dialect === 'mysql') {
res.json(await Project.findAllMySQLFts(req.query.search));
res.json(await Project.findAllMySQLFts(search, orgUid));
}
} else {
res.json(
await Project.findAll({
include: [
ProjectLocation,
Qualification,
Vintage,
CoBenefit,
RelatedProject,
],
}),
);
const query = {
include: [
ProjectLocation,
Qualification,
Vintage,
CoBenefit,
RelatedProject,
],
};

if (orgUid) {
query.where = {
orgUid,
};
}
res.json(await Project.findAll(query));
}
};

Expand Down
49 changes: 38 additions & 11 deletions src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,54 @@ class Project extends Model {
Project.hasMany(ProjectLocation);
}

static findAllMySQLFts(queryStr) {
const sql = `SELECT * FROM projects WHERE MATCH(
warehouseProjectId, currentRegistry, registryOfOrigin, program, projectName,
projectLink, projectDeveloper, sector, projectType, NDCLinkage, projectStatus,
unitMetric, methodology, methodologyVersion, validationApproach, projectTag,
estimatedAnnualAverageEmissionReduction, owner
) AGAINST ":search" ORDER BY relevance DESC`;
static findAllMySQLFts(searchStr, orgUid) {
let sql = `
SELECT * FROM projects WHERE MATCH (
warehouseProjectId,
currentRegistry,
registryOfOrigin,
program,
projectName,
projectLink,
projectDeveloper,
sector,
projectType,
NDCLinkage,
projectStatus,
unitMetric,
methodology,
methodologyVersion,
validationApproach,
projectTag,
estimatedAnnualAverageEmissionReduction
) AGAINST ":search"
`;

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

sql = `${sql} ORDER BY relevance DESC`;

return sequelize.query(sql, {
model: Project,
replacements: { search: queryStr },
replacements: { search: searchStr, orgUid },
mapToModel: true, // pass true here if you have any mapped fields
});
}

static findAllSqliteFts(queryStr) {
const sql = `SELECT * FROM projects_fts WHERE projects_fts MATCH :search ORDER BY rank DESC`;
static findAllSqliteFts(searchStr, orgUid) {
let sql = `SELECT * FROM projects_fts WHERE projects_fts MATCH :search`;

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

sql = `${sql} ORDER BY rank DESC`;

return sequelize.query(sql, {
model: Project,
replacements: { search: `${queryStr}*` },
replacements: { search: `${searchStr}*`, orgUid },
mapToModel: true, // pass true here if you have any mapped fields
});
}
Expand Down
3 changes: 3 additions & 0 deletions src/models/projects/projects.modeltypes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ module.exports = {
primaryKey: true,
autoIncrement: true,
},
// The orgUid is teh singeltonId of the
// organizations tables on the datalayer
orgUid: Sequelize.STRING,
warehouseProjectId: Sequelize.STRING,
projectId: Sequelize.STRING,
projectLocationId: Sequelize.NUMBER,
Expand Down
11 changes: 9 additions & 2 deletions src/models/projects/projects.stub.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[
{
"orgUid": "f1c54511-865e-4611-976c-7c3c1f704662",
"warehouseprojectId": "81e05bfa-e93f-458f-b907-96bf170e52cd",
"currentRegistry": "Feedfire",
"registryOfOrigin": "Skinte",
Expand All @@ -26,6 +27,7 @@
"updatedAt": "11/22/2021"
},
{
"orgUid": "f1c54511-865e-4611-976c-7c3c1f704662",
"warehouseprojectId": "897891e2-cc66-4867-8da2-c17d69d018cb",
"currentRegistry": "Feedfire",
"registryOfOrigin": "Skinte",
Expand All @@ -52,6 +54,7 @@
"updatedAt": "11/22/2021"
},
{
"orgUid": "f1c54511-865e-4611-976c-7c3c1f704662",
"warehouseprojectId": "8a02a620-8be8-44f9-ba90-eff00ccc3a70",
"currentRegistry": "Feedfire",
"registryOfOrigin": "Skinte",
Expand All @@ -78,6 +81,7 @@
"updatedAt": "11/22/2021"
},
{
"orgUid": "f1c54511-865e-4611-976c-7c3c1f704662",
"warehouseprojectId": "11954678-f7a5-47d2-94f8-f4f3138a529c",
"currentRegistry": "Feedfire",
"registryOfOrigin": "Skinte",
Expand All @@ -91,7 +95,7 @@
"projectType": "Topicshots",
"coveredByNDC": 0,
"ndcLinkage": "Shuffletag",
"projectStatus": "I had a friend in high school named Rick Shaw, but he was fairly useless as a mode of transport.",
"projectStatus": "The rusty nail stood erect, I had a friend in high school named Rick Shaw, but he was fairly useless as a mode of transport.",
"projectStatusDate": "8/23/2021",
"unitMetric": "Fuscia",
"methodology": "Quatz",
Expand All @@ -104,6 +108,7 @@
"updatedAt": "11/22/2021"
},
{
"orgUid": "35f92331-c8d7-4e9e-a8d2-cd0a86cbb2cf",
"warehouseprojectId": "634f3d01-ca5c-4c0c-80d4-9dec6e0ec712",
"currentRegistry": "Feedfire",
"registryOfOrigin": "Skinte",
Expand All @@ -130,6 +135,7 @@
"updatedAt": "11/22/2021"
},
{
"orgUid": "35f92331-c8d7-4e9e-a8d2-cd0a86cbb2cf",
"warehouseprojectId": "c28e5850-5ba9-4ac4-9065-1c1195efb9db",
"currentRegistry": "Feedfire",
"registryOfOrigin": "Skinte",
Expand All @@ -156,6 +162,7 @@
"updatedAt": "11/22/2021"
},
{
"orgUid": "35f92331-c8d7-4e9e-a8d2-cd0a86cbb2cf",
"warehouseprojectId": "3b84ef9e-610b-4144-95c7-f5cb6f2c4aa2",
"currentRegistry": "Feedfire",
"registryOfOrigin": "Skinte",
Expand All @@ -169,7 +176,7 @@
"projectType": "Topicshots",
"coveredByNDC": 0,
"ndcLinkage": "Shuffletag",
"projectStatus": "in magna bibendum imperdiet nullam orci pede venenatis non sodales sed tincidunt eu felis fusce posuere felis sed lacus",
"projectStatus": "The rusty nail stood erect, angled at a 45-degree angle, just waiting for the perfect barefoot to come along.",
"projectStatusDate": "8/23/2021",
"unitMetric": "Fuscia",
"methodology": "Quatz",
Expand Down
3 changes: 3 additions & 0 deletions src/models/units/units.modeltypes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ module.exports = {
primaryKey: true,
autoIncrement: true,
},
// The orgUid is teh singeltonId of the
// organizations tables on the datalayer
orgUid: Sequelize.STRING,
buyer: Sequelize.STRING,
registry: Sequelize.STRING,
blockIdentifier: Sequelize.STRING,
Expand Down
5 changes: 5 additions & 0 deletions src/models/units/units.stub.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
{
"id": 1,
"orgUid": "f1c54511-865e-4611-976c-7c3c1f704662",
"unitLink": "https://unit.link",
"buyer": "person 1",
"registry": "registry 1",
Expand All @@ -19,6 +20,7 @@
},
{
"id": 2,
"orgUid": "f1c54511-865e-4611-976c-7c3c1f704662",
"unitLink": "https://unit.link",
"buyer": "person 2",
"registry": "registry 2",
Expand All @@ -37,6 +39,7 @@
},
{
"id": 3,
"orgUid": "35f92331-c8d7-4e9e-a8d2-cd0a86cbb2cf",
"unitLink": "https://unit.link",
"buyer": "person 3",
"registry": "registry 3",
Expand All @@ -55,6 +58,7 @@
},
{
"id": 4,
"orgUid": "f1c54511-865e-4611-976c-7c3c1f704662",
"unitLink": "https://unit.link",
"buyer": "person 4",
"registry": "registry 4",
Expand All @@ -73,6 +77,7 @@
},
{
"id": 5,
"orgUid": "35f92331-c8d7-4e9e-a8d2-cd0a86cbb2cf",
"unitLink": "https://unit.link",
"buyer": "person 5",
"registry": "registry 5",
Expand Down

0 comments on commit f66066d

Please sign in to comment.