-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathserver.js
More file actions
618 lines (606 loc) · 19.2 KB
/
Copy pathserver.js
File metadata and controls
618 lines (606 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
require("dotenv").config();
const express = require("express");
const fs = require("fs");
const quote = require("regexp-quote");
const path = require("path");
const manifest = require("../ssr/ssr-manifest.json");
const appPath = path.join(__dirname, "../ssr", manifest["app.js"]);
const renderer = require(appPath).default;
const pageSize = 20;
const versionFile = `${__dirname}/../version.txt`;
const version = fs.existsSync(versionFile)
? fs.readFileSync(versionFile, "utf8")
: "0";
const axios = require("axios");
const ini = require("ini");
// Load query rules from queryrules.ini at startup
const queryRulesPath = path.join(__dirname, "queryrules.ini");
let queryRules = {};
if (fs.existsSync(queryRulesPath)) {
const parsed = ini.parse(fs.readFileSync(queryRulesPath, "utf8"));
for (const [name, rule] of Object.entries(parsed)) {
try {
queryRules[name] = {
chip: rule.Chip,
keywords: String(rule.Keywords || "")
.split(/,\s*/)
.filter(Boolean),
query: JSON.parse(rule.Query),
};
if (rule.Filters) {
try {
queryRules[name].filters = JSON.parse(rule.Filters);
} catch (e) {
console.error(`Failed to parse filters for rule ${name}:`, e);
}
}
} catch (e) {
console.error(`Failed to parse query rule ${name}:`, e);
}
}
}
// Disabled for now because it causes confusion when we update the data
// const cache = {};
module.exports = async function ({ plants, nurseries }) {
const port = process.env.PORT || 3000;
const app = express();
const publicDir = `${__dirname}/../public`;
if (!fs.existsSync(publicDir)) {
fs.mkdirSync(publicDir);
}
app.use(express.static(publicDir));
app.use("/images", express.static(`${__dirname}/../images`));
app.use(express.json());
app.post("/get-vendors", async (req, res) => {
const baseUrl = process.env.PAC_API_BASE_URL;
const apiKey = process.env.PAC_API_KEY;
const url = `${baseUrl}/Plant/FindVendorsForPlantName`;
let response = await axios.get(url, {
headers: { Authorization: "Bearer " + apiKey },
params: { plantName: req.body.plantName, zipCode: req.body.zipCode, radius: req.body.radius, limit: 5}
});
return res.send(response.data);
});
app.post("/get-city", async (req, res) => {
//console.log(req.params)
//call vendor/FindZip
const baseUrl = process.env.PAC_API_BASE_URL;
const apiKey = process.env.PAC_API_KEY;
const url = `${baseUrl}/vendor/FindCity`;
let response = await axios.get(url, {
headers: { Authorization: "Bearer " + apiKey },
params: {zipCode: req.body.zipCode}
});
return res.send(response.data);
});
app.post("/get-zip", async (req, res) => {
const baseUrl = process.env.PAC_API_BASE_URL;
const apiKey = process.env.PAC_API_KEY;
const url = `${baseUrl}/vendor/FindZip`;
let response = await axios.get(url, {
headers: { Authorization: "Bearer " + apiKey },
params: {lat: req.body.latitude, lng: req.body.longitude}
});
return res.send(response.data);
});
// Provide query rules to the frontend
app.get("/api/v1/queryrules", (req, res) => {
res.json(queryRules);
});
app.get("/api/v1/plants", async (req, res) => {
try {
const fetchResults = req.query.results !== "0";
const fetchTotal = req.query.total !== "0";
const query = {};
const ruleChips = [];
const rulesParam = req.query.rules;
if (rulesParam) {
const names = Array.isArray(rulesParam)
? rulesParam
: String(rulesParam).split(/,/);
for (const name of names) {
const rule = queryRules[name];
if (rule) {
if (!query.$and) query.$and = [];
query.$and.push(rule.query);
ruleChips.push({ name, chip: rule.chip });
}
}
}
const sorts = {
"Sort by Common Name (A-Z)": {
"Common Name": 1,
},
"Sort by Common Name (Z-A)": {
"Common Name": -1,
},
"Sort by Scientific Name (A-Z)": {
"Scientific Name": 1,
},
"Sort by Scientific Name (Z-A)": {
"Scientific Name": -1,
},
"Sort by Recommendation Score": {
"Recommendation Score": -1,
"Common Name": 1,
},
"Sort by Flower Color": {
"Flower Color": 1,
},
"Sort by Height (L-H)": {
"Average Height": 1,
},
"Sort by Height (H-L)": {
"Average Height": -1,
},
"Sort by Soil Moisture (Dry to Wet)": {
"Soil Moisture Flags": 1,
},
"Sort by Soil Moisture (Wet to Dry)": {
"Soil Moisture Flags": -1,
},
};
const sort = sorts[req.query.sort]
? sorts[req.query.sort]
: Object.values(sorts)[0];
let projection;
let page = parseInt(req.query.page);
if (isNaN(page) || page < 1) {
page = 1;
}
if (req.query.q && req.query.q.length) {
// Use regex search instead of text search to avoid text index issues
const searchTerm = req.query.q;
const searchRegex = new RegExp(searchTerm, 'i');
// Search in both Common Name and Scientific Name
query.$or = [
{ 'Common Name': searchRegex },
{ 'Scientific Name': searchRegex }
];
console.log('Search query:', JSON.stringify(query));
// Original text search approach (commented out)
// query.$text = {
// $search: req.query.q,
// };
// projection = {
// score: {
// $meta: 'textScore'
// }
// };
// sort = {
// score: {
// $meta: 'textScore'
// }
// };
}
const doc = (
await plants
.find({})
.project({ "Height (feet) By Number": 1 })
.sort({ "Height (feet) By Number": -1 })
.limit(1)
.toArray()
)[0];
const heightsOfTallest = doc && doc["Height (feet) By Number"];
let maxHeight =
(heightsOfTallest && heightsOfTallest[heightsOfTallest.length - 1]) ||
0;
const heights = [];
for (let i = 0; i <= maxHeight; i++) {
heights.push(i);
}
const filters = [
{
name: "States",
value: [],
array: true,
},
{
name: "Sun Exposure Flags",
value: [],
array: true,
},
{
name: "Soil Moisture Flags",
value: [],
array: true,
},
{
name: "Plant Type Flags",
value: [],
array: true,
},
{
name: "Life Cycle Flags",
value: [],
array: true,
},
{
name: "Pollinator Flags",
value: [],
ignore: ["Wind"],
array: true,
},
{
name: "Superplant",
choices: ["Super Plant"],
value: [],
boolean: true,
},
{
name: "Flower Color Flags",
value: [],
array: true,
},
{
name: "Availability Flags",
value: [],
array: true,
},
{
name: "Flowering Months",
range: true,
byNumber: "Flowering Months By Number",
choices: [
"jan",
"feb",
"mar",
"apr",
"may",
"jun",
"jul",
"aug",
"sep",
"oct",
"nov",
"dec",
],
value: [],
},
{
name: "Height (feet)",
range: true,
byNumber: "Height (feet) By Number",
choices: heights,
value: [],
},
{
name: "Showy",
choices: ["Showy"],
value: [],
boolean: true,
},
];
const $and = [];
for (const filter of filters) {
if (filter.range) {
const min = parseInt(req.query?.[filter.name]?.min);
const max = parseInt(req.query?.[filter.name]?.max);
// Ignore invalid queries. Also ignore when the range is all possible values,
// which is currently the only way to include plants for which there is
// no flowering months data
if (
!isNaN(min) &&
!isNaN(max) &&
min <= max &&
(min !== 0 || max !== filter.choices.length - 1)
) {
const $in = [];
for (let i = min; i <= max; i++) {
$in.push(i);
}
if ($in.length) {
$and.push({
[filter.byNumber]: {
$in,
},
});
}
}
} else if (filter.boolean) {
// For frontend convenience it arrives as an array, but
// we only care if it has an element or not
let input = req.query[filter.name] || [];
if (input && input.length) {
$and.push({
[filter.name]: true,
});
}
} else if (filter.array) {
const input = req.query[filter.name] || [];
const array = Array.isArray(input) ? input : [input];
const values = array.map((value) => value.toString());
if (values.length) {
$and.push({
[filter.name]: {
$in: values,
},
});
}
} else {
const input = req.query[filter.name] || [];
const array = Array.isArray(input) ? input : [input];
const values = array.map((value) => value.toString());
const $or = values
.map(
(value) =>
new RegExp(`(?:^|;|,)\\s*${quote(value)}\\s*(?:;|,|$)`, "i")
)
.map((value) => ({
[filter.name]: value,
}));
if ($or.length) {
$and.push({
$or,
});
}
}
}
// If we are sorting by a field we should not return results for which
// its value is unknown, it's too confusing
Object.keys(sort).forEach((key) => {
$and.push({
[key]: {
$exists: 1,
},
});
});
if (Array.isArray(req.query.favorites)) {
$and.push({
_id: {
$in: req.query.favorites.map((v) =>
typeof v == "string" ? v : ""
),
},
});
}
if ($and.length) {
query.$and = $and;
}
const total = fetchTotal && (await plants.countDocuments(query));
const qb = plants.find(query);
if (projection) {
qb.project(projection);
}
if (!req.query.favorites) {
qb.skip((page - 1) * pageSize);
qb.limit(pageSize);
}
const results = fetchResults && (await qb.sort(sort).toArray());
if (!req.query.favorites) {
for (const filter of filters) {
if (filter.array) {
const aQuery = [
{
$match: {
...query,
$and: query.$and.filter(
(clause) => clause[filter.name] === undefined
),
},
},
{
$unwind: `$${filter.name}`,
},
{
$group: {
_id: `$${filter.name}`,
count: { $sum: 1 },
},
},
];
filter.counts = await plants.aggregate(aQuery).toArray();
} else if (filter.boolean) {
filter.counts = await plants
.aggregate([
{
$match: {
...query,
$and: query.$and.filter(
(clause) => clause[filter.name] === undefined
),
},
},
{
$group: {
_id: `$${filter.name}`,
count: {
$sum: 1,
},
},
},
])
.toArray();
const trueChoice = filter.counts.find((count) => !!count._id);
if (trueChoice) {
trueChoice._id = filter.choices[0];
} else {
filter.counts.push({
_id: filter.choices[0],
count: 0,
});
}
}
if (!filter.choices) {
if (filter.counts) {
// Avoid redundant query
filter.choices = filter.counts.map((count) => count._id);
filter.choices.sort();
} else {
if (filter.byNumber) {
filter.choices = (
await plants.distinct(filter.byNumber)
).filter((choice) => typeof choice === "number");
} else {
filter.choices = (await plants.distinct(filter.name)).filter(
(choice) => typeof choice === "string" && choice.length
);
}
}
}
}
}
let response = {
results,
total,
};
if (!req.query.favorites) {
response = {
...response,
choices: Object.fromEntries(
filters.map((filter) => [filter.name, filter.choices])
),
counts: Object.fromEntries(
filters.map((filter) => [
filter.name,
filter.counts &&
Object.fromEntries(
filter.counts.map((item) => [item._id, item.count])
),
])
),
};
}
for (const filter of filters) {
const choices = response.choices && response.choices[filter.name];
if (choices && filter.ignore) {
response.choices[filter.name] = choices.filter(
(choice) => !filter.ignore.includes(choice)
);
}
const counts = response.counts && response.counts[filter.name];
if (counts && filter.ignore) {
response.counts[filter.name] = Object.fromEntries(
Object.entries(counts).filter(
(value, count) => !filter.ignore.includes(value)
)
);
}
}
if (ruleChips.length) {
response.ruleChips = ruleChips;
}
// setCache(req, response);
return res.send(response);
} catch (e) {
console.error("error:", e);
return res.status(500).send(e);
}
});
app.get("/api/v1/plants/:name", async (req, res) => {
const result = await plants.findOne({
_id: req.params.name,
});
if (!result) {
return res.status(404).send("Not Found");
} else {
return res.send(result);
}
});
app.get("/api/v1/nurseries", async (req, res) => {
console.log("PARAMS", req.params)
const baseUrl = process.env.PAC_API_BASE_URL;
const apiKey = process.env.PAC_API_KEY;
const url = `${baseUrl}/Vendor/FindByState`;
let response = await axios.get(url, {
headers: { Authorization: "Bearer " + apiKey },
params: { state: req.query.state }
});
return res.send({results: response.data.map(x => {
return {
"SOURCE": x.storeName,
"Lat": x.lat,
"Long": x.lng,
"lon": x.lng,
"lat": x.lat,
"PHONE": x.publicPhone,
"URL": x.storeUrl,
"ADDRESS": x.address,
"STATE": x.state,
"EMAIL": x.publicEmail
}
})});
});
app.get("*", async (req, res) => {
const appContent = await renderer({ port, url: req.url });
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="stylesheet" href="${manifest["app.css"]}?version=${version}" />
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A==" crossorigin="" />
<link rel="icon" href="/favicon.ico">
<link rel="icon" href="/assets/images/logo-32x32.png" sizes="32x32" />
<link rel="icon" href="/assets/images/logo-192x192.png" sizes="192x192" />
<link rel="apple-touch-icon" href="/assets/images/logo-192x192.png" />
<meta name="msapplication-TileImage" content="/assets/images/logo-192x192.png" />
<meta property="og:title" content="Choose Native Plants" />
<meta property="og:description" content="Find out which native shrubs, plants and flowers have the right conditions to flourish in your garden." />
<meta property="og:image" content="https://choosenativeplants.com/assets/images/og-image.png" />
<meta property="og:url" content="https://choosenativeplants.com/" />
<meta property="og:site_name" content="Choose Native Plants" />
<meta name="twitter:image" content="https://choosenativeplants.com/assets/images/twitter-large-image.png" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:site" content="@CodeForPhilly" />
<meta name="twitter:title" content="Choose Native Plants" />
<meta name="twitter:description" content="Find out which native shrubs, plants and flowers from Pennsylvania have the right conditions to flourish in your garden." />
<meta name="twitter:image:alt" content="Choose Native Plants logo" />
<title>Choose Native Plants</title>
</head>
<body>
<noscript>
<strong>We're sorry but this application doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app">${appContent}</div>
<script src="/js/chunk-vendors.js?version=${version}"></script>
<script src="/js/app.js?version=${version}"></script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-CHCN0FC8JD"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-CHCN0FC8JD');
</script>
</body>
</html>
`.trim();
res.end(html);
});
console.log(`Listening on port ${port}`);
app.listen(port);
return app;
};
// function hashKey(req) {
// return JSON.stringify(req.query);
// }
// function getCache(req) {
// const key = hashKey(req);
// if (cache[key]) {
// cache[key].last = Date.now();
// return cache[key].data;
// }
// }
// function setCache(req, data) {
// cache[hashKey(req)] = {
// last: Date.now(),
// data
// };
// if (Object.keys(cache).length > 100) {
// const oldest = Object.entries(cache, (a, [key, value]) => {
// if ((!a) || (value.last < a.last)) {
// return [key, value];
// }
// }, null);
// if (oldest) {
// // console.log('Deleting:', oldest[0]);
// delete cache[oldest[0]];
// }
// }
// // Object.keys(cache).map(key => {
// // console.log(`${key} ${cache[key].last}`);
// // });
// }