-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBambooHR.ts
110 lines (85 loc) · 3.28 KB
/
BambooHR.ts
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
import { IHttp, ILogger } from '@rocket.chat/apps-engine/definition/accessors';
import { formatDate, getMonthAndDay } from './utils';
interface IWhosOutParams {
today: Date;
http: IHttp;
bambooSubdomain: string;
bambooToken: string;
logger: ILogger;
}
interface ITimeOffItem {
result: string;
start: string;
end: string;
}
function getTimeoffItem(item, tag): ITimeOffItem | undefined {
const result = new RegExp(`<${ tag }[^>]+>(.*)<\\/${ tag }>`).exec(item);
const start = /<start>(.*)<\/start>/.exec(item);
const end = /<end>(.*)<\/end>/.exec(item);
if (!result) {
return;
}
if (!start) {
return;
}
if (!end) {
return;
}
return { result: result[1], start: start[1], end: end[1] };
}
function getItems(content, type, tag): Array<ITimeOffItem> {
const regex = new RegExp(`<item type="${ type }"[^\\0]*?<\\/item>`, 'mg');
const items: any = [];
let item;
// tslint:disable-next-line
while ((item = regex.exec(content)) !== null) {
items.push(getTimeoffItem(item[0], tag));
}
return items;
}
export async function getBirthdays({ today, http, bambooSubdomain, bambooToken, logger }: IWhosOutParams): Promise<{ birthdays: Array<string> }> {
const todayString = getMonthAndDay(today);
const report = 110;
const url = `https://api.bamboohr.com/api/gateway.php/${ bambooSubdomain }/v1/reports/${ report }?format=json&fd=yes&employeeStatusFilter=active`;
const options = {
auth: `${ bambooToken }:x`,
};
const result = await http.get(url, options);
// logger.log(result);
const content = JSON.parse(result.content || '');
const birthdays = [];
if (!content.employees) {
return { birthdays };
}
return {
birthdays: content.employees
.filter(({ birthday }) => birthday === todayString)
.map(({ preferredName, firstName, lastName }) => preferredName || `${ firstName } ${ lastName }`),
};
}
export async function getWhosOut({ today, http, bambooSubdomain, bambooToken, logger }: IWhosOutParams): Promise<{ whosout: Array<string>, holidaysToday: Array<string>, holidaysTomorrow: Array<string> }> {
const tomorrow = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1);
const todayString = formatDate(today);
const tomorrowString = formatDate(tomorrow);
const url = `https://api.bamboohr.com/api/gateway.php/${ bambooSubdomain }/v1/time_off/whos_out/?start=${ todayString }&end=${ tomorrowString }`;
const options = {
auth: `${ bambooToken }:x`,
};
const result = await http.get(url, options);
// logger.log(result);
const content = result.content as string;
const whosout = getItems(content, 'timeOff', 'employee')
.filter((timeoff) => timeoff.start <= todayString)
.map((timeoff) => timeoff.result);
const holidaysToday = getItems(content, 'holiday', 'holiday')
.filter((holiday) => holiday.start <= todayString)
.map((timeoff) => timeoff.result);
const holidaysTomorrow = getItems(content, 'holiday', 'holiday')
.filter((holiday) => holiday.end >= tomorrowString)
.map((timeoff) => timeoff.result);
return {
whosout,
holidaysToday,
holidaysTomorrow,
};
}