-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
243 lines (210 loc) · 7.68 KB
/
index.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const qs = require("querystring");
const fetch = require("node-fetch");
const moment = require("moment");
const fs = require("fs");
const {promisify} = require('util');
const path = require("path");
const API_PASSWORD = "api_token";
const ENDPOINT = "https://api.track.toggl.com/reports/api/v2/details";
const USER_AGENT = "[email protected]";
const HEADERS = ["Description","Date","Hours","Order","Opportunity","Non Chargeable"]
let API_KEY = null;
let WORKSPACE = null;
let USERNAME = null;
let DEFAULT_LOC = null;
let HALT_ON_MISSING = false;
/** @type {({Project?:(string|RegExp),Client?:(string|RegExp),Ignore?:boolean,Order?:string,Opportunity?:string})[]} */
let PROJECT_CONFIGS = null;
async function LoadConfig(){
const rawConfig = await promisify(fs.readFile)(path.join(__dirname,"config.json"));
const config = JSON.parse(rawConfig);
API_KEY = config.Toggl.ApiKey;
WORKSPACE = config.Toggl.Workspace;
USERNAME = config.Crm.Username;
DEFAULT_LOC = config.Output.DefaultLocation;
if ("HaltOnMissing" in config.Settings){
HALT_ON_MISSING = config.Settings.HaltOnMissing;
}
PROJECT_CONFIGS = config.Projects.map((input,i) => {
var copy = {...input};
if ("ProjectRegex" in copy){
copy.Project = new RegExp(input.ProjectRegex);
delete copy.ProjectRegex;
}
if ("ClientRegex" in copy){
copy.Client = new RegExp(input.ClientRegex);
delete copy.ClientRegex;
}
if(!("Order" in copy) && !("Opportunity" in copy) && !(copy.Ignore === true)){
throw `Bad configuration, Rule {i} lacks both Order and Opportunity, and Ignore is not true`;
}
return copy;
});
}
/**
* Checks if a test is true, or missing
* @param {string|RegExp} test The test to run
* @param {string} against The string to test against
*/
function CheckTest(test,against){
return !test || (test.test && test.test(against)) || test === against;
}
/**
* Looks up the given project & client in the config
* If multiple match the first is returned
* @param {string} project
* @param {string} client
* @returns {{ignore:boolean,order:string,opportunity:string}} The details for the project and client
*/
function getDetailsForProject(project,client){
for (const config of PROJECT_CONFIGS){
if ( CheckTest(config.Project,project) && CheckTest(config.Client,client) ){
return {
ignore:config.Ignore || false,
order:config.Order||"",
opportunity:config.Opportunity||"",
}
}
}
}
async function getAllPages(start,end){
let result = {data:[null]}
const allResults = [];
for(let page=1; result.data.length != 0; page++){
const query = ENDPOINT + "?" + qs.stringify({
user_agent:USER_AGENT,
workspace_id:WORKSPACE,
since:start,
until:end,
page
});
const headers = {
Authorization:"Basic "+Buffer.from(API_KEY+":"+API_PASSWORD).toString("base64")
}
const fetchResult = await fetch(query,{headers});
result = await fetchResult.json();
if(result.error){
throw result.error;
}
allResults.push(...result.data);
}
return allResults;
}
async function getFromToggle({since,until}){
//let start = moment().add(-weeksAgo,"weeks").startOf('isoWeek').format("YYYY-MM-DD")
//let end = moment().add(-weeksAgo,"weeks").endOf('isoWeek').format("YYYY-MM-DD")
let start = since? since: moment().startOf("isoWeek").format("YYYY-MM-DD");
let end = until? until: moment().format("YYYY-MM-DD");
console.log(`From ${start} until ${end} (both inclusive)`);
let result = await getAllPages(start,end);
let output = [HEADERS];
for (let r of result){
let start = new Date(r.start);
let end = new Date(r.end);
let durationMin = Math.round((end-start)/(1000*60));
let order;
try {
let config = getDetailsForProject(r.project,r.client);
if (config === undefined){
throw new Error(`Missing config for: ${r.project}, ${r.client}`);
}
if (config.ignore){
continue;
}
order = config.order;
opportunity = config.opportunity
} catch (e){
console.error(`Cannot match for project:client for "${r.project}":"${r.client}", used by time slot at "${start}". Add to config to process`);
if(HALT_ON_MISSING){
console.error("Project is set to halt on missing config. Aborting.")
throw e;
}
continue;
}
let description = r.description;
if(description == ""){
description = r.project? `(Undescribed ${r.project} work)`: "(Undescribed work)";
}
//Field order: Description, Date, Hours, Order, Opportunity, Non Chargeable
output.push([
description,
start.toISOString().substr(0,10),
(durationMin/60).toFixed(2),
order,
opportunity, //Opportunity
false?"Yes":"No", //non-chargeable
]);
}
return output;
}
function help(){
console.log(
`Toggle -> CRM formatted
--help:
show this text and exit
--since/--until format:yyyy-mm-dd
sets the dates to run between, both inclusive
defaults: start of the week; today.
--today:
sets --since and --until to today
--yesterday:
sets --since and --until to yesterday
--outfile:
sets where to output
if blank: "${DEFAULT_LOC}" is used
if omitted: prints to stdout only
`)
}
async function run(){
var ops = {};
await LoadConfig();
for(let i=2;i<process.argv.length;i++){
let input = process.argv[i];
switch (input){
case "--help":
return help();
case "--today":
ops.since = moment().format("YYYY-MM-DD");
ops.until = ops.since;
break;
case "--yesterday":
ops.since = moment().add(-1,"day").format("YYYY-MM-DD");
ops.until = ops.since;
break;
case "--days-ago":
let arg = process.argv[++i].split("-",2);
ops.since = moment().subtract(arg[0],"days").format("YYYY-MM-DD");
ops.until = arg.length == 2? moment().subtract(arg[1],"days").format("YYYY-MM-DD"):ops.since;
break;
case "--since":
case "--until":
case "--outfile":
if (process.argv.length != i+1 && !process.argv[i+1].startsWith("-")){
ops[input.substr(2)] = process.argv[++i];
} else {
console.log(`using defualt out file ${DEFAULT_LOC}`)
ops[input.substr(2)] = DEFAULT_LOC;
}
break;
default:
console.error(`unknown input ${input}`);
}
}
let result = await getFromToggle(ops);
const out = ops.outfile? fs.createWriteStream(ops.outfile) : process.stdout;
const writeOut = promisify(out.write.bind(out))
let totalHours = 0;
let isHeader = true;
for(row of result){
await writeOut(row.map(n=>typeof(n) == "string"? `"${n.replace(/"/g,'""')}"`:n).join(",")+"\n");
if(!isHeader){
totalHours += (row[2])*1
} else {
isHeader = false;
}
}
console.log(`${result.length-1} row(s) produced`);
console.log(`Total hours worked: ${totalHours.toFixed(2)} apx`)
process.exit();
}
run().then(console.log,console.error)