-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclockify.ts
87 lines (82 loc) · 2.56 KB
/
clockify.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
import { config, cosmicSync } from "@anandchowdhary/cosmic";
import axios from "axios";
import dayjs from "dayjs";
import { join } from "path";
import { integrationConfig, write } from "../common";
import type { Integration } from "../integration";
cosmicSync("stethoscope");
const apiKey = config("clockifyApiKey");
const workspaceId = config("clockifyWorkspaceId");
const userId = config("clockifyUserId");
type ClockifyResult = {
description?: string;
id?: string;
userId?: string;
projectId?: string;
workspaceId?: string;
timeInterval: {
start: string;
end: string;
duration: string;
};
}[];
const getTimeData = async (date: Date) => {
if (integrationConfig("clockify", "entries")) {
const {
data,
}: {
data: ClockifyResult;
} = await axios.get(
`https://api.clockify.me/api/v1/workspaces/${workspaceId}/user/${userId}/time-entries?start=${encodeURIComponent(
dayjs(date).toISOString()
)}&end=${encodeURIComponent(dayjs(date).toISOString())}`,
{
headers: { "X-Api-Key": apiKey },
}
);
const items = data.map((item) => {
delete item.description;
delete item.userId;
delete item.projectId;
delete item.workspaceId;
delete item.id;
return item;
});
const itemsByDate: { [index: string]: ClockifyResult } = {};
for await (const item of items) {
const date = dayjs(item.timeInterval.start);
const year = date.format("YYYY");
const month = date.format("MM");
const day = date.format("DD");
itemsByDate[`${year}/${month}/${day}`] = itemsByDate[`${year}/${month}/${day}`] || [];
itemsByDate[`${year}/${month}/${day}`].push(item);
}
for await (const key of Object.keys(itemsByDate)) {
await write(
join(".", "data", "clockify-time-tracking", "daily", key, "time-entries.json"),
JSON.stringify(itemsByDate[key], null, 2)
);
}
console.log("Clockify: Added time tracking data");
}
};
const getUserId = async () => {
const { data } = await axios.get(`https://api.clockify.me/api/v1/user`, {
headers: { "X-Api-Key": apiKey },
});
console.log("User ID", data.id);
};
export default class Clockify implements Integration {
name = "clockify";
cli = { getUserId };
async update() {
console.log("Clockify: Starting...");
for await (const day of [0, 1, 2, 3, 4]) {
await getTimeData(dayjs().subtract(day, "day").toDate());
console.log("Clockify: Added data");
}
console.log("Clockify: Added daily summaries");
}
async summary() {}
async legacy() {}
}