-
Notifications
You must be signed in to change notification settings - Fork 110
/
timesheets.js
196 lines (172 loc) · 7.1 KB
/
timesheets.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// 入力内容を解析して、メソッドを呼び出す
// Timesheets = loadTimesheets();
loadTimesheets = function (exports) {
var Timesheets = function(storage, settings, responder) {
this.storage = storage;
this.responder = responder;
this.settings = settings;
var self = this;
this.responder.on('receiveMessage', function(username, message) {
self.receiveMessage(username, message);
});
};
// メッセージを受信する
Timesheets.prototype.receiveMessage = function(username, message) {
// 日付は先に処理しておく
this.date = DateUtils.parseDate(message);
this.time = DateUtils.parseTime(message);
this.datetime = DateUtils.normalizeDateTime(this.date, this.time);
if(this.datetime !== null) {
this.dateStr = DateUtils.format("Y/m/d", this.datetime);
this.datetimeStr = DateUtils.format("Y/m/d H:M", this.datetime);
}
// コマンド集
var commands = [
['actionSignOut', /(バ[ー〜ァ]*イ|ば[ー〜ぁ]*い|おやすみ|お[つっ]ー|おつ|さらば|お先|お疲|帰|乙|bye|night|(c|see)\s*(u|you)|退勤|ごきげんよ|グ[ッ]?バイ)/],
['actionWhoIsOff', /(だれ|誰|who\s*is).*(休|やす(ま|み|む))/],
['actionWhoIsIn', /(だれ|誰|who\s*is)/],
['actionCancelOff', /(休|やす(ま|み|む)|休暇).*(キャンセル|消|止|やめ|ません)/],
['actionOff', /(休|やす(ま|み|む)|休暇)/],
['actionSignIn', /(モ[ー〜]+ニン|も[ー〜]+にん|おっは|おは|へろ|はろ|ヘロ|ハロ|hi|hello|morning|出勤)/],
['confirmSignIn', /__confirmSignIn__/],
['confirmSignOut', /__confirmSignOut__/],
];
// メッセージを元にメソッドを探す
var command = _.find(commands, function(ary) {
return(ary && message.match(ary[1]));
});
// メッセージを実行
if(command && this[command[0]]) {
return this[command[0]](username, message);
}
}
// 出勤
Timesheets.prototype.actionSignIn = function(username, message) {
if(this.datetime) {
var data = this.storage.get(username, this.datetime);
if(!data.signIn || data.signIn === '-') {
this.storage.set(username, this.datetime, {signIn: this.datetime});
this.responder.template("出勤", username, this.datetimeStr);
}
else {
// 更新の場合は時間を明示する必要がある
if(!!this.time) {
this.storage.set(username, this.datetime, {signIn: this.datetime});
this.responder.template("出勤更新", username, this.datetimeStr);
}
}
}
};
// 退勤
Timesheets.prototype.actionSignOut = function(username, message) {
if(this.datetime) {
var data = this.storage.get(username, this.datetime);
if(!data.signOut || data.signOut === '-') {
this.storage.set(username, this.datetime, {signOut: this.datetime});
this.responder.template("退勤", username, this.datetimeStr);
}
else {
// 更新の場合は時間を明示する必要がある
if(!!this.time) {
this.storage.set(username, this.datetime, {signOut: this.datetime});
this.responder.template("退勤更新", username, this.datetimeStr);
}
}
}
};
// 休暇申請
Timesheets.prototype.actionOff = function(username, message) {
if(this.date) {
var dateObj = new Date(this.date[0], this.date[1]-1, this.date[2]);
var data = this.storage.get(username, dateObj);
if(!data.signOut || data.signOut === '-') {
this.storage.set(username, dateObj, {signIn: '-', signOut: '-', note: message});
this.responder.template("休暇", username, DateUtils.format("Y/m/d", dateObj));
}
}
};
// 休暇取消
Timesheets.prototype.actionCancelOff = function(username, message) {
if(this.date) {
var dateObj = new Date(this.date[0], this.date[1]-1, this.date[2]);
var data = this.storage.get(username, dateObj);
if(!data.signOut || data.signOut === '-') {
this.storage.set(username, dateObj, {signIn: null, signOut: null, note: message});
this.responder.template("休暇取消", username, DateUtils.format("Y/m/d", dateObj));
}
}
};
// 出勤中
Timesheets.prototype.actionWhoIsIn = function(username, message) {
var dateObj = DateUtils.toDate(DateUtils.now());
var result = _.compact(_.map(this.storage.getByDate(dateObj), function(row) {
return _.isDate(row.signIn) && !_.isDate(row.signOut) ? row.user : undefined;
}));
if(_.isEmpty(result)) {
this.responder.template("出勤なし");
}
else {
this.responder.template("出勤中", result.sort().join(', '));
}
};
// 休暇中
Timesheets.prototype.actionWhoIsOff = function(username, message) {
var dateObj = DateUtils.toDate(DateUtils.now());
var dateStr = DateUtils.format("Y/m/d", dateObj);
var result = _.compact(_.map(this.storage.getByDate(dateObj), function(row){
return row.signIn === '-' ? row.user : undefined;
}));
// 定休の処理
var wday = dateObj.getDay();
var self = this;
_.each(this.storage.getUsers(), function(username) {
if(_.contains(self.storage.getDayOff(username), wday)) {
result.push(username);
}
});
result = _.uniq(result);
if(_.isEmpty(result)) {
this.responder.template("休暇なし", dateStr);
}
else {
this.responder.template("休暇中", dateStr, result.sort().join(', '));
}
};
// 出勤していない人にメッセージを送る
Timesheets.prototype.confirmSignIn = function(username, message) {
var self = this;
var holidays = _.compact(_.map((this.settings.get("休日") || "").split(','), function(s) {
var date = DateUtils.parseDateTime(s);
return date ? DateUtils.format("Y/m/d", date) : undefined;
}));
var today = DateUtils.toDate(DateUtils.now());
// 休日ならチェックしない
if(_.contains(holidays, DateUtils.format("Y/m/d",today))) return;
var wday = DateUtils.now().getDay();
var signedInUsers = _.compact(_.map(this.storage.getByDate(today), function(row) {
var signedIn = _.isDate(row.signIn);
var off = (row.signIn === '-') || _.contains(self.storage.getDayOff(row.user), wday);
return (signedIn || off) ? row.user : undefined;
}));
var users = _.difference(this.storage.getUsers(), signedInUsers);
if(!_.isEmpty(users)) {
this.responder.template("出勤確認", users.sort());
}
// バージョンチェックを行う
if(typeof checkUpdate == 'function') checkUpdate(this.responder);
};
// 退勤していない人にメッセージを送る
Timesheets.prototype.confirmSignOut = function(username, message) {
var dateObj = DateUtils.toDate(DateUtils.now());
var users = _.compact(_.map(this.storage.getByDate(dateObj), function(row) {
return _.isDate(row.signIn) && !_.isDate(row.signOut) ? row.user : undefined;
}));
if(!_.isEmpty(users)) {
this.responder.template("退勤確認", users.sort());
}
};
return Timesheets;
};
if(typeof exports !== 'undefined') {
exports.Timesheets = loadTimesheets();
}