-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
212 lines (178 loc) · 5.75 KB
/
api.js
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
const config = require('./config');
const Router = require('express').Router();
const request = require('request');
const Redis = require('ioredis');
const Moment = require('moment');
const MAIN_CALL = `https://cdn.contentful.com/spaces/${config.CONTENTFUL_SPACE_ID}/entries?access_token=${config.CONTENTFUL_API_TOKEN}`;
const ENTRIES_CALL = `${MAIN_CALL}&include=1&content_type=site&order=-fields.date`;
const ART_CALL = `${MAIN_CALL}&include=1&content_type=art&order=-fields.date`;
const SHOWS_CALL = `${MAIN_CALL}&include=1&content_type=show&order=-fields.date`;
const CATS_CALL = `${MAIN_CALL}&include=1&content_type=categories`;
const EXPIRATION = 60 * 60;
const mailgun = require('mailgun-js')({
apiKey: config.MAILGUN_KEY,
domain: config.MAILGUN_DOMAIN
})
console.log(config);
let redis = new Redis({
hostname: config.REDIS_HOST,
port: config.REDIS_PORT,
password: config.REDIS_PASS
});
let extractAssetsForEntries = function(items, assets) {
// Return a formatted Data Object with necessary information.
return items.map((item, index, array) => {
let i = 0;
let formattedItem = {
link: item.fields.url,
client: item.fields.client,
agency: item.fields.agency,
roles: item.fields.role,
tech: item.fields.technology,
isActive: item.fields.active,
name: item.fields.name,
date: Moment(item.fields.date, 'YYYY-MM-DD').format()
}
for(i; i < assets.length; i++) {
// Check for the correct matching asset.
if(assets[i].sys.id === item.fields.thumbnail.sys.id) {
formattedItem['thumbnail'] = assets[i].fields.file.url;
break;
}
}
return formattedItem;
})
}
let extractAssetsForArts = function(items, assets) {
// Return a formatted Data Object with necessary information.
return items.map((item, index, array) => {
let i = 0;
let formattedItem = {
title: item.fields.title,
description: item.fields.description,
date: item.fields.date,
id: item.fields.id
}
for(i; i < assets.length; i++) {
// Check for the correct matching asset.
if(assets[i].sys.id === item.fields.image.sys.id) {
formattedItem['image'] = assets[i].fields.file.url;
break;
}
}
return formattedItem;
})
}
let extractAssetsForShows = function(items, assets) {
// Return a formatted Data Object with necessary information.
return items.map((item, index, array) => {
let i = 0;
let formattedItem = {
title: item.fields.title,
venue: item.fields.venue,
date: item.fields.date,
location: item.fields.locationName,
link: item.fields.link,
type: item.fields.type,
coordinates: item.fields.coordinates
}
for(i; i < assets.length; i++) {
// Check for the correct matching asset.
if(item.fields.flyer && assets[i].sys.id === item.fields.flyer.sys.id) {
formattedItem['flyer'] = assets[i].fields.file.url;
break;
}
}
return formattedItem;
})
}
Router.get('/entries', (req, res) => {
redis.get('entries')
.then(result => {
if(result) {
console.log('From Redis');
return res.json(JSON.parse(result));
}
console.log('Calling Contentful');
request.get(ENTRIES_CALL, (error, response, body) => {
let parsedReturn = JSON.parse(body);
let formattedData = extractAssetsForEntries(parsedReturn.items, parsedReturn.includes.Asset);
redis.set('entries', JSON.stringify(formattedData), 'ex', EXPIRATION);
res.json(formattedData);
})
})
})
Router.get('/arts', (req, res) => {
redis.get('arts')
.then(result => {
if(result) {
console.log('From Redis');
return res.json(JSON.parse(result));
}
console.log('Calling Contentful');
request.get(ART_CALL, (error, response, body) => {
let parsedReturn = JSON.parse(body);
let formattedData = extractAssetsForArts(parsedReturn.items, parsedReturn.includes.Asset);
redis.set('arts', JSON.stringify(formattedData), 'ex', EXPIRATION);
res.json(formattedData);
})
})
})
Router.get('/shows', (req, res) => {
redis.get('shows')
.then(result => {
if(result) {
console.log('From Redis');
return res.json(JSON.parse(result));
}
console.log('Calling Contentful');
request.get(SHOWS_CALL, (error, response, body) => {
let parsedReturn = JSON.parse(body);
let assets = parsedReturn.includes ? parsedReturn.includes.Asset : [];
let formattedData = extractAssetsForShows(parsedReturn.items, assets);
redis.set('shows', JSON.stringify(formattedData), 'ex', EXPIRATION);
res.json(formattedData);
})
})
})
Router.get('/cats', (req, res) => {
redis.get('cats')
.then(result => {
if(result) {
console.log('From Redis');
return res.json(JSON.parse(result));
}
console.log('Calling Contentful');
request.get(CATS_CALL, (error, response, body) => {
redis.set('entries', body, 'ex', EXPIRATION);
res.json(JSON.parse(body));
})
})
})
Router.post('/send-email', (req, res) => {
const data = {
from: `${req.body.name}<${req.body.email}>`,
to: '[email protected]',
subject: `Message from Artbymente`,
text: req.body.message
}
mailgun.messages().send(data, (error, response) => {
res.json(response);
})
})
Router.post('/mailgun-hook', (req, res) => {
console.log(req.body);
console.log("HIT");
})
Router.post('/save-email', (req, res) => {
mailgun.lists('[email protected]').members().add({
members: [{
address: req.body.email,
name: req.body.fname
}],
subscribed: true
}, function(err, response){
res.json({status: 200});
})
})
module.exports = Router;