diff --git a/api/src/controllers/users.js b/api/src/controllers/users.js index 1bbf3526301..e9457060087 100644 --- a/api/src/controllers/users.js +++ b/api/src/controllers/users.js @@ -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); }; diff --git a/api/tests/mocha/controllers/users.spec.js b/api/tests/mocha/controllers/users.spec.js index 309f89160a2..0d6bc0d4c9c 100644 --- a/api/tests/mocha/controllers/users.spec.js +++ b/api/tests/mocha/controllers/users.spec.js @@ -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'); @@ -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); @@ -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', + }); + }); }); }); diff --git a/shared-libs/user-management/src/users.js b/shared-libs/user-management/src/users.js index d7d2f413eb5..a72edc2a09d 100644 --- a/shared-libs/user-management/src/users.js +++ b/shared-libs/user-management/src/users.js @@ -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]; diff --git a/shared-libs/user-management/test/unit/users.spec.js b/shared-libs/user-management/test/unit/users.spec.js index c01b33b8b84..f87c88a4387 100644 --- a/shared-libs/user-management/test/unit/users.spec.js +++ b/shared-libs/user-management/test/unit/users.spec.js @@ -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 = {