-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeOffApp.ts
More file actions
98 lines (87 loc) · 3.54 KB
/
TimeOffApp.ts
File metadata and controls
98 lines (87 loc) · 3.54 KB
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
import {
IAppAccessors,
IConfigurationExtend,
IConfigurationModify,
IEnvironmentRead,
IHttp,
ILogger,
IModify,
IPersistence,
IRead,
} from '@rocket.chat/apps-engine/definition/accessors';
import { App } from '@rocket.chat/apps-engine/definition/App';
import { IAppInfo } from '@rocket.chat/apps-engine/definition/metadata';
import { TimeOffCommand } from './commands/TimeOffCommand';
import { IMessage, IPostMessageSent } from '@rocket.chat/apps-engine/definition/messages';
import { RoomType } from '@rocket.chat/apps-engine/definition/rooms';
import { SettingType } from '@rocket.chat/apps-engine/definition/settings';
import { UserRepository } from './repositories/UserRepository';
import { TimeOffService } from './services/TimeOffService';
import { AppNotifier } from './notifiers/AppNotifier';
import { PostMessageSentHandler } from './handlers/PostMessageSentHandler';
import { TimeOffRepository } from './repositories/TimeOffRepository';
import { TimeOffCache } from './TimeOffCache';
import { UserService } from './services/UserService';
import { APP_SETTINGS, DEFAULT_TIME_OFF_REPLY_COOLDOWN_HOURS } from './helpers/AppSettings';
export class TimeOffApp extends App implements IPostMessageSent {
constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
super(info, logger, accessors);
}
public async extendConfiguration(
configuration: IConfigurationExtend,
_environmentRead: IEnvironmentRead,
): Promise<void> {
configuration.slashCommands.provideSlashCommand(new TimeOffCommand(this));
await configuration.settings.provideSetting({
id: APP_SETTINGS.TIME_OFF_REPLY_COOLDOWN_HOURS,
type: SettingType.NUMBER,
packageValue: DEFAULT_TIME_OFF_REPLY_COOLDOWN_HOURS,
required: false,
public: true,
i18nLabel: 'TimeOff Reply Cooldown (hours)',
i18nDescription: 'Hours to wait before sending another TimeOff message to the same sender.',
});
}
public async onEnable(
_environmentRead: IEnvironmentRead,
_configurationModify: IConfigurationModify,
): Promise<boolean> {
TimeOffCache.getInstance().invalidateCache();
return Promise.resolve(true);
}
public async checkPostMessageSent?(message: IMessage, _read: IRead, _http: IHttp): Promise<boolean> {
// We only want to notify the user if the message was sent in a direct message
return Promise.resolve(message.room.type === RoomType.DIRECT_MESSAGE);
}
public async executePostMessageSent(
message: IMessage,
read: IRead,
_http: IHttp,
persistence: IPersistence,
_modify: IModify,
): Promise<void> {
const userRepository = new UserRepository(read);
const userService = new UserService(userRepository);
const timeOffRepository = new TimeOffRepository(this, read, persistence);
const timeOffService = new TimeOffService(timeOffRepository);
const notifier = new AppNotifier(this, read);
const cooldownHours = await this.getTimeOffReplyCooldownHours(read);
const handler = new PostMessageSentHandler(this, userService, timeOffService, notifier, cooldownHours);
await handler.handle(message);
}
private async getTimeOffReplyCooldownHours(read: IRead): Promise<number> {
try {
const settingValue = await read
.getEnvironmentReader()
.getSettings()
.getValueById(APP_SETTINGS.TIME_OFF_REPLY_COOLDOWN_HOURS);
const parsedSettingValue = Number(settingValue);
if (Number.isFinite(parsedSettingValue) && parsedSettingValue >= 0) {
return parsedSettingValue;
}
} catch (error) {
this.getLogger().error('[TimeOffApp] Error while reading time-off reply cooldown setting:', error);
}
return DEFAULT_TIME_OFF_REPLY_COOLDOWN_HOURS;
}
}