Skip to content

Commit

Permalink
feature: add mechanism to pass callback to @kfarranger-server
Browse files Browse the repository at this point in the history
  • Loading branch information
evans-g-crsj committed May 11, 2020
1 parent 57f00a8 commit be1445c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ import { port, egoURL, projectId, esHost } from './env';
import { version, dependencies } from '../package.json';
import { shortUrlStatic, statistics, survival, searchByIds } from './endpoints';
import { onlyAdminMutations, injectBodyHttpHeaders } from './middleware';
import { postProcessSaveSet } from './utils/saveSet';
import SQS from 'aws-sdk/clients/sqs';

const app = express();
const http = Server(app);
const io = socketIO(http);

const sqs = new SQS({ apiVersion: '2012-11-05' });

app.use(cors());

/*
Expand Down Expand Up @@ -52,7 +56,13 @@ app.use(
},
{
type: 'allow',
route: [`/(.*)/graphql`, `/(.*)/graphql/(.*)`, `/(.*)/download`, `/survival`, '/searchByIds'],
route: [
`/(.*)/graphql`,
`/(.*)/graphql/(.*)`,
`/(.*)/download`,
`/survival`,
'/searchByIds',
],
status: ['approved'],
role: 'user',
},
Expand Down Expand Up @@ -82,6 +92,11 @@ Arranger({
context: ({ jwt }) => ({ jwt }),
middleware: [onlyAdminMutations],
},
callbacks: {
resolvers: {
saveSet: postProcessSaveSet(sqs),
},
},
}).then(router => {
app.use(router);
http.listen(port, async () => {
Expand Down
6 changes: 6 additions & 0 deletions src/utils/saveSet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { sqsQueueUrl } from '../env';

export const postProcessSaveSet = (sqs) => async data => {
const params = { MessageBody: JSON.stringify(data), QueueUrl: sqsQueueUrl };
return sqs.sendMessage(params).promise();
};
57 changes: 57 additions & 0 deletions src/utils/saveSet.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import AWSMock from 'aws-sdk-mock';
import AWS from 'aws-sdk';
import { postProcessSaveSet } from './saveSet';
import SQS from 'aws-sdk/clients/sqs';
import { describe, it, beforeEach, afterEach } from 'mocha';
import { expect } from 'chai';

const mockSaveSetData = {
setId: '123xyyz',
createdAt: Date.now(),
ids: ['id1', '1d2'],
size: 2,
sqon: {
op: 'and',
content: [
{
op: 'in',
content: {
field: 'observed_phenotype.name',
value: ['Abnormality of the cardiovascular system (HP:0001626)'],
},
},
],
},
type: 'participant',
userId: 'd1c3f78f-a68f-46c4-a44f-9d1249d6337e',
path: 'kf_id',
sort: [],
tag: 'test',
};

describe('Test case for SQS SendMessage', () => {
beforeEach(() => {
AWSMock.setSDKInstance(AWS);
});

afterEach(() => {
AWSMock.restore();
});

it('should return a successful response', async () => {
AWSMock.mock('SQS', 'sendMessage', () => Promise.resolve({ MessageId: '123' }));
const sqs = new AWS.SQS({ apiVersion: '2012-11-05' });
const res = await postProcessSaveSet(sqs)(mockSaveSetData);
expect(res.MessageId).to.equal('123');
});

it('should throw when the queue does not respond', async () => {
AWSMock.mock('SQS', 'sendMessage', () => Promise.reject('could not send data to queue'));
const sqs = new AWS.SQS({ apiVersion: '2012-11-05' });
try {
await postProcessSaveSet(sqs)(mockSaveSetData);
} catch (err) {
expect(err).to.eql('could not send data to queue');
}
});
});

0 comments on commit be1445c

Please sign in to comment.