-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
132 lines (123 loc) · 5.81 KB
/
main.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import chalk from 'chalk';
import YAML from 'yaml';
import fs from 'fs';
import { Config, NavigationOption, TimeEntry } from './model';
import { enterTimeEntryID, getTimeEntry, navigation, selectFromList } from './prompts';
import { createSummary, getMoneybirdFormattedDateTime } from './helpers';
import { postTimeEntry } from './moneybird';
import axios, { AxiosError } from 'axios';
const main = async () => {
const file = fs.readFileSync('./config.yaml', 'utf8');
const config: Config = YAML.parse(file);
console.log(chalk.bgBlue.white.bold(' \n moneybird-time-cli \n \n'));
let verified = false;
let lastEndDate: string = '';
let entries: TimeEntry[] = [];
while (!verified) {
if (config.summary.menu && entries.length) {
console.log(createSummary(entries, config.presets));
}
const navigationOption: NavigationOption = await navigation(entries.length > 0);
switch (navigationOption) {
case 'Add from preset':
const addEntry = await getTimeEntry(config.defaults, config.presets, config.rememberLastDate && !!lastEndDate ? {start: lastEndDate} : {});
lastEndDate = addEntry.end;
entries = [
...entries,
addEntry,
];
break;
case 'Add from preset group':
const group = await selectFromList(Object.keys(config.presetGroups), 'group');
const groupConfigValue = Object.entries(config.presetGroups).find(([key, _]) => key === group)?.[1];
if (groupConfigValue) {
for await (const groupValuePreset of groupConfigValue) {
const presetsWithPresetOverrides = {
...config.presets,
...(typeof groupValuePreset !== 'string' && groupValuePreset.preset ? {
[groupValuePreset.preset]: {...config.presets[groupValuePreset.preset], defaults: groupValuePreset}
} : {}),
};
const value = await getTimeEntry(
config.defaults,
presetsWithPresetOverrides,
{
...(typeof groupValuePreset === 'string'
? {preset: groupValuePreset}
: {}
),
...(config.rememberLastDate && !!lastEndDate ? {start: lastEndDate} : {}),
},
);
lastEndDate = value.end;
entries = [
...entries,
value,
];
}
}
break;
case 'Edit':
if (config.summary.edit) {
console.log(createSummary(entries, config.presets));
}
const editEntryID = await enterTimeEntryID();
const editedEntry = await getTimeEntry(
config.defaults,
config.presets,
entries.find((_, index) => index === editEntryID - 1)
) as TimeEntry;
entries = entries.map((entry, index) => index !== editEntryID - 1 ? entry : editedEntry);
break;
case 'Remove':
if (config.summary.remove) {
console.log(createSummary(entries, config.presets));
}
const removeEntryID = await enterTimeEntryID();
if (removeEntryID) {
entries = entries.filter((_, index) => index !== removeEntryID - 1);
}
break;
case 'Summary':
console.log(createSummary(entries, config.presets));
break;
case 'Send':
if (config.summary.send) {
console.log(createSummary(entries, config.presets));
}
verified = true;
break;
}
}
console.log('Sending the time entries to Moneybird...');
entries.forEach(async (entry, index) => {
const presetConfig = Object.entries(config.presets).find(([key, _]) => key === entry.preset);
if (presetConfig) {
const presetConfigValues = presetConfig[1];
try {
await postTimeEntry(
{
user_id: config.moneybird.userId,
started_at: getMoneybirdFormattedDateTime(entry.start),
ended_at: getMoneybirdFormattedDateTime(entry.end),
description: presetConfigValues.description,
contact_id: presetConfigValues.contact,
project_id: presetConfigValues.project,
billable: presetConfigValues.billable,
paused_duration: entry.pausedDuration*60,
},
config.moneybird.administrationId,
config.moneybird.token,
);
} catch(error: unknown | Error | AxiosError) {
if (axios.isAxiosError(error)) {
console.error(`Could not send entry ${index+1} to Moneybird.`, error.response?.statusText);
} else {
console.error(`Could not send entry ${index+1} to Moneybird. Unknown error.`);
}
}
}
});
console.log('Time entries sent.');
};
main();