Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShelly Shaver <shelly.shaver@rtspecialty.com>2020-01-29 14:46:30 -0500
committerShelly Shaver <shelly.shaver@rtspecialty.com>2020-02-03 16:19:17 -0500
commit44a42bd256aea77f309effb02403cbca2912782f (patch)
treede66bebeb47bb91214400859ecf09bd11ad0dde1
parent41343fccddb7b068662b462677fe4b6fac786f3a (diff)
downloadliza-44a42bd256aea77f309effb02403cbca2912782f.tar.gz
liza-44a42bd256aea77f309effb02403cbca2912782f.tar.bz2
liza-44a42bd256aea77f309effb02403cbca2912782f.zip
[DEV-6323] Send action to client for re-rating
Delay action is sent if any raters have not completed rating and a re-try is needed. The __rate_pending count of pending suppliers is saved in ratedate and used to prevent post-rate messages from being processed by the delta processor.
-rw-r--r--src/server/service/RatingService.ts29
-rw-r--r--src/system/db/MongoDeltaDao.ts2
-rw-r--r--test/server/service/RatingServiceTest.ts70
-rw-r--r--test/system/db/MongoDeltaDaoTest.ts112
4 files changed, 213 insertions, 0 deletions
diff --git a/src/server/service/RatingService.ts b/src/server/service/RatingService.ts
index 7eb2de8..3f32ec2 100644
--- a/src/server/service/RatingService.ts
+++ b/src/server/service/RatingService.ts
@@ -315,6 +315,20 @@ export class RatingService
// rating worksheets are returned as metadata
this._processWorksheetData( quote.getId(), data );
+ // set count of pending raters
+ const retry_count = this._getRetryCount( data );
+
+ data[ '__rate_pending' ] = retry_count;
+
+ if ( retry_count > 0 )
+ {
+ actions.push( {
+ "action": "delay",
+ "seconds": 2,
+ "then": { action: "rate" }
+ } );
+ }
+
if ( ( program.ineligibleLockCount > 0 )
&& ( +meta.count_ineligible >= program.ineligibleLockCount )
)
@@ -396,6 +410,21 @@ export class RatingService
/**
+ * Retrieve the number of raters that are pending
+ *
+ * @param data Rating results
+ */
+ private _getRetryCount( data: RateResult ): number
+ {
+ const retry_pattern = /^(.+)__retry$/;
+
+ return Object.keys( data )
+ .filter( field => field.match( retry_pattern ) && !!data[ field ][ 0 ] )
+ .length;
+ }
+
+
+ /**
* Process and save worksheet data from rating results
*
* @param qid - quote id
diff --git a/src/system/db/MongoDeltaDao.ts b/src/system/db/MongoDeltaDao.ts
index cd62a0f..90853a1 100644
--- a/src/system/db/MongoDeltaDao.ts
+++ b/src/system/db/MongoDeltaDao.ts
@@ -72,10 +72,12 @@ export class MongoDeltaDao implements DeltaDao
{
return new Promise( ( resolve, reject ) =>
{
+ // documents that have rates pending should not be processed yet
this._collection.find(
{
published: false,
deltaError: { $ne: true },
+ 'ratedata.__rate_pending': { $in: [ 0, null ] },
env: this._env,
},
{ fields: this.RESULT_FIELDS },
diff --git a/test/server/service/RatingServiceTest.ts b/test/server/service/RatingServiceTest.ts
index e781ceb..1434a5c 100644
--- a/test/server/service/RatingServiceTest.ts
+++ b/test/server/service/RatingServiceTest.ts
@@ -423,6 +423,76 @@ describe( 'RatingService', () =>
.then( () => expect( sent ).to.be.true );
} );
+ ( <[string, Record<string, any>, number, boolean][]>[
+ [
+ "delay action is returned when raters are pending",
+ {
+ 'supplier-a__retry': [ 0 ],
+ 'supplier-b__retry': [ 1 ],
+ 'supplier-c__retry': [ 1 ],
+ 'supplier-d__retry': [ 0 ],
+ },
+ 2,
+ true
+ ],
+ [
+ "delay action is not returned when all raters are completed",
+ {
+ 'supplier-a__retry': [ 0 ],
+ 'supplier-b__retry': [ 0 ],
+ 'supplier-c__retry': [ 0 ],
+ 'supplier-d__retry': [ 0 ],
+ },
+ 0,
+ false
+ ],
+ ] ).forEach( ( [label, supplier_data, expected_count, expected_delay_action ]) =>
+ {
+ it( label, () =>
+ {
+ const {
+ dao,
+ logger,
+ quote,
+ raters,
+ request,
+ response,
+ server,
+ stub_rate_data,
+ createDelta,
+ } = getStubs();
+
+ let sent = false;
+
+ Object.assign( stub_rate_data, supplier_data );
+
+ server.sendResponse = (
+ _request: any,
+ _quote: any,
+ resp: RateRequestResult,
+ actions: ClientActions
+ ) =>
+ {
+ const expected_action = {
+ "action": "delay",
+ "seconds": 2,
+ "then": { action: "rate" }
+ };
+
+ ( expected_delay_action )
+ ? expect( actions ).to.deep.equal( [ expected_action ] )
+ : expect( actions ).to.not.equal( [ expected_action ] );
+
+ expect( resp.data[ '__rate_pending' ] ).to.equal( expected_count );
+ sent = true;
+ return server;
+ };
+
+ const sut = new Sut( logger, dao, server, raters, createDelta );
+ return sut.request( request, response, quote, "" )
+ .then( () => expect( sent ).to.be.true );
+ } );
+ } );
describe( "protected API", () =>
{
diff --git a/test/system/db/MongoDeltaDaoTest.ts b/test/system/db/MongoDeltaDaoTest.ts
new file mode 100644
index 0000000..9c9a15f
--- /dev/null
+++ b/test/system/db/MongoDeltaDaoTest.ts
@@ -0,0 +1,112 @@
+/**
+ * Delta data access test
+ *
+ * Copyright (C) 2010-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 <http://www.gnu.org/licenses/>.
+ */
+
+import { MongoDeltaDao as Sut } from "../../../src/system/db/MongoDeltaDao";
+import { MongoCollection } from "mongodb";
+import { DeltaDocument } from "../../../src/bucket/delta";
+import { DaoError } from '../../../src/error/DaoError';
+import { expect, use as chai_use } from 'chai';
+import {DocumentId} from "../../../src/document/Document";
+chai_use( require( 'chai-as-promised' ) );
+
+
+describe( 'system.db.MongoDeltaDao', () =>
+{
+ describe( '#getUnprocessedDocuments', () =>
+ {
+ it( "finds unprocessed documents that do not have rates pending", () =>
+ {
+ const stub_env = 'foo';
+ let find_called = false;
+
+ const delta_docs = <DeltaDocument[]>[ {
+ id: <DocumentId>123,
+ programId: '',
+ agentName: '',
+ agentEntityId: 0,
+ startDate: <UnixTimestamp>0,
+ lastUpdate: <UnixTimestamp>0,
+ data: {},
+ ratedata: {},
+ rdelta: {},
+ } ];
+
+ const coll: MongoCollection = {
+ findAndModify() {},
+ findOne() {},
+ update() {},
+ createIndex() {},
+ insert() {},
+ find(
+ selector,
+ _fields,
+ callback )
+ {
+ expect( selector[ 'ratedata.__rate_pending' ] )
+ .to.deep.equal( { $in: [ 0, null ] } );
+ expect( selector.deltaError )
+ .to.deep.equal( { $ne: true } );
+ expect( selector.published )
+ .to.be.false;
+ expect( selector.env )
+ .to.equal( stub_env );
+
+ find_called = true;
+ callback( null, {
+ toArray: ( c: any ) => c( null, delta_docs ),
+ } );
+ },
+ };
+
+ const sut = new Sut( coll, stub_env );
+ return sut.getUnprocessedDocuments()
+ .then( () => expect( find_called ).to.be.true );
+ } );
+
+ it( "rejects with error on query failure", () =>
+ {
+ const stub_env = 'foo';
+ let find_called = false;
+ const expected_error_msg = 'expected error!';
+
+ const coll: MongoCollection = {
+ findAndModify() {},
+ findOne() {},
+ update() {},
+ createIndex() {},
+ insert() {},
+ find(
+ _selector,
+ _fields,
+ callback )
+ {
+ find_called = true;
+ callback( new Error( expected_error_msg ), {} );
+ },
+ };
+
+ const sut = new Sut( coll, stub_env );
+ return expect( sut.getUnprocessedDocuments() )
+ .to.eventually.rejectedWith( DaoError, expected_error_msg )
+ .then( () => expect( find_called ).to.be.true );
+ } );
+ } );
+} );