1
0
Fork 0

[DEV-2871] DocumentProgramFormatter: Match on fields from FieldClassMatcher instead of __classes and program.whens

master
Mark Goldsmith 2018-06-13 15:42:45 -04:00
parent de931cf91b
commit 7935c699de
2 changed files with 109 additions and 69 deletions

View File

@ -76,11 +76,10 @@ module.exports = Class( 'DocumentProgramFormatter',
{
const cmatch = this._program.classify( bucket.getData() );
this._class_matcher.match( cmatch, ( field_matches ) =>
this._class_matcher.match( cmatch, ( matches ) =>
{
const len = this._program.steps.length;
const classes = field_matches.__classes;
const data = this._parseSteps( len, bucket, classes );
const data = this._parseSteps( len, bucket, matches );
resolve( data );
} );
@ -93,12 +92,12 @@ module.exports = Class( 'DocumentProgramFormatter',
* Parses step data
*
* @param {Number} len step length
* @param {Bucket} bucket document bucket
* @param {Object} classes class/field matches
* @param {Bucket} bucket document bucket
* @param {Object} matches field matches
*
* @return {Object} step data
*/
'private _parseSteps'( len, bucket, classes )
'private _parseSteps'( len, bucket, matches )
{
const data = { steps: [] };
@ -107,7 +106,7 @@ module.exports = Class( 'DocumentProgramFormatter',
const step = {};
const step_groups = this._program.steps[ i ].groups;
const groups = this._parseGroups( step_groups, bucket, classes );
const groups = this._parseGroups( step_groups, bucket, matches );
step.title = this._program.steps[ i ].title;
step.groups = groups;
@ -124,11 +123,11 @@ module.exports = Class( 'DocumentProgramFormatter',
*
* @param {Array} step_groups array of group data
* @param {Bucket} bucket document bucket
* @param {Object} classes class/field matches
* @param {Object} matches field matches
*
* @return {Array} array of groups
*/
'private _parseGroups'( step_groups, bucket, classes )
'private _parseGroups'( step_groups, bucket, matches )
{
return step_groups.map( group_id =>
{
@ -139,7 +138,7 @@ module.exports = Class( 'DocumentProgramFormatter',
id: group_id,
title: title || "",
link: link || "",
questions: this._parseFields( fields, bucket, classes ),
questions: this._parseFields( fields, bucket, matches ),
};
} );
},
@ -150,11 +149,11 @@ module.exports = Class( 'DocumentProgramFormatter',
*
* @param {Array} fields array of field data
* @param {Bucket} bucket document bucket
* @param {Object} classes class/field matches
* @param {Object} matches field matches
*
* @return {Array} array of questions
*/
'private _parseFields'( fields, bucket, classes )
'private _parseFields'( fields, bucket, matches )
{
const questions = [];
@ -176,7 +175,7 @@ module.exports = Class( 'DocumentProgramFormatter',
label: label || "",
value: value,
type: type || "",
applicable: this._getApplicable( classes, field_id, value ),
applicable: this._getApplicable( matches, field_id, value ),
} );
}
@ -189,31 +188,31 @@ module.exports = Class( 'DocumentProgramFormatter',
* Determine when a field is shown by index
* Map boolean values of [0, 1] to [true, false]
*
* @param {Object} classes object of visibility classes
* @param {Object} matches field matches
* @param {String} field_id id of field
* @param {Object} field_value field object
*
* @return {Array.<boolean>} array of booleans
*/
'private _getApplicable'( classes, field_id, field_value )
'private _getApplicable'( matches, field_id, field_value )
{
// If object is undefined, default to array of true
if ( typeof this._program.whens[ field_id ] === "undefined" )
if ( typeof matches[ field_id ] === "undefined" )
{
return field_value.map( _ => true );
}
const class_id = this._program.whens[ field_id ][ 0 ];
const indexes = classes[ class_id ].indexes;
const indexes = matches[ field_id ].indexes;
const any_match = matches[ field_id ].any;
// Map indexes of 0, 1 to true, false
if ( Array.isArray( indexes ) )
if ( indexes.length > 0 )
{
// Map indexes of 0, 1 to true, false
return indexes.map( x => !!x );
}
else
{
return field_value.map( _ => !!indexes );
return field_value.map( _ => any_match );
}
},
} );

View File

@ -39,13 +39,14 @@ describe( 'DocumentProgramFormatter', () =>
it( "formats bucket data by steps, groups and fields", () =>
{
const bucket_data = {
sell_alcohol: [ "foo", "" ],
serve_alcohol: [ "" ],
sell_ecigs: [ "", "bar" ],
dist_ecigs: [ "" ],
alcohol_shown: [ "foo", "" ],
alcohol_not_shown: [ "" ],
ecigs_shown_twice: [ "foo", "bar" ],
ecigs_not_shown: [ "" ],
field_no_label: [ "" ],
field_no_array: [ "bar" ],
field_no_vis: [ "true" ]
field_no_vis: [ "true" ],
field_empty_array_any_true: [ "bar" ],
field_empty_array_any_false: [ "" ]
};
const expected_object = {
@ -63,22 +64,22 @@ describe( 'DocumentProgramFormatter', () =>
link: "locations",
questions: [
{
id: "sell_alcohol",
label: "Does the insured sell alcohol?",
id: "alcohol_shown",
label: "Alcohol?",
value: [ "foo", "" ],
type: "noyes",
applicable: [ true, false ]
},
{
id: "serve_alcohol",
label: "Does the insured serve alcohol?",
id: "alcohol_not_shown",
label: "No alcohol?",
value: [ "" ],
type: "noyes",
applicable: [ false ]
},
{
id: "field_no_vis",
label: "Does this field have a visibility class?",
label: "Is this field found in FieldMatcher?",
value: [ "true" ],
type: "noyes",
applicable: [ true ]
@ -91,25 +92,32 @@ describe( 'DocumentProgramFormatter', () =>
link: "",
questions: [
{
id: "sell_ecigs",
label: "Does the insured sell e-cigarettes?",
value: [ "", "bar" ],
id: "ecigs_shown_twice",
label: "Two ecig answers?",
value: [ "foo", "bar" ],
type: "noyes",
applicable: [ false, true ]
applicable: [ true, true ]
},
{
id: "dist_ecigs",
label: "Does the Insured distribute Electronic Cigarette products?",
id: "ecigs_not_shown",
label: "No ecigs?",
value: [ "" ],
type: "noyes",
applicable: [ false ]
},
{
id: "field_no_array",
label: "Does this field have an array for the visibility class?",
id: "field_empty_array_any_true",
label: "Empty match array?",
value: [ "bar" ],
type: "noyes",
applicable: [ true ]
},
{
id: "field_empty_array_any_false",
label: "Empty match array and 'any' is false?",
value: [ "" ],
type: "noyes",
applicable: [ false ]
}
]
}
@ -135,14 +143,39 @@ function createStubClassMatcher()
match( _, callback )
{
callback({
__classes:
{
'--vis-sell-alcohol': { is: true, indexes: [1,0] },
'--vis-serve-alcohol': { is: false, indexes: [0] },
'--vis-sell-ecigs': { is: false, indexes: [0,1] },
'--vis-dist-ecigs': { is: true, indexes: [0] },
'--vis-no-array': { is: true, indexes: 1 },
}
"__classes": {
'--vis-foo': { is: true, indexes: [1,0] },
},
"alcohol_shown": {
"all": false,
"any": true,
"indexes": [1, 0]
},
"alcohol_not_shown": {
"all": false,
"any": false,
"indexes": [0]
},
"ecigs_shown_twice": {
"all": false,
"any": true,
"indexes": [1, 1]
},
"ecigs_not_shown": {
"all": false,
"any": false,
"indexes": []
},
"field_empty_array_any_false": {
"all": false,
"any": false,
"indexes": []
},
"field_empty_array_any_true": {
"all": true,
"any": true,
"indexes": []
},
}) ;
}
}
@ -192,55 +225,63 @@ function createStubProgram()
},
fields:
{
sell_alcohol:
alcohol_shown:
{
label: "Does the insured sell alcohol?",
label: "Alcohol?",
type: "noyes",
required: "true",
},
serve_alcohol:
alcohol_not_shown:
{
label: "Does the insured serve alcohol?",
label: "No alcohol?",
type: "noyes",
required: "true"
},
sell_ecigs:
ecigs_shown_twice:
{
label: "Does the insured sell e-cigarettes?",
label: "Two ecig answers?",
type: "noyes",
required: "true"
},
dist_ecigs:
ecigs_not_shown:
{
label: "Does the Insured distribute Electronic Cigarette products?",
label: "No ecigs?",
type: "noyes",
required: "true"
},
field_no_array:
field_empty_array_any_true:
{
label: "Does this field have an array for the visibility class?",
label: "Empty match array?",
type: "noyes",
required: "true"
},
field_empty_array_any_false:
{
label: "Empty match array and 'any' is false?",
type: "noyes",
required: "true"
},
field_no_vis:
{
label: "Does this field have a visibility class?",
label: "Is this field found in FieldMatcher?",
type: "noyes",
required: "true"
}
},
groupExclusiveFields:
{
'group_one': [ "sell_alcohol", "serve_alcohol", "field_no_label", "field_no_vis" ],
'group_two': [ "sell_ecigs", "dist_ecigs", "field_no_array" ],
},
whens:
{
sell_alcohol: [ "--vis-sell-alcohol" ],
serve_alcohol: [ "--vis-serve-alcohol" ],
sell_ecigs: [ "--vis-sell-ecigs" ],
dist_ecigs: [ "--vis-dist-ecigs" ],
field_no_array: [ "--vis-no-array" ],
'group_one': [
"alcohol_shown",
"alcohol_not_shown",
"field_no_label",
"field_no_vis"
],
'group_two': [
"ecigs_shown_twice",
"ecigs_not_shown",
"field_empty_array_any_true",
"field_empty_array_any_false"
],
},
};
}