-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.go
More file actions
314 lines (283 loc) · 6.47 KB
/
log.go
File metadata and controls
314 lines (283 loc) · 6.47 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//contains all functions and structs to store and retreive log entry data
package main
import (
"fmt"
"time"
)
//DisplayLog is the struct to hold a log entry with all info necessary to be
//displayed on the website
type DisplayLog struct {
FromDate *time.Time `db:"begin" json:"-"`
ToDate *time.Time `db:"end" json:"-"`
DateFrom string
TimeFrom string
DateTo string
TimeTo string
Type string
EntryID string `db:"entry_id"`
Duration string
Active bool `db:"active"`
}
var dbDateTime = "2006-01-02 15:04:05"
var jsFormatString = "02.01.2006 15:04"
var jsDateFormatString = "02.01.2006"
//GetEntry fetches the log entry for a given id
func GetEntry(entryID string) (DisplayLog, error) {
Info.Printf("GetEntry(%s)\n", entryID)
var entry DisplayLog
err := db.Get(&entry,
`select
ed.begin as begin,
ed.end as end,
ed.type as type,
e.entry_id,
e.active
from
entry e,
entry_data ed
where
e.entry_data=ed.id and
e.entry_id=$1
order by
ed.begin desc`,
entryID)
if err != nil {
return DisplayLog{}, err
}
return prepareDateTime(entry, false), nil
}
//StoreEntry stores a time entry into the database
func StoreEntry(userID string, begin, end time.Time, entryType, createType, entryID, impostorID string, active, shouldRound bool) error {
Info.Printf("StoreEntry(%s,%v,%v,%s,%s,%s,%s,%t)\n",
userID, begin.Format(jsDateTime), end.Format(jsDateTime), entryType, createType, entryID, impostorID, active)
var preparedBegin, preparedEnd string
var err error
now := time.Now()
if shouldRound {
now = round(time.Now())
}
if active && createType == createConst {
preparedBegin = now.Format(dbDateTime)
} else if active && createType == updateType {
if shouldRound {
begin = round(begin)
}
preparedBegin = begin.Format(dbDateTime)
preparedEnd = now.Format(dbDateTime)
} else {
if shouldRound {
begin = round(begin)
}
preparedBegin = begin.Format(dbDateTime)
if shouldRound {
end = round(end)
}
preparedEnd = end.Format(dbDateTime)
}
tx, err := db.Beginx()
if err != nil {
return err
}
var insertedID int64
if preparedEnd == "" {
err = tx.QueryRowx(`insert into entry_data
(created,"begin",type,create_type,by) values
($1,$2,$3,$4,$5) returning id`,
now.Format(dbDateTime), preparedBegin, entryType, createType, impostorID,
).Scan(&insertedID)
if err != nil {
fmt.Println(err)
tx.Rollback()
return err
}
} else {
err = tx.QueryRowx(
`insert into entry_data
(created,"begin","end",type,create_type,by) values
($1,$2,$3,$4,$5,$6) returning id`,
now.Format(dbDateTime), preparedBegin, preparedEnd, entryType, createType, impostorID,
).Scan(&insertedID)
if err != nil {
fmt.Println(err)
tx.Rollback()
return err
}
}
if createType == createConst {
_, err = tx.Exec(
`insert into entry
(entry_data,modified,user_id,active,by) values
($1,$2,$3,$4,$5)`,
insertedID, now.Format(dbDateTime), userID, active, impostorID)
if err != nil {
fmt.Println(err)
tx.Rollback()
return err
}
} else if createType == updateType {
_, err = tx.Exec(
`update entry set
entry_data=$1,
active=false,
by=$2
where entry_id=$3`,
insertedID, impostorID, entryID)
if err != nil {
fmt.Println(err)
tx.Rollback()
return err
}
}
err = tx.Commit()
if err != nil {
fmt.Println(err)
return err
}
return nil
}
//GetLogsForUser fetches the log entries for a given user in a given timespan
func GetLogsForUser(userID string, from, to time.Time, reverse bool) ([]DisplayLog, error) {
Info.Printf("GetLogsForUser(%s,%v,%v,%t)\n", userID, from.Format(jsDateTime), to.Format(jsDateTime), reverse)
var logs []DisplayLog
var err error
query := `
select
ed.begin as begin,
ed.end as end,
ed.type as type,
e.entry_id,
e.active
from
entry e,
entry_data ed
where
e.entry_data=ed.id and
ed.begin between $1 and $2 and
e.user_id=$3
order by
ed.begin desc`
if reverse {
query = `
select
ed.begin as begin,
ed.end as end,
ed.type as type,
e.entry_id,
e.active
from
entry e,
entry_data ed
where
e.entry_data=ed.id and
ed.begin between $1 and $2 and
e.user_id=$3
order by
ed.begin asc`
}
rows, err := db.Queryx(query, from, to, userID)
if err != nil {
return make([]DisplayLog, 0), err
}
defer rows.Close()
for rows.Next() {
var l DisplayLog
err = rows.StructScan(&l)
if err != nil {
continue
}
logs = append(logs, l)
}
ret := []DisplayLog{}
for _, l := range logs {
if l.Type == workTimeConst {
ret = append(ret, prepareDateTime(l, true))
} else if l.Type == holidayConst || l.Type == sickConst {
ret = append(ret, prepareDate(l))
}
}
return ret, nil
}
//GetAbsenceForUser retrievs entries for holidays and sick leave for the given period
func GetAbsenceForUser(userID string, from, to time.Time) ([]DisplayLog, error) {
Info.Printf("GetAbsenceForUser(%s,%v,%v)\n", userID, from.Format(jsDateTime), to.Format(jsDateTime))
var logs []DisplayLog
rows, err := db.Queryx(
`select
ed.begin as begin,
ed.end as end,
ed.type as type,
e.entry_id
from
entry e,
entry_data ed
where
e.entry_data=ed.id and
ed.begin between $1 and $2 and
e.user_id=$3 and
ed.type in ('Holiday','Sick leave')`,
from, to, userID)
if err != nil {
return []DisplayLog{}, err
}
for rows.Next() {
var row DisplayLog
err = rows.StructScan(&row)
if err == nil {
logs = append(logs, row)
}
}
return logs, nil
}
//DeleteEntry detaches an entry from its data.
//For the user this has the same effect as deleting the entry
func DeleteEntry(entryID, by string) error {
Info.Printf("DeleteEntry(%s,%s)\n", entryID, by)
tx, err := db.Beginx()
if err != nil {
return err
}
now := time.Now().Format(dbDateTime)
_, err = tx.Exec(
`update entry set
entry_data=NULL,
modified=$1,
by=$2
where entry_id=$3`,
now, by, entryID)
if err != nil {
tx.Rollback()
return err
}
err = tx.Commit()
if err != nil {
return err
}
return nil
}
//ActiveEntry checks for a user if there is an active log entry
func ActiveEntry(userID string) (string, error) {
Info.Printf("ActiveEntry(%s)", userID)
var entryID string
rows, err := db.Queryx(
`select
entry_id
from
entry
where
user_id=$1 and
active=true and
entry_data is not null`,
userID)
if err != nil {
return "", err
}
if !rows.Next() {
return "", nil
}
defer rows.Close()
err = rows.Scan(&entryID)
if err != nil {
return "", err
}
return entryID, nil
}