Skip to content

Commit

Permalink
feat: added get all users method to brain
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellepintz committed Apr 30, 2018
1 parent d9a044b commit 308d086
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/botfuel-dialog/src/brains/brain.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ class Brain {
throw new MissingImplementationError();
}

/**
* Gets all users.
* @async
* @returns {Promise.<Object>} the users
*/
async getAllUsers() {
throw new MissingImplementationError();
}

/**
* Gets the init value for creating a new conversation.
* @returns {Object}
Expand Down
8 changes: 7 additions & 1 deletion packages/botfuel-dialog/src/brains/memory-brain.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,18 @@ class MemoryBrain extends Brain {
/** @inheritdoc */
async getUser(userId) {
logger.debug('getUser', userId);
if (!await this.hasUser(userId)) {
if (!(await this.hasUser(userId))) {
throw new Error('User does not exist');
}
return this.users[userId];
}

/** @inheritdoc */
async getAllUsers() {
logger.debug('getAllUsers');
return this.users;
}

/** @inheritdoc */
async userSet(userId, key, value) {
logger.debug('userSet', userId, key, value);
Expand Down
6 changes: 6 additions & 0 deletions packages/botfuel-dialog/src/brains/mongo-brain.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ class MongoBrain extends Brain {
return user;
}

/** @inheritdoc */
async getAllUsers() {
logger.debug('getAllUsers');
return this.users.find().toArray();
}

/**
* Wraps mongodb findOneAndUpdate and throws if user does not exist
* @async
Expand Down
9 changes: 9 additions & 0 deletions packages/botfuel-dialog/tests/brains/brains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ const brainTest = (brainLabel) => {
expect(user._conversations).toHaveLength(1);
});

test('get all users', async () => {
await brain.addUser('d8372804-2716-47aa-81bf-dd0908f9f9f7');
await brain.addUser('8042b7e4-445f-4fa8-891a-d734595ac706');
await brain.addUser('e93428x4-2236-12da-c9jf-le983nxnl2k3');

const users = await brain.getAllUsers();
expect(Object.keys(users)).toHaveLength(3);
});

test('sets user key', async () => {
await brain.addUser(USER_ID);
const user = await brain.userSet(USER_ID, 'name', 'test');
Expand Down

0 comments on commit 308d086

Please sign in to comment.