This repository has been archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
relationships.go
297 lines (236 loc) · 7.51 KB
/
relationships.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
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
package api
import (
"encoding/json"
"github.com/pkg/errors"
)
// RelationshipsMarital represents the payload for the relationships marital section.
type RelationshipsMarital struct {
PayloadStatus Payload `json:"Status" sql:"-"`
PayloadCivilUnion Payload `json:"CivilUnion" sql:"-"`
PayloadDivorcedList Payload `json:"DivorcedList" sql:"-"`
// Validator specific fields
Status *Radio `json:"-"`
CivilUnion *CivilUnion `json:"-"`
DivorcedList *Collection `json:"-"`
// Persister specific fields
ID int `json:"-"`
StatusID int `json:"-" pg:", fk:Status"`
CivilUnionID int `json:"-" pg:", fk:CivilUnion"`
DivorcedListID int `json:"-" pg:", fk:DivorcedList"`
}
// Unmarshal bytes in to the entity properties.
func (entity *RelationshipsMarital) Unmarshal(raw []byte) error {
err := json.Unmarshal(raw, entity)
if err != nil {
return err
}
status, err := entity.PayloadStatus.Entity()
if err != nil {
return err
}
entity.Status = status.(*Radio)
civilUnion, err := entity.PayloadCivilUnion.Entity()
if err != nil {
return err
}
entity.CivilUnion = civilUnion.(*CivilUnion)
divorcedList, err := entity.PayloadDivorcedList.Entity()
if err != nil {
return err
}
entity.DivorcedList = divorcedList.(*Collection)
return err
}
// Marshal to payload structure
func (entity *RelationshipsMarital) Marshal() Payload {
if entity.Status != nil {
entity.PayloadStatus = entity.Status.Marshal()
}
if entity.CivilUnion != nil {
entity.PayloadCivilUnion = entity.CivilUnion.Marshal()
}
if entity.DivorcedList != nil {
entity.PayloadDivorcedList = entity.DivorcedList.Marshal()
}
return MarshalPayloadEntity("relationships.status.marital", entity)
}
// Valid checks the value(s) against an battery of tests.
func (entity *RelationshipsMarital) Valid() (bool, error) {
var stack ErrorStack
sv := entity.Status.Value
switch {
case sv == "Married" || sv == "Separated":
// Check if the civil union information is valid
if ok, err := entity.CivilUnion.Valid(); !ok {
stack.Append("CitizenshipMarital", err)
}
if entity.CivilUnion.Divorced.Value == "Yes" {
if ok, err := entity.DivorcedList.Valid(); !ok {
stack.Append("CitizenshipMarital", err)
}
}
case sv == "Annulled" || sv == "Divorced" || sv == "Widowed":
if ok, err := entity.DivorcedList.Valid(); !ok {
stack.Append("CitizenshipMarital", err)
}
}
return !stack.HasErrors(), stack
}
// ClearNoBranches clears any questions answered nos on a kickback
func (entity *RelationshipsMarital) ClearNoBranches() error {
if entity.CivilUnion != nil {
entity.CivilUnion.Separated.ClearNo()
entity.CivilUnion.Divorced.ClearNo()
}
if entity.DivorcedList != nil {
for _, divorcedItem := range entity.DivorcedList.Items {
deceased, itemErr := divorcedItem.GetItemValue("Deceased")
if itemErr != nil {
return errors.Wrap(itemErr, "Failed to pull deceased from a divorcee")
}
deceasedRadio := deceased.(*Radio)
if deceasedRadio.Value == "No" {
deceasedRadio.Value = ""
}
setErr := divorcedItem.SetItemValue("Deceased", deceasedRadio)
if setErr != nil {
return errors.Wrap(setErr, "Failed to set deceased for a divorcee")
}
}
}
entity.DivorcedList.ClearBranchNo()
return nil
}
// RelationshipsCohabitants represents the payload for the relationships cohabitants section.
type RelationshipsCohabitants struct {
PayloadHasCohabitant Payload `json:"HasCohabitant" sql:"-"`
PayloadCohabitantList Payload `json:"CohabitantList" sql:"-"`
// Validator specific fields
HasCohabitant *Branch `json:"-"`
CohabitantList *Collection `json:"-"`
// Persister specific fields
ID int `json:"-"`
HasCohabitantID int `json:"-" pg:", fk:HasCohabitant"`
CohabitantListID int `json:"-" pg:", fk:CohabitantList"`
}
// Unmarshal bytes in to the entity properties.
func (entity *RelationshipsCohabitants) Unmarshal(raw []byte) error {
err := json.Unmarshal(raw, entity)
if err != nil {
return err
}
hasCohabitant, err := entity.PayloadHasCohabitant.Entity()
if err != nil {
return err
}
entity.HasCohabitant = hasCohabitant.(*Branch)
cohabitantList, err := entity.PayloadCohabitantList.Entity()
if err != nil {
return err
}
entity.CohabitantList = cohabitantList.(*Collection)
return err
}
// Marshal to payload structure
func (entity *RelationshipsCohabitants) Marshal() Payload {
if entity.HasCohabitant != nil {
entity.PayloadHasCohabitant = entity.HasCohabitant.Marshal()
}
if entity.CohabitantList != nil {
entity.PayloadCohabitantList = entity.CohabitantList.Marshal()
}
return MarshalPayloadEntity("relationships.status.cohabitant", entity)
}
// Valid checks the value(s) against an battery of tests.
func (entity *RelationshipsCohabitants) Valid() (bool, error) {
if entity.HasCohabitant.Value == "No" {
return true, nil
}
return entity.CohabitantList.Valid()
}
// ClearNoBranches clears any questions answered nos on a kickback
func (entity *RelationshipsCohabitants) ClearNoBranches() error {
entity.HasCohabitant.ClearNo()
entity.CohabitantList.ClearBranchNo()
return nil
}
// RelationshipsPeople represents the payload for the relationships people section.
type RelationshipsPeople struct {
PayloadList Payload `json:"List" sql:"-"`
// Validator specific fields
List *Collection `json:"-"`
// Persister specific fields
ID int `json:"-"`
ListID int `json:"-" pg:", fk:List"`
}
// Unmarshal bytes in to the entity properties.
func (entity *RelationshipsPeople) Unmarshal(raw []byte) error {
err := json.Unmarshal(raw, entity)
if err != nil {
return err
}
list, err := entity.PayloadList.Entity()
if err != nil {
return err
}
entity.List = list.(*Collection)
return err
}
// Marshal to payload structure
func (entity *RelationshipsPeople) Marshal() Payload {
if entity.List != nil {
entity.PayloadList = entity.List.Marshal()
}
return MarshalPayloadEntity("relationships.people", entity)
}
// Valid checks the value(s) against an battery of tests.
func (entity *RelationshipsPeople) Valid() (bool, error) {
return entity.List.Valid()
}
// ClearNoBranches clears any questions answered nos on a kickback
func (entity *RelationshipsPeople) ClearNoBranches() error {
entity.List.ClearBranchNo()
return nil
}
// RelationshipsRelatives represents the payload for the relationships relatives section.
type RelationshipsRelatives struct {
PayloadList Payload `json:"List" sql:"-"`
// Validator specific fields
List *Collection `json:"-"`
// Persister specific fields
ID int `json:"-"`
ListID int `json:"-" pg:", fk:List"`
}
// Unmarshal bytes in to the entity properties.
func (entity *RelationshipsRelatives) Unmarshal(raw []byte) error {
err := json.Unmarshal(raw, entity)
if err != nil {
return err
}
list, err := entity.PayloadList.Entity()
if err != nil {
return err
}
entity.List = list.(*Collection)
return err
}
// Marshal to payload structure
func (entity *RelationshipsRelatives) Marshal() Payload {
if entity.List != nil {
entity.PayloadList = entity.List.Marshal()
}
return MarshalPayloadEntity("relationships.relatives", entity)
}
// Valid checks the value(s) against an battery of tests.
func (entity *RelationshipsRelatives) Valid() (bool, error) {
return entity.List.Valid()
}
// ClearNoBranches clears any questions answered nos on a kickback
func (entity *RelationshipsRelatives) ClearNoBranches() error {
deceasedErr := entity.List.ClearBranchItemsNo("IsDeceased")
if deceasedErr != nil {
return errors.Wrap(deceasedErr, "Couldn't clear deceased from relative")
}
entity.List.ClearBranchNo()
return nil
}