From 723a7f7ff76e0b7a35a0c3e589bb06420d6dbd9a Mon Sep 17 00:00:00 2001 From: Austin Schaffer Date: Tue, 11 Jun 2019 14:42:31 -0400 Subject: [PATCH 1/3] [DEV-5333] Expose initialRatedDate to the client from the bucket [DEV-5333] WIP Add RateEventHandlerTest and fix RatingServiceSubmitNotifyTest WIP: Add RateEventHandlerTest Add stubs for RateEventHandlerTest Finish RateEventHandlerTest Move done to fix test bug --- src/client/event/RateEventHandler.js | 4 +- src/client/quote/ClientQuote.js | 18 +++++ src/server/service/RatingService.js | 4 +- test/client/event/RateEventHandlerTest.js | 81 +++++++++++++++++++ .../service/RatingServiceSubmitNotifyTest.js | 2 +- 5 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 test/client/event/RateEventHandlerTest.js diff --git a/src/client/event/RateEventHandler.js b/src/client/event/RateEventHandler.js index 483992e..2eec775 100644 --- a/src/client/event/RateEventHandler.js +++ b/src/client/event/RateEventHandler.js @@ -141,7 +141,7 @@ module.exports = Class( 'RateEventHandler' ) this._dataProxy.get( this._genRateUrl( quote, indv ), function( response, err ) { - var data = ( response.content.data || {} ); + const { initialRatedDate: initial_rated = 0, data = {} } = response.content || {}; if ( err ) { @@ -154,6 +154,8 @@ module.exports = Class( 'RateEventHandler' ) // save only; no transport is specified) quote.refreshData( data ); + quote.setInitialRatedDate( initial_rated ); + // let subtypes handle additional processing _self.postRate( err, data, _self._client, quote ); diff --git a/src/client/quote/ClientQuote.js b/src/client/quote/ClientQuote.js index ced0ce1..750a46b 100644 --- a/src/client/quote/ClientQuote.js +++ b/src/client/quote/ClientQuote.js @@ -565,6 +565,24 @@ module.exports = Class( 'ClientQuote' ) 'public proxy getInitialRatedDate': '_quote', + /** + * Sets the quote's initial rated date + * + * @param {number} time initial rated date as a Unix timestamp + * + * @return {Quote} self + */ + 'public proxy setInitialRatedDate': '_quote', + + + /** + * Returns the quote's expiration date + * + * @return {number} quote's initial rated date + */ + 'public proxy getExpirationDate': '_quote', + + /** * Returns the id of the agent that owns the quote * diff --git a/src/server/service/RatingService.js b/src/server/service/RatingService.js index 14e90fd..b44ff62 100644 --- a/src/server/service/RatingService.js +++ b/src/server/service/RatingService.js @@ -178,7 +178,8 @@ module.exports = Class( 'RatingService', // no need to wait for the save; send the response _self._server.sendResponse( request, quote, { - data: cleaned + data: cleaned, + initialRatedDate: quote.getInitialRatedDate() }, actions ); }, function( message ) @@ -216,6 +217,7 @@ module.exports = Class( 'RatingService', quote.setLastPremiumDate( cur_date ); quote.setRatedDate( cur_date ); + quote.setInitialRatedDate( cur_date ); function done() { diff --git a/test/client/event/RateEventHandlerTest.js b/test/client/event/RateEventHandlerTest.js new file mode 100644 index 0000000..7cbc3a6 --- /dev/null +++ b/test/client/event/RateEventHandlerTest.js @@ -0,0 +1,81 @@ +/** + * Tests RateEventHandler + * + * Copyright (C) 2019, 2019 R-T Specialty, LLC. + * + * This file is part of the Liza Data Collection Framework. + * + * liza is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +'use strict'; + +const { expect } = require( 'chai' ); +const Sut = require( '../../..' ).client.event.RateEventHandler; + +describe( 'RateEventHandler', () => +{ + describe( "Handle Rating Event", () => + { + it( "calls #handle to do the rating", done => + { + const stepId = 0; + const lockStep = 1; + const indv = "somerater"; + + const quote = { + getExplicitLockStep: () => lockStep, + setInitialRatedDate: ( value ) => {}, + getCurrentStepId: () => stepId, + refreshData: () => {}, + isLocked: () => false + }; + + const step = { + invalidate: () => {} + }; + + const ui = { + getStep: ( dest ) => step + }; + + const client = { + getQuote: () => quote, + isSaving: () => false, + once: ( event, callback ) => {}, + getUi: () => ui + }; + + const response = { + content: { + data: "Some Data" + } + }; + + const error = "ERROR"; + + const proxy = { + get: ( url, callback ) => callback( response, error ) + }; + + const sut = Sut( client, proxy ); + + sut.handle( "", function() {}, { + indv: indv, + stepId: stepId + } ); + done(); + } ) + } ) +} ) diff --git a/test/server/service/RatingServiceSubmitNotifyTest.js b/test/server/service/RatingServiceSubmitNotifyTest.js index d053755..80e0bc3 100644 --- a/test/server/service/RatingServiceSubmitNotifyTest.js +++ b/test/server/service/RatingServiceSubmitNotifyTest.js @@ -179,8 +179,8 @@ describe( 'RatingServiceSubmitNotify', () => // only save notification status if we're notifying expect( notify_saved ).to.equal( expected.save ); - done(); } ); + done(); } ) ); } ); From 23889cbfca570b21d38b2c72aa97aec16acc4ddf Mon Sep 17 00:00:00 2001 From: Austin Schaffer Date: Wed, 12 Jun 2019 11:27:00 -0400 Subject: [PATCH 2/3] [DEV-5333] Use the correct function for day calculations --- src/calc/Calc.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/calc/Calc.js b/src/calc/Calc.js index ba653ca..4e637da 100644 --- a/src/calc/Calc.js +++ b/src/calc/Calc.js @@ -274,7 +274,7 @@ exports.relativeDate = function( data, value ) // days case 'd': - date_new.setDay( date_new.getDay() + +tval ); + date_new.setDate( date_new.getUTCDate() + +tval ); break; // seconds From 9637fc22e1cbf4d0bf5ff1ea0479b76abf796d49 Mon Sep 17 00:00:00 2001 From: Austin Schaffer Date: Wed, 12 Jun 2019 17:51:31 -0400 Subject: [PATCH 3/3] [DEV-5333] Pass last rated date from the server quote back to the client quote Correct and add more elaborate tests for RateEventHandler --- src/client/event/RateEventHandler.js | 3 +- src/client/quote/ClientQuote.js | 19 ++ src/quote/BaseQuote.js | 31 +++ src/server/quote/ServerSideQuote.js | 30 --- src/server/service/RatingService.js | 6 +- src/test/server/service/RatingServiceStub.js | 13 +- test/client/event/RateEventHandlerTest.js | 197 ++++++++++++++++-- test/client/quote/ClientQuoteTest.js | 17 +- .../service/RatingServiceSubmitNotifyTest.js | 2 +- test/server/service/RatingServiceTest.js | 40 ++++ 10 files changed, 301 insertions(+), 57 deletions(-) diff --git a/src/client/event/RateEventHandler.js b/src/client/event/RateEventHandler.js index 2eec775..c5f31f5 100644 --- a/src/client/event/RateEventHandler.js +++ b/src/client/event/RateEventHandler.js @@ -141,7 +141,7 @@ module.exports = Class( 'RateEventHandler' ) this._dataProxy.get( this._genRateUrl( quote, indv ), function( response, err ) { - const { initialRatedDate: initial_rated = 0, data = {} } = response.content || {}; + const { initialRatedDate: initial_rated = 0, lastRatedDate: last_rated = 0, data = {} } = response.content || {}; if ( err ) { @@ -155,6 +155,7 @@ module.exports = Class( 'RateEventHandler' ) quote.refreshData( data ); quote.setInitialRatedDate( initial_rated ); + quote.setLastPremiumDate( last_rated ); // let subtypes handle additional processing _self.postRate( err, data, _self._client, quote ); diff --git a/src/client/quote/ClientQuote.js b/src/client/quote/ClientQuote.js index 750a46b..264d2fa 100644 --- a/src/client/quote/ClientQuote.js +++ b/src/client/quote/ClientQuote.js @@ -139,6 +139,7 @@ module.exports = Class( 'ClientQuote' ) .setAgentEntityId( data.agentEntityId || "" ) .setStartDate( data.startDate || 0 ) .setInitialRatedDate( data.initialRatedDate || 0 ) + .setLastPremiumDate( data.lastRatedDate || 0 ) .setImported( data.imported || false ) .setBound( data.bound || false ) .needsImport( data.needsImport || false ) @@ -575,6 +576,24 @@ module.exports = Class( 'ClientQuote' ) 'public proxy setInitialRatedDate': '_quote', + /** + * Set the date that the premium was calculated as a Unix timestamp + * + * @param {number} timestamp Unix timestamp representing premium date + * + * @return {Quote} self + */ + 'public proxy setLastPremiumDate': '_quote', + + + /** + * Retrieve the last time the premium was calculated + * + * @return {number} last calculated time or 0 + */ + 'public proxy getLastPremiumDate': '_quote', + + /** * Returns the quote's expiration date * diff --git a/src/quote/BaseQuote.js b/src/quote/BaseQuote.js index 8f16c3a..5201cd4 100644 --- a/src/quote/BaseQuote.js +++ b/src/quote/BaseQuote.js @@ -85,6 +85,12 @@ module.exports = Class( 'BaseQuote' ) */ 'private _initialRatedDate': 0, + /** + * Unix timestamp containing date of last premium calculation + * @type {number} + */ + 'private _lastPremDate': 0, + /** * Id of agent that owns the quote * @type {number} @@ -270,6 +276,31 @@ module.exports = Class( 'BaseQuote' ) }, + /** + * Set the date that the premium was calculated as a Unix timestamp + * + * @param {number} timestamp Unix timestamp representing premium date + * + * @return {Quote} self + */ + 'public setLastPremiumDate': function( timestamp ) + { + this._lastPremDate = ( timestamp || 0 ); + return this; + }, + + + /** + * Retrieve the last time the premium was calculated + * + * @return {number} last calculated time or 0 + */ + 'public getLastPremiumDate': function() + { + return ( this._lastPremDate || 0 ); + }, + + /** * Returns the quote's expiration date * diff --git a/src/server/quote/ServerSideQuote.js b/src/server/quote/ServerSideQuote.js index 8f35f42..c624c75 100644 --- a/src/server/quote/ServerSideQuote.js +++ b/src/server/quote/ServerSideQuote.js @@ -40,11 +40,6 @@ module.exports = Class( 'ServerSideQuote' ) */ 'private _creditScoreRef': 0, - /** - * Unix timestamp containing date of last premium calculation - * @type {number} - */ - 'private _lastPremDate': 0, /** * Unix timestamp containing date of first premium calculation @@ -85,20 +80,6 @@ module.exports = Class( 'ServerSideQuote' ) }, - /** - * Set the date that the premium was calculated as a Unix timestamp - * - * @param {number} timestamp Unix timestamp representing premium date - * - * @return {Quote} self - */ - 'public setLastPremiumDate': function( timestamp ) - { - this._lastPremDate = ( timestamp || 0 ); - return this; - }, - - /** * Set the timestamp of the first time quote was rated * @@ -118,17 +99,6 @@ module.exports = Class( 'ServerSideQuote' ) }, - /** - * Retrieve the last time the premium was calculated - * - * @return {number} last calculated time or 0 - */ - 'public getLastPremiumDate': function() - { - return ( this._lastPremDate || 0 ); - }, - - /** * If the quote has been rated * diff --git a/src/server/service/RatingService.js b/src/server/service/RatingService.js index b44ff62..820bc44 100644 --- a/src/server/service/RatingService.js +++ b/src/server/service/RatingService.js @@ -178,8 +178,9 @@ module.exports = Class( 'RatingService', // no need to wait for the save; send the response _self._server.sendResponse( request, quote, { - data: cleaned, - initialRatedDate: quote.getInitialRatedDate() + data: cleaned, + initialRatedDate: quote.getRatedDate(), + lastRatedDate: quote.getLastPremiumDate() }, actions ); }, function( message ) @@ -217,7 +218,6 @@ module.exports = Class( 'RatingService', quote.setLastPremiumDate( cur_date ); quote.setRatedDate( cur_date ); - quote.setInitialRatedDate( cur_date ); function done() { diff --git a/src/test/server/service/RatingServiceStub.js b/src/test/server/service/RatingServiceStub.js index c58d1cf..e4aea1f 100644 --- a/src/test/server/service/RatingServiceStub.js +++ b/src/test/server/service/RatingServiceStub.js @@ -53,6 +53,7 @@ exports.getStubs = function() mergeBucket: () => {}, saveQuoteClasses: () => {}, setWorksheets: () => {}, + saveQuote: () => {}, }; const session = { @@ -60,15 +61,19 @@ exports.getStubs = function() }; const request = { - getSession: () => session, + getSession: () => session, getSessionIdName: () => {}, }; const response = {}; const quote = { - getProgramId: () => program_id, - getProgram: () => program, - getId: () => 0, + getProgramId: () => program_id, + getProgram: () => program, + getId: () => 0, + setLastPremiumDate: () => {}, + setRatedDate: () => {}, + getRatedDate: () => 0, + getLastPremiumDate: () => 0 }; return { diff --git a/test/client/event/RateEventHandlerTest.js b/test/client/event/RateEventHandlerTest.js index 7cbc3a6..39d8f12 100644 --- a/test/client/event/RateEventHandlerTest.js +++ b/test/client/event/RateEventHandlerTest.js @@ -26,20 +26,40 @@ const Sut = require( '../../..' ).client.event.RateEventHandler; describe( 'RateEventHandler', () => { - describe( "Handle Rating Event", () => + describe( "Handle Rating Event with all conditions met for clean rating", () => { it( "calls #handle to do the rating", done => { - const stepId = 0; - const lockStep = 1; + let test_init_date = false; + let test_last_prem_date = false; + let test_lock_quote = false; + + const stepId = 1; + const lockStep = 0; const indv = "somerater"; + const initial_rated_date = 111; + const last_premium_date = 222; const quote = { getExplicitLockStep: () => lockStep, - setInitialRatedDate: ( value ) => {}, + setLastPremiumDate: given => + { + expect( given ).to.equal( last_premium_date ); + test_init_date = true; + }, + setInitialRatedDate: given => + { + expect( given ).to.equal( initial_rated_date ); + test_last_prem_date = true; + }, getCurrentStepId: () => stepId, refreshData: () => {}, - isLocked: () => false + isLocked: given => + { + test_lock_quote = true; + return false; + }, + getId: () => "111111" }; const step = { @@ -51,6 +71,7 @@ describe( 'RateEventHandler', () => }; const client = { + showRatingInProgressDialog: () => "Some Dialog", getQuote: () => quote, isSaving: () => false, once: ( event, callback ) => {}, @@ -59,11 +80,13 @@ describe( 'RateEventHandler', () => const response = { content: { - data: "Some Data" + data: "Some Data", + initialRatedDate: initial_rated_date, + lastRatedDate: last_premium_date } }; - const error = "ERROR"; + const error = null; const proxy = { get: ( url, callback ) => callback( response, error ) @@ -71,11 +94,159 @@ describe( 'RateEventHandler', () => const sut = Sut( client, proxy ); - sut.handle( "", function() {}, { - indv: indv, - stepId: stepId - } ); - done(); + sut.handle( + "", + ( err, result ) => + { + expect( err ).to.equal( error ); + expect( result ).to.equal( response.content.data ); + expect( test_init_date ).to.equal( true ); + expect( test_last_prem_date ).to.equal( true ); + expect( test_lock_quote ).to.equal( true ); + done(); + }, + { + indv: indv, + stepId: stepId + } + ) + } ) - } ) + } ); + + describe( "Handle Rating Event with locked quote", () => + { + it( "calls #handle to do the rating with a locked quote", done => + { + + let test_lock_quote = false; + + const stepId = 1; + const lockStep = 0; + const indv = "somerater"; + const error = null; + const proxy = { + get: ( url, callback ) => callback( response, error ) + }; + + const quote = { + getExplicitLockStep: () => lockStep, + getCurrentStepId: () => stepId, + isLocked: given => + { + test_lock_quote = true; + return true; + } + }; + + const client = { + getQuote: () => quote, + isSaving: () => false, + getUi: () => ui + }; + + + const sut = Sut( client, proxy ); + + sut.handle( + "", + ( err, result ) => + { + expect( test_lock_quote ).to.equal( true ); + done(); + }, + { + indv: indv, + stepId: stepId + } + ) + } ) + } ); + + describe( "Handle Rating Event during a save event", () => + { + it( "calls #handle to do the rating with a saving quote", done => + { + let test_init_date = false; + let test_last_prem_date = false; + let test_save_quote = false; + + const stepId = 1; + const lockStep = 0; + const indv = "somerater"; + const initial_rated_date = 111; + const last_premium_date = 222; + + const quote = { + getExplicitLockStep: () => lockStep, + setLastPremiumDate: given => + { + expect( given ).to.equal( last_premium_date ); + test_init_date = true; + }, + setInitialRatedDate: given => + { + expect( given ).to.equal( initial_rated_date ); + test_last_prem_date = true; + }, + getCurrentStepId: () => stepId, + refreshData: () => {}, + isLocked: () => false, + getId: () => "111111" + }; + + const step = { + invalidate: () => {} + }; + + const ui = { + getStep: ( dest ) => step + }; + + const client = { + showRatingInProgressDialog: () => "Some Dialog", + getQuote: () => quote, + isSaving: given => + { + test_save_quote = true; + return true; + }, + once: ( event, callback ) => callback(), + getUi: () => ui + }; + + const response = { + content: { + data: "Some Data", + initialRatedDate: initial_rated_date, + lastRatedDate: last_premium_date + } + }; + + const error = null; + + const proxy = { + get: ( url, callback ) => callback( response, error ) + }; + + const sut = Sut( client, proxy ); + + sut.handle( + "", + ( err, result ) => + { + expect( err ).to.equal( error ); + expect( result ).to.equal( response.content.data ); + expect( test_init_date ).to.equal( true ); + expect( test_last_prem_date ).to.equal( true ); + expect( test_save_quote ).to.equal( true ); + done(); + }, + { + indv: indv, + stepId: stepId + } + ) + } ) + } ); } ) diff --git a/test/client/quote/ClientQuoteTest.js b/test/client/quote/ClientQuoteTest.js index 3ad9260..0a249aa 100644 --- a/test/client/quote/ClientQuoteTest.js +++ b/test/client/quote/ClientQuoteTest.js @@ -35,16 +35,18 @@ describe( 'ClientQuote', () => const agent_name = 'John Doe'; const agent_entity_id = 12434300; const initial_rated_date = 1531507748; + const last_rated_date = 1531508748; const quote = ClientQuote( base_quote, { - startDate: start_date, - agentId: agent_id, - agentName: agent_name, - agentEntityId: agent_entity_id, + startDate: start_date, + agentId: agent_id, + agentName: agent_name, + agentEntityId: agent_entity_id, initialRatedDate: initial_rated_date, + lastRatedDate: last_rated_date, }, - bucket => bucket + bucket => bucket ); it( 'getStartDate returns base quote startDate', () => @@ -71,4 +73,9 @@ describe( 'ClientQuote', () => { expect( quote.getInitialRatedDate() ).to.equal( initial_rated_date ); } ); + + it( 'getLastPremiumDate returns base quote lastRatedDate', () => + { + expect( quote.getLastPremiumDate() ).to.equal( last_rated_date ); + } ); } ); diff --git a/test/server/service/RatingServiceSubmitNotifyTest.js b/test/server/service/RatingServiceSubmitNotifyTest.js index 80e0bc3..d053755 100644 --- a/test/server/service/RatingServiceSubmitNotifyTest.js +++ b/test/server/service/RatingServiceSubmitNotifyTest.js @@ -179,8 +179,8 @@ describe( 'RatingServiceSubmitNotify', () => // only save notification status if we're notifying expect( notify_saved ).to.equal( expected.save ); + done(); } ); - done(); } ) ); } ); diff --git a/test/server/service/RatingServiceTest.js b/test/server/service/RatingServiceTest.js index 948af9a..2586945 100644 --- a/test/server/service/RatingServiceTest.js +++ b/test/server/service/RatingServiceTest.js @@ -61,5 +61,45 @@ describe( 'RatingService', () => sut.request( request, response, quote, 'something', () => {} ); } ); + + it( "calls getLastPremiumDate during #_performRating", done => + { + let getLastPremiumDateCallCount = 0; + + const last_date = 1234; + const initial_date = 2345; + + const { + logger, + server, + raters, + dao, + request, + response, + quote, + } = RatingServiceStub.getStubs(); + + quote.getLastPremiumDate = () => + { + getLastPremiumDateCallCount++; + return last_date + }; + + quote.getRatedDate = () => initial_date; + + const sut = Sut( logger, dao, server, raters ); + + server.sendResponse = ( request, quote, resp, actions ) => + { + expect( getLastPremiumDateCallCount ).to.equal( 2 ); + expect( resp.initialRatedDate ).to.equal( initial_date ); + expect( resp.lastRatedDate ).to.equal( last_date ); + + done(); + }; + + sut.request( request, response, quote, null, () => {} ); + } ); + } ); } );