Skip to content

Commit

Permalink
Merge pull request #3 from holidayextras/stopHarvestCrashingWorks
Browse files Browse the repository at this point in the history
WEB-5852 Stop thrown harvest errors crashing The Works
  • Loading branch information
philmeehan committed Aug 26, 2015
2 parents 850eef4 + 3da2ccc commit 974d82a
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 44 deletions.
12 changes: 0 additions & 12 deletions CHANGELOG.md

This file was deleted.

17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand All @@ -66,13 +68,16 @@ 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

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',
Expand All @@ -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',
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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'
Expand Down
6 changes: 5 additions & 1 deletion lib/getBasket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}
}
} );

Expand Down
20 changes: 12 additions & 8 deletions lib/postBasket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}
}
} );

Expand Down
26 changes: 17 additions & 9 deletions lib/postBasketVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}
}
} );

Expand Down
29 changes: 18 additions & 11 deletions lib/putBasket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
Expand Down
16 changes: 16 additions & 0 deletions test/getBasketTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
} );
} );
} );
2 changes: 1 addition & 1 deletion test/getBasketsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions test/postBasketTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
} );
} );

} );
34 changes: 34 additions & 0 deletions test/postBasketVersionTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
} );
} );

} );
34 changes: 34 additions & 0 deletions test/putBasketTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
} );
} );

} );

0 comments on commit 974d82a

Please sign in to comment.