Skip to content

Commit

Permalink
Merge pull request #147 from kids-first/2477-save-set-callbacks
Browse files Browse the repository at this point in the history
2477 save set callbacks
  • Loading branch information
evans-g-crsj authored May 11, 2020
2 parents f307a40 + be1445c commit cdc8dcc
Show file tree
Hide file tree
Showing 8 changed files with 2,074 additions and 1,137 deletions.
11 changes: 10 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"presets": ["@babel/preset-env"],
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "11"
}
}
]
],
"plugins": [
"@babel/plugin-proposal-optional-chaining",
"@babel/plugin-proposal-object-rest-spread"
Expand Down
1 change: 1 addition & 0 deletions .env.schema
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ PROJECT_ID=
ES_HOST=
ES_FILE_INDEX=
ES_FILE_TYPE=
SQS_QUEUE_URL=

PYTHON_PATH=
SURVIVAL_PY_FILE=
3,097 changes: 1,971 additions & 1,126 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kf-arranger",
"version": "1.5.0",
"version": "1.6.0",
"description": "",
"main": "index.js",
"scripts": {
Expand All @@ -21,13 +21,14 @@
},
"homepage": "https://github.com/kids-first/kf-arranger#readme",
"dependencies": {
"@kfarranger/server": "1.6.2",
"@babel/core": "^7.5.5",
"@babel/plugin-proposal-object-rest-spread": "^7.5.5",
"@babel/plugin-proposal-optional-chaining": "^7.2.0",
"@babel/preset-env": "^7.5.5",
"@babel/register": "^7.5.5",
"@babel/core": "7.9.6",
"@babel/plugin-proposal-object-rest-spread": "7.9.0",
"@babel/plugin-proposal-optional-chaining": "7.9.0",
"@babel/preset-env": "7.9.0",
"@babel/register": "7.9.0",
"@kfarranger/server": "1.8.0",
"apollo-server-core": "1.3.2",
"aws-sdk": "^2.669.0",
"body-parser": "^1.18.2",
"chalk-animation": "^1.5.0",
"core-js": "^3.1.4",
Expand All @@ -43,12 +44,13 @@
"url-join": "^4.0.0"
},
"devDependencies": {
"aws-sdk-mock": "^5.1.0",
"chai": "^4.2.0",
"cross-env": "^6.0.1",
"mocha": "^6.1.4",
"mocha": "7.1.2",
"nodemon": "^1.17.5",
"nyc": "^14.1.1",
"prettier": "^1.11.1",
"prettier": "^1.19.1",
"sinon": "^7.3.2",
"sinon-express-mock": "^2.2.0",
"supertest": "^4.0.2"
Expand Down
2 changes: 2 additions & 0 deletions src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ export const esFileType = process.env.ES_FILE_TYPE;

export const survivalPyFile = process.env.SURVIVAL_PY_FILE || 'resource/py/survival.py';
export const pythonPath = process.env.PYTHON_PATH || '/usr/local/bin/python3';

export const sqsQueueUrl = process.env.SQS_QUEUE_URL || ''
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 cdc8dcc

Please sign in to comment.