forked from MissionMarsFourthHorizon/operation-max
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.js
62 lines (50 loc) · 1.65 KB
/
provider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* jshint esversion: 6 */
// Conversation state enumeration
const ConversationState = {
ConnectedToBot: 0,
WaitingForAgent: 1,
ConnectedToAgent: 2
};
function Provider () {
'use strict';
const data = [];
// return all conversations
const currentConversations = () => {
return data;
};
// creates a new conversation
const createConversation = (address) => {
const conversation = {
timestamp: new Date().getTime(),
user: address,
state: ConversationState.ConnectedToBot
};
data.push(conversation);
return conversation;
};
// find a conversation by its user conversation id
const findByConversationId = (id) => {
return data.find((conversation) => conversation.user.conversation.id === id);
};
// find a conversation by its agent conversation id
const findByAgentId = (id) => {
return data.find((conversation) => conversation.agent && conversation.agent.conversation.id === id);
};
// find a conversation by its agent conversation id
const peekConversation = (agent) => {
var conversation = data.sort((a,b) => a.timestamp < b.timestamp).find((conversation) => conversation.state === ConversationState.WaitingForAgent);
if (conversation) {
conversation.state = ConversationState.ConnectedToAgent;
conversation.agent = agent;
}
return conversation;
};
return {
createConversation,
currentConversations,
findByAgentId,
findByConversationId,
peekConversation
};
}
module.exports = { Provider, ConversationState };