diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index dcd2a0e..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,12 +0,0 @@ -## **1.2.3** -- [**WEB-4809**](https://hxshortbreaks.atlassian.net/browse/WEB-4809) - Swapping build agent to Shippable. -## **1.2.2** -- [**WEB-4542**](https://hxshortbreaks.atlassian.net/browse/WEB-4542) Upgrade coding styles to current level. -## **1.2.1** -- [**WEB-4582**](https://hxshortbreaks.atlassian.net/browse/WEB-4582) Allow searching on baskets with familyName. -## **1.2.0** -- [**WEB-4162**](https://hxshortbreaks.atlassian.net/browse/WEB-4162) Upgrade to hapijs v8. -## **1.1.0** -- [**FEA-360**](https://hxshortbreaks.atlassian.net/browse/FEA-360) Allow the searching of baskets within the design documents. -## **1.0.0** -- [**FEA-359**](https://hxshortbreaks.atlassian.net/browse/FEA-359) Creation of plugin to speak to both Couch and Harvest for Baskets. \ No newline at end of file diff --git a/README.md b/README.md index d1e1480..0d6476f 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,8 @@ var options = { return request.server.plugins['plugin-harvest'].postBasket( options ); ``` +This is called from the Works endpoint **baskets/(basketId)::post**. + ### Function: postBasketVersion This function will create a new version on the basket that will be named after the tag passed in and will call the version passed its 'base version'. @@ -66,6 +68,8 @@ var options = { return request.server.plugins['plugin-harvest'].postBasketVersion( options ); ``` +This is called from the Works endpoint **baskets/(basketId)::put**, when you pass in the **newVersion** key with a **true** value. + **Note:** This will not update anything based on the data passed in. The idea of this is that it is the start of the journey for this 'version' of the basket. You will need to do a separate call down to the putBasket to store new data - we can revisit this functionality if this causes an issue. ### Function: putBasket @@ -73,6 +77,7 @@ return request.server.plugins['plugin-harvest'].postBasketVersion( options ); This function updates the current tag of the basket based on what has been passed in, using Harvest to work out the differences between the current and parent version. After the differences have been calculated the basket will be persisted to the CouchDB instance, and the shared representation of the basket will be returned. + ``` var options = { id: '755204ffaceb748a71522f9e550160af', @@ -86,10 +91,13 @@ var options = { return request.server.plugins['plugin-harvest'].putBasket( options ); ``` +This is called from the Works endpoint **baskets/(basketId)::put**, when you don't pass in a **newVersion** key. + ### Function: getBasket This function returns a basket that has been stored under its id and will take the version of the basket identified by the tag passed in. After finding the Basket in the Couch instance the shared representation of the basket will be returned. + ``` var options = { id: '755204ffaceb748a71522f9e550160af', @@ -98,15 +106,20 @@ var options = { return request.server.plugins['plugin-harvest'].getBasket( options ); ``` +This is called from the Works endpoint **baskets/(basketId)::get**. + ### Function: getBaskets This function calls Design Documents on the CouchDB instance to retrieve collections of baskets based on what has been passed in. _Please note the Design Documents may need to be manually created on your local CouchDB instance_ After finding the matching baskets each basket will be returned with its shared representation. +These are all called from the Works endpoint **baskets::get**. + #### all_baskets::by_pin Pass in a pin to search for baskets + ``` var options = { pin: '1234' @@ -117,6 +130,7 @@ return request.server.plugins['plugin-harvest'].getBaskets( options ); #### all_baskets::by_email Pass in a email to search for baskets + ``` var options = { email: 'person@domain.com' @@ -127,6 +141,7 @@ return request.server.plugins['plugin-harvest'].getBaskets( options ); #### all_baskets::by_telephone Pass in a telephone to search for baskets + ``` var options = { telephone: '01234567890' @@ -137,6 +152,7 @@ return request.server.plugins['plugin-harvest'].getBaskets( options ); #### phone_baskets::by_created_at Pass in type=phone and a since and until timestamps (in ISO date format). If timestamps not included they will default to the current time + ``` var options = { type: 'phone' @@ -149,6 +165,7 @@ return request.server.plugins['plugin-harvest'].getBaskets( options ); #### email_baskets::by_created_at Pass in type=email and a since and until timestamps (in ISO date format). If timestamps not included they will default to the current time + ``` var options = { type: 'email' diff --git a/lib/getBasket.js b/lib/getBasket.js index cb165e8..d40de48 100644 --- a/lib/getBasket.js +++ b/lib/getBasket.js @@ -32,7 +32,11 @@ module.exports = function GetBasket( harvest, harvestDb, options ) { if( error ) { deferred.reject( buildReject( error, options ) ); } else { - deferred.resolve( harvest.getSharedBasket( storedBasket, options.tag ) ); + try { + deferred.resolve( harvest.getSharedBasket( storedBasket, options.tag ) ); + } catch ( error ) { + deferred.reject( buildReject( error, options ) ); + } } } ); diff --git a/lib/postBasket.js b/lib/postBasket.js index 5b01c10..3dadd95 100644 --- a/lib/postBasket.js +++ b/lib/postBasket.js @@ -36,14 +36,18 @@ module.exports = function PostBasket( harvest, harvestDb, options ) { if( error ) { deferred.reject( buildReject( error, options ) ); } else { - // Nano just replies back with ok:true so lets smash the id and rev onto the document that we asked to be stored. - var persistedBasket = { - _id: document.id, - _rev: document.rev - }; - _.extend( persistedBasket, storedBasket ); - - deferred.resolve( harvest.getSharedBasket( persistedBasket, options.tag ) ); + try { + // Nano just replies back with ok:true so lets smash the id and rev onto the document that we asked to be stored. + var persistedBasket = { + _id: document.id, + _rev: document.rev + }; + _.extend( persistedBasket, storedBasket ); + + deferred.resolve( harvest.getSharedBasket( persistedBasket, options.tag ) ); + } catch ( error ) { + deferred.reject( buildReject( error, options ) ); + } } } ); diff --git a/lib/postBasketVersion.js b/lib/postBasketVersion.js index 815dc20..8928afe 100644 --- a/lib/postBasketVersion.js +++ b/lib/postBasketVersion.js @@ -40,15 +40,23 @@ module.exports = function PostBasketVersion( harvest, harvestDb, options ) { if( error ) { deferred.reject( buildReject( error, options ) ); } else { - harvest.addVersion( storedBasket, sharedBasket.version, sharedBasket.tag ); - harvestDb.insert( storedBasket, sharedBasket.id, function( error, document ) { - if( error ) { - deferred.reject( buildReject( error, options ) ); - } else { - storedBasket._rev = document.rev; - deferred.resolve( harvest.getSharedBasket( storedBasket, sharedBasket.tag ) ); - } - } ); + try { + harvest.addVersion( storedBasket, sharedBasket.version, sharedBasket.tag ); + harvestDb.insert( storedBasket, sharedBasket.id, function( error, document ) { + if( error ) { + deferred.reject( buildReject( error, options ) ); + } else { + try { + storedBasket._rev = document.rev; + deferred.resolve( harvest.getSharedBasket( storedBasket, sharedBasket.tag ) ); + } catch ( error ) { + deferred.reject( buildReject( error, options ) ); + } + } + } ); + } catch ( error ) { + deferred.reject( buildReject( error, options ) ); + } } } ); diff --git a/lib/putBasket.js b/lib/putBasket.js index d75cc70..6966606 100644 --- a/lib/putBasket.js +++ b/lib/putBasket.js @@ -38,18 +38,25 @@ module.exports = function PutBasket( harvest, harvestDb, options ) { if( error ) { deferred.reject( buildReject( error, options ) ); } else { - harvest.saveBasket( sharedBasket, storedBasket ); - harvestDb.insert( storedBasket, sharedBasket.id, function( error, document ) { - if( error ) { - deferred.reject( buildReject( error, options ) ); - } else { - // Update the revision from the Couch/Nano reply - storedBasket._rev = document.rev; - deferred.resolve( harvest.getSharedBasket( storedBasket, sharedBasket.tag ) ); - } - } ); + try { + harvest.saveBasket( sharedBasket, storedBasket ); + harvestDb.insert( storedBasket, sharedBasket.id, function( error, document ) { + if( error ) { + deferred.reject( buildReject( error, options ) ); + } else { + try { + // Update the revision from the Couch/Nano reply + storedBasket._rev = document.rev; + deferred.resolve( harvest.getSharedBasket( storedBasket, sharedBasket.tag ) ); + } catch ( error ) { + deferred.reject( buildReject( error, options ) ); + } + } + } ); + } catch ( error ) { + deferred.reject( buildReject( error, options ) ); + } } - } ); } catch ( error ) { diff --git a/package.json b/package.json index d882cb6..5bd01f9 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "plugin-harvest", "description": "A hapi Plugin to connect to a CouchDB database with encoded baskets", - "version": "1.2.3", + "version": "1.3.0", "homepage": "https://github.com/holidayextras/plugin-harvest", "author": { "name": "Shortbreaks", @@ -20,7 +20,7 @@ "test": "grunt jshint && grunt jscs && istanbul cover _mocha -- --recursive -R spec" }, "dependencies": { - "harvest": "git+ssh://git@github.com:holidayextras/harvest.git", + "harvest": "git+ssh://git@github.com:holidayextras/harvest.git#v1.3.0", "lodash": "3.1.0", "nano": "6.0.2", "q": "1.1.2" diff --git a/test/getBasketTest.js b/test/getBasketTest.js index d83c506..af5e5f1 100644 --- a/test/getBasketTest.js +++ b/test/getBasketTest.js @@ -84,4 +84,20 @@ describe( 'LIB: getBasket', function() { done(); } ); + describe( 'exceptions thrown', function() { + + before( function() { + sinon.stub( harvest, 'getSharedBasket', function() { + throw new Error( 'Fake error' ); + } ); + } ); + + it( 'getBasket should reject if Harvest getSharedBasket throws an error', function() { + return getBasket( harvest, successfulHarvestDb, loadTestResource( './fixtures/requestWithIdAndTag' ) ).should.be.rejected.and.eventually.have.property( 'error' ).that.is.an.instanceof( Error ); + } ); + + after( function() { + harvest.getSharedBasket.restore(); + } ); + } ); } ); \ No newline at end of file diff --git a/test/getBasketsTest.js b/test/getBasketsTest.js index 29b4669..7c836bb 100644 --- a/test/getBasketsTest.js +++ b/test/getBasketsTest.js @@ -129,7 +129,7 @@ describe( 'LIB: getBaskets', function() { done(); } ); - it( 'getBaskets should call the email_baskets::by_created_at design document even if timestamps not passed in', function( done ) { + it( 'getBaskets should call the email_baskets::by_created_at design document even if timestamps not passed in', function( done ) { sinon.spy( successfulHarvestDb, 'view' ); getBaskets( harvest, successfulHarvestDb, { type: 'email' } ); successfulHarvestDb.view.should.be.calledOnce; diff --git a/test/postBasketTest.js b/test/postBasketTest.js index b6d0be0..f012fb0 100644 --- a/test/postBasketTest.js +++ b/test/postBasketTest.js @@ -87,4 +87,21 @@ describe( 'LIB: postBasket', function() { done(); } ); + describe( 'exceptions thrown', function() { + + before( function() { + sinon.stub( harvest, 'getSharedBasket', function() { + throw new Error( 'Fake error' ); + } ); + } ); + + it( 'postBasket should reject if Harvest getSharedBasket throws an error', function() { + return postBasket( harvest, successfulHarvestDb, loadTestResource( './fixtures/requestWithIdAndTag' ) ).should.be.rejected.and.eventually.have.property( 'error' ).that.is.an.instanceof( Error ); + } ); + + after( function() { + harvest.getSharedBasket.restore(); + } ); + } ); + } ); \ No newline at end of file diff --git a/test/postBasketVersionTest.js b/test/postBasketVersionTest.js index f76d3f3..2b2b6d0 100644 --- a/test/postBasketVersionTest.js +++ b/test/postBasketVersionTest.js @@ -130,4 +130,38 @@ describe( 'LIB: postBasketVersion', function() { done(); } ); + describe( 'exception thrown in addVersion', function() { + + before( function() { + sinon.stub( harvest, 'addVersion', function() { + throw new Error( 'Fake error' ); + } ); + } ); + + it( 'postBasket should reject if Harvest addVersion throws an error', function() { + return postBasketVersion( harvest, harvestDbWithSuccessfulGetAndInsert, loadTestResource( './fixtures/requestWithIdAndTagAndVersion' ) ).should.be.rejected.and.eventually.have.property( 'error' ).that.is.an.instanceof( Error ); + } ); + + after( function() { + harvest.addVersion.restore(); + } ); + } ); + + describe( 'exception thrown in getSharedBasket', function() { + + before( function() { + sinon.stub( harvest, 'getSharedBasket', function() { + throw new Error( 'Fake error' ); + } ); + } ); + + it( 'postBasket should reject if Harvest getSharedBasket throws an error', function() { + return postBasketVersion( harvest, harvestDbWithSuccessfulGetAndInsert, loadTestResource( './fixtures/requestWithIdAndTagAndVersion' ) ).should.be.rejected.and.eventually.have.property( 'error' ).that.is.an.instanceof( Error ); + } ); + + after( function() { + harvest.getSharedBasket.restore(); + } ); + } ); + } ); \ No newline at end of file diff --git a/test/putBasketTest.js b/test/putBasketTest.js index 429f4da..5ce461f 100644 --- a/test/putBasketTest.js +++ b/test/putBasketTest.js @@ -122,4 +122,38 @@ describe( 'LIB: putBasket', function() { done(); } ); + describe( 'exception thrown in saveBasket', function() { + + before( function() { + sinon.stub( harvest, 'saveBasket', function() { + throw new Error( 'Fake error' ); + } ); + } ); + + it( 'postBasket should reject if Harvest saveBasket throws an error', function() { + return putBasket( harvest, harvestDbWithSuccessfulGetAndInsert, loadTestResource( './fixtures/requestWithIdAndTag' ) ).should.be.rejected.and.eventually.have.property( 'error' ).that.is.an.instanceof( Error ); + } ); + + after( function() { + harvest.saveBasket.restore(); + } ); + } ); + + describe( 'exception thrown in getSharedBasket', function() { + + before( function() { + sinon.stub( harvest, 'getSharedBasket', function() { + throw new Error( 'Fake error' ); + } ); + } ); + + it( 'postBasket should reject if Harvest getSharedBasket throws an error', function() { + return putBasket( harvest, harvestDbWithSuccessfulGetAndInsert, loadTestResource( './fixtures/requestWithIdAndTag' ) ).should.be.rejected.and.eventually.have.property( 'error' ).that.is.an.instanceof( Error ); + } ); + + after( function() { + harvest.getSharedBasket.restore(); + } ); + } ); + } ); \ No newline at end of file