Skip to content

Commit 37ba97c

Browse files
bmarchenaTylerMatteo
authored andcommitted
Change FEMA filter logic to be inclusive
1 parent aa90904 commit 37ba97c

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

server/src/crm/crm.utilities.ts

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function coerceToNumber(numericStrings) {
1111
numericStrings
1212
// filter out blank strings and undefined, which aren't meaningfully
1313
// coercible in CRM
14-
.filter(stringish => stringish !== "" && stringish !== undefined)
14+
.filter(stringish => stringish !== '' && stringish !== undefined)
1515
.map(stringish => {
1616
// smelly; but let's prefer actual null
1717
// coercing 'null' turns to 0, which we don't
@@ -21,7 +21,7 @@ export function coerceToNumber(numericStrings) {
2121
// Coercing an empty string into a number returns
2222
// NaN, which, although a number, is a Double in CRM
2323
// which typically expects an Int
24-
if (stringish === "") return stringish;
24+
if (stringish === '') return stringish;
2525

2626
return Number(stringish);
2727
})
@@ -39,25 +39,44 @@ export function mapInLookup(arrayOfStrings, lookupHash) {
3939
}
4040

4141
export function all(...statements): string {
42-
return statements.filter(Boolean).join(" and ");
42+
const femaFloodZoneFilters = statements.filter(statement => {
43+
if (statement.includes('femafloodzone') && statement.includes('true'))
44+
return statement;
45+
});
46+
const filters = statements.filter(statement => {
47+
if (!statement.includes('femafloodzone')) return statement;
48+
});
49+
50+
if (femaFloodZoneFilters.length > 0) {
51+
const femaFilterString = femaFloodZoneFilters.join(' and ');
52+
const filterString = filters.filter(Boolean).join(' and ');
53+
54+
const allStatements = filterString.concat(
55+
' and ',
56+
`((${femaFilterString}))`
57+
);
58+
return allStatements;
59+
}
60+
61+
return statements.filter(Boolean).join(' and ');
4362
}
4463

4564
export function any(...statements): string {
46-
return `(${statements.join(" or ")})`;
65+
return `(${statements.join(' or ')})`;
4766
}
4867

4968
export function comparisonOperator(propertyName, operator, value) {
5069
let typeSafeValue = value;
5170

52-
if (typeof value === "string") {
53-
if (value !== "false" && value !== "true") {
71+
if (typeof value === 'string') {
72+
if (value !== 'false' && value !== 'true') {
5473
typeSafeValue = `'${value}'`;
5574
}
5675
}
5776

5877
// most likely means it's a date. we want the date formatting that
5978
// json stringify provides.
60-
if (typeof value === "object") {
79+
if (typeof value === 'object') {
6180
const stringyDate = JSON.stringify(value).replace(/"/g, "'");
6281

6382
typeSafeValue = `${stringyDate}`;
@@ -72,22 +91,22 @@ export function containsString(propertyName, string) {
7291

7392
export function equalsAnyOf(propertyName, strings = []) {
7493
const querySegment = strings
75-
.map(string => comparisonOperator(propertyName, "eq", string))
76-
.join(" or ");
94+
.map(string => comparisonOperator(propertyName, 'eq', string))
95+
.join(' or ');
7796

7897
// Empty parenthases are invalid
79-
return querySegment ? `(${querySegment})` : "";
98+
return querySegment ? `(${querySegment})` : '';
8099
}
81100

82101
export function containsAnyOf(propertyName, strings = [], options?) {
83-
const { childEntity = "", comparisonStrategy = containsString, not = false } =
102+
const { childEntity = '', comparisonStrategy = containsString, not = false } =
84103
options || {};
85104

86105
const containsQuery = strings
87106
.map((string, i) => {
88107
// in odata syntax, this character o is a variable for scoping
89108
// logic for related entities. it needs to only appear once.
90-
const lambdaScope = childEntity && i === 0 ? `${childEntity}:` : "";
109+
const lambdaScope = childEntity && i === 0 ? `${childEntity}:` : '';
91110
const lambdaScopedProperty = childEntity
92111
? `${childEntity}/${propertyName}`
93112
: propertyName;
@@ -97,18 +116,18 @@ export function containsAnyOf(propertyName, strings = [], options?) {
97116
string
98117
)}`;
99118
})
100-
.join(" or ");
101-
const lambdaQueryPrefix = childEntity ? `${childEntity}/any` : "";
119+
.join(' or ');
120+
const lambdaQueryPrefix = childEntity ? `${childEntity}/any` : '';
102121

103-
return `(${not ? "not " : ""}${lambdaQueryPrefix}(${containsQuery}))`;
122+
return `(${not ? 'not ' : ''}${lambdaQueryPrefix}(${containsQuery}))`;
104123
}
105124

106125
export const dateParser = function(key, value) {
107-
if (typeof value === "string") {
126+
if (typeof value === 'string') {
108127
// YYYY-MM-DDTHH:mm:ss.sssZ => parsed as UTC
109128
// YYYY-MM-DD => parsed as local date
110129

111-
if (value != "") {
130+
if (value != '') {
112131
const a = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(
113132
value
114133
);
@@ -148,7 +167,7 @@ export const dateParser = function(key, value) {
148167
return value;
149168
};
150169

151-
const COMMUNITY_DISPLAY_TOKEN = "@OData.Community.Display.V1.FormattedValue";
170+
const COMMUNITY_DISPLAY_TOKEN = '@OData.Community.Display.V1.FormattedValue';
152171

153172
// CRM provides numeric codes for picklist types
154173
// for example, "yes" might appear as "1"
@@ -161,7 +180,7 @@ export function overwriteCodesWithLabels(records, targetFields) {
161180
// parent record
162181
Object.keys(record)
163182
.filter(key => key.includes(COMMUNITY_DISPLAY_TOKEN))
164-
.map(key => key.replace(COMMUNITY_DISPLAY_TOKEN, ""))
183+
.map(key => key.replace(COMMUNITY_DISPLAY_TOKEN, ''))
165184
.forEach(key => {
166185
if (targetFields.includes(key)) {
167186
newRecord[key] = record[`${key}${COMMUNITY_DISPLAY_TOKEN}`];
@@ -182,7 +201,7 @@ export function overwriteCodesWithLabels(records, targetFields) {
182201

183202
Object.keys(record)
184203
.filter(key => key.includes(COMMUNITY_DISPLAY_TOKEN))
185-
.map(key => key.replace(COMMUNITY_DISPLAY_TOKEN, ""))
204+
.map(key => key.replace(COMMUNITY_DISPLAY_TOKEN, ''))
186205
.forEach(key => {
187206
if (targetFields.includes(key)) {
188207
newRecord[key] = record[`${key}${COMMUNITY_DISPLAY_TOKEN}`];

0 commit comments

Comments
 (0)