-
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
34edb11
commit 43a1683
Showing
5 changed files
with
130 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const db = require('./db'); | ||
|
||
const list = async (users, settings) => { | ||
const ids = new Set(); | ||
for (const user of users) { | ||
ids.add(user?.doc?.facility_id); | ||
} | ||
for (const setting of settings) { | ||
ids.add(setting?.contact_id); | ||
} | ||
ids.delete(undefined); | ||
if (!ids.size) { | ||
return []; | ||
} | ||
const response = await db.medic.allDocs({ keys: Array.from(ids), include_docs: true }); | ||
return response.rows.map(row => row?.doc).filter(doc => !!doc); | ||
}; | ||
|
||
module.exports = { | ||
list, | ||
}; | ||
|
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
50 changes: 50 additions & 0 deletions
50
shared-libs/user-management/test/unit/libs/facility.spec.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,50 @@ | ||
const { expect } = require('chai'); | ||
const sinon = require('sinon'); | ||
|
||
const { list } = require('../../../src/libs/facility'); | ||
const db = require('../../../src/libs/db'); | ||
|
||
describe('facility', () => { | ||
|
||
const userA = { doc: { facility_id: 'a' } }; | ||
const userB = { doc: { facility_id: 'b' } }; | ||
|
||
const settingA = { contact_id: 'a' }; | ||
const settingB = { contact_id: 'e' }; | ||
|
||
const facilityA = { _id: 'a' }; | ||
const facilityB = { _id: 'b' }; | ||
const facilityNotFound = { error: 'not_found' }; | ||
|
||
let allDocs; | ||
|
||
beforeEach(() => { | ||
allDocs = sinon.stub(); | ||
db.init({ medic: { allDocs } }); | ||
}); | ||
|
||
it('handles empty args', async () => { | ||
const result = await list([], []); | ||
expect(result).to.be.empty; | ||
expect(allDocs.callCount).to.equal(0); | ||
}); | ||
|
||
it('handles incomplete arg docs', async () => { | ||
const result = await list([{}], [{}]); | ||
expect(result).to.be.empty; | ||
expect(allDocs.callCount).to.equal(0); | ||
}); | ||
|
||
it('finds all facilities', async () => { | ||
allDocs.resolves({ rows: [ | ||
{ doc: facilityA }, | ||
{ doc: facilityB }, | ||
facilityNotFound, | ||
] }); | ||
const result = await list([ userA, userB ], [ settingA, settingB ]); | ||
expect(result).to.deep.equal([ facilityA, facilityB ]); | ||
expect(allDocs.callCount).to.equal(1); | ||
expect(allDocs.args[0][0].keys).to.deep.equal([ 'a', 'b', 'e' ]); | ||
}); | ||
|
||
}); |
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