diff --git a/src/server/db/MongoServerDao.d.ts b/src/server/db/MongoServerDao.d.ts index 18492cc..1481b9b 100644 --- a/src/server/db/MongoServerDao.d.ts +++ b/src/server/db/MongoServerDao.d.ts @@ -41,12 +41,14 @@ export declare class MongoServerDao implements ServerDao * @param success - function to call on success * @param failure - function to call if save fails * @param save_data - quote data to save (optional) + * @param push_data - quote data to push (optional) */ saveQuote( quote: ServerSideQuote, success: Callback, failure: Callback, save_data: Record, + push_data: Record, ): this; diff --git a/src/server/db/MongoServerDao.js b/src/server/db/MongoServerDao.js index 2cc2001..c90422b 100644 --- a/src/server/db/MongoServerDao.js +++ b/src/server/db/MongoServerDao.js @@ -280,11 +280,12 @@ module.exports = Class( 'MongoServerDao' ) * @param Function success_callback function to call on success * @param Function failure_callback function to call if save fails * @param Object save_data quote data to save (optional) + * @param Object push_data quote data to push (optional) * * @return MongoServerDao self to allow for method chaining */ 'public saveQuote': function( - quote, success_callback, failure_callback, save_data + quote, success_callback, failure_callback, save_data, push_data ) { var dao = this; @@ -341,10 +342,15 @@ module.exports = Class( 'MongoServerDao' ) key => save_data[ 'meta.' + key ] = meta[ key ] ); + // do not push empty objects + const document = ( !push_data || !Object.keys( push_data ).length ) + ? { '$set': save_data } + : { '$set': save_data, '$push': push_data }; + // update the quote data if it already exists (same id), otherwise // insert it this._collection.update( { id: id }, - { '$set': save_data }, + document, // create record if it does not yet exist { upsert: true }, diff --git a/src/server/db/ServerDao.d.ts b/src/server/db/ServerDao.d.ts index 9c8ce32..6cc8025 100644 --- a/src/server/db/ServerDao.d.ts +++ b/src/server/db/ServerDao.d.ts @@ -45,12 +45,14 @@ export interface ServerDao * @param success - function to call on success * @param failure - function to call if save fails * @param save_data - quote data to save (optional) + * @param push_data - quote data to push (optional) */ saveQuote( quote: ServerSideQuote, success: Callback, failure: Callback, save_data: Record, + push_data: Record, ): this; diff --git a/src/server/service/RatingService.ts b/src/server/service/RatingService.ts index f95f4ea..df81c18 100644 --- a/src/server/service/RatingService.ts +++ b/src/server/service/RatingService.ts @@ -276,7 +276,7 @@ export class RatingService // data, which may cause a race condition with the below merge call) this._dao.saveQuote( quote, c, c, { ratedata: data, - } ); + }, {} ); } else { diff --git a/test/server/db/MongoServerDaoTest.js b/test/server/db/MongoServerDaoTest.js index 0f42f2b..0fa5d30 100644 --- a/test/server/db/MongoServerDaoTest.js +++ b/test/server/db/MongoServerDaoTest.js @@ -30,7 +30,7 @@ describe( 'MongoServerDao', () => { describe( '#saveQuote', () => { - describe( "with no save data", () => + describe( "with no save data or push data", () => { it( "saves entire metabucket record individually", done => { @@ -51,6 +51,9 @@ describe( 'MongoServerDao', () => expect( data.$set[ 'meta.bar' ] ) .to.deep.equal( metadata.bar ); + + expect( data.$push ).to.equal( undefined ); + done(); } ) ); @@ -60,6 +63,70 @@ describe( 'MongoServerDao', () => ); } ); } ); + + describe( "with push data", () => + { + it( "adds push data to the collection", done => + { + const push_data = { + foo: [ 'bar', 'baz' ], + bar: [ { quux: 'quuux' } ], + }; + + const quote = createStubQuote( {} ); + + const sut = Sut( createMockDb( + // update + ( selector, data ) => + { + expect( data.$push[ 'foo' ] ) + .to.deep.equal( push_data.foo ); + + expect( data.$push[ 'bar' ] ) + .to.deep.equal( push_data.bar ); + + done(); + } + ) ); + + sut.init( () => + sut.saveQuote( + quote, + () => {}, + () => {}, + undefined, + push_data + ) + ); + } ); + + it( "skips push data when it is an empty object", done => + { + const push_data = {}; + + const quote = createStubQuote( {} ); + + const sut = Sut( createMockDb( + // update + ( selector, data ) => + { + expect( data.$push ).to.equal( undefined ); + + done(); + } + ) ); + + sut.init( () => + sut.saveQuote( + quote, + () => {}, + () => {}, + undefined, + push_data + ) + ); + } ); + } ); } ); } ); diff --git a/test/server/service/RatingServiceTest.ts b/test/server/service/RatingServiceTest.ts index 3bc72c0..5dd821f 100644 --- a/test/server/service/RatingServiceTest.ts +++ b/test/server/service/RatingServiceTest.ts @@ -157,15 +157,14 @@ describe( 'RatingService', () => let saved_rates = false; dao.saveQuote = ( - quote: ServerSideQuote, - success: ServerDaoCallback, - _failure: ServerDaoCallback, - save_data: Record, + quote: ServerSideQuote, + success: ServerDaoCallback, + _failure: ServerDaoCallback, + save_data: Record, + _push_data: Record, ) => { - expect( save_data ).to.deep.equal( { - ratedata: stub_rate_data, - } ); + expect( save_data.ratedata ).to.deep.equal( stub_rate_data ); saved_rates = true; success( quote ); @@ -453,6 +452,7 @@ function getStubs() success: ServerDaoCallback, _failure: ServerDaoCallback, _save_data: Record, + _push_data: Record, ): this { success( quote );