Skip to content

Commit

Permalink
fix: Make userId reliant functions throw when user does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
windkomo committed Feb 22, 2018
1 parent c00cb16 commit 9fbc4a3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
7 changes: 5 additions & 2 deletions packages/botfuel-dialog/src/brains/memory-brain.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,18 @@ class MemoryBrain extends Brain {
/** @inheritdoc */
async getLastConversation(userId) {
logger.debug('getLastConversation', userId);
const conversation = last(this.users[userId].conversations);
const user = await this.getUser(userId);
const conversation = last(user.conversations);
return this.isConversationValid(conversation) ? conversation : this.addConversation(userId);
}

/** @inheritdoc */
async addConversation(userId) {
logger.debug('addConversation', userId);
const user = await this.getUser(userId);
const conversation = this.getConversationInitValue();
this.users[userId].conversations.push(conversation);

user.conversations.push(conversation);
return conversation;
}

Expand Down
40 changes: 34 additions & 6 deletions packages/botfuel-dialog/src/brains/mongo-brain.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,50 +80,78 @@ class MongoBrain extends Brain {
}

/** @inheritdoc */
async getUser(userId) {
async getUser(userId, ...params) {
logger.debug('getUser', userId);
return this.users.findOne({ userId });

const user = await this.users.findOne({ userId }, ...params);

if (!user) {
throw new Error('User does not exist');
}

return user;
}

/**
* Wraps mongodb findOneAndUpdate and throws if user does not exist
* @async
* @abstract
* @param {String} userId - user id
* @returns {Promise.<Object>} the user
*/
async findUserAndUpdate(...params) {
const user = await this.users.findOneAndUpdate(...params);

if (!user) {
throw new Error('User does not exist');
}

return user;
}

/** @inheritdoc */
async userSet(userId, key, value) {
logger.debug('userSet', userId, key, value);
const result = await this.users.findOneAndUpdate(
const result = await this.findUserAndUpdate(
{ userId },
{ $set: { [key]: value } },
{ returnOriginal: false },
);

return result.value;
}

/** @inheritdoc */
async getLastConversation(userId) {
logger.debug('getLastConversation', userId);
const user = await this.users.findOne({ userId }, { conversations: { $slice: 1 } });
const user = await this.getUser(userId, { conversations: { $slice: 1 } });

const conversation = user.conversations[0];
return this.isConversationValid(conversation) ? conversation : this.addConversation(userId);
}

/** @inheritdoc */
async addConversation(userId) {
logger.debug('addConversation', userId);
const result = await this.users.findOneAndUpdate(
const result = await this.findUserAndUpdate(
{ userId },
{ $push: { conversations: { $each: [this.getConversationInitValue()], $position: 0 } } },
{ returnOriginal: false },
);

return result.value.conversations[0];
}

/** @inheritdoc */
async conversationSet(userId, key, value) {
logger.debug('conversationSet', userId, key, value);
const lastConversation = await this.getLastConversation(userId);
const result = await this.users.findOneAndUpdate(
const result = await this.findUserAndUpdate(
{ userId, 'conversations.createdAt': lastConversation.createdAt },
{ $set: { [`conversations.0.${key}`]: value } },
{ returnOriginal: false, sort: { 'conversations.createdAt': -1 } },
);

return result.value.conversations[0];
}

Expand Down

0 comments on commit 9fbc4a3

Please sign in to comment.