RatingServiceSubmitNotify: Do not flag as notified on error
If the submission failed, we probably want to try again next time around. * src/server/service/RatingServiceSubmitNotify.js (_maybeNotify): Extract logic from `#postProcessRaterData'. Only set notification flag in absence of dapi error. (postProcessRaterData): Use it. * test/server/service/RatingServiceSubmitNotifyTest.js: Update tests accordingly.master
parent
7a1af0253a
commit
99e33a5089
|
@ -35,7 +35,8 @@ const RatingService = require( './RatingService' );
|
||||||
*
|
*
|
||||||
* Notification status will persist using the provided DAO. The next time
|
* Notification status will persist using the provided DAO. The next time
|
||||||
* such a notification is requested, it will only occur if the flag is not
|
* such a notification is requested, it will only occur if the flag is not
|
||||||
* set.
|
* set. The flag is not set in the event of an error (determined by the
|
||||||
|
* DataApi; usually an HTTP error).
|
||||||
*/
|
*/
|
||||||
module.exports = Trait( 'RatingServiceSubmitNotify' )
|
module.exports = Trait( 'RatingServiceSubmitNotify' )
|
||||||
.extend( RatingService,
|
.extend( RatingService,
|
||||||
|
@ -69,7 +70,10 @@ module.exports = Trait( 'RatingServiceSubmitNotify' )
|
||||||
/**
|
/**
|
||||||
* Trigger previously provided DataApi when no results are available
|
* Trigger previously provided DataApi when no results are available
|
||||||
*
|
*
|
||||||
* Result count is determined by DATA.__prem_avail_count.
|
* Result count is determined by DATA.__prem_avail_count. If the
|
||||||
|
* notification is successful (determined by the DataApi), then a
|
||||||
|
* flag will be set preventing the request from being trigerred for
|
||||||
|
* subsequent rating data.
|
||||||
*
|
*
|
||||||
* @param {UserRequest} request user request
|
* @param {UserRequest} request user request
|
||||||
* @param {Object} data rating data returned
|
* @param {Object} data rating data returned
|
||||||
|
@ -88,24 +92,43 @@ module.exports = Trait( 'RatingServiceSubmitNotify' )
|
||||||
|
|
||||||
if ( avail === 0 )
|
if ( avail === 0 )
|
||||||
{
|
{
|
||||||
this._getNotifyState( quote_id, notified =>
|
this._maybeNotify( quote_id, request );
|
||||||
{
|
|
||||||
if ( notified === true )
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this._dapif( request )
|
|
||||||
.request( { quote_id: quote_id }, () => {} );
|
|
||||||
|
|
||||||
this._setNotified( quote_id );
|
|
||||||
} );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.__super( request, data, actions, program, quote );
|
this.__super( request, data, actions, program, quote );
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Perform notification if flag has not been set
|
||||||
|
*
|
||||||
|
* See #postProcessRaterData for more information.
|
||||||
|
*
|
||||||
|
* @param {number} quote_id effective quote/document id
|
||||||
|
* @param {UserRequest} request user request
|
||||||
|
*
|
||||||
|
* @return {undefined}
|
||||||
|
*/
|
||||||
|
'private _maybeNotify'( quote_id, request )
|
||||||
|
{
|
||||||
|
this._getNotifyState( quote_id, notified =>
|
||||||
|
{
|
||||||
|
if ( notified === true )
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// make the request, only setting the notification flag if
|
||||||
|
// it is successful
|
||||||
|
this._dapif( request )
|
||||||
|
.request( { quote_id: quote_id }, err =>
|
||||||
|
{
|
||||||
|
err || this._setNotified( quote_id );
|
||||||
|
} );
|
||||||
|
} );
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get value of notification flag
|
* Get value of notification flag
|
||||||
*
|
*
|
||||||
|
|
|
@ -48,41 +48,63 @@ const {
|
||||||
describe( 'RatingServiceSubmitNotify', () =>
|
describe( 'RatingServiceSubmitNotify', () =>
|
||||||
{
|
{
|
||||||
[
|
[
|
||||||
|
// not available; make successful request and save flag
|
||||||
{
|
{
|
||||||
prem_avail_count: [ 0 ],
|
prem_avail_count: [ 0 ],
|
||||||
prev_called: false,
|
prev_called: false,
|
||||||
expected_request: true,
|
expected_request: true,
|
||||||
|
request_err: null,
|
||||||
|
save: true,
|
||||||
},
|
},
|
||||||
|
// not available; make failing request, don't save flag
|
||||||
|
{
|
||||||
|
prem_avail_count: [ 0 ],
|
||||||
|
prev_called: false,
|
||||||
|
expected_request: true,
|
||||||
|
request_err: Error(),
|
||||||
|
save: false,
|
||||||
|
},
|
||||||
|
// available
|
||||||
{
|
{
|
||||||
prem_avail_count: [ 2 ],
|
prem_avail_count: [ 2 ],
|
||||||
prev_called: false,
|
prev_called: false,
|
||||||
expected_request: false,
|
expected_request: false,
|
||||||
|
request_err: null,
|
||||||
|
save: false,
|
||||||
},
|
},
|
||||||
|
// this shouldn't happen; ignore all but first index
|
||||||
{
|
{
|
||||||
// this shouldn't happen; ignore all but first index
|
|
||||||
prem_avail_count: [ 2, 2 ],
|
prem_avail_count: [ 2, 2 ],
|
||||||
prev_called: false,
|
prev_called: false,
|
||||||
expected_request: false,
|
expected_request: false,
|
||||||
|
request_err: null,
|
||||||
|
save: false,
|
||||||
},
|
},
|
||||||
|
|
||||||
// save as above, but already saved
|
// save as above, but already saved
|
||||||
{
|
{
|
||||||
prem_avail_count: [ 0 ],
|
prem_avail_count: [ 0 ],
|
||||||
prev_called: true,
|
prev_called: true,
|
||||||
expected_request: false,
|
expected_request: false,
|
||||||
|
request_err: null,
|
||||||
|
save: false,
|
||||||
},
|
},
|
||||||
|
// available; don't make request
|
||||||
{
|
{
|
||||||
prem_avail_count: [ 2 ],
|
prem_avail_count: [ 2 ],
|
||||||
prev_called: true,
|
prev_called: true,
|
||||||
expected_request: false,
|
expected_request: false,
|
||||||
|
request_err: null,
|
||||||
|
save: false,
|
||||||
},
|
},
|
||||||
|
// this shouldn't happen; ignore all but first index
|
||||||
{
|
{
|
||||||
// this shouldn't happen; ignore all but first index
|
|
||||||
prem_avail_count: [ 2, 2 ],
|
prem_avail_count: [ 2, 2 ],
|
||||||
prev_called: true,
|
prev_called: true,
|
||||||
expected_request: false,
|
expected_request: false,
|
||||||
|
request_err: null,
|
||||||
|
save: false,
|
||||||
},
|
},
|
||||||
].forEach( ( { prem_avail_count, expected_request, prev_called }, i ) =>
|
].forEach( ( expected, i ) =>
|
||||||
it( `sends request on post process if no premiums (#${i})`, done =>
|
it( `sends request on post process if no premiums (#${i})`, done =>
|
||||||
{
|
{
|
||||||
const {
|
const {
|
||||||
|
@ -111,6 +133,8 @@ describe( 'RatingServiceSubmitNotify', () =>
|
||||||
expect( data ).to.deep.equal( { quote_id: quote_id } );
|
expect( data ).to.deep.equal( { quote_id: quote_id } );
|
||||||
|
|
||||||
requested = true;
|
requested = true;
|
||||||
|
|
||||||
|
callback( expected.request_err, null );
|
||||||
},
|
},
|
||||||
} )();
|
} )();
|
||||||
|
|
||||||
|
@ -133,7 +157,7 @@ describe( 'RatingServiceSubmitNotify', () =>
|
||||||
expect( qid ).to.equal( quote_id );
|
expect( qid ).to.equal( quote_id );
|
||||||
expect( key ).to.equal( 'submitNotified' );
|
expect( key ).to.equal( 'submitNotified' );
|
||||||
|
|
||||||
callback( null, prev_called );
|
callback( expected.flag_error, expected.prev_called );
|
||||||
};
|
};
|
||||||
|
|
||||||
dao.setDocumentField = ( qid, key, value, callback ) =>
|
dao.setDocumentField = ( qid, key, value, callback ) =>
|
||||||
|
@ -145,17 +169,15 @@ describe( 'RatingServiceSubmitNotify', () =>
|
||||||
notify_saved = true;
|
notify_saved = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
stub_rate_data.__prem_avail_count = prem_avail_count;
|
stub_rate_data.__prem_avail_count = expected.prem_avail_count;
|
||||||
|
|
||||||
sut.request( request, response, quote, 'something', () =>
|
sut.request( request, response, quote, 'something', () =>
|
||||||
{
|
{
|
||||||
expect( requested ).to.equal( expected_request );
|
expect( requested ).to.equal( expected.expected_request );
|
||||||
expect( save_called ).to.be.true;
|
expect( save_called ).to.be.true;
|
||||||
|
|
||||||
// only save notification status if we're notifying
|
// only save notification status if we're notifying
|
||||||
expect( notify_saved ).to.equal(
|
expect( notify_saved ).to.equal( expected.save );
|
||||||
!prev_called && expected_request
|
|
||||||
);
|
|
||||||
|
|
||||||
done();
|
done();
|
||||||
} );
|
} );
|
||||||
|
|
Loading…
Reference in New Issue