-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add offline ddoc logic to webapp along with initial contacts_by_freet…
…ext view
- Loading branch information
Showing
13 changed files
with
315 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"globals": { | ||
"emit": true | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
webapp/src/js/offline-ddocs/medic-offline-freetext/contacts_by_freetext.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
module.exports.map = function(doc) { | ||
const skip = [ '_id', '_rev', 'type', 'contact_type', 'refid', 'geolocation' ]; | ||
|
||
const usedKeys = []; | ||
const emitMaybe = (key, value) => { | ||
if (usedKeys.indexOf(key) === -1 && // Not already used | ||
key.length > 2 // Not too short | ||
) { | ||
usedKeys.push(key); | ||
emit([key], value); | ||
} | ||
}; | ||
|
||
const emitField = (key, value, order) => { | ||
if (!value) { | ||
return; | ||
} | ||
const lowerKey = key.toLowerCase(); | ||
if (skip.indexOf(lowerKey) !== -1 || /_date$/.test(lowerKey)) { | ||
return; | ||
} | ||
if (typeof value === 'string') { | ||
value | ||
.toLowerCase() | ||
.split(/\s+/) | ||
.forEach((word) => emitMaybe(word, order)); | ||
} | ||
}; | ||
|
||
const getTypeIndex = () => { | ||
const types = [ 'district_hospital', 'health_center', 'clinic', 'person' ]; | ||
if (doc.type !== 'contact') { | ||
return types.indexOf(doc.type); | ||
} | ||
|
||
const contactTypeIdx = types.indexOf(doc.contact_type); | ||
if (contactTypeIdx >= 0) { | ||
return contactTypeIdx; | ||
} | ||
|
||
return doc.contact_type; | ||
}; | ||
|
||
const idx = getTypeIndex(); | ||
if (idx !== -1) { | ||
const dead = !!doc.date_of_death; | ||
const muted = !!doc.muted; | ||
const order = dead + ' ' + muted + ' ' + idx + ' ' + (doc.name && doc.name.toLowerCase()); | ||
Object.keys(doc).forEach(function(key) { | ||
emitField(key, doc[key], order); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export interface DesignDoc { | ||
readonly _id: `_design/${string}`; | ||
readonly _rev?: string; | ||
readonly views: { | ||
[key: string]: { | ||
map: string; | ||
}; | ||
}; | ||
} | ||
|
||
export const packageView = ({ map }: { map: Function }) => ({ map: map.toString() }); |
10 changes: 10 additions & 0 deletions
10
webapp/src/ts/services/offline-ddocs/medic-offline-freetext.ddoc.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { DesignDoc, packageView } from './design-doc'; | ||
|
||
import * as contactByFreetext from '../../../js/offline-ddocs/medic-offline-freetext/contacts_by_freetext.js'; | ||
|
||
export default <DesignDoc>{ | ||
_id: '_design/medic-offline-freetext', | ||
views: { | ||
contacts_by_freetext: packageView(contactByFreetext), | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const chaiAsPromised = require('chai-as-promised'); | ||
const chai = require('chai'); | ||
chai.use(chaiAsPromised); | ||
|
||
module.exports = { | ||
spec: 'tests/mocha/ts/**/*.spec.ts', | ||
require: 'ts-node/register', | ||
}; |
Oops, something went wrong.