Skip to content
Open
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
1 change: 1 addition & 0 deletions api/src/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ const getUserList = async (req) => {
const filters = {
facilityId: req.query?.facility_id,
contactId: req.query?.contact_id,
oidcUsername: req.query?.oidc_username,
};
return await users.getList(filters);
};
Expand Down
18 changes: 16 additions & 2 deletions api/tests/mocha/controllers/users.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ describe('Users Controller', () => {

await controller.v2.list(req, res);
chai.expect(users.getList.firstCall.args[0])
.to.deep.equal({ facilityId: 'chw-facility', contactId: undefined });
.to.deep.equal({ facilityId: 'chw-facility', contactId: undefined, oidcUsername: undefined });
const result = res.json.args[0][0];
chai.expect(result.length).to.equal(1);
chai.expect(result[0].id).to.equal(PREFIXES.COUCH_USER + 'chw');
Expand All @@ -291,6 +291,7 @@ describe('Users Controller', () => {
chai.expect(users.getList.firstCall.args[0]).to.deep.equal({
contactId: 'chw-contact',
facilityId: 'chw-facility',
oidcUsername: undefined,
});
const result = res.json.args[0][0];
chai.expect(result.length).to.equal(1);
Expand All @@ -306,13 +307,26 @@ describe('Users Controller', () => {
req.query = { roles: ['chw'], name: 'admin' };

await controller.v2.list(req, res);
chai.expect(users.getList.firstCall.args[0]).to.deep.equal({ facilityId: undefined, contactId: undefined });
chai.expect(users.getList.firstCall.args[0]).to.deep.equal({ facilityId: undefined, contactId: undefined,
oidcUsername: undefined});
const result = res.json.args[0][0];
chai.expect(result.length).to.equal(3);
chai.expect(result[0].id).to.equal(PREFIXES.COUCH_USER + 'admin');
chai.expect(result[1].id).to.equal(PREFIXES.COUCH_USER + 'chw');
chai.expect(result[2].id).to.equal(PREFIXES.COUCH_USER + 'unknown');
});
it('gets the list of users with oidc_username filter', async () => {
sinon.stub(auth, 'check').resolves();
users.getList.resolves([userList[1]]);
req.query = { oidc_username: 'cha@registry.com' };

await controller.v2.list(req, res);
chai.expect(users.getList.firstCall.args[0]).to.deep.equal({
facilityId: undefined,
contactId: undefined,
oidcUsername: 'cha@registry.com',
});
});
});

});
Expand Down
12 changes: 9 additions & 3 deletions shared-libs/user-management/src/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,17 @@ const getUsers = async (facilityId, contactId) => {
return usersForContactId.filter(user => forceArray(user.facility_id).includes(facilityId));
};

const getUsersAndSettings = async ({ facilityId, contactId } = {}) => {
if (!facilityId && !contactId) {
const getUsersAndSettings = async ({ facilityId, contactId, oidcUsername } = {}) => {
if (!facilityId && !contactId && !oidcUsername) {
return Promise.all([getAllUsers(), getAllUserSettings()]);
}
const users = await getUsers(facilityId, contactId);
let users;
if (oidcUsername) {
users = await queryDocs(db.users, 'users/users_by_field', ['oidc_username', oidcUsername]);
} else {
users = await getUsers(facilityId, contactId);
}

const ids = users.map(({ _id }) => _id);
const settings = await getSettingsByIds(ids);
return [users, settings];
Expand Down
47 changes: 47 additions & 0 deletions shared-libs/user-management/test/unit/users.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,53 @@ describe('Users service', () => {
chai.expect(milan.roles).to.deep.equal(['district-admin']);
});

it('with oidc_username', async () => {
const filters = { oidcUsername: 'cha@registry.com' };
const usersResponse = {
rows: [{
doc: {
_id: PREFIXES.COUCH_USER + 'cha',
name: 'cha',
facility_id: 'a',
oidc_username: 'cha@registry.com',
roles: ['chp'],
}
}],
};
db.users.query.withArgs('users/users_by_field', {
include_docs: true,
key: ['oidc_username', filters.oidcUsername],
}).resolves(usersResponse);

const userSettingsResponse = {
rows: [{
doc: {
_id: PREFIXES.COUCH_USER + 'cha',
name: 'cha',
fullname: 'CHA User',
email: 'cha@registry.com',
phone: '123456789',
facility_id: 'a',
},
}],
};
db.medic.allDocs.withArgs({
keys: [PREFIXES.COUCH_USER + 'cha'],
include_docs: true
}).resolves(userSettingsResponse);

const data = await service.getList(filters);
chai.expect(data.length).to.equal(1);
const cha = data[0];
chai.expect(cha.id).to.equal(PREFIXES.COUCH_USER + 'cha');
chai.expect(cha.username).to.equal('cha');
chai.expect(cha.fullname).to.equal('CHA User');
chai.expect(cha.oidc_username).to.equal('cha@registry.com');
chai.expect(cha.place).to.deep.equal([facilityA]);
chai.expect(cha.roles).to.deep.equal(['chp']);
chai.expect(db.users.query.args[0][1].key).to.deep.equal(['oidc_username', 'cha@registry.com']);
});

it('with both facility_id and contact_id', async () => {
const filters = { facilityId: 'b', contactId: 'milan-contact' };
const usersResponse = {
Expand Down