-
Notifications
You must be signed in to change notification settings - Fork 1
/
planning.go
178 lines (149 loc) · 4.63 KB
/
planning.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
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
package planning
import (
"fmt"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
"github.com/ttab/darknut"
"github.com/ttab/elephant-repository/postgres"
"github.com/ttab/elephantine/pg"
"github.com/ttab/newsdoc"
)
type Item struct {
UUID uuid.UUID `newsdoc:"uuid"`
Title string `newsdoc:"title"`
Meta ItemMeta `newsdoc:"meta,type=core/planning-item"`
InternalDescription *DescriptionBlock `newsdoc:"meta,type=core/description,role=internal"`
PublicDescription *DescriptionBlock `newsdoc:"meta,type=core/description,role=public"`
Assignments []AssignmentBlock `newsdoc:"meta,type=core/assignment"`
Event *EventLink `newsdoc:"links,type=core/event,rel=associated"`
}
type DescriptionBlock struct {
Role string `newsdoc:"role"`
Text string `newsdoc:"data.text"`
}
type ItemMeta struct {
StartDate time.Time `newsdoc:"data.start_date,format=2006-01-02"`
EndDate time.Time `newsdoc:"data.end_date,format=2006-01-02"`
Public bool `newsdoc:"data.public"`
Tentative bool `newsdoc:"data.tentative"`
Priority *int16 `newsdoc:"data.priority"`
}
type AssignmentBlock struct {
ID uuid.UUID `newsdoc:"id"`
Publish *time.Time `newsdoc:"data.publish"`
PublishSlot *int16 `newsdoc:"data.publish_slot"`
Starts time.Time `newsdoc:"data.start"`
Ends *time.Time `newsdoc:"data.end"`
StartDate time.Time `newsdoc:"data.start_date,format=2006-01-02"`
EndDate time.Time `newsdoc:"data.end_date,format=2006-01-02"`
Status *string `newsdoc:"data.status"`
FullDay bool `newsdoc:"data.full_day"`
Public bool `newsdoc:"data.public"`
Kind []AssignmentKind `newsdoc:"meta,type=core/assignment-type"`
Assignees []AssigneeLink `newsdoc:"links,rel=assignee"`
Deliverables []DeliverableLink `newsdoc:"links,rel=deliverable"`
}
type AssignmentKind struct {
Value string `newsdoc:"value"`
}
type AssigneeLink struct {
UUID uuid.UUID `newsdoc:"uuid"`
Role string `newsdoc:"role"`
}
type EventLink struct {
UUID uuid.UUID `newsdoc:"uuid"`
}
type DeliverableLink struct {
UUID uuid.UUID `newsdoc:"uuid"`
Type string `newsdoc:"type"`
}
type Rows struct {
Item postgres.SetPlanningItemParams
Assignments []postgres.SetPlanningAssignmentParams
Assignees []postgres.SetPlanningAssigneeParams
Deliverables []postgres.SetPlanningItemDeliverableParams
}
func NewItemFromDocument(doc newsdoc.Document) (*Item, error) {
var p Item
err := darknut.UnmarshalDocument(doc, &p)
if err != nil {
return nil, fmt.Errorf(
"failed to convert document to a planning item: %w", err)
}
return &p, nil
}
func (p *Item) ToRows(version int64) (*Rows, error) {
rows := Rows{
Item: postgres.SetPlanningItemParams{
UUID: p.UUID,
Version: version,
Title: p.Title,
Public: p.Meta.Public,
Tentative: p.Meta.Tentative,
StartDate: pg.Date(p.Meta.StartDate),
EndDate: pg.Date(p.Meta.EndDate),
Priority: pg.PInt2(p.Meta.Priority),
},
}
if p.PublicDescription != nil {
rows.Item.Description = p.PublicDescription.Text
}
if p.InternalDescription != nil {
rows.Item.Description = textAppend("\n\n",
rows.Item.Description,
p.InternalDescription.Text)
}
if p.Event != nil {
rows.Item.Event = pgtype.UUID{
Bytes: p.Event.UUID,
Valid: true,
}
}
for _, a := range p.Assignments {
ar := postgres.SetPlanningAssignmentParams{
UUID: a.ID,
Version: version,
PlanningItem: p.UUID,
Status: pg.PText(a.Status),
Publish: pg.PTime(a.Publish),
PublishSlot: pg.PInt2(a.PublishSlot),
Starts: pg.Time(a.Starts),
Ends: pg.PTime(a.Ends),
StartDate: pg.Date(a.StartDate),
EndDate: pg.Date(a.EndDate),
FullDay: a.FullDay,
}
for _, k := range a.Kind {
ar.Kind = append(ar.Kind, k.Value)
}
rows.Assignments = append(rows.Assignments, ar)
for _, as := range a.Assignees {
asr := postgres.SetPlanningAssigneeParams{
Assignment: a.ID,
Assignee: as.UUID,
Version: version,
Role: as.Role,
}
rows.Assignees = append(rows.Assignees, asr)
}
for _, d := range a.Deliverables {
dr := postgres.SetPlanningItemDeliverableParams{
Assignment: a.ID,
Document: d.UUID,
Version: version,
}
rows.Deliverables = append(rows.Deliverables, dr)
}
}
return &rows, nil
}
func textAppend(separator string, a string, b string) string {
if a == "" {
return b
}
if b == "" {
return a
}
return a + separator + b
}