Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: resolve sonar issues in contact-summary-emitter #646

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 64 additions & 14 deletions src/contact-summary/contact-summary-emitter.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,51 @@

const contactTypeIncluded = (contactType, includedTypes) => {
return includedTypes.length === 0 || includedTypes.includes(contactType);
};

const contactTypeExcluded = (contactType, excludedTypes) => {
return excludedTypes.length === 0 || excludedTypes.includes('!' + contactType); // TODO
};

const fieldFilter = (contactType, field) => {
// These two arrays should be disjoint sets but they are not.
const includedTypes = convertToArray(field.appliesToType);
const excludedTypes = includedTypes.filter(function(type) {
return type && type.charAt(0) === '!';
});

const noTypesConfigured = includedTypes.length === 0;
const configuredTypeIncluded = includedTypes.includes(contactType);
const excludedTypesConfigured = excludedTypes.length > 0;
const contactTypeExcluded = excludedTypes.includes('!' + contactType);

const typeMatches = noTypesConfigured || configuredTypeIncluded || (excludedTypesConfigured && !contactTypeExcluded);

const hasAppliesIf = field.appliesIf && typeof field.appliesIf === 'function';

if (typeMatches && (!hasAppliesIf || field.appliesIf())) {
delete field.appliesToType;
delete field.appliesIf;
return true;
}
};

/**
* @param contactSummary {{
* fields: {
* appliesToType: string[] | undefined,
* appliesIf: () => boolean | undefined,
* }[] | undefined,
* context: object | undefined,
* cards: {
* appliesToType: string[] | undefined,
* appliesIf: (report: object) => boolean | boolean | undefined,
* }[] | undefined,
* }}
* @param contact {object}
* @param reports {object[]}
* @returns {{cards: object[], fields: object[]}}
*/
function emitter(contactSummary, contact, reports) {
var fields = contactSummary.fields || [];
var context = contactSummary.context || {};
Expand All @@ -7,20 +55,22 @@ function emitter(contactSummary, contact, reports) {

var result = {
cards: [],
fields: fields.filter(function(f) {
var appliesToType = convertToArray(f.appliesToType);
var appliesToNotType = appliesToType.filter(function(type) {
return type && type.charAt(0) === '!';
});
if (appliesToType.length === 0 || appliesToType.includes(contactType) ||
(appliesToNotType.length > 0 && !appliesToNotType.includes('!' + contactType))) {
if (!f.appliesIf || f.appliesIf()) {
delete f.appliesToType;
delete f.appliesIf;
return true;
}
}
}),

fields: fields.filter(field => fieldFilter(contactType, field)),
// fields: fields.filter(function(f) {
// var appliesToType = convertToArray(f.appliesToType);
// var appliesToNotType = appliesToType.filter(function(type) {
// return type && type.charAt(0) === '!';
// });
// if (appliesToType.length === 0 || appliesToType.includes(contactType) ||
// (appliesToNotType.length > 0 && !appliesToNotType.includes('!' + contactType))) {
// if (!f.appliesIf || f.appliesIf()) {
// delete f.appliesToType;
// delete f.appliesIf;
// return true;
// }
// }
// }),
};

cards.forEach(function(card) {
Expand Down
2 changes: 1 addition & 1 deletion test/contact-summary/contact-summary-emitter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const rewire = require('rewire');

const emitter = rewire('../../src/contact-summary/contact-summary-emitter');

describe('contact-summary-emitter', function() {
describe.only('contact-summary-emitter', function() {
describe('test-setup', function() {
it('should provide the lib', function() {
// given
Expand Down
Loading