Skip to content

Commit

Permalink
[GD-1290] [GD-1372] Add fuzzy search to nonprofit search by name.
Browse files Browse the repository at this point in the history
  • Loading branch information
cj.ohara committed Jan 30, 2019
1 parent 5273167 commit c48fef6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/lambda/bin/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ const seedNonprofits = function () {
}
]).then(function (answers) {
const count = parseInt(answers.count);
const nonprofits = generator.modelCollection('nonprofit', count, {donationsCount: 0, donationsFees: 0, donationsFeesCovered: 0, donationsSubtotal: 0, donationsTotal: 0});
const nonprofits = generator.modelCollection('nonprofit', count, {donationsCount: 0, donationsFees: 0, donationsFeesCovered: 0, donationsSubtotal: 0, donationsTotal: 0, status: 'ACTIVE'});

_.each(nonprofits, function (nonprofit) {
const slideCount = Math.floor(Math.random() * 8) + 1;
Expand Down
6 changes: 6 additions & 0 deletions packages/lambda/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/lambda/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"change-case": "^3.0.1",
"commander": "^2.11.0",
"faker": "^4.1.0",
"fuse.js": "^3.3.0",
"fuzzy": "^0.1.3",
"inquirer": "^3.2.1",
"inquirer-autocomplete-prompt": "^0.11.0",
Expand Down
30 changes: 19 additions & 11 deletions packages/lambda/src/api/searchNonprofits/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 Firespring, Inc.
* Copyright 2019 Firespring, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,25 +14,26 @@
* limitations under the License.
*/

const Fuse = require('fuse.js');
const HttpException = require('./../../exceptions/http');
const NonprofitsRepository = require('./../../repositories/nonprofits');
const ResourceNotFoundException = require('./../../exceptions/resourceNotFound');
const Request = require('./../../aws/request');
const SettingsRepository = require('./../../repositories/settings');
const SettingHelper = require('./../../helpers/setting');

exports.handle = function (event, context, callback) {
exports.handle = (event, context, callback) => {
const repository = new NonprofitsRepository();
const request = new Request(event, context);
const settingsRepository = new SettingsRepository();
const includeMatchFund = parseInt(request.queryParam('includeMatchFund', 1));
let matchFundNonprofitUuid = null;

request.validate().then(function () {
request.validate().then(() => {
if (!includeMatchFund) {
return settingsRepository.get(SettingHelper.SETTING_MATCH_FUND_NONPROFIT_UUID).then(function (setting) {
return settingsRepository.get(SettingHelper.SETTING_MATCH_FUND_NONPROFIT_UUID).then(setting => {
matchFundNonprofitUuid = setting.value;
}).catch(function (err) {
}).catch(err => {
if (err instanceof ResourceNotFoundException) {
return Promise.resolve();
} else {
Expand All @@ -42,28 +43,35 @@ exports.handle = function (event, context, callback) {
}

return Promise.resolve();
}).then(function () {
}).then(() => {
if (!request.queryParam('category', false) && !request.queryParam('legalName', false) && !request.queryParam('status', false)) {
return Promise.reject(new Error('Missing required query parameter: category, legalName or status'));
}
if (request.queryParam('category', false)) {
return repository.search(['category1', 'category2', 'category3'], request.queryParam('category'), transformFilters(request.queryParamsExcept(['category', 'c']), matchFundNonprofitUuid));
}
if (request.queryParam('legalName', false)) {
return repository.search(['legalNameSearch'], request.queryParam('legalName'), transformFilters(request.queryParamsExcept(['legalName', 'c']), matchFundNonprofitUuid));
return repository.getAll().then(nonprofits => {
const options = {
keys: ['legalName'],
threshold: 0.3,
};
const fuse = new Fuse(nonprofits, options);
return fuse.search(request.queryParam('legalName'));
});
}
return repository.search(['status'], request.queryParam('status'), transformFilters(request.queryParamsExcept(['status', 'c']), matchFundNonprofitUuid));
}).then(function (response) {
const results = response.map(function (model) {
}).then(response => {
const results = response.map(model => {
return model.all();
});
callback(null, results);
}).catch(function (err) {
}).catch(err => {
(err instanceof HttpException) ? callback(err.context(context)) : callback(err);
});
};

const transformFilters = function (filters, matchFundNonprofitUuid) {
const transformFilters = (filters, matchFundNonprofitUuid) => {
if (filters.hasOwnProperty('legalName')) {
filters.legalNameSearch = filters.legalName;
delete filters.legalName;
Expand Down

0 comments on commit c48fef6

Please sign in to comment.