Skip to content

Commit 766ae94

Browse files
committed
fix build and move search to netlify
1 parent dffb5bc commit 766ae94

File tree

4 files changed

+71
-80
lines changed

4 files changed

+71
-80
lines changed

netlify/functions/requestSlackInvite.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const Archetype = require('archetype');
44
const extrovert = require('extrovert');
5-
const requestInvite = require('../src/actions/requestInvite');
5+
const requestInvite = require('../../src/actions/requestInvite');
66

77
const RequestSlackInviteParams = new Archetype({
88
email: {
Lines changed: 67 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,68 @@
1-
'use strict';
2-
3-
const cheerio = require('cheerio');
4-
const config = require('../.config');
5-
const mongoose = require('mongoose');
6-
7-
let conn = null;
8-
const contentSchema = new mongoose.Schema({
9-
title: { type: String, required: true },
10-
body: { type: String, required: true },
11-
url: { type: String, required: true },
12-
version: { type: String },
13-
versionNumber: { type: Number }
14-
});
15-
16-
module.exports = async function search(context, req) {
17-
let Content;
18-
if (conn == null) {
19-
conn = mongoose.createConnection(config.uri);
20-
await conn.asPromise();
21-
}
22-
23-
Content = conn.model('Content', contentSchema, 'Content');
24-
25-
const query = req.query.search.toString();
26-
const version = req.query.version ? +req.query.version.replace(/\.x$/, '') : null;
27-
let results = await Content.aggregate([
28-
{
29-
$search: {
30-
index: 'mongoose-content',
31-
compound: {
32-
must: [
33-
...(version ? [{
34-
equals: {
35-
path: 'versionNumber',
36-
value: version
37-
}
38-
}] : []),
39-
{ text: { query, path: { wildcard: '*' }, fuzzy: {} } }
40-
]
41-
}
42-
}
43-
},
44-
{ $limit: 10 }
45-
]);
46-
47-
results = results.map(doc => {
48-
const $ = cheerio.load(doc.body);
49-
50-
doc.body = $('p').get(0) ? $('p').first().html() : doc.body;
51-
52-
return doc;
53-
});
54-
55-
context.res = {
56-
// status: 200, /* Defaults to 200 */
57-
body: JSON.stringify({ results })
58-
};
1+
'use strict';
2+
3+
const Archetype = require('archetype');
4+
const cheerio = require('cheerio');
5+
const extrovert = require('extrovert');
6+
const mongoose = require('mongoose');
7+
8+
let conn = null;
9+
const contentSchema = new mongoose.Schema({
10+
title: { type: String, required: true },
11+
body: { type: String, required: true },
12+
url: { type: String, required: true },
13+
version: { type: String },
14+
versionNumber: { type: Number }
15+
});
16+
17+
const SearchParams = new Archetype({
18+
version: {
19+
$type: 'string'
20+
},
21+
search: {
22+
$type: 'string',
23+
$required: true
24+
}
25+
})
26+
27+
module.exports = extrovert.toNetlifyFunction(async function search(params) {
28+
params = new SearchParams(params);
29+
let Content;
30+
if (conn == null) {
31+
conn = mongoose.createConnection(config.uri);
32+
await conn.asPromise();
33+
}
34+
35+
Content = conn.model('Content', contentSchema, 'Content');
36+
37+
const query = params.search;
38+
const version = params.version ? +params.version.replace(/\.x$/, '') : null;
39+
let results = await Content.aggregate([
40+
{
41+
$search: {
42+
index: 'mongoose-content',
43+
compound: {
44+
must: [
45+
...(version ? [{
46+
equals: {
47+
path: 'versionNumber',
48+
value: version
49+
}
50+
}] : []),
51+
{ text: { query, path: { wildcard: '*' }, fuzzy: {} } }
52+
]
53+
}
54+
}
55+
},
56+
{ $limit: 10 }
57+
]);
58+
59+
results = results.map(doc => {
60+
const $ = cheerio.load(doc.body);
61+
62+
doc.body = $('p').get(0) ? $('p').first().html() : doc.body;
63+
64+
return doc;
65+
});
66+
67+
return { results };
5968
}

search/function.json

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/actions/requestInvite.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

33
const axios = require('axios');
4-
const config = require('../../.config/development');
4+
5+
const slackToken = process.env.SLACK_TOKEN;
56

67
const url = 'https://slack.com/api/chat.postMessage';
78
const emailRegexp = /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+$/;
@@ -26,7 +27,7 @@ module.exports = () => async function requestInvite(email) {
2627
}
2728
},
2829
]
29-
}, { headers: { authorization: `Bearer ${config.slackToken}` } });
30+
}, { headers: { authorization: `Bearer ${slackToken}` } });
3031

3132
return { success: true };
3233
}

0 commit comments

Comments
 (0)