diff --git a/lib/response.js b/lib/response.js index 4be0d98..b342a38 100644 --- a/lib/response.js +++ b/lib/response.js @@ -20,6 +20,7 @@ Response = (function () { this._platform = platform this._store = {} this._headers = {} + this._attachments = [] this._http_headers = [] this._context = context } @@ -43,6 +44,20 @@ Response = (function () { this._respond(data) } } + Response.prototype.job_attach = function (...attachments) { + const requiredLabels = [ 'type', 'label', 'value' ] + const allowedTypes = [ 'link', 'password' ] + for (const attachment of attachments) { + if (!~requiredLabels.every(l => ~Object.keys(attachment).indexOf(l))) { + throw new Error(`Attachment requires mandatory properties ${requiredLabels.join(', ')}`) + } + if (!~allowedTypes.indexOf(attachment.type)) { + throw new Error(`Attachment must be of one of the following types ${allowedTypes.join(', ')}`) + } + } + this._attachments.push(...attachments) + this.meta('set_job_attachments', JSON.stringify(this._attachments)) + } Response.prototype.job_ignore = function (status, data) { this.meta('set_job_status', 'ignored') this.meta('set_job_status_message', status) diff --git a/test/index.spec.js b/test/index.spec.js index 3652ccc..cc8d9e2 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -161,36 +161,56 @@ describe('index', function () { } }, 500) }) - it('job should call .job_failed in case of unhandled asynchronous promise error', function (done) { + }) + describe('worker', function () { + it('worker should add attachments on job_success', function () { let context = { succeed: sinon.spy(), fail: sinon.spy(), - job_failed: sinon.spy(), awsRequestId: 'LAMBDA_INVOKE', logStreamName: 'LAMBDA_INVOKE' } - let routeEvent = { name: 'route', request_meta: { route: 'welcome', job: { id: 1 } } } + let workerEvent = { name: 'event', request_meta: { event: 'welcome' } } let Sdk = proxyquire('../index', {}) let platformInstance = new Sdk({}) - platformInstance.registerRoute('welcome', (req, res) => { return Promise.reject(new Error('no')) }) + platformInstance.registerWorker('welcome', function (req, res) { + res.job_attach({ + type: 'link', + label: 'NDA', + value: 'some dropbox url' + }) + res.job_attach({ + type: 'link', + label: 'NDA2', + value: 'some dropbox url' + }, { + type: 'link', + label: 'NDA3', + value: 'some dropbox url' + }) + res.job_complete('Uploaded NDA', {}) + }) let handler = platformInstance.getHandler() - handler(routeEvent, context) - setTimeout(() => { - try { - expect(context.succeed).to.have.been.called() - let args = context.succeed.args[0][0] - expect(args.meta.set_job_status).to.equal('failed') - expect(args.meta.set_job_status_message).to.equal('Unhandled Exception') - expect(args.meta.set_job_failure_message).to.equal('no') - expect(args.body.message).to.equal('no') - done() - } catch (e) { - done(e) - } - }, 500) + handler(workerEvent, context) + expect(context.succeed).to.have.been.called() + let args = context.succeed.args[0][0] + expect(args.meta.set_job_status).to.equal('done') + expect(args.meta.set_job_status_message).to.equal('Uploaded NDA') + expect(JSON.parse(args.meta.set_job_attachments)).to.deep.equal([{ + type: 'link', + label: 'NDA', + value: 'some dropbox url' + }, { + type: 'link', + label: 'NDA2', + value: 'some dropbox url' + }, { + type: 'link', + label: 'NDA3', + value: 'some dropbox url' + }]) + expect(args.body).to.deep.equal({}) }) - }) - describe('worker', function () { it('worker should call .job_failed in case of unhandled synchronous error', function () { let context = { succeed: sinon.spy(), @@ -211,5 +231,33 @@ describe('index', function () { expect(args.meta.set_job_failure_message).to.equal('no') expect(args.body.message).to.equal('no') }) + it('job should call .job_failed in case of unhandled asynchronous promise error', function (done) { + let context = { + succeed: sinon.spy(), + fail: sinon.spy(), + job_failed: sinon.spy(), + awsRequestId: 'LAMBDA_INVOKE', + logStreamName: 'LAMBDA_INVOKE' + } + let routeEvent = { name: 'route', request_meta: { route: 'welcome', job: { id: 1 } } } + let Sdk = proxyquire('../index', {}) + let platformInstance = new Sdk({}) + platformInstance.registerRoute('welcome', (req, res) => { return Promise.reject(new Error('no')) }) + let handler = platformInstance.getHandler() + handler(routeEvent, context) + setTimeout(() => { + try { + expect(context.succeed).to.have.been.called() + let args = context.succeed.args[0][0] + expect(args.meta.set_job_status).to.equal('failed') + expect(args.meta.set_job_status_message).to.equal('Unhandled Exception') + expect(args.meta.set_job_failure_message).to.equal('no') + expect(args.body.message).to.equal('no') + done() + } catch (e) { + done(e) + } + }, 500) + }) }) })