-
Notifications
You must be signed in to change notification settings - Fork 1
/
response.go
136 lines (119 loc) · 3.07 KB
/
response.go
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
package monday
import (
"encoding/json"
"errors"
"fmt"
"time"
"github.com/grokify/simplego/time/timeutil"
)
const (
ColumnValueTitleDate = "Date"
ColumnValueTitleStatus = "Status"
)
type Response struct {
AccountId int `json:"account_id"`
Data Data `json:"data"`
}
type Data struct {
Boards []Board `json:"boards"`
}
type Board struct {
Id string `json:"id"`
Name string `json:"name"`
Columns []Column `json:"columns"`
Owner Owner `json:"owner"`
Items []Item `json:"items"`
State string `json:"state"`
}
type Column struct {
Id string `json:"id"`
Title string `json:"title"`
Type string `json:"type"`
}
type Owner struct {
Id int `json:"id"`
}
type Item struct {
Id string `json:"id"`
Name string `json:"name"`
State string `json:"state"`
ColumnValues []ColumnValue `json:"column_values"`
}
type ColumnValue struct {
Id string `json:"id"`
Title string `json:"title"`
Value *string `json:"value"`
Text *string `json:"text"`
}
type ColumnValueValue struct {
ChangedAt *time.Time `json:"changed_at"`
}
func ParseColumnValueValue(data []byte) (ColumnValueValue, error) {
var cvv ColumnValueValue
err := json.Unmarshal(data, &cvv)
return cvv, err
}
func (item *Item) GetColumnValue(name string, errorOnDupe bool) (ColumnValue, error) {
cvs := []ColumnValue{}
for _, cv := range item.ColumnValues {
if cv.Title == name {
cvs = append(cvs, cv)
}
}
if len(cvs) == 0 {
return ColumnValue{}, fmt.Errorf("column value not found [%s]", name)
} else if len(cvs) > 1 && errorOnDupe {
return ColumnValue{}, fmt.Errorf("more than one column values found for [%s] count [%d]",
name, len(cvs))
}
return cvs[0], nil
}
func (item *Item) Date() (time.Time, error) {
dateCv, err := item.GetColumnValue("Date", true)
if err != nil {
return time.Now(), err
}
/*
"title":"Date",
"id":"date4",
"value":"{\"date\":\"2021-08-03\",\"icon\":null,\"changed_at\":\"2021-08-06T16:49:57.071Z\"}",
"text":"2021-08-03"
*/
if dateCv.Text == nil {
return time.Now(), errors.New("date text is nil")
}
return time.Parse(timeutil.RFC3339FullDate, *dateCv.Text)
}
func (item *Item) FieldsSimple() map[string]string {
msi := map[string]string{}
for _, cv := range item.ColumnValues {
if cv.Text == nil {
msi[cv.Title] = ""
} else {
msi[cv.Title] = *cv.Text
}
}
return msi
}
func (item *Item) LastChangedAtDateStatus() (time.Time, error) {
dates := []time.Time{}
dtCv, err := item.GetColumnValue(ColumnValueTitleDate, true)
if err == nil && dtCv.Value != nil {
cvv, err := ParseColumnValueValue([]byte(*dtCv.Value))
if err == nil && cvv.ChangedAt != nil {
dates = append(dates, *cvv.ChangedAt)
}
}
stCv, err := item.GetColumnValue(ColumnValueTitleStatus, true)
if err == nil && stCv.Value != nil {
cvv, err := ParseColumnValueValue([]byte(*stCv.Value))
if err == nil && cvv.ChangedAt != nil {
dates = append(dates, *cvv.ChangedAt)
}
}
latest, err := timeutil.Latest(dates, true)
if err != nil {
return timeutil.TimeZeroRFC3339(), nil
}
return latest, nil
}