Skip to content

Commit b25ab70

Browse files
committed
contacts_by_type_freetext & reports_by_freetext nouveau-style
1 parent baaed8e commit b25ab70

File tree

3 files changed

+111
-9
lines changed
  • ddocs/medic-db/medic-nouveau/nouveau

3 files changed

+111
-9
lines changed

ddocs/medic-db/medic-nouveau/nouveau/contacts_by_freetext/index.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
function(doc) {
2-
const skip = [ '_id', '_rev', 'type', 'refid', 'geolocation' ];
1+
function (doc) {
2+
const skip = ['_id', '_rev', 'type', 'refid', 'geolocation'];
33
let toIndex = '';
44

5-
const types = [ 'district_hospital', 'health_center', 'clinic', 'person' ];
5+
const types = ['district_hospital', 'health_center', 'clinic', 'person'];
66
let idx;
77
if (doc.type === 'contact') {
88
idx = types.indexOf(doc.contact_type);
@@ -15,7 +15,7 @@ function(doc) {
1515

1616
const isContactDoc = idx !== -1;
1717
if (isContactDoc) {
18-
Object.keys(doc).forEach(function(key) {
18+
Object.keys(doc).forEach(function (key) {
1919
const value = doc[key];
2020
if (!key || !value) {
2121
return;
@@ -28,14 +28,16 @@ function(doc) {
2828

2929
if (typeof value === 'string') {
3030
toIndex += ' ' + value;
31+
// index('text', key, value, { store: true });
3132
}
3233

33-
/*if (typeof value === 'number') {
34-
index('double', key, value, { store: true });
35-
}*/
34+
if (typeof value === 'number') {
35+
// index('double', key, value, { store: true });
36+
}
3637

37-
/*if (typeof value === 'string') {
38-
index('text', key, value, { store: true });
38+
/*const fieldNameRegex = /^\$?[a-zA-Z][a-zA-Z0-9_]*$/g
39+
if (fieldNameRegex.test(key)) {
40+
console.log(`key "${key}" doesn't pass regex`);
3941
}*/
4042
});
4143

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
function (doc) {
2+
var skip = ['_id', '_rev', 'type', 'refid', 'geolocation'];
3+
let toIndex = '';
4+
5+
var types = ['district_hospital', 'health_center', 'clinic', 'person'];
6+
var idx;
7+
var type;
8+
if (doc.type === 'contact') {
9+
type = doc.contact_type;
10+
idx = types.indexOf(type);
11+
if (idx === -1) {
12+
idx = type;
13+
}
14+
} else {
15+
type = doc.type;
16+
idx = types.indexOf(type);
17+
}
18+
if (idx !== -1) {
19+
Object.keys(doc).forEach(function (key) {
20+
var value = doc[key];
21+
if (!key || !value) {
22+
return;
23+
}
24+
key = key.toLowerCase();
25+
if (skip.indexOf(key) !== -1 || /_date$/.test(key)) {
26+
return;
27+
}
28+
29+
if (typeof value === 'string') {
30+
toIndex += ' ' + value;
31+
// index('text', key, value, { store: true });
32+
}
33+
34+
if (typeof value === 'number') {
35+
// index('double', key, value, { store: true });
36+
}
37+
38+
/*const fieldNameRegex = /^\$?[a-zA-Z][a-zA-Z0-9_]*$/g
39+
if (fieldNameRegex.test(key)) {
40+
console.log(`key "${key}" doesn't pass regex`);
41+
}*/
42+
});
43+
}
44+
45+
toIndex = toIndex.trim();
46+
if (toIndex) {
47+
index('text', 'default', toIndex, { store: true });
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
function (doc) {
2+
var skip = ['_id', '_rev', 'type', 'refid', 'content'];
3+
let toIndex = '';
4+
5+
var emitField = function (key, value) {
6+
if (!key || !value) {
7+
return;
8+
}
9+
key = key.toLowerCase();
10+
if (skip.indexOf(key) !== -1 || /_date$/.test(key)) {
11+
return;
12+
}
13+
14+
if (typeof value === 'string') {
15+
toIndex += ' ' + value;
16+
// index('text', key, value, { store: true });
17+
}
18+
19+
if (typeof value === 'number') {
20+
// index('double', key, value, { store: true });
21+
}
22+
23+
const fieldNameRegex = /^\$?[a-zA-Z][a-zA-Z0-9_]*$/g
24+
if (fieldNameRegex.test(key)) {
25+
console.log(`key "${key}" doesn't pass regex`);
26+
}
27+
};
28+
29+
if (doc.type === 'data_record' && doc.form) {
30+
Object.keys(doc).forEach(function (key) {
31+
emitField(key, doc[key]);
32+
});
33+
if (doc.fields) {
34+
Object.keys(doc.fields).forEach(function (key) {
35+
emitField(key, doc.fields[key]);
36+
});
37+
}
38+
if (doc.contact && doc.contact._id) {
39+
// index('text', 'contact', doc.contact._id.toLowerCase(), { store: true });
40+
/*const fieldNameRegex = /^\$?[a-zA-Z][a-zA-Z0-9_]*$/g
41+
if (fieldNameRegex.test('contact')) {
42+
console.log(`key "contact" doesn't pass regex`);
43+
}*/
44+
}
45+
}
46+
47+
toIndex = toIndex.trim();
48+
if (toIndex) {
49+
index('text', 'default', toIndex, { store: true });
50+
}
51+
}

0 commit comments

Comments
 (0)