[DEV-6353] Accept push data in MongoDao
parent
48aa315c2a
commit
26c4367ead
|
@ -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<string, any>,
|
||||
push_data: Record<string, any>,
|
||||
): this;
|
||||
|
||||
|
||||
|
|
|
@ -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 },
|
||||
|
|
|
@ -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<string, any>,
|
||||
push_data: Record<string, any>,
|
||||
): this;
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
|
@ -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
|
||||
)
|
||||
);
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
|
||||
|
|
|
@ -157,15 +157,14 @@ describe( 'RatingService', () =>
|
|||
let saved_rates = false;
|
||||
|
||||
dao.saveQuote = (
|
||||
quote: ServerSideQuote,
|
||||
success: ServerDaoCallback,
|
||||
_failure: ServerDaoCallback,
|
||||
save_data: Record<string, any>,
|
||||
quote: ServerSideQuote,
|
||||
success: ServerDaoCallback,
|
||||
_failure: ServerDaoCallback,
|
||||
save_data: Record<string, any>,
|
||||
_push_data: Record<string, any>,
|
||||
) =>
|
||||
{
|
||||
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<string, any>,
|
||||
_push_data: Record<string, any>,
|
||||
): this
|
||||
{
|
||||
success( quote );
|
||||
|
|
Loading…
Reference in New Issue