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() ); 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 len = this._program.steps.length;
const classes = field_matches.__classes; const data = this._parseSteps( len, bucket, matches );
const data = this._parseSteps( len, bucket, classes );
resolve( data ); resolve( data );
} ); } );
@ -93,12 +92,12 @@ module.exports = Class( 'DocumentProgramFormatter',
* Parses step data * Parses step data
* *
* @param {Number} len step length * @param {Number} len step length
* @param {Bucket} bucket document bucket * @param {Bucket} bucket document bucket
* @param {Object} classes class/field matches * @param {Object} matches field matches
* *
* @return {Object} step data * @return {Object} step data
*/ */
'private _parseSteps'( len, bucket, classes ) 'private _parseSteps'( len, bucket, matches )
{ {
const data = { steps: [] }; const data = { steps: [] };
@ -107,7 +106,7 @@ module.exports = Class( 'DocumentProgramFormatter',
const step = {}; const step = {};
const step_groups = this._program.steps[ i ].groups; 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.title = this._program.steps[ i ].title;
step.groups = groups; step.groups = groups;
@ -124,11 +123,11 @@ module.exports = Class( 'DocumentProgramFormatter',
* *
* @param {Array} step_groups array of group data * @param {Array} step_groups array of group data
* @param {Bucket} bucket document bucket * @param {Bucket} bucket document bucket
* @param {Object} classes class/field matches * @param {Object} matches field matches
* *
* @return {Array} array of groups * @return {Array} array of groups
*/ */
'private _parseGroups'( step_groups, bucket, classes ) 'private _parseGroups'( step_groups, bucket, matches )
{ {
return step_groups.map( group_id => return step_groups.map( group_id =>
{ {
@ -139,7 +138,7 @@ module.exports = Class( 'DocumentProgramFormatter',
id: group_id, id: group_id,
title: title || "", title: title || "",
link: link || "", 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 {Array} fields array of field data
* @param {Bucket} bucket document bucket * @param {Bucket} bucket document bucket
* @param {Object} classes class/field matches * @param {Object} matches field matches
* *
* @return {Array} array of questions * @return {Array} array of questions
*/ */
'private _parseFields'( fields, bucket, classes ) 'private _parseFields'( fields, bucket, matches )
{ {
const questions = []; const questions = [];
@ -176,7 +175,7 @@ module.exports = Class( 'DocumentProgramFormatter',
label: label || "", label: label || "",
value: value, value: value,
type: type || "", 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 * Determine when a field is shown by index
* Map boolean values of [0, 1] to [true, false] * 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 {String} field_id id of field
* @param {Object} field_value field object * @param {Object} field_value field object
* *
* @return {Array.<boolean>} array of booleans * @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 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 ); return field_value.map( _ => true );
} }
const class_id = this._program.whens[ field_id ][ 0 ]; const indexes = matches[ field_id ].indexes;
const indexes = classes[ class_id ].indexes; const any_match = matches[ field_id ].any;
// Map indexes of 0, 1 to true, false if ( indexes.length > 0 )
if ( Array.isArray( indexes ) )
{ {
// Map indexes of 0, 1 to true, false
return indexes.map( x => !!x ); return indexes.map( x => !!x );
} }
else 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", () => it( "formats bucket data by steps, groups and fields", () =>
{ {
const bucket_data = { const bucket_data = {
sell_alcohol: [ "foo", "" ], alcohol_shown: [ "foo", "" ],
serve_alcohol: [ "" ], alcohol_not_shown: [ "" ],
sell_ecigs: [ "", "bar" ], ecigs_shown_twice: [ "foo", "bar" ],
dist_ecigs: [ "" ], ecigs_not_shown: [ "" ],
field_no_label: [ "" ], 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 = { const expected_object = {
@ -63,22 +64,22 @@ describe( 'DocumentProgramFormatter', () =>
link: "locations", link: "locations",
questions: [ questions: [
{ {
id: "sell_alcohol", id: "alcohol_shown",
label: "Does the insured sell alcohol?", label: "Alcohol?",
value: [ "foo", "" ], value: [ "foo", "" ],
type: "noyes", type: "noyes",
applicable: [ true, false ] applicable: [ true, false ]
}, },
{ {
id: "serve_alcohol", id: "alcohol_not_shown",
label: "Does the insured serve alcohol?", label: "No alcohol?",
value: [ "" ], value: [ "" ],
type: "noyes", type: "noyes",
applicable: [ false ] applicable: [ false ]
}, },
{ {
id: "field_no_vis", id: "field_no_vis",
label: "Does this field have a visibility class?", label: "Is this field found in FieldMatcher?",
value: [ "true" ], value: [ "true" ],
type: "noyes", type: "noyes",
applicable: [ true ] applicable: [ true ]
@ -91,25 +92,32 @@ describe( 'DocumentProgramFormatter', () =>
link: "", link: "",
questions: [ questions: [
{ {
id: "sell_ecigs", id: "ecigs_shown_twice",
label: "Does the insured sell e-cigarettes?", label: "Two ecig answers?",
value: [ "", "bar" ], value: [ "foo", "bar" ],
type: "noyes", type: "noyes",
applicable: [ false, true ] applicable: [ true, true ]
}, },
{ {
id: "dist_ecigs", id: "ecigs_not_shown",
label: "Does the Insured distribute Electronic Cigarette products?", label: "No ecigs?",
value: [ "" ], value: [ "" ],
type: "noyes", type: "noyes",
applicable: [ false ] applicable: [ false ]
}, },
{ {
id: "field_no_array", id: "field_empty_array_any_true",
label: "Does this field have an array for the visibility class?", label: "Empty match array?",
value: [ "bar" ], value: [ "bar" ],
type: "noyes", type: "noyes",
applicable: [ true ] 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 ) match( _, callback )
{ {
callback({ callback({
__classes: "__classes": {
{ '--vis-foo': { is: true, indexes: [1,0] },
'--vis-sell-alcohol': { is: true, indexes: [1,0] }, },
'--vis-serve-alcohol': { is: false, indexes: [0] }, "alcohol_shown": {
'--vis-sell-ecigs': { is: false, indexes: [0,1] }, "all": false,
'--vis-dist-ecigs': { is: true, indexes: [0] }, "any": true,
'--vis-no-array': { is: true, indexes: 1 }, "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: fields:
{ {
sell_alcohol: alcohol_shown:
{ {
label: "Does the insured sell alcohol?", label: "Alcohol?",
type: "noyes", type: "noyes",
required: "true", required: "true",
}, },
serve_alcohol: alcohol_not_shown:
{ {
label: "Does the insured serve alcohol?", label: "No alcohol?",
type: "noyes", type: "noyes",
required: "true" required: "true"
}, },
sell_ecigs: ecigs_shown_twice:
{ {
label: "Does the insured sell e-cigarettes?", label: "Two ecig answers?",
type: "noyes", type: "noyes",
required: "true" required: "true"
}, },
dist_ecigs: ecigs_not_shown:
{ {
label: "Does the Insured distribute Electronic Cigarette products?", label: "No ecigs?",
type: "noyes", type: "noyes",
required: "true" 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", type: "noyes",
required: "true" required: "true"
}, },
field_no_vis: field_no_vis:
{ {
label: "Does this field have a visibility class?", label: "Is this field found in FieldMatcher?",
type: "noyes", type: "noyes",
required: "true" required: "true"
} }
}, },
groupExclusiveFields: groupExclusiveFields:
{ {
'group_one': [ "sell_alcohol", "serve_alcohol", "field_no_label", "field_no_vis" ], 'group_one': [
'group_two': [ "sell_ecigs", "dist_ecigs", "field_no_array" ], "alcohol_shown",
}, "alcohol_not_shown",
whens: "field_no_label",
{ "field_no_vis"
sell_alcohol: [ "--vis-sell-alcohol" ], ],
serve_alcohol: [ "--vis-serve-alcohol" ], 'group_two': [
sell_ecigs: [ "--vis-sell-ecigs" ], "ecigs_shown_twice",
dist_ecigs: [ "--vis-dist-ecigs" ], "ecigs_not_shown",
field_no_array: [ "--vis-no-array" ], "field_empty_array_any_true",
"field_empty_array_any_false"
],
}, },
}; };
} }