Skip to content

Commit b4df84c

Browse files
author
Mohammed Shaffi
committed
[Shaffi] Refactored stores and few other code cleanups
1 parent 44674b0 commit b4df84c

File tree

10 files changed

+122
-125
lines changed

10 files changed

+122
-125
lines changed

src/index.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ const pairingStore = new PairingStore();
3131
const notificationStore = new NotificationStore();
3232
const pairingService = new PairingService(peopleStore, pairingStore);
3333
const notificationService = new NotificationService(notificationStore, pairingService, sendMessage);
34-
const commonUtils = new CommonUtils();
3534

3635
controller.on('rtm_open',function(bot) {
3736
console.log('** The RTM api just connected! **');
@@ -118,28 +117,32 @@ controller.hears([/remove member ([a-zA-Z]*)/i], 'direct_message,direct_mention'
118117
});
119118

120119
controller.hears([/add solo ([a-zA-Z]*)/i], 'direct_message,direct_mention', function (bot, message) {
121-
let pairName = message.match[1];
122-
if(pairingService.addPair([pairName])) {
123-
bot.reply(message, `Oh! we have a lone wolf today!\nHave updated ${pairName} to the stats :thumbsup:`);
124-
return;
120+
if(isTeamConfigured(bot, message)) {
121+
let pairName = message.match[1];
122+
if(pairingService.addPair([pairName])) {
123+
bot.reply(message, `Oh! we have a lone wolf today!\nHave updated ${pairName} to the stats :thumbsup:`);
124+
return;
125+
}
126+
bot.reply(message, `Unable to identify \`${pairName}\` in the team :white_frowning_face:`);
125127
}
126-
bot.reply(message, `Unable to identify ${pairName} in the team :white_frowning_face:`);
127128
});
128129

129130
controller.hears([/add pair ([a-zA-Z]*)(?:,\s?)([a-zA-Z]*)/i], 'direct_message,direct_mention', function (bot, message) {
130-
let pair = [message.match[1], message.match[2]];
131-
if(pairingService.addPair(pair)) {
132-
bot.reply(message, `Added ${pair[0]} and ${pair[1]} to today's stats :thumbsup:`);
133-
return;
131+
if(isTeamConfigured(bot, message)){
132+
let pair = [message.match[1], message.match[2]];
133+
if(pairingService.addPair(pair)) {
134+
bot.reply(message, `Added ${pair[0]} and ${pair[1]} to today's stats :thumbsup:`);
135+
return;
136+
}
137+
bot.reply(message, `Unable to identify \`${pair.join(' / ')}\` in the team :white_frowning_face:`);
134138
}
135-
bot.reply(message, `Unable to identify ${pair.toString()} in the team :white_frowning_face:`);
136139
});
137140

138141
controller.hears(['uptime', 'identify yourself', 'who are you', 'what is your name'],
139142
'direct_message,direct_mention', function (bot, message) {
140143

141144
const hostname = os.hostname();
142-
const uptime = commonUtils.formatTime(process.uptime());
145+
const uptime = CommonUtils.formatTime(process.uptime());
143146

144147
bot.reply(message,
145148
':robot_face: I am a bot named <@' + bot.identity.name +
@@ -188,7 +191,9 @@ controller.hears([/^pairing stats/i], 'direct_message,direct_mention', function
188191
});
189192

190193
controller.hears([/^missing stats/i], 'direct_message,direct_mention', function (bot, message) {
191-
bot.reply(message, pairingService.getMissingStatsMessage());
194+
if (isTeamConfigured(bot, message)) {
195+
bot.reply(message, pairingService.getMissingStatsMessage());
196+
}
192197
});
193198

194199
controller.hears([/^(bye|see you later|tata|ciao|adieu)/i], ['direct_message,direct_mention'], function (bot, message) {
@@ -248,8 +253,9 @@ const isTeamConfigured = (bot, message) => {
248253

249254
if(peopleStore.getMemberList().length !== peopleStore.getExpectedMemberCount()) {
250255
bot.reply(message, `(*${peopleStore.getMemberList().length}/${peopleStore.getExpectedMemberCount()}*)`
251-
+ ' member added. Please complete the list to start tracking.'
256+
+ ' members added. Please complete the list to start tracking.'
252257
);
258+
return false;
253259
}
254260

255261
return true;

src/models/pair.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
class Pair {
22
constructor(pairString) {
3-
this.pair = pairString.split(',');
3+
this._pair = pairString.split(',');
44
}
55

66
isSolo() {
7-
return this.pair.length === 1;
7+
return this._pair.length === 1;
88
}
99

1010
toString() {
11-
return this.pair.join(',');
11+
return this._pair.join(',');
1212
}
1313

1414
contains(name) {
15-
return this.pair.includes(name);
15+
return this._pair.includes(name);
1616
}
1717

1818
getOtherPairOf(name) {
1919
if(this.isSolo())
2020
return null;
2121

22-
return this.pair.find(pairName => pairName !== name);
22+
return this._pair.find(pairName => pairName !== name);
2323
}
2424

2525
getPair() {
26-
return this.pair;
26+
return this._pair;
2727
}
2828
}
2929

src/models/pairStat.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
class PairStat {
22
constructor(pair, pairInfo) {
3-
this.pair = pair;
4-
this.pairInfo = pairInfo;
3+
this._pair = pair;
4+
this._pairInfo = pairInfo;
55
}
66

77
getPair() {
8-
return this.pair;
8+
return this._pair;
99
}
1010

1111
getPairInfo() {
12-
return this.pairInfo;
12+
return this._pairInfo;
1313
}
1414
}
1515

src/service/notificationService.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class NotificationService {
5252

5353
_sendNotification(name, channel) {
5454
const notificationMessage = this._notificationsConfig.get(name);
55-
this._notificationCallback({channel, text: `@here Your notification for \`${name}\`.`});
55+
this._notificationCallback({channel, text: `<!channel> Your notification for \`${name}\`.`});
5656
this._notificationCallback(Object.assign({channel}, notificationMessage()));
5757
}
5858

src/service/pairingService.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ const randomColor = require('randomcolor');
33
class PairingService {
44

55
constructor(peopleStore, pairingStore) {
6-
this.peopleStore = peopleStore;
7-
this.pairingStore = pairingStore;
6+
this._peopleStore = peopleStore;
7+
this._pairingStore = pairingStore;
88
}
99

1010
processCommitFrom(message) {
@@ -27,7 +27,7 @@ class PairingService {
2727
});
2828

2929
if (matchFound) {
30-
this.pairingStore.saveCurrentStatToJsonStore();
30+
this._pairingStore.saveCurrentStatToJsonStore();
3131
}
3232
}
3333

@@ -37,7 +37,7 @@ class PairingService {
3737
if(!this._updatePairInfoFor(pair))
3838
return false;
3939

40-
this.pairingStore.saveCurrentStatToJsonStore();
40+
this._pairingStore.saveCurrentStatToJsonStore();
4141
return true;
4242
}
4343

@@ -65,12 +65,12 @@ class PairingService {
6565
}
6666

6767
_getPairingStats() {
68-
return this.pairingStore.getPairingStats();
68+
return this._pairingStore.getPairingStats();
6969
}
7070

7171
_getPairingStatsAsSlackFormattedMessage() {
7272
let pairStats = this._getPairingStats();
73-
let message = this.peopleStore.getMemberList().map(member => {
73+
let message = this._peopleStore.getMemberList().map(member => {
7474
let mostRecentUpdatedAt = 0;
7575

7676
let fields = pairStats.filter(pairStat => pairStat.getPair().contains(member))
@@ -108,11 +108,11 @@ class PairingService {
108108
}
109109

110110
_getMembersWithoutStatsUpdatedToday(){
111-
const updatedMembers = this.pairingStore.getPairsWithStatsUpdatedToday()
111+
const updatedMembers = this._pairingStore.getPairsWithStatsUpdatedToday()
112112
.map(pair => pair.getPair())
113113
.reduce((prev, current) => prev.concat(current), []);
114114
const membersWithUpdatedStats = new Set(updatedMembers);
115-
const membersWithoutUpdatedStats = this.peopleStore.getMemberList().filter(member => !membersWithUpdatedStats.has(member));
115+
const membersWithoutUpdatedStats = this._peopleStore.getMemberList().filter(member => !membersWithUpdatedStats.has(member));
116116
return membersWithoutUpdatedStats;
117117
}
118118

@@ -125,7 +125,7 @@ class PairingService {
125125
}
126126

127127
let pairInfo = this._findPairInfo(pairMatch);
128-
this.pairingStore.updatePairInfo(pairMatch, pairInfo);
128+
this._pairingStore.updatePairInfo(pairMatch, pairInfo);
129129
return true;
130130
}
131131

@@ -145,14 +145,14 @@ class PairingService {
145145
}
146146

147147
_findPairInfo(pairMatch) {
148-
let pairInfo = (pairMatch.length > 1)? this.pairingStore.getPairInfo(pairMatch.toString())
149-
|| this.pairingStore.getPairInfo(`${pairMatch[1]},${pairMatch[0]}`)
150-
: this.pairingStore.getPairInfo(pairMatch.toString());
148+
let pairInfo = (pairMatch.length > 1)? this._pairingStore.getPairInfo(pairMatch.toString())
149+
|| this._pairingStore.getPairInfo(`${pairMatch[1]},${pairMatch[0]}`)
150+
: this._pairingStore.getPairInfo(pairMatch.toString());
151151
return pairInfo;
152152
}
153153

154154
_findNearestMatch(pair) {
155-
return pair.map(name => this.peopleStore.getMembersFuzzyMatch(name).value);
155+
return pair.map(name => this._peopleStore.getMembersFuzzyMatch(name).value);
156156
}
157157
}
158158

src/store/jsonFileStore.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@ var Store = require("jfs");
22

33
class JsonFileStore {
44
constructor() {
5-
this.db = new Store("data");
5+
this._db = new Store("data");
66
}
77

88
getData(storeName, callback) {
9-
this.db.get(storeName, function (err, data) {
10-
callback(data, err);
9+
this._db.get(storeName, function (err, data) {
10+
if (err) {
11+
console.log('Store retrieve failed');
12+
callback(null);
13+
return;
14+
}
15+
16+
callback(data);
1117
});
1218
}
1319

1420
getSyncData(storeName){
15-
let data = this.db.getSync(storeName);
21+
let data = this._db.getSync(storeName);
1622

1723
if(data.message) {
1824
console.log('Store retrieve failed');
@@ -22,9 +28,11 @@ class JsonFileStore {
2228
return data;
2329
}
2430

25-
save(storeName, data, callback) {
26-
this.db.save(storeName, data, function (err) {
27-
callback(err);
31+
save(storeName, data) {
32+
this._db.save(storeName, data, function (err) {
33+
if (err) {
34+
console.log('Store update failed');
35+
}
2836
});
2937
}
3038
}

src/store/notificationStore.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const JsonFileStore = require('./jsonFileStore');
22

3-
class NotificationStore {
3+
class NotificationStore extends JsonFileStore {
44

55
constructor() {
6+
super();
67
this._fileStoreName = 'notificationStore';
7-
this._jsonStore = new JsonFileStore();
88
this._notifications = [];
99
this._initializeNotificationStore();
1010
}
@@ -26,15 +26,11 @@ class NotificationStore {
2626
}
2727

2828
_saveChannelToJsonStore() {
29-
this._jsonStore.save(this._fileStoreName, this._notifications, (err) => {
30-
if (err) {
31-
console.log('Store update failed');
32-
}
33-
})
29+
this.save(this._fileStoreName, this._notifications);
3430
}
3531

3632
_initializeNotificationStore() {
37-
const data = this._jsonStore.getSyncData(this._fileStoreName);
33+
const data = this.getSyncData(this._fileStoreName);
3834
this._notifications = (data)? data : [];
3935
}
4036
}

0 commit comments

Comments
 (0)