Skip to content

Commit

Permalink
feat: improve project search
Browse files Browse the repository at this point in the history
  • Loading branch information
Yakitrak committed Jan 3, 2025
1 parent 15fabbc commit c1cfc2a
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,25 @@ describe('/api/v1/applications', () => {
Region: { [Op.ne]: 'Wales' },
CaseReference: { [Op.notIn]: [] },
[Op.or]: [
{ CaseReference: { [Op.like]: '%London Resort%' } },
{
[Op.or]: [
{ CaseReference: { [Op.like]: '%London%' } },
{ ProjectName: { [Op.like]: '%London%' } },
{ PromoterName: { [Op.like]: '%London%' } }
[Op.and]: [
{
ProjectName: { [Op.like]: `%London%` }
},
{
ProjectName: { [Op.like]: `%Resort%` }
}
]
},
{
[Op.or]: [
{ CaseReference: { [Op.like]: '%Resort%' } },
{ ProjectName: { [Op.like]: '%Resort%' } },
{ PromoterName: { [Op.like]: '%Resort%' } }
[Op.and]: [
{
PromoterName: { [Op.like]: `%London%` }
},
{
PromoterName: { [Op.like]: `%Resort%` }
}
]
}
]
Expand Down Expand Up @@ -235,11 +242,19 @@ describe('/api/v1/applications', () => {
}
],
[Op.or]: [
{ CaseReference: { [Op.like]: '%Nuclear%' } },
{
[Op.or]: [
{ CaseReference: { [Op.like]: '%Nuclear%' } },
{ ProjectName: { [Op.like]: '%Nuclear%' } },
{ PromoterName: { [Op.like]: '%Nuclear%' } }
[Op.and]: [
{
ProjectName: { [Op.like]: `%Nuclear%` }
}
]
},
{
[Op.and]: [
{
PromoterName: { [Op.like]: `%Nuclear%` }
}
]
}
]
Expand Down Expand Up @@ -338,20 +353,17 @@ describe('/api/v1/applications', () => {
AND: [
{
OR: [
{ projectName: { contains: 'London Resort' } },
{ caseReference: { contains: 'London Resort' } },
{
OR: [
{ applicant: { organisationName: { contains: 'London' } } },
{ applicant: { firstName: { contains: 'London' } } },
{ applicant: { lastName: { contains: 'London' } } }
AND: [
{ projectName: { contains: 'London' } },
{ projectName: { contains: 'Resort' } }
]
},
{
OR: [
{ applicant: { organisationName: { contains: 'Resort' } } },
{ applicant: { firstName: { contains: 'Resort' } } },
{ applicant: { lastName: { contains: 'Resort' } } }
AND: [
{ applicant: { organisationName: { contains: 'London' } } },
{ applicant: { organisationName: { contains: 'Resort' } } }
]
}
]
Expand All @@ -377,21 +389,23 @@ describe('/api/v1/applications', () => {
AND: [
{
OR: [
{ projectName: { contains: 'London Resort' } },
{ caseReference: { contains: 'London Resort' } },
{ projectNameWelsh: { contains: 'London Resort' } },
{
OR: [
{ applicant: { organisationName: { contains: 'London' } } },
{ applicant: { firstName: { contains: 'London' } } },
{ applicant: { lastName: { contains: 'London' } } }
AND: [
{ projectName: { contains: 'London' } },
{ projectName: { contains: 'Resort' } }
]
},
{
AND: [
{ projectNameWelsh: { contains: 'London' } },
{ projectNameWelsh: { contains: 'Resort' } }
]
},
{
OR: [
{ applicant: { organisationName: { contains: 'Resort' } } },
{ applicant: { firstName: { contains: 'Resort' } } },
{ applicant: { lastName: { contains: 'Resort' } } }
AND: [
{ applicant: { organisationName: { contains: 'London' } } },
{ applicant: { organisationName: { contains: 'Resort' } } }
]
}
]
Expand Down Expand Up @@ -425,15 +439,15 @@ describe('/api/v1/applications', () => {
AND: [
{
OR: [
{ projectName: { contains: 'Nuclear' } },
{ caseReference: { contains: 'Nuclear' } },
{ projectNameWelsh: { contains: 'Nuclear' } },
{
OR: [
{ applicant: { organisationName: { contains: 'Nuclear' } } },
{ applicant: { firstName: { contains: 'Nuclear' } } },
{ applicant: { lastName: { contains: 'Nuclear' } } }
]
AND: [{ projectName: { contains: 'Nuclear' } }]
},
{
AND: [{ projectNameWelsh: { contains: 'Nuclear' } }]
},
{
AND: [{ applicant: { organisationName: { contains: 'Nuclear' } } }]
}
]
},
Expand Down Expand Up @@ -577,15 +591,15 @@ describe('/api/v1/applications', () => {
AND: [
{
OR: [
{ projectName: { contains: 'Nuclear' } },
{ caseReference: { contains: 'Nuclear' } },
{ projectNameWelsh: { contains: 'Nuclear' } },
{
OR: [
{ applicant: { organisationName: { contains: 'Nuclear' } } },
{ applicant: { firstName: { contains: 'Nuclear' } } },
{ applicant: { lastName: { contains: 'Nuclear' } } }
]
AND: [{ projectName: { contains: 'Nuclear' } }]
},
{
AND: [{ projectNameWelsh: { contains: 'Nuclear' } }]
},
{
AND: [{ applicant: { organisationName: { contains: 'Nuclear' } } }]
}
]
},
Expand Down Expand Up @@ -620,11 +634,19 @@ describe('/api/v1/applications', () => {
}
],
[Op.or]: [
{ CaseReference: { [Op.like]: '%Nuclear%' } },
{
[Op.or]: [
{ CaseReference: { [Op.like]: '%Nuclear%' } },
{ ProjectName: { [Op.like]: '%Nuclear%' } },
{ PromoterName: { [Op.like]: '%Nuclear%' } }
[Op.and]: [
{
ProjectName: { [Op.like]: `%Nuclear%` }
}
]
},
{
[Op.and]: [
{
PromoterName: { [Op.like]: `%Nuclear%` }
}
]
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,23 @@ describe('project repository', () => {
AND: [
{
OR: [
{ projectName: { contains: searchTerm } },
{ caseReference: { contains: searchTerm } },
{ projectNameWelsh: { contains: searchTerm } },
{
OR: [
{ applicant: { organisationName: { contains: 'test' } } },
{ applicant: { firstName: { contains: 'test' } } },
{ applicant: { lastName: { contains: 'test' } } }
AND: [
{ projectName: { contains: searchTerm.split(' ')[0] } },
{ projectName: { contains: searchTerm.split(' ')[1] } }
]
},
{
OR: [
{ applicant: { organisationName: { contains: 'search' } } },
{ applicant: { firstName: { contains: 'search' } } },
{ applicant: { lastName: { contains: 'search' } } }
AND: [
{ projectNameWelsh: { contains: searchTerm.split(' ')[0] } },
{ projectNameWelsh: { contains: searchTerm.split(' ')[1] } }
]
},
{
AND: [
{ applicant: { organisationName: { contains: searchTerm.split(' ')[0] } } },
{ applicant: { organisationName: { contains: searchTerm.split(' ')[1] } } }
]
}
]
Expand Down Expand Up @@ -141,15 +143,15 @@ describe('project repository', () => {
AND: [
{
OR: [
{ projectName: { contains: searchTerm } },
{ caseReference: { contains: searchTerm } },
{ projectNameWelsh: { contains: searchTerm } },
{
OR: [
{ applicant: { organisationName: { contains: searchTerm } } },
{ applicant: { firstName: { contains: searchTerm } } },
{ applicant: { lastName: { contains: searchTerm } } }
]
AND: [{ projectName: { contains: searchTerm } }]
},
{
AND: [{ projectNameWelsh: { contains: searchTerm } }]
},
{
AND: [{ applicant: { organisationName: { contains: searchTerm } } }]
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,19 @@ describe('project ni repository', () => {
CaseReference: { [Op.notIn]: [] },
DateOfDCOSubmission: { [Op.gt]: 0, [Op.ne]: null },
[Op.or]: [
{ CaseReference: { [Op.like]: '%foo%' } },
{
[Op.or]: [
{ CaseReference: { [Op.like]: '%foo%' } },
{ ProjectName: { [Op.like]: '%foo%' } },
{ PromoterName: { [Op.like]: '%foo%' } }
[Op.and]: [
{
ProjectName: { [Op.like]: `%foo%` }
}
]
},
{
[Op.and]: [
{
PromoterName: { [Op.like]: `%foo%` }
}
]
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { prismaClient } = require('../lib/prisma');
const { featureFlag } = require('../lib/config');
const { english: stopWordList } = require('../utils/stopwords');

const getByCaseReference = async (caseReference) => {
return prismaClient.project.findUnique({
Expand Down Expand Up @@ -29,21 +30,31 @@ const getAllApplications = async (options = {}) => {
}

if (searchTerm) {
const terms = options.searchTerm.split(' ');
const terms = options.searchTerm
.split(' ')
.filter((term) => !stopWordList.includes(term.toLowerCase()));
where['AND'].push({
OR: [
{ projectName: { contains: searchTerm } },
{ caseReference: { contains: searchTerm } },
{ caseReference: { contains: options.searchTerm } },
{
AND: terms.map((term) => ({
projectName: { contains: term }
}))
},
...(featureFlag.allowWelshTranslation
? [{ projectNameWelsh: { contains: searchTerm } }]
? [
{
AND: terms.map((term) => ({
projectNameWelsh: { contains: term }
}))
}
]
: []),
...terms.map((term) => ({
OR: [
{ applicant: { organisationName: { contains: term } } },
{ applicant: { firstName: { contains: term } } },
{ applicant: { lastName: { contains: term } } }
]
}))
{
AND: terms.map((term) => ({
applicant: { organisationName: { contains: term } }
}))
}
]
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const db = require('../models');
const { Op } = require('sequelize');
const { pick } = require('lodash');
const { mapNISearchTermToQuery } = require('../utils/queries');
const config = require('../lib/config');
const { english: stopWordList } = require('../utils/stopwords');
const getApplication = async (caseReference) =>
db.Project.findOne({ where: { CaseReference: caseReference } });

Expand Down Expand Up @@ -33,17 +33,30 @@ const getAllApplications = async (options = {}) => {
filterStatements.push({ [Op.or]: sectorStatements });
}

const searchTermStatements = mapNISearchTermToQuery(searchTerm, [
'CaseReference',
'ProjectName',
'PromoterName'
]);

// build where clause
if (filterStatements.length > 0)
findAllOptions.where = { ...findAllOptions.where, [Op.and]: filterStatements };
if (searchTermStatements)
if (searchTerm) {
const terms = searchTerm
.split(' ')
.filter((term) => !stopWordList.includes(term.toLowerCase()));
const searchTermStatements = {
[Op.or]: [
{ CaseReference: { [Op.like]: `%${searchTerm}%` } },
{
[Op.and]: terms.map((term) => ({
ProjectName: { [Op.like]: `%${term}%` }
}))
},
{
[Op.and]: terms.map((term) => ({
PromoterName: { [Op.like]: `%${term}%` }
}))
}
]
};
findAllOptions.where = { ...findAllOptions.where, ...searchTermStatements };
}
const { rows, count } = await db.Project.findAndCountAll({
...findAllOptions,
raw: true
Expand Down

0 comments on commit c1cfc2a

Please sign in to comment.