Skip to content

Commit

Permalink
Merge pull request #1140 from opencollective/eventsApi
Browse files Browse the repository at this point in the history
add limit and offset to allEvents + fix for lru cache content type
  • Loading branch information
xdamman authored Feb 15, 2018
2 parents 0dce39d + 4001449 commit c969488
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
10 changes: 6 additions & 4 deletions server/graphql/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,17 +420,19 @@ const queries = {
allEvents: {
type: new GraphQLList(CollectiveInterfaceType),
args: {
slug: {
type: GraphQLString
}
slug: { type: GraphQLString },
limit: { type: GraphQLInt },
offset: { type: GraphQLInt }
},
resolve(_, args) {
if (args.slug) {
return models.Collective
.findBySlug(args.slug, { attributes: ['id'] })
.then(collective => models.Collective.findAll({
where: { ParentCollectiveId: collective.id, type: 'EVENT' },
order: [['startsAt', 'DESC'], ['createdAt', 'DESC']]
order: [['startsAt', 'DESC'], ['createdAt', 'DESC']],
limit: args.limit || 10,
offset: args.offset || 0
}))
.catch(e => {
console.error(e.message);
Expand Down
9 changes: 9 additions & 0 deletions server/middleware/lru_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import LRUCache from 'lru-cache';

import { hashCode } from '../lib/utils';
import { EventEmitter } from 'events';
import debugLib from 'debug';
const debug = debugLib("cache");

const cache = LRUCache({
max: 1000,
Expand Down Expand Up @@ -31,8 +33,11 @@ export default () => {

let cached = cache.get(checksum);
if (cached) {
debug("cache hit", cached.status);
switch (cached.status) {
case 'finished':
debug("sending response", cached.contentType, cached.response);
res.setHeader("content-type", cached.contentType);
return res.send(new Buffer(cached.response, "base64"));
case 'running':
return cached.once('finished', () => {
Expand All @@ -51,7 +56,11 @@ export default () => {
if (req.cached && req.checksum) {
req.cached.status = 'finished';
req.cached.response = data;
if (typeof data === 'object') {
req.cached.contentType = "application/json; charset=utf-8";
}
req.cached.emit('finished');
debug("set cache", req.checksum, req.cached);
cache.set(req.checksum, req.cached)
}
temp.apply(this,arguments);
Expand Down
5 changes: 4 additions & 1 deletion test/email.routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,15 @@ describe("email.routes.test", () => {

it("forwards the email for approval to the core members", () => {
const spy = sandbox.spy(emailLib, 'send');

return request(app)
.post('/webhooks/mailgun')
.send(webhookBodyPayload)
.then((res) => {
expect(res.statusCode).to.equal(200);
if (spy.args[0][1] !== 'admins@testcollective.opencollective.com') {
console.log(">>> emailLib.send spy.callCount", spy.callCount);
utils.inspectSpy(spy, 4);
}
expect(spy.args[0][1]).to.equal('admins@testcollective.opencollective.com');
const emailSentTo = [spy.args[0][3].bcc,spy.args[1][3].bcc];
expect(emailSentTo.indexOf(usersData[0].email) !== -1).to.be.true;
Expand Down

0 comments on commit c969488

Please sign in to comment.