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
/
location.go
621 lines (587 loc) · 13.1 KB
/
location.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
package api
import (
"encoding/json"
"strings"
)
// Different potential layouts used by the frontend.
// File: /src/components/Form/Location/Layouts.js
const (
LayoutAddress = "Address"
LayoutBirthPlace = "Birthplace"
LayoutBirthPlaceWithoutCounty = "Birthplace without County"
LayoutCountry = "Country"
LayoutUSCityStateInternationalCity = "US City, State, International city"
LayoutUSCityStateInternationalCityCountry = "US City, State, International city country"
LayoutState = "State"
LayoutCityState = "City, State"
LayoutStreetCityCountry = "Street, City, Country"
LayoutCityCountry = "City, Country"
LayoutUSCityStateZipcodeInternationalCity = "US City, State, Zipcode International city"
LayoutCityStateCountry = "City, State, Country"
LayoutUSAddress = "US Address"
LayoutStreetCity = "Street, City"
LayoutOffense = "Offense"
)
// Special layout flags for e-QIP integration for...
const (
// Country elements with CountriesNoUS type
LayoutBirthPlaceWithoutCountyNoUS = "Birthplace without County CountriesNoUS"
LayoutBirthPlaceNoUS = "Birthplace CountriesNoUS"
// APOFPO elements
LayoutMilitaryAddress = "Military Address"
LayoutPhysicalDomestic = "PhysicalDomestic"
LayoutPhysicalInternational = "PhysicalInternational"
)
// Location is a basic input.
type Location struct {
ID int `json:"-"`
Layout string `json:"layout"`
Street1 string `json:"street,omitempty"`
Street2 string `json:"street2,omitempty"`
City string `json:"city,omitempty"`
State string `json:"state,omitempty"`
Zipcode string `json:"zipcode,omitempty"`
County string `json:"county,omitempty"`
Country string `json:"country"`
CountryComments string `json:"countryComments,omitempty"`
Validated bool `json:"validated,omitempty"`
}
// Unmarshal bytes in to the entity properties.
func (entity *Location) Unmarshal(raw []byte) error {
return json.Unmarshal(raw, entity)
}
// Marshal to payload structure
func (entity *Location) Marshal() Payload {
return MarshalPayloadEntity("location", entity)
}
// Save the location to data storage.
func (entity *Location) Save(context DatabaseService, account int) (int, error) {
if err := context.CheckTable(entity); err != nil {
return entity.ID, err
}
if err := context.Save(entity); err != nil {
return entity.ID, err
}
return entity.ID, nil
}
// Delete the location from data storage.
func (entity *Location) Delete(context DatabaseService, account int) (int, error) {
if err := context.CheckTable(entity); err != nil {
return entity.ID, err
}
if entity.ID != 0 {
if err := context.Delete(entity); err != nil {
return entity.ID, err
}
}
return entity.ID, nil
}
// Get the location from data storage.
func (entity *Location) Get(context DatabaseService, account int) (int, error) {
if err := context.CheckTable(entity); err != nil {
return entity.ID, err
}
if entity.ID != 0 {
if err := context.Select(entity); err != nil {
return entity.ID, err
}
}
return entity.ID, nil
}
// Valid checks the value(s) against an battery of tests.
func (entity *Location) Valid() (bool, error) {
if entity.Validated {
return true, nil
}
var stack ErrorStack
domestic := entity.IsDomestic()
postoffice := entity.IsPostOffice()
international := !domestic && !postoffice
switch entity.Layout {
case LayoutBirthPlace:
if domestic {
stack = validateFields(entity, "city", "state", "county")
} else {
stack = validateFields(entity, "city", "county")
}
case LayoutBirthPlaceWithoutCounty:
if domestic {
stack = validateFields(entity, "city", "state")
} else {
stack = validateFields(entity, "city", "county")
}
case LayoutCountry:
stack = validateFields(entity, "country")
case LayoutUSCityStateInternationalCity:
if domestic {
stack = validateFields(entity, "city", "state")
} else {
stack = validateFields(entity, "city", "country")
}
case LayoutUSCityStateInternationalCityCountry:
if domestic {
stack = validateFields(entity, "city", "state")
} else {
stack = validateFields(entity, "city", "country")
}
case LayoutState:
stack = validateFields(entity, "state")
case LayoutCityState:
stack = validateFields(entity, "city", "state")
case LayoutStreetCityCountry:
stack = validateFields(entity, "street", "city", "country")
case LayoutCityCountry:
stack = validateFields(entity, "city", "country")
case LayoutUSCityStateZipcodeInternationalCity:
if domestic {
stack = validateFields(entity, "city", "state", "zipcode")
} else {
stack = validateFields(entity, "city", "country")
}
case LayoutCityStateCountry:
stack = validateFields(entity, "city", "state", "country")
case LayoutUSAddress:
stack = validateFields(entity, "street", "city", "state", "zipcode")
case LayoutStreetCity:
stack = validateFields(entity, "street", "city")
case LayoutOffense:
if domestic {
stack = validateFields(entity, "city", "state", "zipcode", "county", "country")
} else {
stack = validateFields(entity, "city", "country")
}
default:
if domestic || postoffice {
stack = validateFields(entity, "street", "city", "state", "zipcode")
}
stack = validateFields(entity, "street", "city", "country")
}
geocode := entity.Layout == LayoutAddress || entity.Layout == LayoutUSAddress
if !stack.HasErrors() && !international && geocode {
// Perform geocoding
results, err := Geocode.Validate(
GeocodeValues{
Street: string(entity.Street1),
Street2: string(entity.Street2),
City: string(entity.City),
State: string(entity.State),
Zipcode: string(entity.Zipcode),
})
if err != nil {
stack.Append("Location", ErrInvalidLocation{
Message: err.Error(),
Suggestions: results,
})
}
}
return !stack.HasErrors(), stack
}
func validateFields(entity *Location, props ...string) ErrorStack {
var stack ErrorStack
for _, prop := range props {
switch strings.ToLower(prop) {
case "street":
street1 := strings.TrimSpace(entity.Street1)
if street1 == "" {
stack.Append("Location", ErrFieldRequired{"Missing street"})
}
case "city":
city := strings.TrimSpace(entity.City)
if city == "" {
stack.Append("Location", ErrFieldRequired{"Missing city"})
}
case "state":
state := strings.TrimSpace(entity.State)
if state == "" {
stack.Append("Location", ErrFieldRequired{"Missing state"})
} else if !has(state, states...) {
stack.Append("Location", ErrFieldInvalid{"Invalid state"})
}
case "zipcode":
zipcode := strings.TrimSpace(entity.Zipcode)
if zipcode == "" {
stack.Append("Location", ErrFieldRequired{"Missing ZIP code"})
}
case "county":
county := strings.TrimSpace(entity.County)
if county == "" {
stack.Append("Location", ErrFieldRequired{"Missing county"})
}
case "country":
country := strings.TrimSpace(entity.Country)
if country == "" {
stack.Append("Location", ErrFieldRequired{"Missing country"})
} else if !has(country, countries...) {
stack.Append("Location", ErrFieldInvalid{"Invalid country"})
}
}
}
return stack
}
func has(target string, options ...string) bool {
for _, option := range options {
if strings.EqualFold(target, option) {
return true
}
}
return false
}
// GetID returns the entity identifier.
func (entity *Location) GetID() int {
return entity.ID
}
// SetID sets the entity identifier.
func (entity *Location) SetID(id int) {
entity.ID = id
}
// Find is not used for locations. Please use the `Get` method.
func (entity *Location) Find(context DatabaseService) error {
return nil
}
// IsDomestic returns whether the location is in the United States.
func (entity *Location) IsDomestic() bool {
return entity.Country == "United States" || entity.Layout == LayoutUSAddress
}
// IsPostOffice returns whether the location is APO, FPO, or DPO.
func (entity *Location) IsPostOffice() bool {
return entity.Country == "POSTOFFICE"
}
// IsInternational returns whether the location is outside the United States and not a postal address.
func (entity *Location) IsInternational() bool {
return !entity.IsDomestic() && !entity.IsPostOffice()
}
// TODO: Read `states` and `countries` from an external source.
var (
states = []string{
"AL",
"AK",
"AZ",
"AR",
"CA",
"CO",
"CT",
"DE",
"DC",
"FL",
"GA",
"HI",
"ID",
"IL",
"IN",
"IA",
"KS",
"KY",
"LA",
"ME",
"MD",
"MA",
"MI",
"MN",
"MS",
"MO",
"MT",
"NE",
"NV",
"NH",
"NJ",
"NM",
"NY",
"NC",
"ND",
"OH",
"OK",
"OR",
"PA",
"RI",
"SC",
"SD",
"TN",
"TX",
"UT",
"VT",
"VA",
"WA",
"WV",
"WI",
"WY",
"AS",
"FQ",
"GU",
"HQ",
"DQ",
"JQ",
"KQ",
"MH",
"FM",
"MQ",
"BQ",
"MP",
"PW",
"LQ",
"PR",
"VI",
"WQ",
"AE",
"AP",
"AA",
}
countries = []string{
"POSTOFFICE",
"United States",
"Afghanistan",
"Akrotiri Sovereign Base Area",
"Albania",
"Algeria",
"Andorra",
"Angola",
"Anguill",
"Antarctica",
"Antigua and Barbuda",
"Argentina",
"Armenia",
"Aruba",
"Ashmore and Cartier Islands",
"Australia",
"Austria",
"Azerbaijan",
"Bahamas, The",
"Bahrain",
"Bangladesh",
"Barbados",
"Bassas da India",
"Belarus",
"Belgium",
"Belize",
"Benin",
"Bermuda",
"Bhutan",
"Bolivia",
"Bosnia and Herzegovina",
"Botswana",
"Bouvet Island",
"Brazil",
"British Indian Ocean Territory",
"British Virgin Islands",
"Brunei",
"Bulgaria",
"Burkina Faso",
"Burma",
"Burundi",
"Cambodia",
"Cameroon",
"Canada",
"Cape Verde",
"Cayman Islands",
"Central African Republic",
"Chad",
"Chile",
"China",
"Christmas Island",
"Clipperton Island",
"Cocos Keeling Islands",
"Colombia",
"Comoros",
"Congo",
"Congo, Democratic Republic of the",
"Cook Islands",
"Coral Sea Islands",
"Costa Rica",
"Ivoire",
"Croatia",
"Cuba",
"Cyprus",
"Czech Republic",
"Denmark",
"Dhekelia Sovereign Base Area",
"Djibouti",
"Dominica",
"Dominican Republic",
"East Timor",
"Ecuador",
"Egypt",
"El Salvador",
"Equatorial Guinea",
"Eritrea",
"Estonia",
"Ethiopia",
"Etorofu, Habomai, Kunashiri And Shikotan Islands",
"Europa Island",
"Falkland Islands Islas Malvinas",
"Faroe Islands",
"Fiji",
"Finland",
"France",
"French Guiana",
"French Polynesia",
"French Southern and Antarctic Lands",
"Gabon",
"Gambia, The",
"Gaza Strip",
"Georgia",
"Germany",
"Ghana",
"Gibraltar",
"Glorioso Islands",
"Greece",
"Greenland",
"Grenada",
"Guadeloupe",
"Guatemala",
"Guernsey",
"Guinea",
"Guinea-Bissau",
"Guyana",
"Haiti",
"Heard Island and McDonald Islands",
"Honduras",
"Hong Kong",
"Hungary",
"Iceland",
"India",
"Indonesia",
"Iran",
"Iraq",
"Ireland",
"Isle of Man",
"Israel",
"Italy",
"Jamaica",
"Jan Mayen",
"Japan",
"Jersey",
"Jordan",
"Juan de Nova Island",
"Kazakhstan",
"Kenya",
"Kiribati",
"Kosovo",
"Kuwait",
"Kyrgyzstan",
"Laos",
"Latvia",
"Lebanon",
"Lesotho",
"Liberia",
"Libya",
"Liechtenstein",
"Lithuania",
"Luxembourg",
"Macau",
"Macedonia",
"Madagascar",
"Malawi",
"Malaysia",
"Maldives",
"Mali",
"Malta",
"Marshall Islands",
"Martinique",
"Mauritania",
"Mauritius",
"Mayotte",
"Mexico",
"Micronesia, Federated States of",
"Moldova",
"Monaco",
"Mongolia",
"Montenegro",
"Montserrat",
"Morocco",
"Mozambique",
"Namibia",
"Nauru",
"Nepal",
"Netherlands",
"Netherlands Antilles",
"New Caledonia",
"New Zealand",
"Nicaragua",
"Niger",
"Nigeria",
"Niue",
"Norfolk Island",
"North Korea",
"Norway",
"Oman",
"Pakistan",
"Palau",
"Panama",
"Papua New Guinea",
"Paracel Islands",
"Paraguay",
"Peru",
"Philippines",
"Pitcairn Islands",
"Poland",
"Portugal",
"Qatar",
"Reunion",
"Romania",
"Russia",
"Rwanda",
"Saint Barthelemy",
"Saint Helena",
"Saint Kitts and Nevis",
"Saint Lucia",
"Saint Martin",
"Saint Pierre and Miquelon",
"Saint Vincent and the Grenadines",
"Samoa",
"San Marino",
"Sao Tome and Principe",
"Saudi Arabia",
"Senegal",
"Serbia",
"Seychelles",
"Sierra Leone",
"Singapore",
"Slovakia",
"Slovenia",
"Solomon Islands",
"Somalia",
"South Africa",
"South Georgia and the South Sandwich Islands",
"South Korea",
"South Sudan",
"Spain",
"Spratly Islands",
"Sri Lanka",
"Sudan",
"Suriname",
"Svalbard",
"Swaziland",
"Sweden",
"Switzerland",
"Syria",
"Taiwan",
"Tajikistan",
"Tanzania",
"Thailand",
"Togo",
"Tokelau",
"Tonga",
"Trinidad and Tobago",
"Tromelin Island",
"Tunisia",
"Turkey",
"Turkmenistan",
"Turks and Caicos Islands",
"Tuvalu",
"Uganda",
"Ukraine",
"United Arab Emirates",
"United Kingdom",
"Uruguay",
"Uzbekistan",
"Vanuatu",
"Vatican City",
"Venezuela",
"Vietnam",
"Wallis and Futuna",
"West Bank",
"Western Sahara",
"Yemen",
"Zambia",
"Zimbabwe",
}
)